sticky documentation popup [take 1]
[fedora-idea.git] / platform / platform-impl / src / com / intellij / openapi / wm / impl / status / PositionPanel.java
blob438becda9e1bd97d765bfc5d8e144609cf01c573
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.wm.impl.status;
18 import com.intellij.ide.DataManager;
19 import com.intellij.ide.util.GotoLineNumberDialog;
20 import com.intellij.openapi.actionSystem.PlatformDataKeys;
21 import com.intellij.openapi.command.CommandProcessor;
22 import com.intellij.openapi.editor.Editor;
23 import com.intellij.openapi.editor.LogicalPosition;
24 import com.intellij.openapi.editor.SelectionModel;
25 import com.intellij.openapi.fileEditor.FileEditorManager;
26 import com.intellij.openapi.fileEditor.ex.IdeDocumentHistory;
27 import com.intellij.openapi.project.Project;
28 import com.intellij.openapi.wm.StatusBar;
29 import com.intellij.ui.UIBundle;
31 import javax.swing.*;
32 import java.awt.event.MouseAdapter;
33 import java.awt.event.MouseEvent;
35 class PositionPanel extends TextPanel implements StatusBarPatch {
36 public PositionPanel(StatusBar statusBar) {
37 super(false, "#############");
39 addMouseListener(new MouseAdapter() {
40 public void mouseClicked(final MouseEvent e) {
41 if (e.getClickCount() == 2) {
42 final Project project = getProject();
43 if (project == null) return;
44 final Editor editor = getEditor(project);
45 if (editor == null) return;
46 final CommandProcessor processor = CommandProcessor.getInstance();
47 processor.executeCommand(
48 project, new Runnable(){
49 public void run() {
50 final GotoLineNumberDialog dialog = new GotoLineNumberDialog(project, editor);
51 dialog.show();
52 IdeDocumentHistory.getInstance(project).includeCurrentCommandAsNavigation();
55 UIBundle.message("go.to.line.command.name"),
56 null
60 });
62 StatusBarTooltipper.install(this, statusBar);
65 public JComponent getComponent() {
66 return this;
69 public String updateStatusBar(final Editor editor, final JComponent componentSelected) {
70 if (editor != null) {
71 SwingUtilities.invokeLater(new Runnable() {
72 public void run() {
73 if (!editor.isDisposed()) {
74 StringBuilder message = new StringBuilder();
76 SelectionModel selectionModel = editor.getSelectionModel();
77 if (selectionModel.hasBlockSelection()) {
78 LogicalPosition start = selectionModel.getBlockStart();
79 LogicalPosition end = selectionModel.getBlockEnd();
80 appendLogicalPosition(start, message);
81 message.append("-");
82 appendLogicalPosition(new LogicalPosition(Math.abs(end.line - start.line), Math.abs(end.column - start.column) - 1), message);
84 else {
85 LogicalPosition caret = editor.getCaretModel().getLogicalPosition();
87 appendLogicalPosition(caret, message);
88 if (selectionModel.hasSelection()) {
89 int len = Math.abs(selectionModel.getSelectionStart() - selectionModel.getSelectionEnd());
90 message.append("/");
91 message.append(len);
95 setText(message.toString());
98 });
99 return UIBundle.message("go.to.line.command.double.click");
101 clear();
102 return null;
105 private static void appendLogicalPosition(LogicalPosition caret, StringBuilder message) {
106 message.append(caret.line + 1);
107 message.append(":");
108 message.append(caret.column + 1);
111 public void clear() {
112 setText("");
115 private static Editor getEditor(final Project project) {
116 return FileEditorManager.getInstance(project).getSelectedTextEditor();
119 private Project getProject() {
120 return PlatformDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext(this));