JAL-1432 updated copyright notices
[jalview.git] / src / jalview / gui / PromptUserConfig.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 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 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  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.gui;
20
21 import jalview.bin.Cache;
22
23 import java.awt.Component;
24
25 import javax.swing.*;
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 jalview.bin.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   public void run()
122   {
123     if (property == null)
124     {
125       return;
126     }
127     // First - check to see if wee have an old questionnaire/response id pair.
128     String lastq = jalview.bin.Cache.getProperty(property);
129
130     if (lastq == null)
131     {
132       raiseDialog();
133       Cache.log.debug("Got user response.");
134     }
135     lastq = jalview.bin.Cache.getProperty(property);
136     String extype = "";
137     Exception e = null;
138     if (lastq == null)
139     {
140       // execute the ifundef
141       try
142       {
143         if (ifundef != null)
144         {
145           ifundef.run();
146         }
147       } catch (Exception ex)
148       {
149         e = ex;
150         extype = "undefined";
151       }
152     }
153     else if (Boolean.valueOf(lastq).booleanValue())
154     {
155       // execute the iftrue
156       try
157       {
158         if (iftrue != null)
159         {
160           iftrue.run();
161         }
162       } catch (Exception ex)
163       {
164         e = ex;
165         extype = "if true";
166       }
167     }
168     else
169     {
170       try
171       {
172         if (iffalse != null)
173         {
174           iffalse.run();
175         }
176       } catch (Exception ex)
177       {
178         e = ex;
179         extype = "if false";
180       }
181     }
182     // report any exceptions
183     if (e != null)
184     {
185       Cache.log.warn("Unexpected exception when executing the " + extype
186               + " runnable for property " + property, e);
187     }
188   }
189
190   /**
191    * raise the property dialog
192    */
193   private void raiseDialog()
194   {
195     if (jalview.bin.Cache.log.isDebugEnabled())
196     {
197       jalview.bin.Cache.log.debug("Prompting user for " + dialogTitle
198               + " for Cache property " + property);
199     }
200     try
201     {
202       int reply = JOptionPane.showConfirmDialog(
203               Desktop.desktop, // component,
204               dialogText, dialogTitle,
205               (allowCancel) ? JOptionPane.YES_NO_CANCEL_OPTION
206                       : JOptionPane.YES_NO_OPTION,
207               JOptionPane.QUESTION_MESSAGE);
208       // now, ask the desktop to relayer any external windows that might have
209       // 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 }