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 import javax.swing.JOptionPane;
29 public class PromptUserConfig implements Runnable
32 * Given a boolean Cache option:
34 * 1. Prompt the user with the given text if the option is unset, and set the
35 * option accordingly (yes/no==true/false).
37 * 2. Execute the given Runnables according to the state of the config option.
41 * boolean property to set
43 String property = null;
46 * can the user cancel rather than set the property ?
48 boolean allowCancel = false;
51 * title of prompt dialog
61 * runnables for all cases.
63 Runnable iftrue = null, iffalse = null, ifundef = null;
65 private Component component;
68 * if set, remove the property if the user says no rather than setting it to
71 private boolean removeifunset;
74 * @return the removeifunset
76 public boolean isRemoveifunset()
82 * @param removeifunset
83 * the removeifunset to set
85 public void setRemoveifunset(boolean removeifunset)
87 this.removeifunset = removeifunset;
92 * - where the dialog box will be shown
94 * - boolean property in jalview.bin.Cache
96 * - title of prompt box
100 * - executed if property is true
102 * - executed if property is false
104 * - executed if property was not set after prompting.
106 * - allow the user to cancel rather than set the property
108 public PromptUserConfig(Component desktop, String property,
109 String dialogTitle, String dialogText, Runnable iftrue,
110 Runnable iffalse, Runnable ifundef, boolean allowCancel)
113 this.component = desktop;
114 this.property = property;
115 this.dialogTitle = dialogTitle;
116 this.dialogText = dialogText;
117 this.iftrue = iftrue;
118 this.iffalse = iffalse;
119 this.ifundef = ifundef;
120 this.allowCancel = allowCancel;
125 if (property == null)
129 // First - check to see if wee have an old questionnaire/response id pair.
130 String lastq = jalview.bin.Cache.getProperty(property);
135 Cache.log.debug("Got user response.");
137 lastq = jalview.bin.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 Cache.log.warn("Unexpected exception when executing the " + extype
188 + " runnable for property " + property, e);
193 * raise the property dialog
195 private void raiseDialog()
197 if (jalview.bin.Cache.log.isDebugEnabled())
199 jalview.bin.Cache.log.debug("Prompting user for " + dialogTitle
200 + " for Cache property " + property);
204 int reply = JvOptionPane.showConfirmDialog(
205 Desktop.desktop, // component,
206 dialogText, dialogTitle,
207 (allowCancel) ? JvOptionPane.YES_NO_CANCEL_OPTION
208 : JvOptionPane.YES_NO_OPTION,
209 JvOptionPane.QUESTION_MESSAGE);
210 // now, ask the desktop to relayer any external windows that might have
212 if (Desktop.instance != null)
214 Desktop.instance.relayerWindows();
216 // and finish parsing the result
217 jalview.bin.Cache.log.debug("Got response : " + reply);
218 if (reply == JvOptionPane.YES_OPTION)
220 jalview.bin.Cache.setProperty(property, "true");
222 else if (reply == JvOptionPane.NO_OPTION)
226 jalview.bin.Cache.removeProperty(property);
230 jalview.bin.Cache.setProperty(property, "false");
235 jalview.bin.Cache.log.debug("User cancelled setting " + property);
238 // verify the property is set for debugging
239 if (jalview.bin.Cache.log.isDebugEnabled())
241 jalview.bin.Cache.log.debug("User set property to "
242 + jalview.bin.Cache.getProperty(property));
244 } catch (Exception e)
246 jalview.bin.Cache.log.warn(
247 "Unexpected exception when prompting user for yes/no setting for property "