update author list in license for (JAL-826)
[jalview.git] / src / jalview / gui / PromptUserConfig.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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 import java.io.*;
24 import java.net.*;
25
26 import javax.swing.*;
27
28 public class PromptUserConfig implements Runnable
29 {
30   /**
31    * Given a boolean Cache option:
32    * 
33    * 1. Prompt the user with the given text if the option is unset, and set the
34    * option accordingly (yes/no==true/false).
35    * 
36    * 2. Execute the given Runnables according to the state of the config option.
37    * 
38    */
39   /**
40    * boolean property to set
41    */
42   String property = null;
43
44   /**
45    * can the user cancel rather than set the property ?
46    */
47   boolean allowCancel = false;
48
49   /**
50    * title of prompt dialog
51    */
52   String dialogTitle;
53
54   /**
55    * text in dialog
56    */
57   String dialogText;
58
59   /**
60    * runnables for all cases.
61    */
62   Runnable iftrue = null, iffalse = null, ifundef = null;
63
64   private Component component;
65
66   /**
67    * if set, remove the property if the user says no rather than setting it to
68    * false.
69    */
70   private boolean removeifunset;
71
72   /**
73    * @return the removeifunset
74    */
75   public boolean isRemoveifunset()
76   {
77     return removeifunset;
78   }
79
80   /**
81    * @param removeifunset
82    *          the removeifunset to set
83    */
84   public void setRemoveifunset(boolean removeifunset)
85   {
86     this.removeifunset = removeifunset;
87   }
88
89   /**
90    * @param desktop
91    *          - where the dialog box will be shown
92    * @param property
93    *          - boolean property in jalview.bin.Cache
94    * @param dialogTitle
95    *          - title of prompt box
96    * @param dialogText
97    *          - text of box
98    * @param iftrue
99    *          - executed if property is true
100    * @param iffalse
101    *          - executed if property is false
102    * @param ifundef
103    *          - executed if property was not set after prompting.
104    * @param allowCancel
105    *          - allow the user to cancel rather than set the property
106    */
107   public PromptUserConfig(Component desktop, String property,
108           String dialogTitle, String dialogText, Runnable iftrue,
109           Runnable iffalse, Runnable ifundef, boolean allowCancel)
110   {
111     super();
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;
120   }
121
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 = jalview.bin.Cache.getProperty(property);
130
131     if (lastq == null)
132     {
133       raiseDialog();
134       Cache.log.debug("Got user response.");
135     }
136     lastq = jalview.bin.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.log.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 (jalview.bin.Cache.log.isDebugEnabled())
197     {
198       jalview.bin.Cache.log.debug("Prompting user for " + dialogTitle
199               + " for Cache property " + property);
200     }
201     try
202     {
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       // now, ask the desktop to relayer any external windows that might have been obsured
210       if (Desktop.instance!=null)
211       {
212         Desktop.instance.relayerWindows();
213       }
214       // and finish parsing the result
215       jalview.bin.Cache.log.debug("Got response : " + reply);
216       if (reply == JOptionPane.YES_OPTION)
217       {
218         jalview.bin.Cache.setProperty(property, "true");
219       }
220       else if (reply == JOptionPane.NO_OPTION)
221       {
222         if (removeifunset)
223         {
224           jalview.bin.Cache.removeProperty(property);
225         }
226         else
227         {
228           jalview.bin.Cache.setProperty(property, "false");
229         }
230       }
231       else
232       {
233         jalview.bin.Cache.log.debug("User cancelled setting " + property);
234         return;
235       }
236       // verify the property is set for debugging
237       if (jalview.bin.Cache.log.isDebugEnabled())
238       {
239         jalview.bin.Cache.log.debug("User set property to "
240                 + jalview.bin.Cache.getProperty(property));
241       }
242     } catch (Exception e)
243     {
244       jalview.bin.Cache.log.warn(
245               "Unexpected exception when prompting user for yes/no setting for property "
246                       + property, e);
247     }
248   }
249 }