d1fd7dd86fed56c0ee2d8d394ac433c9f92fc2e2
[jalview.git] / src / jalview / gui / PromptUserConfig.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.bin.Cache;
24
25 import java.awt.Component;
26
27 public class PromptUserConfig implements Runnable
28 {
29   /**
30    * Given a boolean Cache option:
31    * 
32    * 1. Prompt the user with the given text if the option is unset, and set the
33    * option accordingly (yes/no==true/false).
34    * 
35    * 2. Execute the given Runnables according to the state of the config option.
36    * 
37    */
38   /**
39    * boolean property to set
40    */
41   String property = null;
42
43   /**
44    * can the user cancel rather than set the property ?
45    */
46   boolean allowCancel = false;
47
48   /**
49    * title of prompt dialog
50    */
51   String dialogTitle;
52
53   /**
54    * text in dialog
55    */
56   String dialogText;
57
58   /**
59    * runnables for all cases.
60    */
61   Runnable iftrue = null, iffalse = null, ifundef = null;
62
63   private Component component;
64
65   /**
66    * if set, remove the property if the user says no rather than setting it to
67    * false.
68    */
69   private boolean removeifunset;
70
71   /**
72    * @return the removeifunset
73    */
74   public boolean isRemoveifunset()
75   {
76     return removeifunset;
77   }
78
79   /**
80    * @param removeifunset
81    *          the removeifunset to set
82    */
83   public void setRemoveifunset(boolean removeifunset)
84   {
85     this.removeifunset = removeifunset;
86   }
87
88   /**
89    * @param desktop
90    *          - where the dialog box will be shown
91    * @param property
92    *          - boolean property in Cache
93    * @param dialogTitle
94    *          - title of prompt box
95    * @param dialogText
96    *          - text of box
97    * @param iftrue
98    *          - executed if property is true
99    * @param iffalse
100    *          - executed if property is false
101    * @param ifundef
102    *          - executed if property was not set after prompting.
103    * @param allowCancel
104    *          - allow the user to cancel rather than set the property
105    */
106   public PromptUserConfig(Component desktop, String property,
107           String dialogTitle, String dialogText, Runnable iftrue,
108           Runnable iffalse, Runnable ifundef, boolean allowCancel)
109   {
110     super();
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;
119   }
120
121   @Override
122   public void run()
123   {
124     if (property == null)
125     {
126       return;
127     }
128     // First - check to see if wee have an old questionnaire/response id pair.
129     String lastq = Cache.getProperty(property);
130
131     if (lastq == null)
132     {
133       raiseDialog();
134       Cache.debug("Got user response.");
135     }
136     lastq = Cache.getProperty(property);
137     String extype = "";
138     Exception e = null;
139     if (lastq == null)
140     {
141       // execute the ifundef
142       try
143       {
144         if (ifundef != null)
145         {
146           ifundef.run();
147         }
148       } catch (Exception ex)
149       {
150         e = ex;
151         extype = "undefined";
152       }
153     }
154     else if (Boolean.valueOf(lastq).booleanValue())
155     {
156       // execute the iftrue
157       try
158       {
159         if (iftrue != null)
160         {
161           iftrue.run();
162         }
163       } catch (Exception ex)
164       {
165         e = ex;
166         extype = "if true";
167       }
168     }
169     else
170     {
171       try
172       {
173         if (iffalse != null)
174         {
175           iffalse.run();
176         }
177       } catch (Exception ex)
178       {
179         e = ex;
180         extype = "if false";
181       }
182     }
183     // report any exceptions
184     if (e != null)
185     {
186       Cache.warn("Unexpected exception when executing the " + extype
187               + " runnable for property " + property, e);
188     }
189   }
190
191   /**
192    * raise the property dialog
193    */
194   private void raiseDialog()
195   {
196     if (Cache.isDebugEnabled())
197     {
198       Cache.debug("Prompting user for " + dialogTitle
199               + " for Cache property " + property);
200     }
201     try
202     {
203       int reply = JvOptionPane.showConfirmDialog(Desktop.desktop, // component,
204               dialogText, dialogTitle,
205               (allowCancel) ? JvOptionPane.YES_NO_CANCEL_OPTION
206                       : JvOptionPane.YES_NO_OPTION,
207               JvOptionPane.QUESTION_MESSAGE);
208
209       // and finish parsing the result
210       Cache.debug("Got response : " + reply);
211       if (reply == JvOptionPane.YES_OPTION)
212       {
213         Cache.setProperty(property, "true");
214       }
215       else if (reply == JvOptionPane.NO_OPTION)
216       {
217         if (removeifunset)
218         {
219           Cache.removeProperty(property);
220         }
221         else
222         {
223           Cache.setProperty(property, "false");
224         }
225       }
226       else
227       {
228         Cache.debug("User cancelled setting " + property);
229         return;
230       }
231       // verify the property is set for debugging
232       if (Cache.isDebugEnabled())
233       {
234         Cache.debug("User set property to "
235                 + Cache.getProperty(property));
236       }
237     } catch (Exception e)
238     {
239       Cache.warn(
240               "Unexpected exception when prompting user for yes/no setting for property "
241                       + property,
242               e);
243     }
244   }
245 }