1 19 20 package org.netbeans.modules.tasklist.timerwin; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import javax.swing.DefaultComboBoxModel ; 25 import javax.swing.SwingUtilities ; 26 import javax.swing.event.ChangeEvent ; 27 import javax.swing.event.ChangeListener ; 28 import org.netbeans.modules.tasklist.core.util.ObjectList; 29 import org.netbeans.modules.tasklist.usertasks.UserTaskView; 30 import org.netbeans.modules.tasklist.usertasks.UserTaskViewRegistry; 31 import org.netbeans.modules.tasklist.usertasks.model.StartedUserTask; 32 import org.netbeans.modules.tasklist.usertasks.model.UserTask; 33 34 39 public class TimeAccPanel extends javax.swing.JPanel { 40 private PropertyChangeListener pcl; 41 private ObjectList.Listener oll; 42 private ChangeListener registryListener; 43 44 47 public TimeAccPanel() { 48 initComponents(); 49 50 registryListener = new ChangeListener () { 51 public void stateChanged(ChangeEvent e) { 52 viewsChanged(); 53 } 54 }; 55 UserTaskViewRegistry.getInstance().addChangeListener( 56 registryListener); 57 58 pcl = new PropertyChangeListener () { 59 public void propertyChange(PropertyChangeEvent evt) { 60 String n = evt.getPropertyName(); 61 if (n == UserTask.PROP_SPENT_TIME || 62 n == UserTask.PROP_PROGRESS) 63 jComboBoxTasks.repaint(); 64 } 65 }; 66 67 oll = new ObjectList.Listener() { 68 public void listChanged(ObjectList.Event e) { 69 if (e.getType() == ObjectList.Event.EVENT_REMOVED) { 70 Object [] list = e.getObjects(); 71 for (int i = 0; i < list.length; i++) { 72 int index = findUserTask((UserTask) list[i]); 73 if (index >= 0) 74 removeUserTask(index); 75 } 76 } 77 } 78 }; 79 80 StartedUserTask.getInstance().addChangeListener(new ChangeListener () { 81 public void stateChanged(ChangeEvent e) { 82 startedTaskChanged(); 83 } 84 }); 85 86 DefaultComboBoxModel m = new DefaultComboBoxModel (); 87 m.addElement(null); 88 jComboBoxTasks.setModel(m); 89 90 UserTask ut = StartedUserTask.getInstance().getStarted(); 91 if (ut != null) { 92 addUserTask(ut); 93 jComboBoxTasks.setSelectedIndex(1); 94 } 95 96 jComboBoxTasks.setRenderer(new UserTaskListCellRenderer()); 97 } 98 99 102 private void startedTaskChanged() { 103 UserTask started = StartedUserTask.getInstance().getStarted(); 104 if (started == null) { 105 jComboBoxTasks.setSelectedIndex(0); 106 } else { 107 DefaultComboBoxModel m = 108 (DefaultComboBoxModel ) jComboBoxTasks.getModel(); 109 int index = findUserTask(started); 110 if (index < 0) { 111 addUserTask(started); 112 index = 1; 113 } 114 jComboBoxTasks.setSelectedIndex(index); 115 } 116 } 117 118 121 private void viewsChanged() { 122 DefaultComboBoxModel m = 123 (DefaultComboBoxModel ) jComboBoxTasks.getModel(); 124 for (int i = 0; i < m.getSize(); ) { 125 UserTask ut = (UserTask) m.getElementAt(i); 126 if (ut == null) { 127 i++; 128 continue; 129 } 130 131 if (ut.getList() == null) { 132 removeUserTask(i); 133 continue; 134 } 135 136 UserTaskView utv = UserTaskViewRegistry.getInstance(). 137 findView(ut.getList().getFile()); 138 if (utv == null) { 139 removeUserTask(i); 140 continue; 141 } 142 143 i++; 144 } 145 } 146 147 152 private void removeUserTask(int index) { 153 DefaultComboBoxModel m = 154 (DefaultComboBoxModel ) jComboBoxTasks.getModel(); 155 UserTask ut = (UserTask) m.getElementAt(index); 156 ut.removePropertyChangeListener(pcl); 157 if (ut.getParent() != null) 158 ut.getParent().getSubtasks().removeListener(oll); 159 if (ut.getList() != null) 160 ut.getList().getSubtasks().removeListener(oll); 161 m.removeElementAt(index); 162 } 163 164 169 private void addUserTask(UserTask ut) { 170 DefaultComboBoxModel m = 171 (DefaultComboBoxModel ) jComboBoxTasks.getModel(); 172 m.insertElementAt(ut, 1); 173 ut.addPropertyChangeListener(pcl); 174 if (ut.getParent() != null) 175 ut.getParent().getSubtasks().addListener(oll); 176 else 177 ut.getList().getSubtasks().addListener(oll); 178 } 179 180 186 private int findUserTask(UserTask ut) { 187 DefaultComboBoxModel m = 188 (DefaultComboBoxModel ) jComboBoxTasks.getModel(); 189 int index = -1; 190 for (int i = 0; i < m.getSize(); i++) { 191 if (m.getElementAt(i) == ut) { 192 index = i; 193 break; 194 } 195 } 196 return index; 197 } 198 199 204 private void initComponents() { 206 jPanel1 = new javax.swing.JPanel (); 207 jComboBoxTasks = new javax.swing.JComboBox (); 208 jButtonChoose = new javax.swing.JButton (); 209 jButtonClose = new javax.swing.JButton (); 210 211 setLayout(new java.awt.FlowLayout (java.awt.FlowLayout.LEFT, 5, 2)); 212 213 setBorder(javax.swing.BorderFactory.createEtchedBorder()); 214 setOpaque(false); 215 jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder()); 216 jPanel1.setPreferredSize(new java.awt.Dimension (10, 22)); 217 add(jPanel1); 218 219 jComboBoxTasks.setModel(new javax.swing.DefaultComboBoxModel (new String [] { "Item 1", "Item 2", "Item 3", "Item 4" })); 220 jComboBoxTasks.setPreferredSize(new java.awt.Dimension (200, 22)); 221 jComboBoxTasks.addActionListener(new java.awt.event.ActionListener () { 222 public void actionPerformed(java.awt.event.ActionEvent evt) { 223 jComboBoxTasksActionPerformed(evt); 224 } 225 }); 226 227 add(jComboBoxTasks); 228 229 jButtonChoose.setIcon(new javax.swing.ImageIcon (getClass().getResource("/org/netbeans/modules/tasklist/timerwin/dots.gif"))); 230 jButtonChoose.setPreferredSize(new java.awt.Dimension (22, 22)); 231 jButtonChoose.addActionListener(new java.awt.event.ActionListener () { 232 public void actionPerformed(java.awt.event.ActionEvent evt) { 233 jButtonChooseActionPerformed(evt); 234 } 235 }); 236 237 add(jButtonChoose); 238 239 jButtonClose.setIcon(new javax.swing.ImageIcon (getClass().getResource("/org/netbeans/modules/tasklist/timerwin/close.gif"))); 240 jButtonClose.setPreferredSize(new java.awt.Dimension (22, 22)); 241 jButtonClose.addActionListener(new java.awt.event.ActionListener () { 242 public void actionPerformed(java.awt.event.ActionEvent evt) { 243 jButtonCloseActionPerformed(evt); 244 } 245 }); 246 247 add(jButtonClose); 248 249 } 251 private void jComboBoxTasksActionPerformed(java.awt.event.ActionEvent evt) { Object sel = jComboBoxTasks.getSelectedItem(); 253 if (sel instanceof UserTask) { 254 if (StartedUserTask.getInstance().getStarted() != null) 255 StartedUserTask.getInstance().start(null); 256 StartedUserTask.getInstance().start((UserTask) sel); 257 } else { 258 if (StartedUserTask.getInstance().getStarted() != null) 259 StartedUserTask.getInstance().start(null); 260 } 261 } 263 private void jButtonChooseActionPerformed(java.awt.event.ActionEvent evt) { UserTask ut = UTChooserPanel.choose(); 265 if (ut != null) { 266 if (StartedUserTask.getInstance().getStarted() != null) 267 StartedUserTask.getInstance().start(null); 268 StartedUserTask.getInstance().start(ut); 269 } 270 } 272 private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt) { SwingUtilities.getWindowAncestor(this).setVisible(false); 274 } 276 277 private javax.swing.JButton jButtonChoose; 279 private javax.swing.JButton jButtonClose; 280 private javax.swing.JComboBox jComboBoxTasks; 281 private javax.swing.JPanel jPanel1; 282 284 } 285 | Popular Tags |