Merge branch 'develop' into spike/JAL-4047/JAL-4048_columns_in_sequenceID
[jalview.git] / src / jalview / gui / LineartOptions.java
index 1fded80..2a96eb4 100644 (file)
  */
 package jalview.gui;
 
-import jalview.bin.Cache;
-import jalview.util.MessageManager;
-
-import java.awt.BorderLayout;
+import java.awt.FlowLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import javax.swing.BorderFactory;
 import javax.swing.ButtonGroup;
-import javax.swing.JButton;
 import javax.swing.JCheckBox;
-import javax.swing.JDialog;
 import javax.swing.JLabel;
-import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JRadioButton;
 
+import jalview.bin.Cache;
+import jalview.util.MessageManager;
+
 /**
  * A dialog where the user may choose Text or Lineart rendering, and optionally
- * save this as a preference
+ * save this as a preference ("Don't ask me again")
  */
 public class LineartOptions extends JPanel
 {
-  JDialog dialog;
+  public static final String PROMPT_EACH_TIME = "Prompt each time";
+
+  JvOptionPane dialog;
 
   public boolean cancelled = false;
 
@@ -53,16 +53,31 @@ public class LineartOptions extends JPanel
 
   JCheckBox askAgainCB = new JCheckBox();
 
+  AtomicBoolean asText;
+
+  private String dialogTitle;
+
   /**
-   * Constructor
+   * Constructor that passes in an initial choice of Text or Lineart, as a
+   * mutable boolean object. User action in the dialog should update this
+   * object, and the <em>same</em> object should be used in any action handler
+   * set by calling <code>setResponseAction</code>.
+   * <p>
+   * If the user chooses an option and also "Don't ask me again", the chosen
+   * option is saved as a property with key type_RENDERING i.e. "EPS_RENDERING",
+   * "SVG_RENDERING" or "HTML_RENDERING".
    * 
-   * @param preferencesKey
-   *          the key under which the choice is saved as a user preference, if
-   *          'Don't ask me again' is selected
    * @param formatType
+   *          image type e.g. EPS, SVG
+   * @param textOption
+   *          true to select Text, false for Lineart
    */
-  public LineartOptions(String preferencesKey, String formatType)
+  public LineartOptions(String formatType, AtomicBoolean textOption)
   {
+    this.asText = textOption;
+    dialogTitle = MessageManager.formatMessage(
+            "label.select_character_style_title", formatType);
+    String preferencesKey = formatType + "_RENDERING";
     try
     {
       jbInit(preferencesKey, formatType);
@@ -71,109 +86,110 @@ public class LineartOptions extends JPanel
       ex.printStackTrace();
     }
 
-    JOptionPane pane = new JOptionPane(null, JvOptionPane.DEFAULT_OPTION,
-            JvOptionPane.DEFAULT_OPTION, null, new Object[]
-            { this });
+    dialog = JvOptionPane.newOptionDialog(Desktop.desktop);
+  }
 
-    String theTitle = MessageManager.formatMessage(
-            "label.select_character_style_title", formatType);
-    dialog = pane.createDialog(Desktop.desktop, theTitle);
-    dialog.setVisible(true);
+  /**
+   * Registers a Runnable action to be performed for a particular user response
+   * in the dialog
+   * 
+   * @param action
+   */
+  public void setResponseAction(Object response, Runnable action)
+  {
+    dialog.setResponseHandler(response, action);
+  }
+
+  /**
+   * Shows the dialog, and performs any registered actions depending on the user
+   * choices
+   */
+  public void showDialog()
+  {
+    Object[] options = new Object[] { MessageManager.getString("action.ok"),
+        MessageManager.getString("action.cancel") };
+    dialog.showInternalDialog(this, dialogTitle,
+            JvOptionPane.OK_CANCEL_OPTION, JvOptionPane.PLAIN_MESSAGE, null,
+            options, MessageManager.getString("action.ok"));
   }
 
+  /**
+   * Initialises the panel layout
+   * 
+   * @param preferencesKey
+   * @param formatType
+   * @throws Exception
+   */
   private void jbInit(String preferencesKey, String formatType)
           throws Exception
   {
+    /*
+     * radio buttons for Text or Lineart - selection updates the value
+     * of field 'asText' so it is correct when used in the confirm action
+     */
     lineartRB = new JRadioButton(MessageManager.getString("label.lineart"));
     lineartRB.setFont(JvSwingUtils.getLabelFont());
-    JRadioButton text = new JRadioButton(
-            MessageManager.getString("action.text"));
-    text.setFont(JvSwingUtils.getLabelFont());
-    text.setSelected(true);
-
-    ButtonGroup bg = new ButtonGroup();
-    bg.add(lineartRB);
-    bg.add(text);
-
-    askAgainCB.setFont(JvSwingUtils.getLabelFont());
-    askAgainCB.setText(MessageManager.getString("label.dont_ask_me_again"));
-    JButton ok = new JButton(MessageManager.getString("action.ok"));
-    ok.addActionListener(new ActionListener()
+    lineartRB.setSelected(!asText.get());
+    lineartRB.addActionListener(new ActionListener()
     {
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        ok_actionPerformed(preferencesKey);
+        asText.set(!lineartRB.isSelected());
       }
     });
-    JButton cancel = new JButton(MessageManager.getString("action.cancel"));
-    cancel.addActionListener(new ActionListener()
+
+    JRadioButton textRB = new JRadioButton(
+            MessageManager.getString("action.text"));
+    textRB.setFont(JvSwingUtils.getLabelFont());
+    textRB.setSelected(asText.get());
+    textRB.addActionListener(new ActionListener()
     {
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        cancel_actionPerformed(e);
+        asText.set(textRB.isSelected());
       }
     });
-    JLabel jLabel1 = new JLabel(MessageManager.formatMessage(
+
+    ButtonGroup bg = new ButtonGroup();
+    bg.add(lineartRB);
+    bg.add(textRB);
+
+    askAgainCB.setFont(JvSwingUtils.getLabelFont());
+    askAgainCB.setText(MessageManager.getString("label.dont_ask_me_again"));
+
+    JLabel prompt = new JLabel(MessageManager.formatMessage(
             "label.select_character_rendering_style", formatType));
-    jLabel1.setFont(JvSwingUtils.getLabelFont());
-
-    this.setLayout(new BorderLayout());
-    JPanel jPanel3 = new JPanel();
-    jPanel3.setBorder(BorderFactory.createEtchedBorder());
-    JPanel optionsPanel = new JPanel();
-    optionsPanel.add(text);
-    optionsPanel.add(lineartRB);
-    optionsPanel.add(askAgainCB);
-    JPanel okCancelPanel = new JPanel();
-    okCancelPanel.add(ok);
-    okCancelPanel.add(cancel);
-    jPanel3.add(jLabel1);
-    jPanel3.add(optionsPanel);
-    this.add(jPanel3, BorderLayout.CENTER);
-    this.add(okCancelPanel, BorderLayout.SOUTH);
+    prompt.setFont(JvSwingUtils.getLabelFont());
+
+    this.setLayout(new FlowLayout(FlowLayout.LEFT));
+    setBorder(BorderFactory.createEtchedBorder());
+    add(prompt);
+    add(textRB);
+    add(lineartRB);
+    add(askAgainCB);
   }
 
   /**
-   * Action on OK is to save the selected option as the <code>value</code> field
-   * and close the dialog. If "Don't ask me again" is selected, it is also saved
-   * as user preference, otherwise the existing user preference (if any) is
-   * removed.
+   * If "Don't ask me again" is selected, saves the selected option as the user
+   * preference, otherwise removes the existing user preference (if any) is
+   * removed
    * 
    * @param preferencesKey
    */
-  protected void ok_actionPerformed(String preferencesKey)
+  protected void updatePreference(String preferencesKey)
   {
-    if (lineartRB.isSelected())
-    {
-      value = "Lineart";
-    }
-    else
-    {
-      value = "Text";
-    }
+    value = lineartRB.isSelected() ? "Lineart" : "Text";
 
-    if (!askAgainCB.isSelected())
+    if (askAgainCB.isSelected())
     {
-      Cache.applicationProperties.remove(preferencesKey);
+      Cache.setProperty(preferencesKey, value);
     }
     else
     {
-      Cache.setProperty(preferencesKey, value);
+      Cache.applicationProperties.remove(preferencesKey);
     }
-    dialog.setVisible(false);
-  }
-
-  /**
-   * Action on Cancel is to hide the dialog
-   * 
-   * @param e
-   */
-  protected void cancel_actionPerformed(ActionEvent e)
-  {
-    cancelled = true;
-    dialog.setVisible(false);
   }
 
   /**
@@ -183,6 +199,7 @@ public class LineartOptions extends JPanel
    */
   public String getValue()
   {
+    // todo remove
     return value;
   }
 }