1 19 20 package org.netbeans.modules.tasklist.usertasks.util; 21 22 import java.awt.Component ; 23 import java.awt.TextComponent ; 24 import java.awt.event.ActionListener ; 25 import java.text.DateFormat ; 26 import java.text.FieldPosition ; 27 import java.text.MessageFormat ; 28 import java.text.ParseException ; 29 import java.text.SimpleDateFormat ; 30 import java.util.Arrays ; 31 import javax.swing.ComboBoxEditor ; 32 import javax.swing.DefaultComboBoxModel ; 33 import javax.swing.DefaultListCellRenderer ; 34 import javax.swing.JComboBox ; 35 import javax.swing.JList ; 36 import javax.swing.text.JTextComponent ; 37 import org.openide.util.NbBundle; 38 39 44 public class TimeComboBox extends JComboBox { 45 private static final MessageFormat FORMAT = new MessageFormat ( 46 NbBundle.getMessage(TimeComboBox.class, "Format")); 47 48 51 public TimeComboBox() { 52 final ComboBoxEditor def = getEditor(); 53 setEditor(new ComboBoxEditor () { 54 public void addActionListener(ActionListener l) { 55 def.addActionListener(l); 56 } 57 public Component getEditorComponent() { 58 return def.getEditorComponent(); 59 } 60 public Object getItem() { 61 try { 62 Object [] obj = FORMAT.parse( 63 ((JTextComponent ) getEditorComponent()).getText()); 64 if (obj[0] == null || obj[1] == null) 65 return 0; 66 int h = ((Long ) obj[0]).intValue(); 67 int m = ((Long ) obj[1]).intValue(); 68 return h * 60 + m; 69 } catch (ParseException ex) { 70 return 0; 71 } 72 } 73 public void removeActionListener(ActionListener l) { 74 def.removeActionListener(l); 75 } 76 public void selectAll() { 77 def.selectAll(); 78 } 79 public void setItem(Object anObject) { 80 String text = ""; 81 if (anObject != null) { 82 int m = ((Integer ) anObject).intValue(); 83 int h = m / 60; 84 m = m % 60; 85 text = FORMAT.format(new Object [] {h, m}); 86 } 87 ((JTextComponent ) getEditorComponent()).setText(text); 88 } 89 }); 90 setEditable(true); 91 DefaultComboBoxModel m = new DefaultComboBoxModel (); 92 for (int i = 0; i < 48; i++) { 93 m.addElement(new Integer (i * 30)); 94 } 95 setModel(m); 96 setRenderer(new DefaultListCellRenderer () { 97 public Component getListCellRendererComponent( 98 JList list, Object value, int index, boolean isSelected, 99 boolean cellHasFocus) { 100 super.getListCellRendererComponent(list, value, index, 101 isSelected, cellHasFocus); 102 int m = ((Integer ) value).intValue(); 103 int h = m / 60; 104 m = m % 60; 105 setText(FORMAT.format(new Object [] {h, m})); 106 return this; 107 } 108 }); 109 } 110 111 116 public int getTime() { 117 return ((Integer ) getSelectedItem()).intValue(); 118 } 119 120 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 ) getModel().getElementAt(i); 129 } 130 131 int index = Arrays.binarySearch(v, minutes); 132 if (index < 0) { 133 index = -index - 1; 134 ((DefaultComboBoxModel ) getModel()).insertElementAt( 135 new Integer (minutes), index); 136 } 137 setSelectedIndex(index); 138 } 139 } 140 | Popular Tags |