package com.intellij.openapi.wm.impl.status;

import com.intellij.ide.DataManager;
import com.intellij.ide.util.GotoLineNumberDialog;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.ex.IdeDocumentHistory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.ui.UIBundle;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

class PositionPanel extends TextPanel implements StatusBarPatch {
  public PositionPanel(StatusBar statusBar) {
    super(false, "#############");

    addMouseListener(new MouseAdapter() {
      public void mouseClicked(final MouseEvent e) {
        if (e.getClickCount() == 2) {
          final Project project = getProject();
          if (project == null) return;
          final Editor editor = getEditor(project);
          if (editor == null) return;
          final CommandProcessor processor = CommandProcessor.getInstance();
          processor.executeCommand(
              project, new Runnable(){
              public void run() {
                final GotoLineNumberDialog dialog = new GotoLineNumberDialog(project, editor);
                dialog.show();
                IdeDocumentHistory.getInstance(project).includeCurrentCommandAsNavigation();
              }
            },
              UIBundle.message("go.to.line.command.name"),
            null
          );
        }
      }
    });

    StatusBarTooltipper.install(this, statusBar);
  }

  public JComponent getComponent() {
    return this;
  }

  public String updateStatusBar(final Editor editor, final JComponent componentSelected) {
    if (editor != null) {
      StringBuilder message = new StringBuilder();

      SelectionModel selectionModel = editor.getSelectionModel();
      if (selectionModel.hasBlockSelection()) {
        appendLogicalPosition(selectionModel.getBlockStart(), message);
        message.append("-");
        appendLogicalPosition(selectionModel.getBlockEnd(), message);
      }
      else {
        LogicalPosition caret = editor.getCaretModel().getLogicalPosition();

        appendLogicalPosition(caret, message);
        if (selectionModel.hasSelection()) {
          int len = Math.abs(selectionModel.getSelectionStart() - selectionModel.getSelectionEnd());
          message.append("/");
          message.append(len);
        }
      }

      setText(message.toString());
      return UIBundle.message("go.to.line.command.double.click");
    }
    clear();
    return null;
  }

  private static void appendLogicalPosition(LogicalPosition caret, StringBuilder message) {
    message.append(caret.line + 1);
    message.append(":");
    message.append(caret.column + 1);
  }

  public void clear() {
    setText("");
  }

  private static Editor getEditor(final Project project) {
    return FileEditorManager.getInstance(project).getSelectedTextEditor();
  }

  private Project getProject() {
    return PlatformDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext(this));
  }
}

@@ -1957,24 +1957,146 @@
  != null) %7B%0A
+      SwingUtilities.invokeLater(new Runnable() %7B%0A        public void run() %7B%0A          if (!editor.isDisposed()) %7B%0A      
       String
@@ -2131,24 +2131,30 @@
 Builder();%0A%0A
+      
       Select
@@ -2209,24 +2209,30 @@
 el();%0A      
+   
+   
 if (selectio
@@ -2261,38 +2261,108 @@
 on()) %7B%0A        
-append
+      LogicalPosition start = selectionModel.getBlockStart();%0A              
 LogicalPosition(
@@ -2352,33 +2352,39 @@
  LogicalPosition
-(
+ end = 
 selectionModel.g
@@ -2390,23 +2390,64 @@
 getBlock
-S
+End();%0A              appendLogicalPosition(s
 tart
-()
 , messag
@@ -2442,32 +2442,38 @@
 tart, message);%0A
+      
         message.
@@ -2485,32 +2485,38 @@
 d(%22-%22);%0A        
+      
 appendLogicalPos
@@ -2525,35 +2525,100 @@
 ion(
-selectionModel.getBlockEnd(
+new LogicalPosition(Math.abs(end.column - start.column) - 1, Math.abs(end.line - start.line)
 ), m
@@ -2636,23 +2636,41 @@
     
-%7D%0A
+      %7D%0A     
       
+ 
 else %7B%0A
+      
     
@@ -2743,32 +2743,38 @@
 ion();%0A%0A        
+      
 appendLogicalPos
@@ -2796,32 +2796,38 @@
 ssage);%0A        
+  
+    
 if (selectionMod
@@ -2839,32 +2839,38 @@
 sSelection()) %7B%0A
+      
           int le
@@ -2956,32 +2956,38 @@
 d());%0A          
+    
+  
 message.append(%22
@@ -2987,24 +2987,30 @@
 ppend(%22/%22);%0A
+      
           me
@@ -3036,24 +3036,30 @@
         
+      
 %7D%0A      
 %7D%0A%0A     
@@ -3050,19 +3050,31 @@
 %7D%0A      
+ 
-%7D%0A%0A
+     %7D%0A%0A      
       se
@@ -3096,24 +3096,56 @@
 oString());%0A
+          %7D%0A        %7D%0A      %7D);%0A
       return

fix selection model assertion