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