JAL-3068 LineartOptions for EPS/HTML/SVG, new preferences, i18n
[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.BorderLayout;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29
30 import javax.swing.BorderFactory;
31 import javax.swing.ButtonGroup;
32 import javax.swing.JButton;
33 import javax.swing.JCheckBox;
34 import javax.swing.JDialog;
35 import javax.swing.JLabel;
36 import javax.swing.JOptionPane;
37 import javax.swing.JPanel;
38 import javax.swing.JRadioButton;
39
40 /**
41  * A dialog where the user may choose Text or Lineart rendering, and optionally
42  * save this as a preference
43  */
44 public class LineartOptions extends JPanel
45 {
46   JDialog dialog;
47
48   public boolean cancelled = false;
49
50   String value;
51
52   JRadioButton lineartRB;
53
54   JCheckBox askAgainCB = new JCheckBox();
55
56   /**
57    * Constructor
58    * 
59    * @param preferencesKey
60    *          the key under which the choice is saved as a user preference, if
61    *          'Don't ask me again' is selected
62    * @param formatType
63    */
64   public LineartOptions(String preferencesKey, String formatType)
65   {
66     try
67     {
68       jbInit(preferencesKey, formatType);
69     } catch (Exception ex)
70     {
71       ex.printStackTrace();
72     }
73
74     JOptionPane pane = new JOptionPane(null, JvOptionPane.DEFAULT_OPTION,
75             JvOptionPane.DEFAULT_OPTION, null, new Object[]
76             { this });
77
78     String theTitle = MessageManager.formatMessage(
79             "label.select_character_style_title", formatType);
80     dialog = pane.createDialog(Desktop.desktop, theTitle);
81     dialog.setVisible(true);
82   }
83
84   private void jbInit(String preferencesKey, String formatType)
85           throws Exception
86   {
87     lineartRB = new JRadioButton(MessageManager.getString("label.lineart"));
88     lineartRB.setFont(JvSwingUtils.getLabelFont());
89     JRadioButton text = new JRadioButton(
90             MessageManager.getString("action.text"));
91     text.setFont(JvSwingUtils.getLabelFont());
92     text.setSelected(true);
93
94     ButtonGroup bg = new ButtonGroup();
95     bg.add(lineartRB);
96     bg.add(text);
97
98     askAgainCB.setFont(JvSwingUtils.getLabelFont());
99     askAgainCB.setText(MessageManager.getString("label.dont_ask_me_again"));
100     JButton ok = new JButton(MessageManager.getString("action.ok"));
101     ok.addActionListener(new ActionListener()
102     {
103       @Override
104       public void actionPerformed(ActionEvent e)
105       {
106         ok_actionPerformed(preferencesKey);
107       }
108     });
109     JButton cancel = new JButton(MessageManager.getString("action.cancel"));
110     cancel.addActionListener(new ActionListener()
111     {
112       @Override
113       public void actionPerformed(ActionEvent e)
114       {
115         cancel_actionPerformed(e);
116       }
117     });
118     JLabel jLabel1 = new JLabel(MessageManager.formatMessage(
119             "label.select_character_rendering_style", formatType));
120     jLabel1.setFont(JvSwingUtils.getLabelFont());
121
122     this.setLayout(new BorderLayout());
123     JPanel jPanel3 = new JPanel();
124     jPanel3.setBorder(BorderFactory.createEtchedBorder());
125     JPanel optionsPanel = new JPanel();
126     optionsPanel.add(text);
127     optionsPanel.add(lineartRB);
128     optionsPanel.add(askAgainCB);
129     JPanel okCancelPanel = new JPanel();
130     okCancelPanel.add(ok);
131     okCancelPanel.add(cancel);
132     jPanel3.add(jLabel1);
133     jPanel3.add(optionsPanel);
134     this.add(jPanel3, BorderLayout.CENTER);
135     this.add(okCancelPanel, BorderLayout.SOUTH);
136   }
137
138   /**
139    * Action on OK is to save the selected option as the <code>value</code> field
140    * and close the dialog. If "Don't ask me again" is selected, it is also saved
141    * as user preference, otherwise the existing user preference (if any) is
142    * removed.
143    * 
144    * @param preferencesKey
145    */
146   protected void ok_actionPerformed(String preferencesKey)
147   {
148     if (lineartRB.isSelected())
149     {
150       value = "Lineart";
151     }
152     else
153     {
154       value = "Text";
155     }
156
157     if (!askAgainCB.isSelected())
158     {
159       Cache.applicationProperties.remove(preferencesKey);
160     }
161     else
162     {
163       Cache.setProperty(preferencesKey, value);
164     }
165     dialog.setVisible(false);
166   }
167
168   /**
169    * Action on Cancel is to hide the dialog
170    * 
171    * @param e
172    */
173   protected void cancel_actionPerformed(ActionEvent e)
174   {
175     cancelled = true;
176     dialog.setVisible(false);
177   }
178
179   /**
180    * Answers "Lineart" or "Text" as selected by the user.
181    * 
182    * @return
183    */
184   public String getValue()
185   {
186     return value;
187   }
188 }