KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > options > UTOptionsPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks.options;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.text.DateFormat JavaDoc;
25 import java.text.SimpleDateFormat JavaDoc;
26 import java.util.Calendar JavaDoc;
27 import java.util.prefs.Preferences JavaDoc;
28 import javax.swing.JLabel JavaDoc;
29 import javax.swing.event.DocumentEvent JavaDoc;
30 import javax.swing.event.DocumentListener JavaDoc;
31 import javax.swing.event.ListDataEvent JavaDoc;
32 import javax.swing.event.ListDataListener JavaDoc;
33 import org.jdesktop.layout.GroupLayout;
34 import org.netbeans.modules.tasklist.core.checklist.CheckList;
35 import org.netbeans.modules.tasklist.core.checklist.DefaultCheckListModel;
36 import org.netbeans.modules.tasklist.usertasks.util.TimeComboBox;
37 import org.openide.DialogDisplayer;
38 import org.openide.NotifyDescriptor;
39 import org.openide.util.NbBundle;
40
41 /**
42  * Panel with options.
43  *
44  * @author tl
45  */

46 public class UTOptionsPanel extends javax.swing.JPanel JavaDoc implements ActionListener JavaDoc,
47 ListDataListener JavaDoc {
48     private TimeComboBox cbDayStart;
49     private TimeComboBox cbPauseStart;
50     private TimeComboBox cbPauseEnd;
51     private TimeComboBox cbDayEnd;
52     private CheckList clWorkingDays;
53     private boolean changed;
54
55     /**
56      * Localizes a message.
57      *
58      * @param key a key
59      * @return localized value
60      */

61     private static String JavaDoc i18n(String JavaDoc key) {
62         return NbBundle.getMessage(UTOptionsPanel.class, key);
63     }
64     
65     /**
66      * Creates new form UTOptionsPanel.
67      */

68     public UTOptionsPanel() {
69         initComponents();
70         jTextFieldFile.getDocument().addDocumentListener(new DocumentListener JavaDoc() {
71             public void changedUpdate(DocumentEvent JavaDoc e) {
72                 fileChanged();
73             }
74             public void insertUpdate(DocumentEvent JavaDoc e) {
75                 fileChanged();
76             }
77             public void removeUpdate(DocumentEvent JavaDoc e) {
78                 fileChanged();
79             }
80         });
81         GroupLayout layout = new GroupLayout(jPanelWorkingHours);
82         jPanelWorkingHours.setLayout(layout);
83         layout.setAutocreateContainerGaps(true);
84         layout.setAutocreateGaps(true);
85         JLabel JavaDoc lDayStart = new JLabel JavaDoc(i18n("StartWorkingDay")); // NOI18N
86
JLabel JavaDoc lPauseStart = new JLabel JavaDoc(i18n("StartPause")); // NOI18N
87
JLabel JavaDoc lPauseEnd = new JLabel JavaDoc(i18n("EndPause")); // NOI18N
88
JLabel JavaDoc lDayEnd = new JLabel JavaDoc(i18n("EndWorkingDay")); // NOI18N
89
JLabel JavaDoc lWorkingDays = new JLabel JavaDoc(i18n("WorkingDays")); // NOI18N
90
cbDayStart = new TimeComboBox();
91         cbDayStart.addActionListener(this);
92         cbPauseStart = new TimeComboBox();
93         cbPauseStart.addActionListener(this);
94         cbPauseEnd = new TimeComboBox();
95         cbPauseEnd.addActionListener(this);
96         cbDayEnd = new TimeComboBox();
97         cbDayEnd.addActionListener(this);
98         Calendar JavaDoc c = Calendar.getInstance();
99         c.set(2006, 0, 2); // monday
100
String JavaDoc[] days = new String JavaDoc[7];
101         DateFormat JavaDoc df = new SimpleDateFormat JavaDoc("EEEE"); // NOI18N
102
for (int i = 0; i < days.length; i++) {
103             days[i] = df.format(c.getTime());
104             c.add(Calendar.DAY_OF_YEAR, 1);
105         }
106         DefaultCheckListModel m = new DefaultCheckListModel(
107                 new boolean[7], days);
108         m.addListDataListener(this);
109         clWorkingDays = new CheckList(m);
110         GroupLayout.ParallelGroup hpg1 = layout.createParallelGroup().
111                 add(lDayStart).add(lPauseStart).add(lPauseEnd).add(lDayEnd).
112                 add(lWorkingDays);
113         GroupLayout.ParallelGroup hpg2 = layout.createParallelGroup().
114                 add(cbDayStart).add(cbPauseStart).add(cbPauseEnd).add(cbDayEnd).
115                 add(clWorkingDays);
116         GroupLayout.SequentialGroup hsg1 = layout.createSequentialGroup().
117                 add(hpg1).add(hpg2);
118         layout.setHorizontalGroup(hsg1);
119         GroupLayout.ParallelGroup vpg1 =
120                 layout.createParallelGroup(GroupLayout.BASELINE).
121                 add(lDayStart).add(cbDayStart);
122         GroupLayout.ParallelGroup vpg2 =
123                 layout.createParallelGroup(GroupLayout.BASELINE).
124                 add(lPauseStart).add(cbPauseStart);
125         GroupLayout.ParallelGroup vpg3 =
126                 layout.createParallelGroup(GroupLayout.BASELINE).
127                 add(lPauseEnd).add(cbPauseEnd);
128         GroupLayout.ParallelGroup vpg4 =
129                 layout.createParallelGroup(GroupLayout.BASELINE).
130                 add(lDayEnd).add(cbDayEnd);
131         GroupLayout.ParallelGroup vpg5 =
132                 layout.createParallelGroup(GroupLayout.LEADING).
133                 add(lWorkingDays).add(clWorkingDays);
134         GroupLayout.SequentialGroup vsg = layout.createSequentialGroup().
135                 add(vpg1).add(vpg2).add(vpg3).add(vpg4).add(vpg5);
136         layout.setVerticalGroup(vsg);
137     }
138
139     /**
140      * Component should update its content.
141      */

142     public void update() {
143         Settings s = Settings.getDefault();
144         jCheckBoxAppend.setSelected(s.getAppend());
145         jCheckBoxCollectWorkPeriods.setSelected(s.getCollectWorkPeriods());
146         jTextFieldFile.setText(s.getFilename());
147         jCheckBoxDetectInactivity.setSelected(s.getDetectInactivity());
148         jCheckBoxAutoSwitchToComputed.setSelected(s.getAutoSwitchToComputed());
149         cbDayStart.setTime(s.getWorkingDayStart());
150         cbPauseStart.setTime(s.getPauseStart());
151         cbPauseEnd.setTime(s.getPauseEnd());
152         cbDayEnd.setTime(s.getWorkingDayEnd());
153         boolean[] wd = s.getWorkingDays();
154         DefaultCheckListModel m =
155                 (DefaultCheckListModel) clWorkingDays.getModel();
156         for (int i = 0; i < 7; i++) {
157             m.setChecked(i, wd[i]);
158         }
159         changed = false;
160     }
161
162     /**
163      * This method is called when Options Dialog "OK" button is pressed.
164      */

165     public void applyChanges() {
166         if (!changed)
167             return;
168         
169         if (!isContentValid()) {
170             DialogDisplayer.getDefault().notify(
171                     new NotifyDescriptor.Message(i18n(
172                     "SettingValidationError"))); // NOI18N
173
return;
174         }
175         
176         Settings s = Settings.getDefault();
177         s.setAppend(jCheckBoxAppend.isSelected());
178         s.setCollectWorkPeriods(jCheckBoxCollectWorkPeriods.isSelected());
179         s.setFilename(jTextFieldFile.getText());
180         s.setDetectInactivity(jCheckBoxDetectInactivity.isSelected());
181         s.setAutoSwitchToComputed(jCheckBoxAutoSwitchToComputed.isSelected());
182         s.setWorkingDayStart(cbDayStart.getTime());
183         s.setPauseStart(cbPauseStart.getTime());
184         s.setPauseEnd(cbPauseEnd.getTime());
185         s.setWorkingDayEnd(cbDayEnd.getTime());
186         DefaultCheckListModel m =
187                 (DefaultCheckListModel) clWorkingDays.getModel();
188         for (int i = 0; i < 7; i++) {
189             s.setWorkingDay(i, m.isChecked(i));
190         }
191     }
192
193     /**
194      * This method is called when Options Dialog "Cancel" button is pressed.
195      */

196     public void cancel() {
197         
198     }
199     
200     /**
201      * Will be called if the text in the default file text field has changed.
202      */

203     private void fileChanged() {
204         changed = true;
205     }
206     
207     /**
208      * Was the data changed?
209      *
210      * @return true = changed
211      */

212     public boolean isChanged() {
213         return changed;
214     }
215     
216     /**
217      * This method is called from within the constructor to
218      * initialize the form.
219      * WARNING: Do NOT modify this code. The content of this method is
220      * always regenerated by the Form Editor.
221      */

222     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
223
private void initComponents() {
224
225         jCheckBoxAppend = new javax.swing.JCheckBox JavaDoc();
226         jCheckBoxCollectWorkPeriods = new javax.swing.JCheckBox JavaDoc();
227         jLabel3 = new javax.swing.JLabel JavaDoc();
228         jTextFieldFile = new javax.swing.JTextField JavaDoc();
229         jPanelWorkingHours = new javax.swing.JPanel JavaDoc();
230         jCheckBoxDetectInactivity = new javax.swing.JCheckBox JavaDoc();
231         jCheckBoxAutoSwitchToComputed = new javax.swing.JCheckBox JavaDoc();
232
233         org.openide.awt.Mnemonics.setLocalizedText(jCheckBoxAppend, org.openide.util.NbBundle.getMessage(UTOptionsPanel.class, "AppendVsPrepend")); // NOI18N
234
jCheckBoxAppend.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
235         jCheckBoxAppend.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
236         jCheckBoxAppend.setOpaque(false);
237         jCheckBoxAppend.addActionListener(new java.awt.event.ActionListener JavaDoc() {
238             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
239                 jCheckBoxAppendActionPerformed(evt);
240             }
241         });
242
243         org.openide.awt.Mnemonics.setLocalizedText(jCheckBoxCollectWorkPeriods, org.openide.util.NbBundle.getMessage(UTOptionsPanel.class, "CollectWorkPeriods")); // NOI18N
244
jCheckBoxCollectWorkPeriods.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
245         jCheckBoxCollectWorkPeriods.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
246         jCheckBoxCollectWorkPeriods.setOpaque(false);
247         jCheckBoxCollectWorkPeriods.addActionListener(new java.awt.event.ActionListener JavaDoc() {
248             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
249                 jCheckBoxCollectWorkPeriodsActionPerformed(evt);
250             }
251         });
252
253         jLabel3.setLabelFor(jTextFieldFile);
254         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(UTOptionsPanel.class, "DefaultUserTasksFile")); // NOI18N
255

256         jPanelWorkingHours.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(UTOptionsPanel.class, "Work"))); // NOI18N
257
jPanelWorkingHours.setOpaque(false);
258
259         org.jdesktop.layout.GroupLayout jPanelWorkingHoursLayout = new org.jdesktop.layout.GroupLayout(jPanelWorkingHours);
260         jPanelWorkingHours.setLayout(jPanelWorkingHoursLayout);
261         jPanelWorkingHoursLayout.setHorizontalGroup(
262             jPanelWorkingHoursLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
263             .add(0, 237, Short.MAX_VALUE)
264         );
265         jPanelWorkingHoursLayout.setVerticalGroup(
266             jPanelWorkingHoursLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
267             .add(0, 194, Short.MAX_VALUE)
268         );
269
270         org.openide.awt.Mnemonics.setLocalizedText(jCheckBoxDetectInactivity, org.openide.util.NbBundle.getBundle(UTOptionsPanel.class).getString("DetectInactivity")); // NOI18N
271
jCheckBoxDetectInactivity.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
272         jCheckBoxDetectInactivity.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
273         jCheckBoxDetectInactivity.setOpaque(false);
274         jCheckBoxDetectInactivity.addActionListener(new java.awt.event.ActionListener JavaDoc() {
275             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
276                 jCheckBoxDetectInactivityActionPerformed(evt);
277             }
278         });
279
280         org.openide.awt.Mnemonics.setLocalizedText(jCheckBoxAutoSwitchToComputed, org.openide.util.NbBundle.getBundle(UTOptionsPanel.class).getString("AutoSwithToComputed")); // NOI18N
281
jCheckBoxAutoSwitchToComputed.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
282         jCheckBoxAutoSwitchToComputed.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
283         jCheckBoxAutoSwitchToComputed.setOpaque(false);
284         jCheckBoxAutoSwitchToComputed.addActionListener(new java.awt.event.ActionListener JavaDoc() {
285             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
286                 jCheckBoxAutoSwitchToComputedActionPerformed(evt);
287             }
288         });
289
290         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
291         this.setLayout(layout);
292         layout.setHorizontalGroup(
293             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
294             .add(layout.createSequentialGroup()
295                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
296                     .add(jCheckBoxAppend)
297                     .add(jCheckBoxCollectWorkPeriods)
298                     .add(jCheckBoxDetectInactivity)
299                     .add(jCheckBoxAutoSwitchToComputed, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 664, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
300                     .add(layout.createSequentialGroup()
301                         .add(jLabel3)
302                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
303                         .add(jTextFieldFile, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 504, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
304                     .add(jPanelWorkingHours, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
305                 .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
306         );
307         layout.setVerticalGroup(
308             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
309             .add(layout.createSequentialGroup()
310                 .add(jCheckBoxAppend)
311                 .add(11, 11, 11)
312                 .add(jCheckBoxCollectWorkPeriods)
313                 .add(11, 11, 11)
314                 .add(jCheckBoxDetectInactivity)
315                 .add(11, 11, 11)
316                 .add(jCheckBoxAutoSwitchToComputed)
317                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
318                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
319                     .add(jLabel3)
320                     .add(jTextFieldFile, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
321                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
322                 .add(jPanelWorkingHours, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
323         );
324     }// </editor-fold>//GEN-END:initComponents
325

326     private void jCheckBoxAutoSwitchToComputedActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jCheckBoxAutoSwitchToComputedActionPerformed
327
changed = true;
328     }//GEN-LAST:event_jCheckBoxAutoSwitchToComputedActionPerformed
329

330     private void jCheckBoxDetectInactivityActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jCheckBoxDetectInactivityActionPerformed
331
changed = true;
332     }//GEN-LAST:event_jCheckBoxDetectInactivityActionPerformed
333

334     private void jCheckBoxCollectWorkPeriodsActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jCheckBoxCollectWorkPeriodsActionPerformed
335
changed = true;
336     }//GEN-LAST:event_jCheckBoxCollectWorkPeriodsActionPerformed
337

338     private void jCheckBoxAppendActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jCheckBoxAppendActionPerformed
339
changed = true;
340     }//GEN-LAST:event_jCheckBoxAppendActionPerformed
341

342     /**
343      * Checks the content of this panel.
344      *
345      * @return true = OK
346      */

347     public boolean isContentValid() {
348         if (jTextFieldFile.getText().trim().length() == 0)
349             return false;
350         int a = cbDayStart.getTime();
351         int b = cbPauseStart.getTime();
352         int c = cbPauseEnd.getTime();
353         int d = cbDayEnd.getTime();
354         if (b < a || c < b || d < c)
355             return false;
356         boolean all = false;
357         DefaultCheckListModel m = (DefaultCheckListModel)
358                 clWorkingDays.getModel();
359         for (int i = 0; i < m.getSize(); i++) {
360             all |= m.isChecked(i);
361         }
362         if (!all)
363             return false;
364         return true;
365     }
366
367     public void actionPerformed(ActionEvent JavaDoc e) {
368         changed = true;
369     }
370
371     public void intervalAdded(ListDataEvent JavaDoc e) {
372         changed = true;
373     }
374
375     public void intervalRemoved(ListDataEvent JavaDoc e) {
376         changed = true;
377     }
378
379     public void contentsChanged(ListDataEvent JavaDoc e) {
380         changed = true;
381     }
382         
383     // Variables declaration - do not modify//GEN-BEGIN:variables
384
public javax.swing.JCheckBox JavaDoc jCheckBoxAppend;
385     public javax.swing.JCheckBox JavaDoc jCheckBoxAutoSwitchToComputed;
386     public javax.swing.JCheckBox JavaDoc jCheckBoxCollectWorkPeriods;
387     public javax.swing.JCheckBox JavaDoc jCheckBoxDetectInactivity;
388     public javax.swing.JLabel JavaDoc jLabel3;
389     public javax.swing.JPanel JavaDoc jPanelWorkingHours;
390     public javax.swing.JTextField JavaDoc jTextFieldFile;
391     // End of variables declaration//GEN-END:variables
392
}
393
Popular Tags