2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import jalview.bin.Cache;
24 import jalview.bin.Console;
26 import java.awt.Component;
28 public class PromptUserConfig implements Runnable
31 * Given a boolean Cache option:
33 * 1. Prompt the user with the given text if the option is unset, and set the
34 * option accordingly (yes/no==true/false).
36 * 2. Execute the given Runnables according to the state of the config option.
40 * boolean property to set
42 String property = null;
45 * can the user cancel rather than set the property ?
47 boolean allowCancel = false;
50 * title of prompt dialog
60 * runnables for all cases.
62 Runnable iftrue = null, iffalse = null, ifundef = null;
64 private Component component;
67 * if set, remove the property if the user says no rather than setting it to
70 private boolean removeifunset;
73 * @return the removeifunset
75 public boolean isRemoveifunset()
81 * @param removeifunset
82 * the removeifunset to set
84 public void setRemoveifunset(boolean removeifunset)
86 this.removeifunset = removeifunset;
91 * - where the dialog box will be shown
93 * - boolean property in Cache
95 * - title of prompt box
99 * - executed if property is true
101 * - executed if property is false
103 * - executed if property was not set after prompting.
105 * - allow the user to cancel rather than set the property
107 public PromptUserConfig(Component desktop, String property,
108 String dialogTitle, String dialogText, Runnable iftrue,
109 Runnable iffalse, Runnable ifundef, boolean allowCancel)
112 this.component = desktop;
113 this.property = property;
114 this.dialogTitle = dialogTitle;
115 this.dialogText = dialogText;
116 this.iftrue = iftrue;
117 this.iffalse = iffalse;
118 this.ifundef = ifundef;
119 this.allowCancel = allowCancel;
125 if (property == null)
129 // First - check to see if wee have an old questionnaire/response id pair.
130 String lastq = Cache.getProperty(property);
135 Console.debug("Got user response.");
137 lastq = Cache.getProperty(property);
142 // execute the ifundef
149 } catch (Exception ex)
152 extype = "undefined";
155 else if (Boolean.valueOf(lastq).booleanValue())
157 // execute the iftrue
164 } catch (Exception ex)
178 } catch (Exception ex)
184 // report any exceptions
187 Console.warn("Unexpected exception when executing the " + extype
188 + " runnable for property " + property, e);
193 * raise the property dialog
195 private void raiseDialog()
197 if (Console.isDebugEnabled())
199 Console.debug("Prompting user for " + dialogTitle
200 + " for Cache property " + property);
204 int reply = JvOptionPane.showConfirmDialog(Desktop.getDesktopPane(), // component,
205 dialogText, dialogTitle,
206 (allowCancel) ? JvOptionPane.YES_NO_CANCEL_OPTION
207 : JvOptionPane.YES_NO_OPTION,
208 JvOptionPane.QUESTION_MESSAGE);
210 // and finish parsing the result
211 Console.debug("Got response : " + reply);
212 if (reply == JvOptionPane.YES_OPTION)
214 Cache.setProperty(property, "true");
216 else if (reply == JvOptionPane.NO_OPTION)
220 Cache.removeProperty(property);
224 Cache.setProperty(property, "false");
229 Console.debug("User cancelled setting " + property);
232 // verify the property is set for debugging
233 if (Console.isDebugEnabled())
236 "User set property to " + Cache.getProperty(property));
238 } catch (Exception e)
241 "Unexpected exception when prompting user for yes/no setting for property "