KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > dependencies > DependencyPanel


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.usertasks.dependencies;
21
22 import java.awt.Image JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25 import javax.swing.ImageIcon JavaDoc;
26 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
27 import javax.swing.tree.TreePath JavaDoc;
28 import org.netbeans.modules.tasklist.usertasks.model.UserTask;
29 import org.openide.DialogDescriptor;
30
31 import org.openide.util.Utilities;
32 import org.netbeans.modules.tasklist.usertasks.model.Dependency;
33 import org.openide.util.NbBundle;
34
35 /**
36  * Panel for editing a dependency
37  */

38 public class DependencyPanel extends javax.swing.JPanel JavaDoc {
39     /**
40      * Tree cell renderer for user tasks
41      */

42     private static class UserTaskTreeCellRenderer extends DefaultTreeCellRenderer JavaDoc {
43         private static final Image JavaDoc IMAGE =
44             Utilities.loadImage("org/netbeans/modules/tasklist/core/task.gif"); // NOI18N
45
private static final Image JavaDoc DONE =
46             Utilities.loadImage("org/netbeans/modules/tasklist/core/doneItem.gif"); // NOI18N
47
private ImageIcon JavaDoc icon = new ImageIcon JavaDoc();
48         
49         public java.awt.Component JavaDoc getTreeCellRendererComponent(
50             javax.swing.JTree JavaDoc tree, Object JavaDoc value, boolean sel, boolean expanded,
51             boolean leaf, int row, boolean hasFocus) {
52             
53             super.getTreeCellRendererComponent(
54                 tree, value, sel, expanded, leaf, row, hasFocus);
55             if (value instanceof UserTask) {
56                 UserTask ut = (UserTask) value;
57                 this.setText(ut.getSummary());
58                 this.setIcon(icon);
59
60                 if (ut.isDone())
61                     icon.setImage(DONE);
62                 else
63                     icon.setImage(IMAGE);
64             }
65             
66             return this;
67         }
68     }
69     
70     private UserTask ut;
71     private DialogDescriptor dd;
72     private List JavaDoc curDeps;
73     
74     /**
75      * Constructor
76      *
77      * @param ut user task that will have a new dependency on another task
78      * @param curDeps <Dependency> current dependencies
79      */

80     public DependencyPanel(UserTask ut, List JavaDoc curDeps) {
81         this.ut = ut;
82         this.curDeps = curDeps;
83         initComponents();
84         jTree.setModel(new UserTaskTreeModel(ut.getList()));
85         jTree.setCellRenderer(new UserTaskTreeCellRenderer());
86         jTreeValueChanged(null);
87     }
88     
89     /**
90      * Sets a dialog descriptor
91      *
92      * @param dd a dialog descriptor
93      */

94     public void setDialogDescriptor(DialogDescriptor dd) {
95         this.dd = dd;
96         jTreeValueChanged(null);
97     }
98     
99     /**
100      * Selects another task in the tree.
101      *
102      * @param ut the task or null
103      */

104     public void setSelectedTask(UserTask ut) {
105         if (ut != null)
106             jTree.setSelectionPath(ut.getPathTo());
107         else
108             jTree.setSelectionPath(null);
109     }
110
111     /**
112      * Returns selected task.
113      *
114      * @return selected task or null
115      */

116     public UserTask getSelectedTask() {
117         TreePath JavaDoc tp = jTree.getSelectionPath();
118         if (tp == null)
119             return null;
120         return (UserTask) tp.getLastPathComponent();
121     }
122     
123     /**
124      * Sets another dependency type
125      *
126      * @param type one of Dependency.BEGIN_BEGIN and Dependency.END_BEGIN
127      */

128     public void setDependencyType(int type) {
129         if (type == Dependency.BEGIN_BEGIN)
130             jRadioButtonBeginBegin.setSelected(true);
131         else
132             jRadioButtonEndBegin.setSelected(true);
133     }
134
135     /**
136      * Returns selected dependency type
137      *
138      * @return one of Dependency.BEGIN_BEGIN and Dependency.END_BEGIN
139      */

140     public int getDependencyType() {
141         if (jRadioButtonBeginBegin.isSelected())
142             return Dependency.BEGIN_BEGIN;
143         else
144             return Dependency.END_BEGIN;
145     }
146     
147     /** This method is called from within the constructor to
148      * initialize the form.
149      * WARNING: Do NOT modify this code. The content of this method is
150      * always regenerated by the Form Editor.
151      */

152     private void initComponents() {//GEN-BEGIN:initComponents
153
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
154
155         buttonGroup = new javax.swing.ButtonGroup JavaDoc();
156         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
157         jTree = new javax.swing.JTree JavaDoc();
158         jRadioButtonEndBegin = new javax.swing.JRadioButton JavaDoc();
159         jRadioButtonBeginBegin = new javax.swing.JRadioButton JavaDoc();
160         jLabelError = new javax.swing.JLabel JavaDoc();
161
162         setLayout(new java.awt.GridBagLayout JavaDoc());
163
164         jTree.setRootVisible(false);
165         jTree.setShowsRootHandles(true);
166         jTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener JavaDoc() {
167             public void valueChanged(javax.swing.event.TreeSelectionEvent JavaDoc evt) {
168                 jTreeValueChanged(evt);
169             }
170         });
171
172         jScrollPane1.setViewportView(jTree);
173
174         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
175         gridBagConstraints.gridx = 0;
176         gridBagConstraints.gridy = 1;
177         gridBagConstraints.gridwidth = 3;
178         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
179         gridBagConstraints.weightx = 1.0;
180         gridBagConstraints.weighty = 1.0;
181         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 0);
182         add(jScrollPane1, gridBagConstraints);
183
184         buttonGroup.add(jRadioButtonEndBegin);
185         jRadioButtonEndBegin.setSelected(true);
186         org.openide.awt.Mnemonics.setLocalizedText(jRadioButtonEndBegin, org.openide.util.NbBundle.getBundle(DependencyPanel.class).getString("LBL_EndBegin"));
187         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
188         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 12);
189         add(jRadioButtonEndBegin, gridBagConstraints);
190
191         buttonGroup.add(jRadioButtonBeginBegin);
192         org.openide.awt.Mnemonics.setLocalizedText(jRadioButtonBeginBegin, org.openide.util.NbBundle.getBundle(DependencyPanel.class).getString("LBL_BeginBegin"));
193         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
194         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 0);
195         add(jRadioButtonBeginBegin, gridBagConstraints);
196
197         jLabelError.setMinimumSize(new java.awt.Dimension JavaDoc(0, 15));
198         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
199         gridBagConstraints.gridx = 0;
200         gridBagConstraints.gridy = 2;
201         gridBagConstraints.gridwidth = 3;
202         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
203         add(jLabelError, gridBagConstraints);
204
205     }//GEN-END:initComponents
206

207     private void jTreeValueChanged(javax.swing.event.TreeSelectionEvent JavaDoc evt) {//GEN-FIRST:event_jTreeValueChanged
208
String JavaDoc err = null;
209         
210         ResourceBundle JavaDoc rb = NbBundle.getBundle(DependencyPanel.class);
211         
212         TreePath JavaDoc tp = jTree.getSelectionPath();
213         if (tp == null || !(tp.getLastPathComponent() instanceof UserTask)) {
214             err = org.openide.util.NbBundle.getBundle(DependencyPanel.class).
215                     getString("NothingSelected"); // NOI18N
216
} else {
217             UserTask task = (UserTask) tp.getLastPathComponent();
218
219             if (task == ut)
220                 err = rb.getString("TaskCannotDependOnItself."); // NOI18N
221
else if (task == ut.getParent())
222                 err = rb.getString("TaskCannotDependOnParent"); // NOI18N
223
else if (task.isAncestorOf(ut))
224                 err = rb.getString("TaskCannotDependOnAncestor"); // NOI18N
225
else if (ut.isAncestorOf(task))
226                 err = rb.getString("TaskCannotDependOnChild"); // NOI18N
227

228             for (int i = 0; i < curDeps.size(); i++) {
229                 Dependency d = (Dependency) curDeps.get(i);
230                 if (d.getDependsOn() == task)
231                     err = rb.getString("DependencyAlreadyExists"); // NOI18N
232
else if (task.isAncestorOf(d.getDependsOn()))
233                     err = rb.getString("DependencyOnChildAlreadyExists"); // NOI18N
234
else if (d.getDependsOn().isAncestorOf(task))
235                     err = rb.getString("DependencyOnAncestorAlreadyExists"); // NOI18N
236

237                 if (err != null)
238                     break;
239             }
240         }
241         
242         jLabelError.setText(err == null ? "" : err); // NOI18N
243
if (dd != null)
244             dd.setValid(err == null);
245     }//GEN-LAST:event_jTreeValueChanged
246

247     
248     // Variables declaration - do not modify//GEN-BEGIN:variables
249
private javax.swing.ButtonGroup JavaDoc buttonGroup;
250     private javax.swing.JLabel JavaDoc jLabelError;
251     private javax.swing.JRadioButton JavaDoc jRadioButtonBeginBegin;
252     private javax.swing.JRadioButton JavaDoc jRadioButtonEndBegin;
253     private javax.swing.JScrollPane JavaDoc jScrollPane1;
254     private javax.swing.JTree JavaDoc jTree;
255     // End of variables declaration//GEN-END:variables
256

257 }
258
Popular Tags