2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 import jalview.bin.Cache;
23 import java.awt.Component;
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
35 * set the 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
45 * can the user cancel rather than set the property ?
47 boolean allowCancel = false;
49 * title of prompt dialog
57 * runnables for all cases.
59 Runnable iftrue=null,iffalse=null,ifundef=null;
60 private Component component;
62 * if set, remove the property if the user says no rather than setting it to false.
64 private boolean removeifunset;
67 * @return the removeifunset
69 public boolean isRemoveifunset()
74 * @param removeifunset the removeifunset to set
76 public void setRemoveifunset(boolean removeifunset)
78 this.removeifunset = removeifunset;
81 * @param desktop - where the dialog box will be shown
82 * @param property - boolean property in jalview.bin.Cache
83 * @param dialogTitle - title of prompt box
84 * @param dialogText - text of box
85 * @param iftrue - executed if property is true
86 * @param iffalse - executed if property is false
87 * @param ifundef - executed if property was not set after prompting.
88 * @param allowCancel - allow the user to cancel rather than set the property
90 public PromptUserConfig(Component desktop, String property, String dialogTitle,
91 String dialogText, Runnable iftrue, Runnable iffalse,
92 Runnable ifundef, boolean allowCancel)
95 this.component = desktop;
96 this.property = property;
97 this.dialogTitle = dialogTitle;
98 this.dialogText = dialogText;
100 this.iffalse = iffalse;
101 this.ifundef = ifundef;
102 this.allowCancel = allowCancel;
110 // First - check to see if wee have an old questionnaire/response id pair.
111 String lastq = jalview.bin.Cache.getProperty(property);
116 Cache.log.debug("Got user response.");
118 lastq = jalview.bin.Cache.getProperty(property);
122 // execute the ifundef
129 } catch (Exception ex)
132 extype = "undefined";
134 } else if (Boolean.valueOf(lastq).booleanValue()) {
135 // execute the iftrue
142 } catch (Exception ex)
154 } catch (Exception ex)
160 // report any exceptions
162 Cache.log.warn("Unexpected exception when executing the "+extype+" runnable for property "+property,e);
166 * raise the property dialog
168 private void raiseDialog() {
169 if (jalview.bin.Cache.log.isDebugEnabled())
171 jalview.bin.Cache.log.debug("Prompting user for "+dialogTitle+" for Cache property "+property);
174 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop, // component,
176 dialogTitle, (allowCancel) ? JOptionPane.YES_NO_CANCEL_OPTION :
177 JOptionPane.YES_NO_OPTION,
178 JOptionPane.QUESTION_MESSAGE);
179 jalview.bin.Cache.log.debug("Got response : "+reply);
180 if (reply == JOptionPane.YES_OPTION)
182 jalview.bin.Cache.setProperty(property, "true");
184 if (reply == JOptionPane.NO_OPTION) {
187 jalview.bin.Cache.removeProperty(property);
189 jalview.bin.Cache.setProperty(property, "false");
193 jalview.bin.Cache.log.debug("User cancelled setting " + property);
196 // verify the property is set for debugging
197 if (jalview.bin.Cache.log.isDebugEnabled())
199 jalview.bin.Cache.log.debug("User set property to "+jalview.bin.Cache.getProperty(property));
201 } catch (Exception e)
203 jalview.bin.Cache.log.warn("Unexpected exception when prompting user for yes/no setting for property "+property, e);