KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyEditorSupport JavaDoc;
23 import java.text.ParseException JavaDoc;
24 import org.netbeans.modules.tasklist.usertasks.options.Settings;
25 import org.netbeans.modules.tasklist.usertasks.model.Duration;
26 import org.netbeans.modules.tasklist.usertasks.util.DurationFormat;
27 import org.openide.explorer.propertysheet.PropertyEnv;
28
29 /**
30  * PropertyEditor for duration in minutes.
31  *
32  * @author tl
33  */

34 public class DurationPropertyEditor extends PropertyEditorSupport JavaDoc {
35      private static final int[] DURATIONS = new int[] {
36         0,
37         5,
38         10,
39         15,
40         20,
41         30,
42         45,
43         60,
44         90,
45         120,
46         150,
47         180,
48         240,
49         300,
50         360,
51         420,
52         480,
53         12 * 60,
54         8 * 60 * 2
55     };
56     private static String JavaDoc[] TAGS;
57      
58     private static final DurationFormat FORMAT = new DurationFormat(
59             DurationFormat.Type.SHORT);
60     private DurationFormat LONG = new DurationFormat(DurationFormat.Type.LONG);
61
62     public String JavaDoc getAsText() {
63         Integer JavaDoc value = (Integer JavaDoc) getValue();
64         int duration = value == null ? 0 : value.intValue();
65         Duration d = new Duration(duration,
66             Settings.getDefault().getMinutesPerDay(),
67             Settings.getDefault().getDaysPerWeek(), true);
68
69         return FORMAT.format(d);
70     }
71
72     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
73         Duration d = null;
74         try {
75             d = FORMAT.parse(text);
76         } catch (ParseException JavaDoc ex) {
77             try {
78                 d = LONG.parse(text);
79             } catch (ParseException JavaDoc e) {
80                 throw new IllegalArgumentException JavaDoc(e);
81             }
82         }
83         
84         setValue(new Integer JavaDoc(d.toMinutes(
85                 Settings.getDefault().getMinutesPerDay(),
86                 Settings.getDefault().getDaysPerWeek(), true)));
87     }
88
89     public void attachEnv(PropertyEnv env) {
90         env.getFeatureDescriptor().setValue( "canEditAsText", Boolean.TRUE );
91     }
92     
93     public String JavaDoc[] getTags() {
94         if (TAGS == null) {
95             int mpd = Settings.getDefault().getMinutesPerDay();
96             int dpw = Settings.getDefault().getDaysPerWeek();
97             TAGS = new String JavaDoc[DURATIONS.length];
98             for (int i = 0; i < TAGS.length; i++) {
99                 TAGS[i] = FORMAT.format(new Duration(
100                         DURATIONS[i], mpd, dpw, false));
101             }
102         }
103         return TAGS;
104     }
105 }
106
Popular Tags