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;
25 import java.awt.Component;
27 public class PromptUserConfig implements Runnable
30 * Given a boolean Cache option:
32 * 1. Prompt the user with the given text if the option is unset, and set the
33 * option accordingly (yes/no==true/false).
35 * 2. Execute the given Runnables according to the state of the config option.
39 * boolean property to set
41 String property = null;
44 * can the user cancel rather than set the property ?
46 boolean allowCancel = false;
49 * title of prompt dialog
59 * runnables for all cases.
61 Runnable iftrue = null, iffalse = null, ifundef = null;
63 private Component component;
66 * if set, remove the property if the user says no rather than setting it to
69 private boolean removeifunset;
72 * @return the removeifunset
74 public boolean isRemoveifunset()
80 * @param removeifunset
81 * the removeifunset to set
83 public void setRemoveifunset(boolean removeifunset)
85 this.removeifunset = removeifunset;
90 * - where the dialog box will be shown
92 * - boolean property in jalview.bin.Cache
94 * - title of prompt box
98 * - executed if property is true
100 * - executed if property is false
102 * - executed if property was not set after prompting.
104 * - allow the user to cancel rather than set the property
106 public PromptUserConfig(Component desktop, String property,
107 String dialogTitle, String dialogText, Runnable iftrue,
108 Runnable iffalse, Runnable ifundef, boolean allowCancel)
111 this.component = desktop;
112 this.property = property;
113 this.dialogTitle = dialogTitle;
114 this.dialogText = dialogText;
115 this.iftrue = iftrue;
116 this.iffalse = iffalse;
117 this.ifundef = ifundef;
118 this.allowCancel = allowCancel;
124 if (property == null)
128 // First - check to see if wee have an old questionnaire/response id pair.
129 String lastq = jalview.bin.Cache.getProperty(property);
134 Cache.log.debug("Got user response.");
136 lastq = jalview.bin.Cache.getProperty(property);
141 // execute the ifundef
148 } catch (Exception ex)
151 extype = "undefined";
154 else if (Boolean.valueOf(lastq).booleanValue())
156 // execute the iftrue
163 } catch (Exception ex)
177 } catch (Exception ex)
183 // report any exceptions
186 Cache.log.warn("Unexpected exception when executing the " + extype
187 + " runnable for property " + property, e);
192 * raise the property dialog
194 private void raiseDialog()
196 if (jalview.bin.Cache.log.isDebugEnabled())
198 jalview.bin.Cache.log.debug("Prompting user for " + dialogTitle
199 + " for Cache property " + property);
203 int reply = JvOptionPane.showConfirmDialog(Desktop.desktop, // component,
204 dialogText, dialogTitle,
205 (allowCancel) ? JvOptionPane.YES_NO_CANCEL_OPTION
206 : JvOptionPane.YES_NO_OPTION,
207 JvOptionPane.QUESTION_MESSAGE);
209 // and finish parsing the result
210 jalview.bin.Cache.log.debug("Got response : " + reply);
211 if (reply == JvOptionPane.YES_OPTION)
213 jalview.bin.Cache.setProperty(property, "true");
215 else if (reply == JvOptionPane.NO_OPTION)
219 jalview.bin.Cache.removeProperty(property);
223 jalview.bin.Cache.setProperty(property, "false");
228 jalview.bin.Cache.log.debug("User cancelled setting " + property);
231 // verify the property is set for debugging
232 if (jalview.bin.Cache.log.isDebugEnabled())
234 jalview.bin.Cache.log.debug("User set property to "
235 + jalview.bin.Cache.getProperty(property));
237 } catch (Exception e)
239 jalview.bin.Cache.log.warn(
240 "Unexpected exception when prompting user for yes/no setting for property "