JAL-3210 Improvements to eclipse detection. New src tree and SwingJS updated from...
[jalview.git] / src / jalview / gui / FeatureEditor.java
index 00ea668..4fe9a65 100644 (file)
@@ -10,7 +10,6 @@ import jalview.io.FeaturesFile;
 import jalview.schemes.FeatureColour;
 import jalview.util.ColorUtils;
 import jalview.util.MessageManager;
-import jalview.util.dialogrunner.RunResponse;
 
 import java.awt.BorderLayout;
 import java.awt.Color;
@@ -33,7 +32,10 @@ import javax.swing.JScrollPane;
 import javax.swing.JSpinner;
 import javax.swing.JTextArea;
 import javax.swing.JTextField;
+import javax.swing.SpinnerNumberModel;
 import javax.swing.SwingConstants;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
 import javax.swing.event.DocumentEvent;
 import javax.swing.event.DocumentListener;
 
@@ -80,8 +82,6 @@ public class FeatureEditor
 
   AlignmentPanel ap;
 
-  Object[] options;
-
   JTextField name;
 
   JTextField group;
@@ -99,7 +99,12 @@ public class FeatureEditor
    * 
    * @param alignPanel
    * @param seqs
+   *          one or more Sequence if create is true; a singleton if create is
+   *          false
    * @param feats
+   *          a list of new SequenceFeature instances if create is true,
+   *          otherwise a list of known features for this sequence at this
+   *          position
    * @param create
    *          if true create a new feature, else amend or delete an existing
    *          feature
@@ -170,10 +175,34 @@ public class FeatureEditor
     });
 
     description = new JTextArea(3, 25);
+    
     start = new JSpinner();
     end = new JSpinner();
     start.setPreferredSize(new Dimension(80, 20));
     end.setPreferredSize(new Dimension(80, 20));
+    
+    /*
+     * ensure that start can never be more than end
+     */
+    start.addChangeListener(new ChangeListener() 
+    {
+      @Override
+      public void stateChanged(ChangeEvent e)
+      {
+        Integer startVal = (Integer) start.getValue(); 
+        ((SpinnerNumberModel) end.getModel()).setMinimum(startVal);
+      }
+    });
+    end.addChangeListener(new ChangeListener() 
+    {
+      @Override
+      public void stateChanged(ChangeEvent e)
+      {
+        Integer endVal = (Integer) end.getValue(); 
+        ((SpinnerNumberModel) start.getModel()).setMaximum(endVal);
+      }
+    });
+
     final JLabel colour = new JLabel();
     colour.setOpaque(true);
     colour.setMaximumSize(new Dimension(30, 16));
@@ -198,7 +227,7 @@ public class FeatureEditor
               updateColourButton(mainPanel, colour, featureColour);
             };
           };
-          JalviewColourChooser.showColourChooser(Desktop.getDesktop(),
+          JalviewColourChooser.showColourChooser(Desktop.getDesktopPane(),
                   title, featureColour.getColour(), listener);
         }
         else
@@ -268,6 +297,8 @@ public class FeatureEditor
             group.setText(sf.getFeatureGroup());
             start.setValue(new Integer(sf.getBegin()));
             end.setValue(new Integer(sf.getEnd()));
+            ((SpinnerNumberModel) start.getModel()).setMaximum(sf.getEnd());
+            ((SpinnerNumberModel) end.getModel()).setMinimum(sf.getBegin());
 
             SearchResultsI highlight = new SearchResults();
             highlight.addResult(sequences.get(0), sf.getBegin(),
@@ -355,29 +386,22 @@ public class FeatureEditor
 
     start.setValue(new Integer(firstFeature.getBegin()));
     end.setValue(new Integer(firstFeature.getEnd()));
+    ((SpinnerNumberModel) start.getModel()).setMaximum(firstFeature.getEnd());
+    ((SpinnerNumberModel) end.getModel()).setMinimum(firstFeature.getBegin());
+
     description.setText(firstFeature.getDescription());
     featureColour = fr.getFeatureStyle(featureType);
     oldColour = featureColour;
     updateColourButton(mainPanel, colour, oldColour);
-    if (forCreate)
-    {
-      options = new Object[] { MessageManager.getString("action.ok"),
-          MessageManager.getString("action.cancel") };
-    }
-    else
-    {
-      options = new Object[] { MessageManager.getString("label.amend"),
-          MessageManager.getString("action.delete"),
-          MessageManager.getString("action.cancel") };
-    }
   }
 
   /**
    * Presents a dialog allowing the user to add new features, or amend or delete
    * an existing feature. Currently this can be on
    * <ul>
-   * <li>double-click on a sequence - Amend/Delete features at position</li>
-   * <li>Create sequence feature from pop-up menu on selected region</li>
+   * <li>double-click on a sequence - Amend/Delete a selected feature at the
+   * position</li>
+   * <li>Create sequence feature(s) from pop-up menu on selected region</li>
    * <li>Create features for pattern matches from Find</li>
    * </ul>
    * If the supplied feature type is null, show (and update on confirm) the type
@@ -386,22 +410,43 @@ public class FeatureEditor
    */
   public void showDialog()
   {
-    RunResponse okAction = forCreate ? getCreateAction() : getAmendAction();
-    RunResponse deleteAction = getDeleteAction();
-    Runnable cancelAction = getCancelAction();
-
-    String title = forCreate
-            ? MessageManager.getString("label.create_new_sequence_features")
-            : MessageManager.formatMessage("label.amend_delete_features",
-                    new String[]
-                    { sequences.get(0).getName() });
-
-    JvOptionPane.newOptionDialog(Desktop.desktop).response(okAction)
-            .response(deleteAction).defaultResponse(cancelAction)
-            .showInternalDialog(mainPanel, title,
-                    JvOptionPane.YES_NO_CANCEL_OPTION,
-                    JvOptionPane.QUESTION_MESSAGE, null, options,
-                    MessageManager.getString("action.ok"));
+         Runnable okAction = forCreate ? getCreateAction() : getAmendAction();
+         Runnable cancelAction = getCancelAction();
+
+    /*
+     * set dialog action handlers for OK (create/Amend) and Cancel options
+     * also for Delete if applicable (when amending features)
+     */
+    JvOptionPane dialog = JvOptionPane.newOptionDialog(Desktop.getDesktopPane())
+            .setResponseHandler(0, okAction).setResponseHandler(2, cancelAction);
+    if (!forCreate)
+    {
+      dialog.setResponseHandler(1, getDeleteAction());
+    }
+
+    String title = null;
+    Object[] options = null;
+    if (forCreate)
+    {
+      title = MessageManager
+              .getString("label.create_new_sequence_features");
+      options = new Object[] { MessageManager.getString("action.ok"),
+          MessageManager.getString("action.cancel") };
+    }
+    else
+    {
+      title = MessageManager.formatMessage("label.amend_delete_features",
+              new String[]
+              { sequences.get(0).getName() });
+      options = new Object[] { MessageManager.getString("label.amend"),
+          MessageManager.getString("action.delete"),
+          MessageManager.getString("action.cancel") };
+    }
+
+    dialog.showInternalDialog(mainPanel, title,
+            JvOptionPane.YES_NO_CANCEL_OPTION,
+            JvOptionPane.PLAIN_MESSAGE, null, options,
+            MessageManager.getString("action.ok"));
   }
 
   /**
@@ -414,15 +459,16 @@ public class FeatureEditor
    */
   protected Runnable getCancelAction()
   {
-    Runnable defaultResponse = new Runnable()
+       Runnable okAction = new Runnable()
     {
+      @Override
       public void run()
       {
         ap.highlightSearchResults(null);
         ap.paintAlignment(false, false);
       }
     };
-    return defaultResponse;
+    return okAction;
   }
 
   /**
@@ -435,12 +481,13 @@ public class FeatureEditor
    * 
    * @return
    */
-  protected RunResponse getCreateAction()
+  protected Runnable getCreateAction()
   {
-    RunResponse okAction = new RunResponse(JvOptionPane.OK_OPTION)
+       Runnable okAction = new Runnable()
     {
       boolean useLastDefaults = features.get(0).getType() == null;
 
+      @Override
       public void run()
       {
         final String enteredType = name.getText().trim();
@@ -493,10 +540,11 @@ public class FeatureEditor
    * 
    * @return
    */
-  protected RunResponse getDeleteAction()
+  protected Runnable getDeleteAction()
   {
-    RunResponse deleteAction = new RunResponse(JvOptionPane.NO_OPTION)
+         Runnable deleteAction = new Runnable()
     {
+      @Override
       public void run()
       {
         SequenceFeature sf = features.get(featureIndex);
@@ -595,9 +643,9 @@ public class FeatureEditor
    * 
    * @return
    */
-  protected RunResponse getAmendAction()
+  protected Runnable getAmendAction()
   {
-    RunResponse okAction = new RunResponse(JvOptionPane.OK_OPTION)
+       Runnable okAction = new Runnable()
     {
       boolean useLastDefaults = features.get(0).getType() == null;
   
@@ -605,6 +653,7 @@ public class FeatureEditor
   
       String featureGroup = group.getText();
   
+      @Override
       public void run()
       {
         final String enteredType = name.getText().trim();