Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 10:11:34 GMT
  2. Package jalview.gui

File PromptUserConfig.java

 

Coverage histogram

../../img/srcFileCovDistChart6.png
36% of files have more coverage

Code metrics

28
55
5
1
246
148
23
0.42
11
5
4.6

Classes

Class Line # Actions
PromptUserConfig 28 55 23
0.556818255.7%
 

Contributing tests

This file is covered by 51 tests. .

Source view

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  0 toggle public boolean isRemoveifunset()
76    {
77  0 return removeifunset;
78    }
79   
80    /**
81    * @param removeifunset
82    * the removeifunset to set
83    */
 
84  0 toggle public void setRemoveifunset(boolean removeifunset)
85    {
86  0 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  69 toggle public PromptUserConfig(Component desktop, String property,
108    String dialogTitle, String dialogText, Runnable iftrue,
109    Runnable iffalse, Runnable ifundef, boolean allowCancel)
110    {
111  69 super();
112  69 this.component = desktop;
113  69 this.property = property;
114  69 this.dialogTitle = dialogTitle;
115  69 this.dialogText = dialogText;
116  69 this.iftrue = iftrue;
117  69 this.iffalse = iffalse;
118  69 this.ifundef = ifundef;
119  69 this.allowCancel = allowCancel;
120    }
121   
 
122  60 toggle @Override
123    public void run()
124    {
125  60 if (property == null)
126    {
127  0 return;
128    }
129    // First - check to see if wee have an old questionnaire/response id pair.
130  60 String lastq = Cache.getProperty(property);
131   
132  60 if (lastq == null)
133    {
134  35 raiseDialog();
135  35 Console.debug("Got user response.");
136    }
137  60 lastq = Cache.getProperty(property);
138  60 String extype = "";
139  60 Exception e = null;
140  60 if (lastq == null)
141    {
142    // execute the ifundef
143  35 try
144    {
145  35 if (ifundef != null)
146    {
147  0 ifundef.run();
148    }
149    } catch (Exception ex)
150    {
151  0 e = ex;
152  0 extype = "undefined";
153    }
154    }
155  25 else if (Boolean.valueOf(lastq).booleanValue())
156    {
157    // execute the iftrue
158  0 try
159    {
160  0 if (iftrue != null)
161    {
162  0 iftrue.run();
163    }
164    } catch (Exception ex)
165    {
166  0 e = ex;
167  0 extype = "if true";
168    }
169    }
170    else
171    {
172  25 try
173    {
174  25 if (iffalse != null)
175    {
176  25 iffalse.run();
177    }
178    } catch (Exception ex)
179    {
180  0 e = ex;
181  0 extype = "if false";
182    }
183    }
184    // report any exceptions
185  60 if (e != null)
186    {
187  0 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  35 toggle private void raiseDialog()
196    {
197  35 if (Console.isDebugEnabled())
198    {
199  0 Console.debug("Prompting user for " + dialogTitle
200    + " for Cache property " + property);
201    }
202  35 try
203    {
204  35 int reply = JvOptionPane.showConfirmDialog(Desktop.desktop, // component,
205    dialogText, dialogTitle,
206  35 (allowCancel) ? JvOptionPane.YES_NO_CANCEL_OPTION
207    : JvOptionPane.YES_NO_OPTION,
208    JvOptionPane.QUESTION_MESSAGE);
209   
210    // and finish parsing the result
211  35 Console.debug("Got response : " + reply);
212  35 if (reply == JvOptionPane.YES_OPTION)
213    {
214  0 Cache.setProperty(property, "true");
215    }
216  35 else if (reply == JvOptionPane.NO_OPTION)
217    {
218  0 if (removeifunset)
219    {
220  0 Cache.removeProperty(property);
221    }
222    else
223    {
224  0 Cache.setProperty(property, "false");
225    }
226    }
227    else
228    {
229  35 Console.debug("User cancelled setting " + property);
230  35 return;
231    }
232    // verify the property is set for debugging
233  0 if (Console.isDebugEnabled())
234    {
235  0 Console.debug(
236    "User set property to " + Cache.getProperty(property));
237    }
238    } catch (Exception e)
239    {
240  0 Console.warn(
241    "Unexpected exception when prompting user for yes/no setting for property "
242    + property,
243    e);
244    }
245    }
246    }