KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > util > TimeComboBox


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  * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks.util;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.TextComponent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.text.DateFormat JavaDoc;
26 import java.text.FieldPosition JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28 import java.text.ParseException JavaDoc;
29 import java.text.SimpleDateFormat JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import javax.swing.ComboBoxEditor JavaDoc;
32 import javax.swing.DefaultComboBoxModel JavaDoc;
33 import javax.swing.DefaultListCellRenderer JavaDoc;
34 import javax.swing.JComboBox JavaDoc;
35 import javax.swing.JList JavaDoc;
36 import javax.swing.text.JTextComponent JavaDoc;
37 import org.openide.util.NbBundle;
38
39 /**
40  * Time editor.
41  *
42  * @author tl
43  */

44 public class TimeComboBox extends JComboBox JavaDoc {
45     private static final MessageFormat JavaDoc FORMAT = new MessageFormat JavaDoc(
46             NbBundle.getMessage(TimeComboBox.class, "Format"));
47     
48     /**
49      * Creates a new instance of TimeComboBox.
50      */

51     public TimeComboBox() {
52         final ComboBoxEditor JavaDoc def = getEditor();
53         setEditor(new ComboBoxEditor JavaDoc() {
54             public void addActionListener(ActionListener JavaDoc l) {
55                 def.addActionListener(l);
56             }
57             public Component JavaDoc getEditorComponent() {
58                 return def.getEditorComponent();
59             }
60             public Object JavaDoc getItem() {
61                 try {
62                     Object JavaDoc[] obj = FORMAT.parse(
63                             ((JTextComponent JavaDoc) getEditorComponent()).getText());
64                     if (obj[0] == null || obj[1] == null)
65                         return 0;
66                     int h = ((Long JavaDoc) obj[0]).intValue();
67                     int m = ((Long JavaDoc) obj[1]).intValue();
68                     return h * 60 + m;
69                 } catch (ParseException JavaDoc ex) {
70                     return 0;
71                 }
72             }
73             public void removeActionListener(ActionListener JavaDoc l) {
74                 def.removeActionListener(l);
75             }
76             public void selectAll() {
77                 def.selectAll();
78             }
79             public void setItem(Object JavaDoc anObject) {
80                 String JavaDoc text = "";
81                 if (anObject != null) {
82                     int m = ((Integer JavaDoc) anObject).intValue();
83                     int h = m / 60;
84                     m = m % 60;
85                     text = FORMAT.format(new Object JavaDoc[] {h, m});
86                 }
87                 ((JTextComponent JavaDoc) getEditorComponent()).setText(text);
88             }
89         });
90         setEditable(true);
91         DefaultComboBoxModel JavaDoc m = new DefaultComboBoxModel JavaDoc();
92         for (int i = 0; i < 48; i++) {
93             m.addElement(new Integer JavaDoc(i * 30));
94         }
95         setModel(m);
96         setRenderer(new DefaultListCellRenderer JavaDoc() {
97             public Component JavaDoc getListCellRendererComponent(
98                    JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected,
99                    boolean cellHasFocus) {
100                 super.getListCellRendererComponent(list, value, index,
101                         isSelected, cellHasFocus);
102                 int m = ((Integer JavaDoc) value).intValue();
103                 int h = m / 60;
104                 m = m % 60;
105                 setText(FORMAT.format(new Object JavaDoc[] {h, m}));
106                 return this;
107             }
108         });
109     }
110     
111     /**
112      * Returns selected time.
113      *
114      * @return selected time in minutes.
115      */

116     public int getTime() {
117         return ((Integer JavaDoc) getSelectedItem()).intValue();
118     }
119     
120     /**
121      * Sets new time.
122      *
123      * @param minutes minutes (e.g. 100 for 01:40)
124      */

125     public void setTime(int minutes) {
126         int[] v = new int[getModel().getSize()];
127         for (int i = 0; i < v.length; i++) {
128             v[i] = (Integer JavaDoc) getModel().getElementAt(i);
129         }
130
131         int index = Arrays.binarySearch(v, minutes);
132         if (index < 0) {
133             index = -index - 1;
134             ((DefaultComboBoxModel JavaDoc) getModel()).insertElementAt(
135                     new Integer JavaDoc(minutes), index);
136         }
137         setSelectedIndex(index);
138     }
139 }
140
Popular Tags