d55733c359f3443a5d7d01951ae76d6c99d697fa
[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
26 import java.awt.FlowLayout;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.util.concurrent.atomic.AtomicBoolean;
30
31 import javax.swing.BorderFactory;
32 import javax.swing.ButtonGroup;
33 import javax.swing.JCheckBox;
34 import javax.swing.JLabel;
35 import javax.swing.JPanel;
36 import javax.swing.JRadioButton;
37
38 /**
39  * A dialog where the user may choose Text or Lineart rendering, and optionally
40  * save this as a preference ("Don't ask me again")
41  */
42 public class LineartOptions extends JPanel
43 {
44   static final String PROMPT_EACH_TIME = "Prompt each time";
45
46   JvOptionPane dialog;
47
48   public boolean cancelled = false;
49
50   String value;
51
52   JRadioButton lineartRB;
53
54   JCheckBox askAgainCB = new JCheckBox();
55
56   AtomicBoolean asText;
57
58   private String dialogTitle;
59
60   /**
61    * Constructor that passes in an initial choice of Text or Lineart, as a
62    * mutable boolean object. User action in the dialog should update this
63    * object, and the <em>same</em> object should be used in any action handler
64    * set by calling <code>setResponseAction</code>.
65    * <p>
66    * If the user chooses an option and also "Don't ask me again", the chosen
67    * option is saved as a property with key type_RENDERING i.e. "EPS_RENDERING",
68    * "SVG_RENDERING" or "HTML_RENDERING".
69    * 
70    * @param formatType
71    *          image type e.g. EPS, SVG
72    * @param textOption
73    *          true to select Text, false for Lineart
74    */
75   public LineartOptions(String formatType, AtomicBoolean textOption)
76   {
77     this.asText = textOption;
78     dialogTitle = MessageManager.formatMessage(
79             "label.select_character_style_title", formatType);
80     String preferencesKey = formatType + "_RENDERING";
81     try
82     {
83       jbInit(preferencesKey, formatType);
84     } catch (Exception ex)
85     {
86       ex.printStackTrace();
87     }
88
89     dialog = JvOptionPane.newOptionDialog(Desktop.desktop);
90   }
91
92   /**
93    * Registers a Runnable action to be performed for a particular user response
94    * in the dialog
95    * 
96    * @param action
97    */
98   public void setResponseAction(Object response, Runnable action)
99   {
100     dialog.setResponseHandler(response, action);
101   }
102
103   /**
104    * Shows the dialog, and performs any registered actions depending on the user
105    * choices
106    */
107   public void showDialog()
108   {
109     Object[] options = new Object[] { MessageManager.getString("action.ok"),
110         MessageManager.getString("action.cancel") };
111     dialog.showInternalDialog(this, dialogTitle,
112             JvOptionPane.OK_CANCEL_OPTION, JvOptionPane.PLAIN_MESSAGE, null,
113             options, MessageManager.getString("action.ok"));
114   }
115
116   /**
117    * Initialises the panel layout
118    * 
119    * @param preferencesKey
120    * @param formatType
121    * @throws Exception
122    */
123   private void jbInit(String preferencesKey, String formatType)
124           throws Exception
125   {
126     /*
127      * radio buttons for Text or Lineart - selection updates the value
128      * of field 'asText' so it is correct when used in the confirm action
129      */
130     lineartRB = new JRadioButton(MessageManager.getString("label.lineart"));
131     lineartRB.setFont(JvSwingUtils.getLabelFont());
132     lineartRB.setSelected(!asText.get());
133     lineartRB.addActionListener(new ActionListener()
134     {
135       @Override
136       public void actionPerformed(ActionEvent e)
137       {
138         asText.set(!lineartRB.isSelected());
139       }
140     });
141
142     JRadioButton textRB = new JRadioButton(
143             MessageManager.getString("action.text"));
144     textRB.setFont(JvSwingUtils.getLabelFont());
145     textRB.setSelected(asText.get());
146     textRB.addActionListener(new ActionListener()
147     {
148       @Override
149       public void actionPerformed(ActionEvent e)
150       {
151         asText.set(textRB.isSelected());
152       }
153     });
154
155     ButtonGroup bg = new ButtonGroup();
156     bg.add(lineartRB);
157     bg.add(textRB);
158
159     askAgainCB.setFont(JvSwingUtils.getLabelFont());
160     askAgainCB.setText(MessageManager.getString("label.dont_ask_me_again"));
161
162     JLabel prompt = new JLabel(MessageManager.formatMessage(
163             "label.select_character_rendering_style", formatType));
164     prompt.setFont(JvSwingUtils.getLabelFont());
165
166     this.setLayout(new FlowLayout(FlowLayout.LEFT));
167     setBorder(BorderFactory.createEtchedBorder());
168     add(prompt);
169     add(textRB);
170     add(lineartRB);
171     add(askAgainCB);
172   }
173
174   /**
175    * If "Don't ask me again" is selected, saves the selected option as the user
176    * preference, otherwise removes the existing user preference (if any) is
177    * removed
178    * 
179    * @param preferencesKey
180    */
181   protected void updatePreference(String preferencesKey)
182   {
183     value = lineartRB.isSelected() ? "Lineart" : "Text";
184
185     if (askAgainCB.isSelected())
186     {
187       Cache.setProperty(preferencesKey, value);
188     }
189     else
190     {
191       Cache.applicationProperties.remove(preferencesKey);
192     }
193   }
194
195   /**
196    * Answers "Lineart" or "Text" as selected by the user.
197    * 
198    * @return
199    */
200   public String getValue()
201   {
202     // todo remove
203     return value;
204   }
205 }