KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.netbeans.modules.tasklist.usertasks.dependencies;
2
3 import java.awt.Dialog JavaDoc;
4 import java.awt.Dimension JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import javax.swing.DefaultListModel JavaDoc;
9 import javax.swing.border.EmptyBorder JavaDoc;
10 import javax.swing.event.ListSelectionEvent JavaDoc;
11 import javax.swing.event.ListSelectionListener JavaDoc;
12 import org.netbeans.modules.tasklist.usertasks.model.UserTask;
13 import org.openide.DialogDescriptor;
14 import org.openide.DialogDisplayer;
15 import org.openide.util.NbBundle;
16 import org.openide.util.Utilities;
17 import org.netbeans.modules.tasklist.usertasks.model.Dependency;
18
19 /**
20  * Dependencies for a task.
21  *
22  * @author tl
23  */

24 public class DependenciesPanel extends javax.swing.JPanel JavaDoc {
25     private static final String JavaDoc BEGIN_BEGIN_MSG =
26         NbBundle.getMessage(DependenciesPanel.class, "BeginBeginDep"); // NOI18N
27
private static final String JavaDoc END_BEGIN_MSG =
28         NbBundle.getMessage(DependenciesPanel.class, "EndBeginDep"); // NOI18N
29

30     private UserTask ut;
31     private List JavaDoc<Dependency> dependencies = new ArrayList JavaDoc<Dependency>();
32     
33     /**
34      * Creates a panel
35      */

36     public DependenciesPanel() {
37         initComponents();
38         jList.getSelectionModel().addListSelectionListener(
39             new ListSelectionListener JavaDoc() {
40                 public void valueChanged(ListSelectionEvent JavaDoc e) {
41                     jButtonRemove.setEnabled(jList.getSelectedIndex() >= 0);
42                 }
43             }
44         );
45     }
46     
47     /**
48      * Fills the panel with the data.
49      *
50      * @param ut a user task
51      */

52     public void fillPanel(UserTask ut) {
53         this.ut = ut;
54         DefaultListModel JavaDoc dlm = new DefaultListModel JavaDoc();
55         Iterator JavaDoc it = ut.getDependencies().iterator();
56         while (it.hasNext()) {
57             Dependency d = (Dependency) it.next();
58             dependencies.add(d);
59             dlm.addElement(createTaskDescription(d.getDependsOn(), d.getType()));
60         }
61         jList.setModel(dlm);
62         if (jList.getModel().getSize() > 0)
63             jList.setSelectedIndex(0);
64     }
65     
66     /**
67      * Creates a description for a task.
68      *
69      * @param ut a task
70      * @param type dependency type. Dependency.BEGIN_BEGIN or END_BEGIN
71      * @return path to the task. e.g. "A/B/C"
72      */

73     private String JavaDoc createTaskDescription(UserTask ut, int type) {
74         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
75         while (ut != null) {
76             if (sb.length() != 0)
77                 sb.insert(0, '/');
78             sb.insert(0, ut.getSummary());
79             ut = ut.getParent();
80         }
81         sb.append(" - "); // NOI18N
82
if (type == Dependency.BEGIN_BEGIN)
83             sb.append(BEGIN_BEGIN_MSG);
84         else
85             sb.append(END_BEGIN_MSG);
86         return sb.toString();
87     }
88     
89     /**
90      * Fills in the fillPanel specified user task with the data from this panel
91      */

92     public void fillObject() {
93         this.ut.getDependencies().clear();
94         this.ut.getDependencies().addAll(dependencies);
95     }
96     
97     /** This method is called from within the constructor to
98      * initialize the form.
99      * WARNING: Do NOT modify this code. The content of this method is
100      * always regenerated by the Form Editor.
101      */

102     private void initComponents() {//GEN-BEGIN:initComponents
103
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
104
105         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
106         jList = new javax.swing.JList JavaDoc();
107         jButtonRemove = new javax.swing.JButton JavaDoc();
108         jButtonAdd = new javax.swing.JButton JavaDoc();
109
110         setLayout(new java.awt.GridBagLayout JavaDoc());
111
112         jScrollPane1.setViewportView(jList);
113
114         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
115         gridBagConstraints.gridx = 0;
116         gridBagConstraints.gridy = 0;
117         gridBagConstraints.gridheight = 2;
118         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
119         gridBagConstraints.weightx = 1.0;
120         gridBagConstraints.weighty = 1.0;
121         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
122         add(jScrollPane1, gridBagConstraints);
123
124         org.openide.awt.Mnemonics.setLocalizedText(jButtonRemove, org.openide.util.NbBundle.getBundle(DependenciesPanel.class).getString("LBL_RemoveDependency"));
125         jButtonRemove.setEnabled(false);
126         jButtonRemove.addActionListener(new java.awt.event.ActionListener JavaDoc() {
127             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
128                 jButtonRemoveActionPerformed(evt);
129             }
130         });
131
132         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
133         gridBagConstraints.gridx = 1;
134         gridBagConstraints.gridy = 1;
135         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
136         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
137         add(jButtonRemove, gridBagConstraints);
138
139         org.openide.awt.Mnemonics.setLocalizedText(jButtonAdd, org.openide.util.NbBundle.getBundle(DependenciesPanel.class).getString("LBL_AddDependency"));
140         jButtonAdd.addActionListener(new java.awt.event.ActionListener JavaDoc() {
141             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
142                 jButtonAddActionPerformed(evt);
143             }
144         });
145
146         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
147         gridBagConstraints.gridx = 1;
148         gridBagConstraints.gridy = 0;
149         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
150         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
151         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 0);
152         add(jButtonAdd, gridBagConstraints);
153
154     }//GEN-END:initComponents
155

156     private void jButtonRemoveActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonRemoveActionPerformed
157
DialogDescriptor.Confirmation conf = new DialogDescriptor.Confirmation(
158             NbBundle.getMessage(DependenciesPanel.class, "DeleteDepConf"), // NOI18N
159
DialogDescriptor.YES_NO_OPTION, DialogDescriptor.QUESTION_MESSAGE
160         );
161         Object JavaDoc ret = DialogDisplayer.getDefault().notify(conf);
162         if (ret == DialogDescriptor.YES_OPTION) {
163             int index = jList.getSelectedIndex();
164             ((DefaultListModel JavaDoc) jList.getModel()).remove(index);
165             dependencies.remove(index);
166             if (jList.getModel().getSize() > index)
167                 jList.setSelectedIndex(index);
168             else if (jList.getModel().getSize() > index - 1)
169                 jList.setSelectedIndex(index - 1);
170         }
171     }//GEN-LAST:event_jButtonRemoveActionPerformed
172

173     private void jButtonAddActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonAddActionPerformed
174
DependencyPanel dp = new DependencyPanel(ut, dependencies);
175         dp.setBorder(new EmptyBorder JavaDoc(11, 11, 12, 12));
176         DialogDescriptor dd = new DialogDescriptor(dp,
177             NbBundle.getMessage(
178                 DependenciesPanel.class, "AddDependency")); // NOI18N
179
dp.setDialogDescriptor(dd);
180         Dialog JavaDoc d = DialogDisplayer.getDefault().createDialog(dd);
181         d.setBounds(Utilities.findCenterBounds(new Dimension JavaDoc(400, 400)));
182         d.setVisible(true);
183         if (dd.getValue() == DialogDescriptor.OK_OPTION) {
184             UserTask sel = dp.getSelectedTask();
185             int type = dp.getDependencyType();
186             dependencies.add(new Dependency(sel, type));
187             ((DefaultListModel JavaDoc) jList.getModel()).addElement(
188                 createTaskDescription(sel, type));
189             jList.setSelectedIndex(jList.getModel().getSize() - 1);
190         }
191     }//GEN-LAST:event_jButtonAddActionPerformed
192

193     
194     // Variables declaration - do not modify//GEN-BEGIN:variables
195
private javax.swing.JButton JavaDoc jButtonAdd;
196     private javax.swing.JButton JavaDoc jButtonRemove;
197     private javax.swing.JList JavaDoc jList;
198     private javax.swing.JScrollPane JavaDoc jScrollPane1;
199     // End of variables declaration//GEN-END:variables
200

201 }
202
Popular Tags