KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > DurationPanel


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;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.text.ParseException JavaDoc;
25 import javax.swing.ComboBoxEditor JavaDoc;
26 import javax.swing.DefaultComboBoxModel JavaDoc;
27 import javax.swing.DefaultListCellRenderer JavaDoc;
28 import javax.swing.JComboBox JavaDoc;
29 import javax.swing.JList JavaDoc;
30 import javax.swing.text.JTextComponent JavaDoc;
31
32 import org.netbeans.modules.tasklist.usertasks.model.Duration;
33 import org.netbeans.modules.tasklist.usertasks.options.Settings;
34 import org.netbeans.modules.tasklist.usertasks.util.DurationFormat;
35
36 /**
37  * Panel for duration
38  *
39  * @author tl
40  */

41 public class DurationPanel extends JComboBox JavaDoc {
42     private static final long serialVersionUID = 2;
43     
44     /**
45      * This array must be sorted.
46      */

47     private static final int[] DURATIONS = new int[] {
48         0,
49         5,
50         10,
51         15,
52         20,
53         30,
54         45,
55         60,
56         90,
57         120,
58         150,
59         180,
60         240,
61         300,
62         360,
63         420,
64         480,
65         12 * 60,
66         8 * 60 * 2
67     };
68     
69     private DurationFormat short_ = new DurationFormat(
70             DurationFormat.Type.SHORT);
71     private DurationFormat long_ = new DurationFormat(DurationFormat.Type.LONG);
72     private Duration dur;
73     
74     /**
75      * Creates new form DurationPanel
76      */

77     public DurationPanel() {
78         final ComboBoxEditor JavaDoc def = getEditor();
79         setEditor(new ComboBoxEditor JavaDoc() {
80             public void addActionListener(ActionListener JavaDoc l) {
81                 def.addActionListener(l);
82             }
83             public Component JavaDoc getEditorComponent() {
84                 return def.getEditorComponent();
85             }
86             public Object JavaDoc getItem() {
87                 String JavaDoc text = ((JTextComponent JavaDoc) getEditorComponent()).getText();
88                 Duration d = null;
89                 try {
90                     d = short_.parse(text);
91                 } catch (ParseException JavaDoc ex) {
92                     // ignore
93
}
94                 if (d == null) {
95                     try {
96                         d = long_.parse(text);
97                     } catch (ParseException JavaDoc ex) {
98                         // ignore
99
}
100                 }
101                 if (d == null)
102                     d = dur;
103                 if (d == null)
104                     return 0;
105
106                 return d.toMinutes(Settings.getDefault().getMinutesPerDay(),
107                         Settings.getDefault().getDaysPerWeek(), true);
108             }
109             public void removeActionListener(ActionListener JavaDoc l) {
110                 def.removeActionListener(l);
111             }
112             public void selectAll() {
113                 def.selectAll();
114             }
115             public void setItem(Object JavaDoc anObject) {
116                 String JavaDoc text = "";
117                 if (anObject != null) {
118                     int m = ((Integer JavaDoc) anObject).intValue();
119                     int mpd = Settings.getDefault().getMinutesPerDay();
120                     int dpw = Settings.getDefault().getDaysPerWeek();
121                     Duration d = new Duration(m, mpd, dpw, true);
122                     text = long_.format(d);
123                 }
124                 ((JTextComponent JavaDoc) getEditorComponent()).setText(text);
125             }
126         });
127         setEditable(true);
128         DefaultComboBoxModel JavaDoc m = new DefaultComboBoxModel JavaDoc();
129         for (int d: DURATIONS) {
130             m.addElement(d);
131         }
132         setModel(m);
133         setRenderer(new DefaultListCellRenderer JavaDoc() {
134             public Component JavaDoc getListCellRendererComponent(
135                    JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected,
136                    boolean cellHasFocus) {
137                 super.getListCellRendererComponent(list, value, index,
138                         isSelected, cellHasFocus);
139                 int m = ((Integer JavaDoc) value).intValue();
140                 int mpd = Settings.getDefault().getMinutesPerDay();
141                 int dpw = Settings.getDefault().getDaysPerWeek();
142                 // UTUtils.LOGGER.fine("mpd=" + mpd + ", dpw=" + dpw);
143
Duration d = new Duration(m, mpd, dpw, true);
144                 String JavaDoc txt = long_.format(d);
145                 if (txt.length() == 0)
146                     txt = " "; // NOI18N
147
setText(txt);
148                 return this;
149             }
150         });
151     }
152     
153     /**
154      * Sets the duration shown in this panel
155      *
156      * @param minutes new duration in minutes
157      */

158     public void setDuration(int minutes) {
159         int[] v = new int[getModel().getSize()];
160         for (int i = 0; i < v.length; i++) {
161             v[i] = (Integer JavaDoc) getModel().getElementAt(i);
162         }
163
164         int index = java.util.Arrays.binarySearch(v, minutes);
165         if (index < 0) {
166             index = -index - 1;
167             ((DefaultComboBoxModel JavaDoc) getModel()).insertElementAt(minutes,
168                     index);
169         }
170         setSelectedIndex(index);
171     }
172     
173     /**
174      * Returns choosed duration in minutes
175      *
176      * @return duration in minutes
177      */

178     public int getDuration() {
179         return ((Integer JavaDoc) getSelectedItem()).intValue();
180     }
181 }
182
Popular Tags