1 19 20 package org.netbeans.modules.tasklist.usertasks; 21 22 import java.awt.Component ; 23 import java.awt.event.ActionListener ; 24 import java.text.ParseException ; 25 import javax.swing.ComboBoxEditor ; 26 import javax.swing.DefaultComboBoxModel ; 27 import javax.swing.DefaultListCellRenderer ; 28 import javax.swing.JComboBox ; 29 import javax.swing.JList ; 30 import javax.swing.text.JTextComponent ; 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 41 public class DurationPanel extends JComboBox { 42 private static final long serialVersionUID = 2; 43 44 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 77 public DurationPanel() { 78 final ComboBoxEditor def = getEditor(); 79 setEditor(new ComboBoxEditor () { 80 public void addActionListener(ActionListener l) { 81 def.addActionListener(l); 82 } 83 public Component getEditorComponent() { 84 return def.getEditorComponent(); 85 } 86 public Object getItem() { 87 String text = ((JTextComponent ) getEditorComponent()).getText(); 88 Duration d = null; 89 try { 90 d = short_.parse(text); 91 } catch (ParseException ex) { 92 } 94 if (d == null) { 95 try { 96 d = long_.parse(text); 97 } catch (ParseException ex) { 98 } 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 l) { 110 def.removeActionListener(l); 111 } 112 public void selectAll() { 113 def.selectAll(); 114 } 115 public void setItem(Object anObject) { 116 String text = ""; 117 if (anObject != null) { 118 int m = ((Integer ) 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 ) getEditorComponent()).setText(text); 125 } 126 }); 127 setEditable(true); 128 DefaultComboBoxModel m = new DefaultComboBoxModel (); 129 for (int d: DURATIONS) { 130 m.addElement(d); 131 } 132 setModel(m); 133 setRenderer(new DefaultListCellRenderer () { 134 public Component getListCellRendererComponent( 135 JList list, Object value, int index, boolean isSelected, 136 boolean cellHasFocus) { 137 super.getListCellRendererComponent(list, value, index, 138 isSelected, cellHasFocus); 139 int m = ((Integer ) value).intValue(); 140 int mpd = Settings.getDefault().getMinutesPerDay(); 141 int dpw = Settings.getDefault().getDaysPerWeek(); 142 Duration d = new Duration(m, mpd, dpw, true); 144 String txt = long_.format(d); 145 if (txt.length() == 0) 146 txt = " "; setText(txt); 148 return this; 149 } 150 }); 151 } 152 153 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 ) getModel().getElementAt(i); 162 } 163 164 int index = java.util.Arrays.binarySearch(v, minutes); 165 if (index < 0) { 166 index = -index - 1; 167 ((DefaultComboBoxModel ) getModel()).insertElementAt(minutes, 168 index); 169 } 170 setSelectedIndex(index); 171 } 172 173 178 public int getDuration() { 179 return ((Integer ) getSelectedItem()).intValue(); 180 } 181 } 182 | Popular Tags |