JAL-3949 - refactor logging from jalview.bin.Cache to jalview.bin.Console
[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 import jalview.bin.Console;
25
26 import java.awt.Component;
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 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   @Override
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 = Cache.getProperty(property);
131
132     if (lastq == null)
133     {
134       raiseDialog();
135       Console.debug("Got user response.");
136     }
137     lastq = 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       Console.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 (Console.isDebugEnabled())
198     {
199       Console.debug("Prompting user for " + dialogTitle
200               + " for Cache property " + property);
201     }
202     try
203     {
204       int reply = JvOptionPane.showConfirmDialog(Desktop.desktop, // component,
205               dialogText, dialogTitle,
206               (allowCancel) ? JvOptionPane.YES_NO_CANCEL_OPTION
207                       : JvOptionPane.YES_NO_OPTION,
208               JvOptionPane.QUESTION_MESSAGE);
209
210       // and finish parsing the result
211       Console.debug("Got response : " + reply);
212       if (reply == JvOptionPane.YES_OPTION)
213       {
214         Cache.setProperty(property, "true");
215       }
216       else if (reply == JvOptionPane.NO_OPTION)
217       {
218         if (removeifunset)
219         {
220           Cache.removeProperty(property);
221         }
222         else
223         {
224           Cache.setProperty(property, "false");
225         }
226       }
227       else
228       {
229         Console.debug("User cancelled setting " + property);
230         return;
231       }
232       // verify the property is set for debugging
233       if (Console.isDebugEnabled())
234       {
235         Console.debug("User set property to "
236                 + Cache.getProperty(property));
237       }
238     } catch (Exception e)
239     {
240       Console.warn(
241               "Unexpected exception when prompting user for yes/no setting for property "
242                       + property,
243               e);
244     }
245   }
246 }