KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > editors > EffortTableCellEditor


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.editors;
21
22 import java.awt.Component JavaDoc;
23 import java.text.ParseException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.swing.DefaultCellEditor JavaDoc;
29 import javax.swing.DefaultComboBoxModel JavaDoc;
30 import javax.swing.JComboBox JavaDoc;
31 import javax.swing.JTable JavaDoc;
32 import org.netbeans.modules.tasklist.usertasks.options.Settings;
33 import org.netbeans.modules.tasklist.usertasks.model.Duration;
34 import org.netbeans.modules.tasklist.usertasks.model.UserTask;
35 import org.netbeans.modules.tasklist.usertasks.util.DurationFormat;
36
37 /**
38  * TableCellEditor for duration values.
39  *
40  * @author tl
41  */

42 public class EffortTableCellEditor extends DefaultCellEditor JavaDoc {
43     /**
44      * This array must be sorted.
45      */

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

78     public EffortTableCellEditor() {
79         super(new JComboBox JavaDoc());
80     }
81
82     public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value,
83         boolean isSelected, int row, int column) {
84         int d;
85         if (value instanceof UserTask) {
86             d = ((UserTask) value).getEffort();
87         } else if (value instanceof Integer JavaDoc) {
88             d = ((Integer JavaDoc) value).intValue();
89         } else {
90             d = 0;
91         }
92
93         durations = new ArrayList JavaDoc<Integer JavaDoc>();
94         for (int dur: DURATIONS) {
95             durations.add(dur);
96         }
97
98         int index = Collections.binarySearch(durations, d);
99         if (index < 0) {
100             index = -index - 1;
101             durations.add(index, d);
102         }
103
104         DurationFormat df = new DurationFormat(DurationFormat.Type.LONG);
105         texts = new ArrayList JavaDoc<String JavaDoc>(durations.size());
106         Settings s = Settings.getDefault();
107         int mpd = s.getMinutesPerDay();
108         int dpw = s.getDaysPerWeek();
109         for (int dur: durations) {
110             texts.add(df.format(new Duration(dur, mpd, dpw, true)));
111         }
112         
113         this.dur = new Duration(durations.get(index), mpd, dpw, true);
114                 
115         ((JComboBox JavaDoc) editorComponent).setModel(
116                 new DefaultComboBoxModel JavaDoc(
117                 texts.toArray(new String JavaDoc[texts.size()])));
118         ((JComboBox JavaDoc) editorComponent).setSelectedIndex(index);
119         ((JComboBox JavaDoc) editorComponent).setEditable(true);
120         return editorComponent;
121     }
122     
123     public Object JavaDoc getCellEditorValue() {
124         int index = ((JComboBox JavaDoc) editorComponent).getSelectedIndex();
125         String JavaDoc txt =
126                 (String JavaDoc) ((JComboBox JavaDoc) editorComponent).getEditor().getItem();
127         Duration d = null;
128         try {
129             d = short_.parse(txt);
130         } catch (ParseException JavaDoc ex) {
131             // ignore
132
}
133         if (d == null) {
134             try {
135                 d = long_.parse(txt);
136             } catch (ParseException JavaDoc ex) {
137                 // ignore
138
}
139         }
140         if (d == null)
141             d = dur;
142         if (d == null)
143             return durations.get(index);
144         else {
145             int mpd = Settings.getDefault().getMinutesPerDay();
146             int dpw = Settings.getDefault().getDaysPerWeek();
147             return d.toMinutes(mpd, dpw, true);
148         }
149     }
150 }
151
Popular Tags