fix for recovery of dna/protein sequence mappings in jar file (mantis bug id 40124)
[jalview.git] / src / jalview / gui / RotatableCanvas.java
index feaabf6..14cd425 100755 (executable)
@@ -29,53 +29,85 @@ import jalview.math.*;
 
 /**
  * DOCUMENT ME!
- *
+ * 
  * @author $author$
  * @version $Revision$
  */
-public class RotatableCanvas
-    extends JPanel implements MouseListener,
-    MouseMotionListener, KeyListener
+public class RotatableCanvas extends JPanel implements MouseListener,
+        MouseMotionListener, KeyListener
 {
   RotatableMatrix idmat = new RotatableMatrix(3, 3);
+
   RotatableMatrix objmat = new RotatableMatrix(3, 3);
+
   RotatableMatrix rotmat = new RotatableMatrix(3, 3);
 
-  //RubberbandRectangle rubberband;
+  // RubberbandRectangle rubberband;
   boolean drawAxes = true;
+
   int omx = 0;
+
   int mx = 0;
+
   int omy = 0;
+
   int my = 0;
+
   Image img;
+
   Graphics ig;
+
   Dimension prefsize;
+
   float[] centre = new float[3];
+
   float[] width = new float[3];
+
   float[] max = new float[3];
+
   float[] min = new float[3];
+
   float maxwidth;
+
   float scale;
+
   int npoint;
+
   Vector points;
+
   float[][] orig;
+
   float[][] axes;
+
   int startx;
+
   int starty;
+
   int lastx;
+
   int lasty;
+
   int rectx1;
+
   int recty1;
+
   int rectx2;
+
   int recty2;
+
   float scalefactor = 1;
+
   AlignViewport av;
+
   AlignmentPanel ap;
+
   boolean showLabels = false;
+
   Color bgColour = Color.black;
+
   boolean applyToAllViews = false;
 
-  //  Controller    controller;
+  // Controller controller;
   public RotatableCanvas(AlignmentPanel ap)
   {
     this.av = ap.av;
@@ -128,7 +160,7 @@ public class RotatableCanvas
       }
     }
 
-    //Initialize the matrices to identity
+    // Initialize the matrices to identity
     for (int i = 0; i < 3; i++)
     {
       for (int j = 0; j < 3; j++)
@@ -188,9 +220,9 @@ public class RotatableCanvas
     max = new float[3];
     min = new float[3];
 
-    max[0] = (float) - 1e30;
-    max[1] = (float) - 1e30;
-    max[2] = (float) - 1e30;
+    max[0] = (float) -1e30;
+    max[1] = (float) -1e30;
+    max[2] = (float) -1e30;
 
     min[0] = (float) 1e30;
     min[1] = (float) 1e30;
@@ -214,9 +246,9 @@ public class RotatableCanvas
       }
     }
 
-    //    System.out.println("xmax " + max[0] + " min " + min[0]);
-    //System.out.println("ymax " + max[1] + " min " + min[1]);
-    //System.out.println("zmax " + max[2] + " min " + min[2]);
+    // System.out.println("xmax " + max[0] + " min " + min[0]);
+    // System.out.println("ymax " + max[1] + " min " + min[1]);
+    // System.out.println("zmax " + max[2] + " min " + min[2]);
     width[0] = Math.abs(max[0] - min[0]);
     width[1] = Math.abs(max[1] - min[1]);
     width[2] = Math.abs(max[2] - min[2]);
@@ -233,12 +265,12 @@ public class RotatableCanvas
       maxwidth = width[2];
     }
 
-    //System.out.println("Maxwidth = " + maxwidth);
+    // System.out.println("Maxwidth = " + maxwidth);
   }
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   public float findScale()
@@ -267,7 +299,7 @@ public class RotatableCanvas
       dim = height;
     }
 
-    return (float) ( (dim * scalefactor) / (2 * maxwidth));
+    return (float) ((dim * scalefactor) / (2 * maxwidth));
   }
 
   /**
@@ -275,21 +307,21 @@ public class RotatableCanvas
    */
   public void findCentre()
   {
-    //Find centre coordinate
+    // Find centre coordinate
     findWidth();
 
     centre[0] = (max[0] + min[0]) / 2;
     centre[1] = (max[1] + min[1]) / 2;
     centre[2] = (max[2] + min[2]) / 2;
 
-    //    System.out.println("Centre x " + centre[0]);
-    //System.out.println("Centre y " + centre[1]);
-    //System.out.println("Centre z " + centre[2]);
+    // System.out.println("Centre x " + centre[0]);
+    // System.out.println("Centre y " + centre[1]);
+    // System.out.println("Centre z " + centre[2]);
   }
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   public Dimension getPreferredSize()
@@ -306,7 +338,7 @@ public class RotatableCanvas
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   public Dimension getMinimumSize()
@@ -316,8 +348,9 @@ public class RotatableCanvas
 
   /**
    * DOCUMENT ME!
-   *
-   * @param g DOCUMENT ME!
+   * 
+   * @param g
+   *                DOCUMENT ME!
    */
   public void paintComponent(Graphics g1)
   {
@@ -325,7 +358,7 @@ public class RotatableCanvas
     Graphics2D g = (Graphics2D) g1;
 
     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
-                       RenderingHints.VALUE_ANTIALIAS_ON);
+            RenderingHints.VALUE_ANTIALIAS_ON);
     if (points == null)
     {
       g.setFont(new Font("Verdana", Font.PLAIN, 18));
@@ -333,16 +366,16 @@ public class RotatableCanvas
     }
     else
     {
-      //Only create the image at the beginning -
-      if ( (img == null) || (prefsize.width != getWidth()) ||
-          (prefsize.height != getHeight()))
+      // Only create the image at the beginning -
+      if ((img == null) || (prefsize.width != getWidth())
+              || (prefsize.height != getHeight()))
       {
         prefsize.width = getWidth();
         prefsize.height = getHeight();
 
         scale = findScale();
 
-        //      System.out.println("New scale = " + scale);
+        // System.out.println("New scale = " + scale);
         img = createImage(getWidth(), getHeight());
         ig = img.getGraphics();
       }
@@ -361,8 +394,9 @@ public class RotatableCanvas
 
   /**
    * DOCUMENT ME!
-   *
-   * @param g DOCUMENT ME!
+   * 
+   * @param g
+   *                DOCUMENT ME!
    */
   public void drawAxes(Graphics g)
   {
@@ -371,17 +405,19 @@ public class RotatableCanvas
 
     for (int i = 0; i < 3; i++)
     {
-      g.drawLine(getWidth() / 2, getHeight() / 2,
-                 (int) ( (axes[i][0] * scale * max[0]) + (getWidth() / 2)),
-                 (int) ( (axes[i][1] * scale * max[1]) + (getHeight() / 2)));
+      g.drawLine(getWidth() / 2, getHeight() / 2, (int) ((axes[i][0]
+              * scale * max[0]) + (getWidth() / 2)), (int) ((axes[i][1]
+              * scale * max[1]) + (getHeight() / 2)));
     }
   }
 
   /**
    * DOCUMENT ME!
-   *
-   * @param g DOCUMENT ME!
-   * @param col DOCUMENT ME!
+   * 
+   * @param g
+   *                DOCUMENT ME!
+   * @param col
+   *                DOCUMENT ME!
    */
   public void drawBackground(Graphics g, Color col)
   {
@@ -391,8 +427,9 @@ public class RotatableCanvas
 
   /**
    * DOCUMENT ME!
-   *
-   * @param g DOCUMENT ME!
+   * 
+   * @param g
+   *                DOCUMENT ME!
    */
   public void drawScene(Graphics g1)
   {
@@ -400,7 +437,7 @@ public class RotatableCanvas
     Graphics2D g = (Graphics2D) g1;
 
     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
-                       RenderingHints.VALUE_ANTIALIAS_ON);
+            RenderingHints.VALUE_ANTIALIAS_ON);
 
     int halfwidth = getWidth() / 2;
     int halfheight = getHeight() / 2;
@@ -408,10 +445,9 @@ public class RotatableCanvas
     for (int i = 0; i < npoint; i++)
     {
       SequencePoint sp = (SequencePoint) points.elementAt(i);
-      int x = (int) ( (float) (sp.coord[0] - centre[0]) * scale) +
-          halfwidth;
-      int y = (int) ( (float) (sp.coord[1] - centre[1]) * scale) +
-          halfheight;
+      int x = (int) ((float) (sp.coord[0] - centre[0]) * scale) + halfwidth;
+      int y = (int) ((float) (sp.coord[1] - centre[1]) * scale)
+              + halfheight;
       float z = sp.coord[1] - centre[2];
 
       if (av.getSequenceColour(sp.sequence) == Color.black)
@@ -426,7 +462,7 @@ public class RotatableCanvas
       if (av.getSelectionGroup() != null)
       {
         if (av.getSelectionGroup().getSequences(null).contains(
-            ( (SequencePoint) points.elementAt(i)).sequence))
+                ((SequencePoint) points.elementAt(i)).sequence))
         {
           g.setColor(Color.gray);
         }
@@ -441,23 +477,22 @@ public class RotatableCanvas
       if (showLabels)
       {
         g.setColor(Color.red);
-        g.drawString( ( (SequencePoint) points.elementAt(i)).sequence.
-                     getName(),
-                     x - 3, y - 4);
+        g.drawString(((SequencePoint) points.elementAt(i)).sequence
+                .getName(), x - 3, y - 4);
       }
     }
 
-    //    //Now the rectangle
-    //    if (rectx2 != -1 && recty2 != -1) {
-    //      g.setColor(Color.white);
+    // //Now the rectangle
+    // if (rectx2 != -1 && recty2 != -1) {
+    // g.setColor(Color.white);
     //
-    //      g.drawRect(rectx1,recty1,rectx2-rectx1,recty2-recty1);
-    //    }
+    // g.drawRect(rectx1,recty1,rectx2-rectx1,recty2-recty1);
+    // }
   }
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   public Dimension minimumsize()
@@ -467,7 +502,7 @@ public class RotatableCanvas
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   public Dimension preferredsize()
@@ -477,8 +512,9 @@ public class RotatableCanvas
 
   /**
    * DOCUMENT ME!
-   *
-   * @param evt DOCUMENT ME!
+   * 
+   * @param evt
+   *                DOCUMENT ME!
    */
   public void keyTyped(KeyEvent evt)
   {
@@ -486,8 +522,9 @@ public class RotatableCanvas
 
   /**
    * DOCUMENT ME!
-   *
-   * @param evt DOCUMENT ME!
+   * 
+   * @param evt
+   *                DOCUMENT ME!
    */
   public void keyReleased(KeyEvent evt)
   {
@@ -495,8 +532,9 @@ public class RotatableCanvas
 
   /**
    * DOCUMENT ME!
-   *
-   * @param evt DOCUMENT ME!
+   * 
+   * @param evt
+   *                DOCUMENT ME!
    */
   public void keyPressed(KeyEvent evt)
   {
@@ -514,7 +552,7 @@ public class RotatableCanvas
     {
       System.err.println("DEBUG: Rectangle selection"); // log.debug
 
-      if ( (rectx2 != -1) && (recty2 != -1))
+      if ((rectx2 != -1) && (recty2 != -1))
       {
         rectSelect(rectx1, recty1, rectx2, recty2);
       }
@@ -525,8 +563,9 @@ public class RotatableCanvas
 
   /**
    * DOCUMENT ME!
-   *
-   * @param evt DOCUMENT ME!
+   * 
+   * @param evt
+   *                DOCUMENT ME!
    */
   public void mouseClicked(MouseEvent evt)
   {
@@ -534,8 +573,9 @@ public class RotatableCanvas
 
   /**
    * DOCUMENT ME!
-   *
-   * @param evt DOCUMENT ME!
+   * 
+   * @param evt
+   *                DOCUMENT ME!
    */
   public void mouseEntered(MouseEvent evt)
   {
@@ -543,8 +583,9 @@ public class RotatableCanvas
 
   /**
    * DOCUMENT ME!
-   *
-   * @param evt DOCUMENT ME!
+   * 
+   * @param evt
+   *                DOCUMENT ME!
    */
   public void mouseExited(MouseEvent evt)
   {
@@ -552,8 +593,9 @@ public class RotatableCanvas
 
   /**
    * DOCUMENT ME!
-   *
-   * @param evt DOCUMENT ME!
+   * 
+   * @param evt
+   *                DOCUMENT ME!
    */
   public void mouseReleased(MouseEvent evt)
   {
@@ -561,8 +603,9 @@ public class RotatableCanvas
 
   /**
    * DOCUMENT ME!
-   *
-   * @param evt DOCUMENT ME!
+   * 
+   * @param evt
+   *                DOCUMENT ME!
    */
   public void mousePressed(MouseEvent evt)
   {
@@ -601,7 +644,7 @@ public class RotatableCanvas
           aps[a].av.setSelectionGroup(new SequenceGroup());
           aps[a].av.getSelectionGroup().addOrRemove(found, true);
           aps[a].av.getSelectionGroup().setEndRes(
-              aps[a].av.alignment.getWidth() - 1);
+                  aps[a].av.alignment.getWidth() - 1);
         }
       }
 
@@ -612,8 +655,9 @@ public class RotatableCanvas
   }
 
   // private void fireSequenceSelectionEvent(Selection sel) {
-  //   controller.handleSequenceSelectionEvent(new SequenceSelectionEvent(this,sel));
-  //}
+  // controller.handleSequenceSelectionEvent(new
+  // SequenceSelectionEvent(this,sel));
+  // }
   public void mouseMoved(MouseEvent evt)
   {
     SequenceI found = findPoint(evt.getX(), evt.getY());
@@ -630,26 +674,27 @@ public class RotatableCanvas
 
   /**
    * DOCUMENT ME!
-   *
-   * @param evt DOCUMENT ME!
+   * 
+   * @param evt
+   *                DOCUMENT ME!
    */
   public void mouseDragged(MouseEvent evt)
   {
     mx = evt.getX();
     my = evt.getY();
 
-    //Check if this is a rectangle drawing drag
-    if ( (evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0)
+    // Check if this is a rectangle drawing drag
+    if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0)
     {
-      //      rectx2 = evt.getX();
-      //      recty2 = evt.getY();
+      // rectx2 = evt.getX();
+      // recty2 = evt.getY();
     }
     else
     {
       rotmat.setIdentity();
 
-      rotmat.rotate( (float) (my - omy), 'x');
-      rotmat.rotate( (float) (mx - omx), 'y');
+      rotmat.rotate((float) (my - omy), 'x');
+      rotmat.rotate((float) (mx - omx), 'y');
 
       for (int i = 0; i < npoint; i++)
       {
@@ -658,10 +703,10 @@ public class RotatableCanvas
         sp.coord[1] -= centre[1];
         sp.coord[2] -= centre[2];
 
-        //Now apply the rotation matrix
+        // Now apply the rotation matrix
         sp.coord = rotmat.vectorMultiply(sp.coord);
 
-        //Now translate back again
+        // Now translate back again
         sp.coord[0] += centre[0];
         sp.coord[1] += centre[1];
         sp.coord[2] += centre[2];
@@ -681,27 +726,30 @@ public class RotatableCanvas
 
   /**
    * DOCUMENT ME!
-   *
-   * @param x1 DOCUMENT ME!
-   * @param y1 DOCUMENT ME!
-   * @param x2 DOCUMENT ME!
-   * @param y2 DOCUMENT ME!
+   * 
+   * @param x1
+   *                DOCUMENT ME!
+   * @param y1
+   *                DOCUMENT ME!
+   * @param x2
+   *                DOCUMENT ME!
+   * @param y2
+   *                DOCUMENT ME!
    */
   public void rectSelect(int x1, int y1, int x2, int y2)
   {
     for (int i = 0; i < npoint; i++)
     {
       SequencePoint sp = (SequencePoint) points.elementAt(i);
-      int tmp1 = (int) ( ( (sp.coord[0] - centre[0]) * scale) +
-                        ( (float) getWidth() / 2.0));
-      int tmp2 = (int) ( ( (sp.coord[1] - centre[1]) * scale) +
-                        ( (float) getHeight() / 2.0));
+      int tmp1 = (int) (((sp.coord[0] - centre[0]) * scale) + ((float) getWidth() / 2.0));
+      int tmp2 = (int) (((sp.coord[1] - centre[1]) * scale) + ((float) getHeight() / 2.0));
 
-      if ( (tmp1 > x1) && (tmp1 < x2) && (tmp2 > y1) && (tmp2 < y2))
+      if ((tmp1 > x1) && (tmp1 < x2) && (tmp2 > y1) && (tmp2 < y2))
       {
         if (av != null)
         {
-          if (!av.getSelectionGroup().getSequences(null).contains(sp.sequence))
+          if (!av.getSelectionGroup().getSequences(null).contains(
+                  sp.sequence))
           {
             av.getSelectionGroup().addSequence(sp.sequence, true);
           }
@@ -710,16 +758,18 @@ public class RotatableCanvas
     }
 
     // if (changedSel) {
-    //    fireSequenceSelectionEvent(av.getSelection());
+    // fireSequenceSelectionEvent(av.getSelection());
     // }
   }
 
   /**
    * DOCUMENT ME!
-   *
-   * @param x DOCUMENT ME!
-   * @param y DOCUMENT ME!
-   *
+   * 
+   * @param x
+   *                DOCUMENT ME!
+   * @param y
+   *                DOCUMENT ME!
+   * 
    * @return DOCUMENT ME!
    */
   public SequenceI findPoint(int x, int y)
@@ -732,12 +782,12 @@ public class RotatableCanvas
     for (int i = 0; i < npoint; i++)
     {
       SequencePoint sp = (SequencePoint) points.elementAt(i);
-      int px = (int) ( (float) (sp.coord[0] - centre[0]) * scale) +
-          halfwidth;
-      int py = (int) ( (float) (sp.coord[1] - centre[1]) * scale) +
-          halfheight;
+      int px = (int) ((float) (sp.coord[0] - centre[0]) * scale)
+              + halfwidth;
+      int py = (int) ((float) (sp.coord[1] - centre[1]) * scale)
+              + halfheight;
 
-      if ( (Math.abs(px - x) < 3) && (Math.abs(py - y) < 3))
+      if ((Math.abs(px - x) < 3) && (Math.abs(py - y) < 3))
       {
         found = i;
       }
@@ -745,7 +795,7 @@ public class RotatableCanvas
 
     if (found != -1)
     {
-      return ( (SequencePoint) points.elementAt(found)).sequence;
+      return ((SequencePoint) points.elementAt(found)).sequence;
     }
     else
     {
@@ -762,8 +812,7 @@ public class RotatableCanvas
     else
     {
       return new AlignmentPanel[]
-          {
-          ap};
+      { ap };
     }
   }
 }