2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
20 import jalview.bin.Cache;
22 import java.awt.Component;
26 public class PromptUserConfig implements Runnable
29 * Given a boolean Cache option:
31 * 1. Prompt the user with the given text if the option is unset, and set the
32 * option accordingly (yes/no==true/false).
34 * 2. Execute the given Runnables according to the state of the config option.
38 * boolean property to set
40 String property = null;
43 * can the user cancel rather than set the property ?
45 boolean allowCancel = false;
48 * title of prompt dialog
58 * runnables for all cases.
60 Runnable iftrue = null, iffalse = null, ifundef = null;
62 private Component component;
65 * if set, remove the property if the user says no rather than setting it to
68 private boolean removeifunset;
71 * @return the removeifunset
73 public boolean isRemoveifunset()
79 * @param removeifunset
80 * the removeifunset to set
82 public void setRemoveifunset(boolean removeifunset)
84 this.removeifunset = removeifunset;
89 * - where the dialog box will be shown
91 * - boolean property in jalview.bin.Cache
93 * - title of prompt box
97 * - executed if property is true
99 * - executed if property is false
101 * - executed if property was not set after prompting.
103 * - allow the user to cancel rather than set the property
105 public PromptUserConfig(Component desktop, String property,
106 String dialogTitle, String dialogText, Runnable iftrue,
107 Runnable iffalse, Runnable ifundef, boolean allowCancel)
110 this.component = desktop;
111 this.property = property;
112 this.dialogTitle = dialogTitle;
113 this.dialogText = dialogText;
114 this.iftrue = iftrue;
115 this.iffalse = iffalse;
116 this.ifundef = ifundef;
117 this.allowCancel = allowCancel;
122 if (property == null)
126 // First - check to see if wee have an old questionnaire/response id pair.
127 String lastq = jalview.bin.Cache.getProperty(property);
132 Cache.log.debug("Got user response.");
134 lastq = jalview.bin.Cache.getProperty(property);
139 // execute the ifundef
146 } catch (Exception ex)
149 extype = "undefined";
152 else if (Boolean.valueOf(lastq).booleanValue())
154 // execute the iftrue
161 } catch (Exception ex)
175 } catch (Exception ex)
181 // report any exceptions
184 Cache.log.warn("Unexpected exception when executing the " + extype
185 + " runnable for property " + property, e);
190 * raise the property dialog
192 private void raiseDialog()
194 if (jalview.bin.Cache.log.isDebugEnabled())
196 jalview.bin.Cache.log.debug("Prompting user for " + dialogTitle
197 + " for Cache property " + property);
201 int reply = JOptionPane.showConfirmDialog(
202 Desktop.desktop, // component,
203 dialogText, dialogTitle,
204 (allowCancel) ? JOptionPane.YES_NO_CANCEL_OPTION
205 : JOptionPane.YES_NO_OPTION,
206 JOptionPane.QUESTION_MESSAGE);
207 // now, ask the desktop to relayer any external windows that might have
209 if (Desktop.instance != null)
211 Desktop.instance.relayerWindows();
213 // and finish parsing the result
214 jalview.bin.Cache.log.debug("Got response : " + reply);
215 if (reply == JOptionPane.YES_OPTION)
217 jalview.bin.Cache.setProperty(property, "true");
219 else if (reply == JOptionPane.NO_OPTION)
223 jalview.bin.Cache.removeProperty(property);
227 jalview.bin.Cache.setProperty(property, "false");
232 jalview.bin.Cache.log.debug("User cancelled setting " + property);
235 // verify the property is set for debugging
236 if (jalview.bin.Cache.log.isDebugEnabled())
238 jalview.bin.Cache.log.debug("User set property to "
239 + jalview.bin.Cache.getProperty(property));
241 } catch (Exception e)
243 jalview.bin.Cache.log.warn(
244 "Unexpected exception when prompting user for yes/no setting for property "