2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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;
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 jalview.bin.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;
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 = JOptionPane.showInternalConfirmDialog(
204 Desktop.desktop, // component,
205 dialogText, dialogTitle,
206 (allowCancel) ? JOptionPane.YES_NO_CANCEL_OPTION
207 : JOptionPane.YES_NO_OPTION,
208 JOptionPane.QUESTION_MESSAGE);
209 jalview.bin.Cache.log.debug("Got response : " + reply);
210 if (reply == JOptionPane.YES_OPTION)
212 jalview.bin.Cache.setProperty(property, "true");
214 else if (reply == JOptionPane.NO_OPTION)
218 jalview.bin.Cache.removeProperty(property);
222 jalview.bin.Cache.setProperty(property, "false");
227 jalview.bin.Cache.log.debug("User cancelled setting " + property);
230 // verify the property is set for debugging
231 if (jalview.bin.Cache.log.isDebugEnabled())
233 jalview.bin.Cache.log.debug("User set property to "
234 + jalview.bin.Cache.getProperty(property));
236 } catch (Exception e)
238 jalview.bin.Cache.log.warn(
239 "Unexpected exception when prompting user for yes/no setting for property "