Amend features/annotations in applet
[jalview.git] / src / jalview / appletgui / AnnotationLabels.java
index cb17e64..a952f9d 100755 (executable)
@@ -35,15 +35,16 @@ public class AnnotationLabels
   AlignViewport av;\r
   boolean resizing = false;\r
   int oldY, mouseX;\r
-  static String EDITNAME = "Edit label/description";\r
-  static String HIDE = "Hide this row";\r
-  static String DELETE = "Delete this row";\r
-  static String SHOWALL = "Show all hidden rows";\r
+\r
+  static String ADDNEW = "Add New Row";\r
+  static String EDITNAME = "Edit Label/Description";\r
+  static String HIDE = "Hide This Row";\r
+  static String SHOWALL = "Show All Hidden Rows";\r
   static String OUTPUT_TEXT = "Show Values In Textbox";\r
   static String COPYCONS_SEQ = "Copy Consensus Sequence";\r
 \r
-  int selectedRow = 0;\r
   int scrollOffset = 0;\r
+  int selectedRow = -1;\r
 \r
   Tooltip tooltip;\r
 \r
@@ -67,14 +68,14 @@ public class AnnotationLabels
     repaint();\r
   }\r
 \r
-  void getSelectedRow(int y)\r
+  int getSelectedRow(int y)\r
   {\r
-    selectedRow = -1;\r
+    int row = -1;\r
     AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();\r
 \r
     if (aa == null)\r
     {\r
-      return;\r
+      return row;\r
     }\r
 \r
     int height = 0;\r
@@ -88,34 +89,37 @@ public class AnnotationLabels
       height += aa[i].height;\r
       if (y < height)\r
       {\r
-        selectedRow = i;\r
+        row = i;\r
         break;\r
       }\r
     }\r
+\r
+    return row;\r
   }\r
 \r
   public void actionPerformed(ActionEvent evt)\r
   {\r
     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();\r
 \r
-    if (evt.getActionCommand().equals(EDITNAME))\r
+    if (evt.getActionCommand().equals(ADDNEW))\r
     {\r
-      EditNameDialog dialog = new EditNameDialog(\r
-          aa[selectedRow].label,\r
-          aa[selectedRow].description,\r
-          "       Annotation Label",\r
-          "Annotation Description",\r
-          ap,\r
-          "Edit Annotation Name / Description");\r
-\r
-      if (dialog.accept)\r
+      AlignmentAnnotation newAnnotation = new AlignmentAnnotation("",\r
+          null,\r
+          new Annotation[ap.av.alignment.getWidth()]);\r
+\r
+      if (!editLabelDescription(newAnnotation))\r
       {\r
-        aa[selectedRow].label = dialog.getName();\r
-        aa[selectedRow].description = dialog.getDescription();\r
-        repaint();\r
+        return;\r
       }\r
+\r
+      ap.av.alignment.addAnnotation(newAnnotation);\r
+      ap.av.alignment.setAnnotationIndex(newAnnotation, 0);\r
+    }\r
+    else if (evt.getActionCommand().equals(EDITNAME))\r
+    {\r
+      editLabelDescription(aa[selectedRow]);\r
     }\r
-    if (evt.getActionCommand().equals(HIDE))\r
+    else if (evt.getActionCommand().equals(HIDE))\r
     {\r
       aa[selectedRow].visible = false;\r
     }\r
@@ -151,23 +155,46 @@ public class AnnotationLabels
     ap.paintAlignment(true);\r
   }\r
 \r
+  boolean editLabelDescription(AlignmentAnnotation annotation)\r
+  {\r
+    EditNameDialog dialog = new EditNameDialog(\r
+        annotation.label,\r
+        annotation.description,\r
+        "       Annotation Label",\r
+        "Annotation Description",\r
+        ap.alignFrame,\r
+        "Edit Annotation Name / Description",\r
+        500, 100);\r
+\r
+    if (dialog.accept)\r
+    {\r
+      annotation.label = dialog.getName();\r
+      annotation.description = dialog.getDescription();\r
+      repaint();\r
+      return true;\r
+    }\r
+    else\r
+      return false;\r
+\r
+  }\r
+\r
   public void mouseMoved(MouseEvent evt)\r
   {\r
-    getSelectedRow(evt.getY() - scrollOffset);\r
+    int row = getSelectedRow(evt.getY() - scrollOffset);\r
 \r
-    if (selectedRow > -1)\r
+    if (row > -1)\r
     {\r
       if (tooltip == null)\r
       {\r
         tooltip = new Tooltip(ap.av.alignment.\r
-                              getAlignmentAnnotation()[selectedRow].\r
+                              getAlignmentAnnotation()[row].\r
                               description,\r
                               this);\r
       }\r
       else\r
       {\r
         tooltip.setTip(ap.av.alignment.\r
-                       getAlignmentAnnotation()[selectedRow].description);\r
+                       getAlignmentAnnotation()[row].description);\r
       }\r
     }\r
     else if (tooltip != null)\r
@@ -194,27 +221,32 @@ public class AnnotationLabels
 \r
   public void mousePressed(MouseEvent evt)\r
   {\r
+    selectedRow = getSelectedRow(evt.getY() - scrollOffset);\r
+\r
     AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();\r
 \r
-    PopupMenu pop = new PopupMenu("Annotations");\r
+    PopupMenu popup = new PopupMenu("Annotations");\r
 \r
-    MenuItem item = new MenuItem(EDITNAME);\r
+    MenuItem item = new MenuItem(ADDNEW);\r
+    item.addActionListener(this);\r
+    popup.add(item);\r
+    item = new MenuItem(EDITNAME);\r
     item.addActionListener(this);\r
-    pop.add(item);\r
+    popup.add(item);\r
     item = new MenuItem(HIDE);\r
     item.addActionListener(this);\r
-    pop.add(item);\r
+    popup.add(item);\r
     item = new MenuItem(SHOWALL);\r
     item.addActionListener(this);\r
-    pop.add(item);\r
-    this.add(pop);\r
+    popup.add(item);\r
+    this.add(popup);\r
     item = new MenuItem(OUTPUT_TEXT);\r
     item.addActionListener(this);\r
-    pop.add(item);\r
+    popup.add(item);\r
 \r
     if (aa[selectedRow] == ap.av.consensus)\r
     {\r
-      pop.addSeparator();\r
+      popup.addSeparator();\r
       final CheckboxMenuItem cbmi = new CheckboxMenuItem(\r
           "Ignore Gaps In Consensus",\r
           ap.av.getIgnoreGapsConsensus());\r
@@ -227,13 +259,13 @@ public class AnnotationLabels
           ap.paintAlignment(true);\r
         }\r
       });\r
-      pop.add(cbmi);\r
-      final MenuItem cpcons = new MenuItem(COPYCONS_SEQ);\r
-      cpcons.addActionListener(this);\r
-      pop.add(cpcons);\r
+      popup.add(cbmi);\r
+      item = new MenuItem(COPYCONS_SEQ);\r
+      item.addActionListener(this);\r
+      popup.add(item);\r
     }\r
 \r
-    pop.show(this, evt.getX(), evt.getY());\r
+    popup.show(this, evt.getX(), evt.getY());\r
 \r
   }\r
 \r