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