2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 import jalview.bin.Cache;
23 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;
123 if (property == null)
127 // First - check to see if wee have an old questionnaire/response id pair.
128 String lastq = jalview.bin.Cache.getProperty(property);
133 Cache.log.debug("Got user response.");
135 lastq = jalview.bin.Cache.getProperty(property);
140 // execute the ifundef
147 } catch (Exception ex)
150 extype = "undefined";
153 else if (Boolean.valueOf(lastq).booleanValue())
155 // execute the iftrue
162 } catch (Exception ex)
176 } catch (Exception ex)
182 // report any exceptions
185 Cache.log.warn("Unexpected exception when executing the " + extype
186 + " runnable for property " + property, e);
191 * raise the property dialog
193 private void raiseDialog()
195 if (jalview.bin.Cache.log.isDebugEnabled())
197 jalview.bin.Cache.log.debug("Prompting user for " + dialogTitle
198 + " for Cache property " + property);
202 int reply = JOptionPane.showConfirmDialog(
203 Desktop.desktop, // component,
204 dialogText, dialogTitle,
205 (allowCancel) ? JOptionPane.YES_NO_CANCEL_OPTION
206 : JOptionPane.YES_NO_OPTION,
207 JOptionPane.QUESTION_MESSAGE);
208 // now, ask the desktop to relayer any external windows that might have
210 if (Desktop.instance != null)
212 Desktop.instance.relayerWindows();
214 // and finish parsing the result
215 jalview.bin.Cache.log.debug("Got response : " + reply);
216 if (reply == JOptionPane.YES_OPTION)
218 jalview.bin.Cache.setProperty(property, "true");
220 else if (reply == JOptionPane.NO_OPTION)
224 jalview.bin.Cache.removeProperty(property);
228 jalview.bin.Cache.setProperty(property, "false");
233 jalview.bin.Cache.log.debug("User cancelled setting " + property);
236 // verify the property is set for debugging
237 if (jalview.bin.Cache.log.isDebugEnabled())
239 jalview.bin.Cache.log.debug("User set property to "
240 + jalview.bin.Cache.getProperty(property));
242 } catch (Exception e)
244 jalview.bin.Cache.log.warn(
245 "Unexpected exception when prompting user for yes/no setting for property "