fix selection model assertion
[fedora-idea.git] / platform-impl / src / com / intellij / openapi / wm / impl / status / PositionPanel.java
blob7d640ca9bae1a362a12fe80086c2d04cd9cb660a
1 package com.intellij.openapi.wm.impl.status;
3 import com.intellij.ide.DataManager;
4 import com.intellij.ide.util.GotoLineNumberDialog;
5 import com.intellij.openapi.actionSystem.PlatformDataKeys;
6 import com.intellij.openapi.command.CommandProcessor;
7 import com.intellij.openapi.editor.Editor;
8 import com.intellij.openapi.editor.LogicalPosition;
9 import com.intellij.openapi.editor.SelectionModel;
10 import com.intellij.openapi.fileEditor.FileEditorManager;
11 import com.intellij.openapi.fileEditor.ex.IdeDocumentHistory;
12 import com.intellij.openapi.project.Project;
13 import com.intellij.openapi.wm.StatusBar;
14 import com.intellij.ui.UIBundle;
16 import javax.swing.*;
17 import java.awt.event.MouseAdapter;
18 import java.awt.event.MouseEvent;
20 class PositionPanel extends TextPanel implements StatusBarPatch {
21 public PositionPanel(StatusBar statusBar) {
22 super(false, "#############");
24 addMouseListener(new MouseAdapter() {
25 public void mouseClicked(final MouseEvent e) {
26 if (e.getClickCount() == 2) {
27 final Project project = getProject();
28 if (project == null) return;
29 final Editor editor = getEditor(project);
30 if (editor == null) return;
31 final CommandProcessor processor = CommandProcessor.getInstance();
32 processor.executeCommand(
33 project, new Runnable(){
34 public void run() {
35 final GotoLineNumberDialog dialog = new GotoLineNumberDialog(project, editor);
36 dialog.show();
37 IdeDocumentHistory.getInstance(project).includeCurrentCommandAsNavigation();
40 UIBundle.message("go.to.line.command.name"),
41 null
45 });
47 StatusBarTooltipper.install(this, statusBar);
50 public JComponent getComponent() {
51 return this;
54 public String updateStatusBar(final Editor editor, final JComponent componentSelected) {
55 if (editor != null) {
56 SwingUtilities.invokeLater(new Runnable() {
57 public void run() {
58 if (!editor.isDisposed()) {
59 StringBuilder message = new StringBuilder();
61 SelectionModel selectionModel = editor.getSelectionModel();
62 if (selectionModel.hasBlockSelection()) {
63 LogicalPosition start = selectionModel.getBlockStart();
64 LogicalPosition end = selectionModel.getBlockEnd();
65 appendLogicalPosition(start, message);
66 message.append("-");
67 appendLogicalPosition(new LogicalPosition(Math.abs(end.column - start.column) - 1, Math.abs(end.line - start.line)), message);
69 else {
70 LogicalPosition caret = editor.getCaretModel().getLogicalPosition();
72 appendLogicalPosition(caret, message);
73 if (selectionModel.hasSelection()) {
74 int len = Math.abs(selectionModel.getSelectionStart() - selectionModel.getSelectionEnd());
75 message.append("/");
76 message.append(len);
80 setText(message.toString());
83 });
84 return UIBundle.message("go.to.line.command.double.click");
86 clear();
87 return null;
90 private static void appendLogicalPosition(LogicalPosition caret, StringBuilder message) {
91 message.append(caret.line + 1);
92 message.append(":");
93 message.append(caret.column + 1);
96 public void clear() {
97 setText("");
100 private static Editor getEditor(final Project project) {
101 return FileEditorManager.getInstance(project).getSelectedTextEditor();
104 private Project getProject() {
105 return PlatformDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext(this));