JAL-3048 JalviewJS compliant use of LineartOptions dialog
[jalview.git] / src / jalview / gui / LineartOptions.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.bin.Cache;
24 import jalview.util.MessageManager;
25 import jalview.util.dialogrunner.RunResponse;
26
27 import java.awt.FlowLayout;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.util.concurrent.atomic.AtomicBoolean;
31
32 import javax.swing.BorderFactory;
33 import javax.swing.ButtonGroup;
34 import javax.swing.JCheckBox;
35 import javax.swing.JLabel;
36 import javax.swing.JPanel;
37 import javax.swing.JRadioButton;
38
39 /**
40  * A dialog where the user may choose Text or Lineart rendering, and optionally
41  * save this as a preference
42  */
43 public class LineartOptions extends JPanel
44 {
45   JvOptionPane dialog;
46
47   public boolean cancelled = false;
48
49   String value;
50
51   JRadioButton lineartRB;
52
53   JCheckBox askAgainCB = new JCheckBox();
54
55   AtomicBoolean asText;
56
57   private String dialogTitle;
58
59   /**
60    * Constructor that passes in an initial choice of Text or Lineart, as a
61    * mutable boolean object. User action in the dialog should update this
62    * object, and the <em>same</em> object should be used in any action handler
63    * set by calling <code>setResponseAction</code>.
64    * 
65    * @param preferencesKey
66    *          the key under which the choice is saved as a user preference, if
67    *          'Don't ask me again' is selected
68    * @param formatType
69    *          image type e.g. EPS, SVG
70    * @param textOption
71    *          true to select Text, false for Lineart
72    */
73   public LineartOptions(String preferencesKey, String formatType,
74           AtomicBoolean textOption)
75   {
76     this.asText = textOption;
77     dialogTitle = MessageManager.formatMessage(
78             "label.select_character_style_title", formatType);
79
80     try
81     {
82       jbInit(preferencesKey, formatType);
83     } catch (Exception ex)
84     {
85       ex.printStackTrace();
86     }
87
88     dialog = JvOptionPane.newOptionDialog(Desktop.desktop);
89   }
90
91   /**
92    * Registers a Runnable action to be performed for a particular user response
93    * in the dialog
94    * 
95    * @param action
96    */
97   public void setResponseAction(RunResponse action)
98   {
99     dialog.response(action);
100   }
101
102   /**
103    * Shows the dialog, and performs any registered actions depending on the user
104    * choices
105    */
106   public void showDialog()
107   {
108     Object[] options = new Object[] { MessageManager.getString("action.ok"),
109         MessageManager.getString("action.cancel") };
110     dialog.showInternalDialog(this, dialogTitle,
111             JvOptionPane.OK_CANCEL_OPTION, JvOptionPane.PLAIN_MESSAGE, null,
112             options, MessageManager.getString("action.ok"));
113   }
114
115   /**
116    * Initialises the panel layout
117    * 
118    * @param preferencesKey
119    * @param formatType
120    * @throws Exception
121    */
122   private void jbInit(String preferencesKey, String formatType)
123           throws Exception
124   {
125     /*
126      * radio buttons for Text or Lineart - selection updates the value
127      * of field 'asText' so it is correct when used in the confirm action
128      */
129     lineartRB = new JRadioButton(MessageManager.getString("label.lineart"));
130     lineartRB.setFont(JvSwingUtils.getLabelFont());
131     lineartRB.setSelected(!asText.get());
132     lineartRB.addActionListener(new ActionListener()
133     {
134       @Override
135       public void actionPerformed(ActionEvent e)
136       {
137         asText.set(!lineartRB.isSelected());
138       }
139     });
140
141     JRadioButton textRB = new JRadioButton(
142             MessageManager.getString("action.text"));
143     textRB.setFont(JvSwingUtils.getLabelFont());
144     textRB.setSelected(asText.get());
145     textRB.addActionListener(new ActionListener()
146     {
147       @Override
148       public void actionPerformed(ActionEvent e)
149       {
150         asText.set(textRB.isSelected());
151       }
152     });
153
154     ButtonGroup bg = new ButtonGroup();
155     bg.add(lineartRB);
156     bg.add(textRB);
157
158     askAgainCB.setFont(JvSwingUtils.getLabelFont());
159     askAgainCB.setText(MessageManager.getString("label.dont_ask_me_again"));
160
161     JLabel prompt = new JLabel(MessageManager.formatMessage(
162             "label.select_character_rendering_style", formatType));
163     prompt.setFont(JvSwingUtils.getLabelFont());
164
165     this.setLayout(new FlowLayout(FlowLayout.LEFT));
166     setBorder(BorderFactory.createEtchedBorder());
167     add(prompt);
168     add(textRB);
169     add(lineartRB);
170     add(askAgainCB);
171   }
172
173   /**
174    * If "Don't ask me again" is selected, saves the selected option as the user
175    * preference, otherwise removes the existing user preference (if any) is
176    * removed
177    * 
178    * @param preferencesKey
179    */
180   protected void updatePreference(String preferencesKey)
181   {
182     value = lineartRB.isSelected() ? "Lineart" : "Text";
183
184     if (askAgainCB.isSelected())
185     {
186       Cache.setProperty(preferencesKey, value);
187     }
188     else
189     {
190       Cache.applicationProperties.remove(preferencesKey);
191     }
192   }
193
194   /**
195    * Answers "Lineart" or "Text" as selected by the user.
196    * 
197    * @return
198    */
199   public String getValue()
200   {
201     // todo remove
202     return value;
203   }
204 }