507d1f2e9902e701f0c5662ecb9e4088e2c5f76f
[jalview.git] / src / jalview / gui / PromptUserConfig.java
1 /*
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
4  * 
5  * This file is part of Jalview.
6  * 
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.
10  *  
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.
15  * 
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  */
18 package jalview.gui;
19
20 import jalview.bin.Cache;
21
22 import java.awt.Component;
23
24 import javax.swing.*;
25
26 public class PromptUserConfig implements Runnable
27 {
28   /**
29    * Given a boolean Cache option:
30    * 
31    * 1. Prompt the user with the given text if the option is unset, and set the
32    * option accordingly (yes/no==true/false).
33    * 
34    * 2. Execute the given Runnables according to the state of the config option.
35    * 
36    */
37   /**
38    * boolean property to set
39    */
40   String property = null;
41
42   /**
43    * can the user cancel rather than set the property ?
44    */
45   boolean allowCancel = false;
46
47   /**
48    * title of prompt dialog
49    */
50   String dialogTitle;
51
52   /**
53    * text in dialog
54    */
55   String dialogText;
56
57   /**
58    * runnables for all cases.
59    */
60   Runnable iftrue = null, iffalse = null, ifundef = null;
61
62   private Component component;
63
64   /**
65    * if set, remove the property if the user says no rather than setting it to
66    * false.
67    */
68   private boolean removeifunset;
69
70   /**
71    * @return the removeifunset
72    */
73   public boolean isRemoveifunset()
74   {
75     return removeifunset;
76   }
77
78   /**
79    * @param removeifunset
80    *          the removeifunset to set
81    */
82   public void setRemoveifunset(boolean removeifunset)
83   {
84     this.removeifunset = removeifunset;
85   }
86
87   /**
88    * @param desktop
89    *          - where the dialog box will be shown
90    * @param property
91    *          - boolean property in jalview.bin.Cache
92    * @param dialogTitle
93    *          - title of prompt box
94    * @param dialogText
95    *          - text of box
96    * @param iftrue
97    *          - executed if property is true
98    * @param iffalse
99    *          - executed if property is false
100    * @param ifundef
101    *          - executed if property was not set after prompting.
102    * @param allowCancel
103    *          - allow the user to cancel rather than set the property
104    */
105   public PromptUserConfig(Component desktop, String property,
106           String dialogTitle, String dialogText, Runnable iftrue,
107           Runnable iffalse, Runnable ifundef, boolean allowCancel)
108   {
109     super();
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;
118   }
119
120   public void run()
121   {
122     if (property == null)
123     {
124       return;
125     }
126     // First - check to see if wee have an old questionnaire/response id pair.
127     String lastq = jalview.bin.Cache.getProperty(property);
128
129     if (lastq == null)
130     {
131       raiseDialog();
132       Cache.log.debug("Got user response.");
133     }
134     lastq = jalview.bin.Cache.getProperty(property);
135     String extype = "";
136     Exception e = null;
137     if (lastq == null)
138     {
139       // execute the ifundef
140       try
141       {
142         if (ifundef != null)
143         {
144           ifundef.run();
145         }
146       } catch (Exception ex)
147       {
148         e = ex;
149         extype = "undefined";
150       }
151     }
152     else if (Boolean.valueOf(lastq).booleanValue())
153     {
154       // execute the iftrue
155       try
156       {
157         if (iftrue != null)
158         {
159           iftrue.run();
160         }
161       } catch (Exception ex)
162       {
163         e = ex;
164         extype = "if true";
165       }
166     }
167     else
168     {
169       try
170       {
171         if (iffalse != null)
172         {
173           iffalse.run();
174         }
175       } catch (Exception ex)
176       {
177         e = ex;
178         extype = "if false";
179       }
180     }
181     // report any exceptions
182     if (e != null)
183     {
184       Cache.log.warn("Unexpected exception when executing the " + extype
185               + " runnable for property " + property, e);
186     }
187   }
188
189   /**
190    * raise the property dialog
191    */
192   private void raiseDialog()
193   {
194     if (jalview.bin.Cache.log.isDebugEnabled())
195     {
196       jalview.bin.Cache.log.debug("Prompting user for " + dialogTitle
197               + " for Cache property " + property);
198     }
199     try
200     {
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
208       // been obsured
209       if (Desktop.instance != null)
210       {
211         Desktop.instance.relayerWindows();
212       }
213       // and finish parsing the result
214       jalview.bin.Cache.log.debug("Got response : " + reply);
215       if (reply == JOptionPane.YES_OPTION)
216       {
217         jalview.bin.Cache.setProperty(property, "true");
218       }
219       else if (reply == JOptionPane.NO_OPTION)
220       {
221         if (removeifunset)
222         {
223           jalview.bin.Cache.removeProperty(property);
224         }
225         else
226         {
227           jalview.bin.Cache.setProperty(property, "false");
228         }
229       }
230       else
231       {
232         jalview.bin.Cache.log.debug("User cancelled setting " + property);
233         return;
234       }
235       // verify the property is set for debugging
236       if (jalview.bin.Cache.log.isDebugEnabled())
237       {
238         jalview.bin.Cache.log.debug("User set property to "
239                 + jalview.bin.Cache.getProperty(property));
240       }
241     } catch (Exception e)
242     {
243       jalview.bin.Cache.log.warn(
244               "Unexpected exception when prompting user for yes/no setting for property "
245                       + property, e);
246     }
247   }
248 }