KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > timerwin > TimeAccPanel


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

19
20 package org.netbeans.modules.tasklist.timerwin;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import javax.swing.DefaultComboBoxModel JavaDoc;
25 import javax.swing.SwingUtilities JavaDoc;
26 import javax.swing.event.ChangeEvent JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
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 /**
35  * Panel with running task.
36  *
37  * @author tl
38  */

39 public class TimeAccPanel extends javax.swing.JPanel JavaDoc {
40     private PropertyChangeListener JavaDoc pcl;
41     private ObjectList.Listener oll;
42     private ChangeListener JavaDoc registryListener;
43     
44     /**
45      * Creates new form TimeAccPanel
46      */

47     public TimeAccPanel() {
48         initComponents();
49
50         registryListener = new ChangeListener JavaDoc() {
51             public void stateChanged(ChangeEvent JavaDoc e) {
52                 viewsChanged();
53             }
54         };
55         UserTaskViewRegistry.getInstance().addChangeListener(
56                 registryListener);
57         
58         pcl = new PropertyChangeListener JavaDoc() {
59             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
60                 String JavaDoc 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 JavaDoc[] 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 JavaDoc() {
81             public void stateChanged(ChangeEvent JavaDoc e) {
82                 startedTaskChanged();
83             }
84         });
85         
86         DefaultComboBoxModel JavaDoc m = new DefaultComboBoxModel JavaDoc();
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     /**
100      * A task was stopped or started.
101      */

102     private void startedTaskChanged() {
103         UserTask started = StartedUserTask.getInstance().getStarted();
104         if (started == null) {
105             jComboBoxTasks.setSelectedIndex(0);
106         } else {
107             DefaultComboBoxModel JavaDoc m =
108                     (DefaultComboBoxModel JavaDoc) 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     /**
119      * A view was closed or opened.
120      */

121     private void viewsChanged() {
122         DefaultComboBoxModel JavaDoc m =
123                 (DefaultComboBoxModel JavaDoc) 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     /**
148      * Removes a task from the combobox.
149      *
150      * @param index index of the task
151      */

152     private void removeUserTask(int index) {
153         DefaultComboBoxModel JavaDoc m =
154                 (DefaultComboBoxModel JavaDoc) 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     /**
165      * Adds a task to the combobox.
166      *
167      * @param ut a task
168      */

169     private void addUserTask(UserTask ut) {
170         DefaultComboBoxModel JavaDoc m =
171                 (DefaultComboBoxModel JavaDoc) 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     /**
181      * Searches a UT in the combo box.
182      *
183      * @param ut a task
184      * @return it's index or -1
185      */

186     private int findUserTask(UserTask ut) {
187         DefaultComboBoxModel JavaDoc m =
188                 (DefaultComboBoxModel JavaDoc) 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     /** This method is called from within the constructor to
200      * initialize the form.
201      * WARNING: Do NOT modify this code. The content of this method is
202      * always regenerated by the Form Editor.
203      */

204     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
205
private void initComponents() {
206         jPanel1 = new javax.swing.JPanel JavaDoc();
207         jComboBoxTasks = new javax.swing.JComboBox JavaDoc();
208         jButtonChoose = new javax.swing.JButton JavaDoc();
209         jButtonClose = new javax.swing.JButton JavaDoc();
210
211         setLayout(new java.awt.FlowLayout JavaDoc(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 JavaDoc(10, 22));
217         add(jPanel1);
218
219         jComboBoxTasks.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(new String JavaDoc[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
220         jComboBoxTasks.setPreferredSize(new java.awt.Dimension JavaDoc(200, 22));
221         jComboBoxTasks.addActionListener(new java.awt.event.ActionListener JavaDoc() {
222             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
223                 jComboBoxTasksActionPerformed(evt);
224             }
225         });
226
227         add(jComboBoxTasks);
228
229         jButtonChoose.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/org/netbeans/modules/tasklist/timerwin/dots.gif")));
230         jButtonChoose.setPreferredSize(new java.awt.Dimension JavaDoc(22, 22));
231         jButtonChoose.addActionListener(new java.awt.event.ActionListener JavaDoc() {
232             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
233                 jButtonChooseActionPerformed(evt);
234             }
235         });
236
237         add(jButtonChoose);
238
239         jButtonClose.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/org/netbeans/modules/tasklist/timerwin/close.gif")));
240         jButtonClose.setPreferredSize(new java.awt.Dimension JavaDoc(22, 22));
241         jButtonClose.addActionListener(new java.awt.event.ActionListener JavaDoc() {
242             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
243                 jButtonCloseActionPerformed(evt);
244             }
245         });
246
247         add(jButtonClose);
248
249     }// </editor-fold>//GEN-END:initComponents
250

251     private void jComboBoxTasksActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jComboBoxTasksActionPerformed
252
Object JavaDoc 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     }//GEN-LAST:event_jComboBoxTasksActionPerformed
262

263     private void jButtonChooseActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonChooseActionPerformed
264
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     }//GEN-LAST:event_jButtonChooseActionPerformed
271

272     private void jButtonCloseActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonCloseActionPerformed
273
SwingUtilities.getWindowAncestor(this).setVisible(false);
274     }//GEN-LAST:event_jButtonCloseActionPerformed
275

276     
277     // Variables declaration - do not modify//GEN-BEGIN:variables
278
private javax.swing.JButton JavaDoc jButtonChoose;
279     private javax.swing.JButton JavaDoc jButtonClose;
280     private javax.swing.JComboBox JavaDoc jComboBoxTasks;
281     private javax.swing.JPanel JavaDoc jPanel1;
282     // End of variables declaration//GEN-END:variables
283

284 }
285
Popular Tags