View Javadoc

1   /*
2    * Copyright (c) 2003, Andreas Pataki 
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    *
9    * Redistributions of source code must retain the above copyright notice,
10   * this list of conditions and the following disclaimer.
11   * 
12   * Redistributions in binary form must reproduce the above copyright notice,
13   * this list of conditions and the following disclaimer in the documentation
14   * and/or other materials provided with the distribution.
15   * 
16   * Neither the name of the author nor the names of its contributors
17   * may be used to endorse or promote products derived from this software
18   * without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS
21   * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  
33  package net.sf.tm.plugin.ui;
34  
35  import net.sf.tm.plugin.ContextManagerPlugin;
36  import net.sf.tm.plugin.PreferenceConstants;
37  
38  import org.apache.commons.lang.StringUtils;
39  import org.apache.commons.lang.math.IntRange;
40  import org.eclipse.jface.dialogs.IDialogConstants;
41  import org.eclipse.jface.preference.IPreferenceStore;
42  import org.eclipse.jface.preference.PreferencePage;
43  import org.eclipse.swt.SWT;
44  import org.eclipse.swt.events.ModifyEvent;
45  import org.eclipse.swt.events.ModifyListener;
46  import org.eclipse.swt.events.SelectionAdapter;
47  import org.eclipse.swt.events.SelectionEvent;
48  import org.eclipse.swt.layout.GridData;
49  import org.eclipse.swt.layout.GridLayout;
50  import org.eclipse.swt.widgets.Button;
51  import org.eclipse.swt.widgets.Composite;
52  import org.eclipse.swt.widgets.Control;
53  import org.eclipse.swt.widgets.Label;
54  import org.eclipse.swt.widgets.Text;
55  import org.eclipse.ui.IWorkbench;
56  import org.eclipse.ui.IWorkbenchPreferencePage;
57  
58  /***
59   * Preference page for the plugin settings. 
60   * 
61   * <p>Available preferences:
62   * 
63   * <ul>
64   *  <li>- de/activate auto update
65   *  <li>- update interval in seconds (only active when auto update is on)
66   * </ul>
67   * 
68   * @author Andreas Pataki
69   * @version $Id: TMPreferencePage.java,v 1.2 2003/09/12 08:36:24 apataki Exp $
70   */
71  public class TMPreferencePage
72      extends PreferencePage
73      implements IWorkbenchPreferencePage {
74  
75      /***
76       * Min value for field intervalField
77       */
78      private static final int INTERVAL_MIN = 1;
79      /***
80       * Max value for field intervalField
81       */
82      private static final int INTERVAL_MAX = 3600;
83      /***
84       * Checkbox for activating auto updates
85       */
86      private Button autoUpdate;
87      /***
88       * Input field for the update interval
89       */
90      private Text intervalField;
91      /***
92       * Label for the update interval update field
93       */
94      private Label intervalLabel;
95  
96      /***
97       * Default constructor
98       *
99       */
100     public TMPreferencePage() {
101         setDescription(
102             ContextManagerPlugin.getDefault().getLocalizedString(
103                 "preferences.description"));
104     }
105 
106     /***
107      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
108      */
109     protected Control createContents(Composite parent) {
110         WidgetFactory factory = new WidgetFactory();
111         initializeDialogUnits(parent);
112 
113         GridLayout layout = new GridLayout();
114         layout.marginHeight =
115             convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
116         layout.marginWidth =
117             convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
118         layout.verticalSpacing =
119             convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
120         layout.horizontalSpacing =
121             convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
122         layout.numColumns = 3;
123 
124         Composite composite = new Composite(parent, SWT.NONE);
125         composite.setLayout(layout);
126         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
127         composite.setFont(parent.getFont());
128 
129         autoUpdate =
130             factory.createCheckbox(
131                 composite,
132                 localizeString("preferences.autoupdate"),
133                 3);
134 
135         factory.createLabel(composite, "     ");
136         intervalLabel =
137             factory.createLabel(
138                 composite,
139                 localizeString("preferences.updateinterval"));
140         intervalField = factory.createTextField(composite);
141 
142         registerActions();
143         loadValues();
144 
145         return composite;
146     }
147 
148     /***
149      * Loads the field values from the plugin's preference store.
150      */
151     private void loadValues() {
152         IPreferenceStore store = getPreferenceStore();
153 
154         autoUpdate.setSelection(
155             store.getBoolean(PreferenceConstants.PREF_AUTOUPDATE));
156         intervalField.setText(
157             String.valueOf(
158                 store.getInt(PreferenceConstants.PREF_UPDATE_INTERVAL)));
159         updateStatus();
160     }
161 
162     /***
163      * @see org.eclipse.jface.preference.PreferencePage#doGetPreferenceStore()
164      */
165     protected IPreferenceStore doGetPreferenceStore() {
166         return ContextManagerPlugin.getDefault().getPreferenceStore();
167     }
168 
169     /***
170      * Creates and registers some event handlers to the input fields.
171      */
172     private void registerActions() {
173         // selection listener for checkbox status changes
174         autoUpdate.addSelectionListener(new SelectionAdapter() {
175 
176             public void widgetSelected(SelectionEvent e) {
177                 updateStatus();
178             }
179 
180         });
181 
182         // create modify listener for the interval field 
183         intervalField.addModifyListener(new ModifyListener() {
184 
185             public void modifyText(ModifyEvent event) {
186                 updateStatus();
187             }
188 
189         });
190 
191     }
192 
193     /***
194      * Updates the status of the interval field depending on the selection
195      * of the update checkbox. If update is activated it validates the 
196      * field value and updates the valid and error status of the page. 
197      */
198     private void updateStatus() {
199         intervalField.setEnabled(autoUpdate.getSelection());
200         intervalLabel.setEnabled(autoUpdate.getSelection());
201 
202         if (autoUpdate.getSelection()) {
203             if (!validateIntervalValue()) {
204                 setErrorMessage(
205                     localizeString("preferences.error.updateinterval"));
206                 setValid(false);
207             } else {
208                 setErrorMessage(null);
209                 setValid(true);
210             }
211         } else {
212             setErrorMessage(null);
213             setValid(true);
214         }
215     }
216 
217     /***
218      * Validates the value of the interval field. The value must be an integer
219      * in the range of 1-3600.
220      * 
221      * @return false if the syntax is wrong
222      */
223     private boolean validateIntervalValue() {
224         String value = intervalField.getText();
225         if (StringUtils.isNotEmpty(value)
226             && StringUtils.isNumericSpace(value)) {
227 
228             int intValue = Integer.parseInt(value);
229 
230             if (new IntRange(INTERVAL_MIN, INTERVAL_MAX)
231                 .containsInteger(intValue)) {
232                 return true;
233             }
234 
235         }
236         return false;
237     }
238 
239     /***
240      * @see org.eclipse.jface.preference.PreferencePage#performOk()
241      */
242     public boolean performOk() {
243         IPreferenceStore store = getPreferenceStore();
244 
245         store.setValue(
246             PreferenceConstants.PREF_AUTOUPDATE,
247             autoUpdate.getSelection());
248         store.setValue(
249             PreferenceConstants.PREF_UPDATE_INTERVAL,
250             Integer.parseInt(intervalField.getText()));
251 
252         return super.performOk();
253     }
254 
255     /***
256      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
257      */
258     protected void performDefaults() {
259         super.performDefaults();
260         IPreferenceStore store = getPreferenceStore();
261 
262         intervalField.setText(
263             String.valueOf(
264                 store.getDefaultInt(PreferenceConstants.PREF_UPDATE_INTERVAL)));
265         autoUpdate.setSelection(
266             store.getDefaultBoolean(PreferenceConstants.PREF_AUTOUPDATE));
267         updateStatus();
268     }
269 
270     /***
271      * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
272      */
273     public void init(IWorkbench workbench) {
274     }
275 
276     /***
277      * Helper method for String localization
278      * 
279      * @param key The property key of the String
280      * @return The localized String
281      */
282     private String localizeString(String key) {
283         return ContextManagerPlugin.getDefault().getLocalizedString(key);
284     }
285 
286 }