Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.gui

File PromptUserConfig.java

 

Coverage histogram

../../img/srcFileCovDistChart4.png
44% of files have more coverage

Code metrics

28
55
5
1
245
147
23
0.42
11
5
4.6

Classes

Class Line # Actions
PromptUserConfig 27 55 23
0.3295454733%
 

Contributing tests

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