enact on a boolean configuration option, prompting the user if the option has not...
[jalview.git] / src / jalview / gui / PromptUserConfig.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.gui;
20
21 import jalview.bin.Cache;
22
23 import java.awt.Component;
24 import java.io.*;
25 import java.net.*;
26
27 import javax.swing.*;
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
35    * set the 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    * can the user cancel rather than set the property ?
46    */
47   boolean allowCancel = false;
48   /**
49    * title of prompt dialog
50    */
51   String dialogTitle;
52   /**
53    * text in dialog
54    */
55   String dialogText;
56   /**
57    * runnables for all cases.
58    */
59   Runnable iftrue=null,iffalse=null,ifundef=null;
60   private Component component;
61   /**
62    * if set, remove the property if the user says no rather than setting it to false.
63    */
64   private boolean removeifunset;
65   
66   /**
67    * @return the removeifunset
68    */
69   public boolean isRemoveifunset()
70   {
71     return removeifunset;
72   }
73   /**
74    * @param removeifunset the removeifunset to set
75    */
76   public void setRemoveifunset(boolean removeifunset)
77   {
78     this.removeifunset = removeifunset;
79   }
80   /**
81    * @param desktop - where the dialog box will be shown
82    * @param property - boolean property in jalview.bin.Cache
83    * @param dialogTitle - title of prompt box
84    * @param dialogText - text of box
85    * @param iftrue - executed if property is true
86    * @param iffalse - executed if property is false
87    * @param ifundef - executed if property was not set after prompting.
88    * @param allowCancel - allow the user to cancel rather than set the property
89    */
90   public PromptUserConfig(Component desktop, String property, String dialogTitle,
91           String dialogText, Runnable iftrue, Runnable iffalse,
92           Runnable ifundef, boolean allowCancel)
93   {
94     super();
95     this.component = desktop;
96     this.property = property;
97     this.dialogTitle = dialogTitle;
98     this.dialogText = dialogText;
99     this.iftrue = iftrue;
100     this.iffalse = iffalse;
101     this.ifundef = ifundef;
102     this.allowCancel = allowCancel;
103   }
104   public void run()
105   {
106     if (property==null) 
107     {
108       return;
109     }
110     // First - check to see if wee have an old questionnaire/response id pair.
111       String lastq = jalview.bin.Cache.getProperty(property);
112       
113       if (lastq == null)
114       {
115         raiseDialog();
116         Cache.log.debug("Got user response.");
117       }
118       lastq = jalview.bin.Cache.getProperty(property);
119       String extype = "";
120       Exception e = null;
121       if (lastq==null) {
122         // execute the ifundef
123         try
124         {
125           if (ifundef!=null)
126           {
127             ifundef.run();
128           }
129         } catch (Exception ex)
130         {
131           e = ex;
132           extype = "undefined";
133         }
134       } else if (Boolean.valueOf(lastq).booleanValue()) {
135         // execute the iftrue
136         try
137         {
138           if (iftrue!=null)
139           {
140             iftrue.run();
141           }
142         } catch (Exception ex)
143         {
144           e = ex;
145           extype = "if true";
146         }
147       } else {
148         try
149         {
150           if (iffalse!=null)
151           {
152             iffalse.run();
153           }
154         } catch (Exception ex)
155         {
156           e = ex;
157           extype = "if false";
158         }
159       }
160       // report any exceptions
161       if (e!=null) {
162         Cache.log.warn("Unexpected exception when executing the "+extype+" runnable for property "+property,e);
163       }
164   }
165   /**
166    * raise the property dialog
167    */
168   private void raiseDialog() {
169     if (jalview.bin.Cache.log.isDebugEnabled())
170     {
171       jalview.bin.Cache.log.debug("Prompting user for "+dialogTitle+" for Cache property "+property);
172     }
173     try {
174       int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop, // component,
175                 dialogText,
176                 dialogTitle,  (allowCancel) ? JOptionPane.YES_NO_CANCEL_OPTION : 
177                   JOptionPane.YES_NO_OPTION,
178                 JOptionPane.QUESTION_MESSAGE); 
179       jalview.bin.Cache.log.debug("Got response : "+reply);
180       if (reply == JOptionPane.YES_OPTION)
181       {
182         jalview.bin.Cache.setProperty(property, "true");
183       } else
184         if (reply == JOptionPane.NO_OPTION) {
185           if (removeifunset)
186           {
187             jalview.bin.Cache.removeProperty(property);
188           } else {
189             jalview.bin.Cache.setProperty(property, "false");
190           }
191         } else
192         {
193           jalview.bin.Cache.log.debug("User cancelled setting " + property);
194           return;
195         }
196       // verify the property is set for debugging
197       if (jalview.bin.Cache.log.isDebugEnabled())
198       {
199         jalview.bin.Cache.log.debug("User set property to "+jalview.bin.Cache.getProperty(property));
200       }
201     } catch (Exception e)
202     {
203       jalview.bin.Cache.log.warn("Unexpected exception when prompting user for yes/no setting for property "+property, e);
204     }
205   }
206 }