1 19 20 package org.netbeans.modules.tasklist.usertasks.editors; 21 22 import java.awt.Component ; 23 import java.text.ParseException ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.List ; 27 28 import javax.swing.DefaultCellEditor ; 29 import javax.swing.DefaultComboBoxModel ; 30 import javax.swing.JComboBox ; 31 import javax.swing.JTable ; 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 42 public class EffortTableCellEditor extends DefaultCellEditor { 43 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 <Integer > durations; 69 private List <String > 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 78 public EffortTableCellEditor() { 79 super(new JComboBox ()); 80 } 81 82 public Component getTableCellEditorComponent(JTable table, Object 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 ) { 88 d = ((Integer ) value).intValue(); 89 } else { 90 d = 0; 91 } 92 93 durations = new ArrayList <Integer >(); 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 <String >(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 ) editorComponent).setModel( 116 new DefaultComboBoxModel ( 117 texts.toArray(new String [texts.size()]))); 118 ((JComboBox ) editorComponent).setSelectedIndex(index); 119 ((JComboBox ) editorComponent).setEditable(true); 120 return editorComponent; 121 } 122 123 public Object getCellEditorValue() { 124 int index = ((JComboBox ) editorComponent).getSelectedIndex(); 125 String txt = 126 (String ) ((JComboBox ) editorComponent).getEditor().getItem(); 127 Duration d = null; 128 try { 129 d = short_.parse(txt); 130 } catch (ParseException ex) { 131 } 133 if (d == null) { 134 try { 135 d = long_.parse(txt); 136 } catch (ParseException ex) { 137 } 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 |