KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > docscan > TaskTagsPanel


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.docscan;
21
22 import org.netbeans.modules.tasklist.client.SuggestionPriority;
23 import org.netbeans.modules.tasklist.core.PriorityListCellRenderer;
24 import org.netbeans.modules.tasklist.core.editors.PriorityTableCellRenderer;
25 import org.openide.DialogDisplayer;
26 import org.openide.NotifyDescriptor;
27 import org.openide.awt.Mnemonics;
28 import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor;
29 import org.openide.util.NbBundle;
30
31 import javax.swing.*;
32 import javax.swing.event.ListSelectionListener JavaDoc;
33 import javax.swing.event.ListSelectionEvent JavaDoc;
34 import javax.swing.table.DefaultTableModel JavaDoc;
35 import javax.swing.table.TableColumn JavaDoc;
36 import java.awt.*;
37 import java.awt.event.ActionEvent JavaDoc;
38 import java.awt.event.ActionListener JavaDoc;
39
40 /**
41  * Customizer panel for the set of tags scanned from source.
42  * <p>
43  * Please read comment at the beginning of initA11y before editing
44  * this file using the form builder.
45  * <p>
46  *
47  * @author Tor Norbye
48  */

49 public final class TaskTagsPanel extends javax.swing.JPanel JavaDoc
50         implements EnhancedCustomPropertyEditor, ActionListener JavaDoc {
51
52     private static final long serialVersionUID = 1;
53
54     private DefaultTableModel JavaDoc model = null;
55
56     /** Creates new form TaskTagsPanel */
57     public TaskTagsPanel(TaskTags tags) {
58         initComponents();
59         initA11y();
60         setPreferredSize(new Dimension(400, 200));
61         this.tags = tags;
62
63         TaskTag[] tagy = tags.getTags();
64         model = new DefaultTableModel JavaDoc(new Object JavaDoc[0][0], new String JavaDoc[] {Util.getString("pat-col"), Util.getString("pri-col")}) {
65             Class JavaDoc[] types = new Class JavaDoc [] {
66                 String JavaDoc.class, SuggestionPriority.class
67             };
68
69             public Class JavaDoc getColumnClass(int columnIndex) {
70                 return types [columnIndex];
71             }
72
73             public boolean isCellEditable(int rowIndex, int columnIndex) {
74                 return columnIndex == 1;
75             }
76         };
77
78         for (int i = 0; i < tagy.length; i++) {
79             model.addRow(new Object JavaDoc[]{
80                 tagy[i].getToken(),
81                 tagy[i].getPriority()
82             });
83         }
84         patternsTable.setModel(model);
85
86         patternsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener JavaDoc() {
87             public void valueChanged(ListSelectionEvent JavaDoc e) {
88                 updateSensitivity();
89             }
90         });
91
92         // FIXME #41228 i18n
93
TableColumn JavaDoc sportColumn = patternsTable.getColumnModel().getColumn(1);
94         JComboBox combo = new JComboBox();
95         combo.addItem(SuggestionPriority.HIGH);
96         combo.addItem(SuggestionPriority.MEDIUM_HIGH);
97         combo.addItem(SuggestionPriority.MEDIUM);
98         combo.addItem(SuggestionPriority.MEDIUM_LOW);
99         combo.addItem(SuggestionPriority.LOW);
100         combo.setRenderer(new PriorityListCellRenderer());
101         sportColumn.setCellEditor(new DefaultCellEditor(combo));
102         sportColumn.setCellRenderer(new PriorityTableCellRenderer());
103
104         addButton.addActionListener(this);
105         changeButton.addActionListener(this);
106         deleteButton.addActionListener(this);
107
108
109 /*
110         ListCellRenderer priorityRenderer = new PriorityListCellRenderer();
111         ComboBoxModel prioritiesModel =
112         new DefaultComboBoxModel(Task.getPriorityNames());
113
114         prioCombo.setModel(prioritiesModel);
115         prioCombo.setRenderer(priorityRenderer);
116
117         tokenList.setCellRenderer(new TaskTagRenderer());
118         TaskTag[] t = tags.getTags();
119         model = new DefaultListModel();
120         for (int i = 0; i < t.length; i++) {
121             model.addElement(t[i]);
122         }
123         tokenList.setModel(model);
124
125
126         tokenList.addListSelectionListener(this);
127         tokenList.setSelectionInterval(0, 0);
128
129         updateSensitivity();
130         nameField.getDocument().addDocumentListener(this);
131         prioCombo.addActionListener(this);
132 */

133     }
134
135     private TaskTags tags = null;
136
137     /** This method is called from within the constructor to
138      * initialize the form.
139      * WARNING: Do NOT modify this code. The content of this method is
140      * always regenerated by the Form Editor.
141      */

142     private void initComponents() {//GEN-BEGIN:initComponents
143
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
144
145         tagLabel = new javax.swing.JLabel JavaDoc();
146         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
147         patternsTable = new javax.swing.JTable JavaDoc();
148         addButton = new javax.swing.JButton JavaDoc();
149         changeButton = new javax.swing.JButton JavaDoc();
150         deleteButton = new javax.swing.JButton JavaDoc();
151
152         setLayout(new java.awt.GridBagLayout JavaDoc());
153
154         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(12, 12, 11, 11)));
155         /*
156         tagLabel.setText("Tag List:");
157         */

158         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
159         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
160         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
161         add(tagLabel, gridBagConstraints);
162
163         jScrollPane1.setViewportView(patternsTable);
164
165         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
166         gridBagConstraints.gridheight = 4;
167         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
168         gridBagConstraints.weightx = 1.0;
169         gridBagConstraints.weighty = 1.0;
170         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 0);
171         add(jScrollPane1, gridBagConstraints);
172
173         /*
174         addButton.setText("Add");
175         */

176         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
177         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
178         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
179         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
180         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 0);
181         add(addButton, gridBagConstraints);
182
183         /*
184         changeButton.setText("Change");
185         */

186         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
187         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
188         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
189         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
190         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 0);
191         add(changeButton, gridBagConstraints);
192
193         /*
194         deleteButton.setText("Delete");
195         */

196         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
197         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
198         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
199         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
200         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 0);
201         add(deleteButton, gridBagConstraints);
202
203     }//GEN-END:initComponents
204

205     /** Initialize accessibility settings on the panel */
206     private void initA11y() {
207         /*
208           I couldn't figure out how to use Mnemonics.setLocalizedText
209           to set labels and checkboxes with a mnemonic using the
210           form builder, so the closest I got was to use "/*" and "* /
211           as code pre-init/post-init blocks, such that I don't actually
212           execute the bundle lookup code - and then call it explicitly
213           below. (I wanted to keep the text on the components so that
214           I can see them when visually editing the GUI.
215         */

216
217         Mnemonics.setLocalizedText(addButton, NbBundle.getMessage(
218                 TaskTagsPanel.class, "AddTag")); // NOI18N
219
addButton.setToolTipText (NbBundle.getMessage (
220             TaskTagsPanel.class,
221             "HINT_AddTag"
222         ));
223         Mnemonics.setLocalizedText(changeButton, NbBundle.getMessage(
224                 TaskTagsPanel.class, "ChangeTag")); // NOI18N
225
changeButton.setToolTipText (NbBundle.getMessage (
226             TaskTagsPanel.class,
227             "HINT_ChangeTag"
228         ));
229         Mnemonics.setLocalizedText(deleteButton, NbBundle.getMessage(
230                 TaskTagsPanel.class, "DeleteTag")); // NOI18N
231
deleteButton.setToolTipText (NbBundle.getMessage (
232             TaskTagsPanel.class,
233             "HINT_DeleteTag"
234         ));
235         Mnemonics.setLocalizedText(tagLabel, NbBundle.getMessage(
236                 TaskTagsPanel.class, "TagList")); // NOI18N
237
tagLabel.setToolTipText (NbBundle.getMessage (
238             TaskTagsPanel.class,
239             "HINT_TagList"
240         ));
241         tagLabel.setLabelFor(patternsTable);
242
243         patternsTable.setToolTipText (NbBundle.getMessage (
244             TaskTagsPanel.class,
245             "HINT_TagList"
246         ));
247
248         this.getAccessibleContext().setAccessibleDescription(
249                 NbBundle.getMessage(TaskTagsPanel.class, "ACSD_Tags")); // NOI18N
250
patternsTable.getAccessibleContext().setAccessibleDescription(
251                 NbBundle.getMessage(TaskTagsPanel.class, "ACSD_List")); // NOI18N
252
}
253
254     // Variables declaration - do not modify//GEN-BEGIN:variables
255
private javax.swing.JButton JavaDoc addButton;
256     private javax.swing.JButton JavaDoc changeButton;
257     private javax.swing.JButton JavaDoc deleteButton;
258     private javax.swing.JScrollPane JavaDoc jScrollPane1;
259     private javax.swing.JTable JavaDoc patternsTable;
260     private javax.swing.JLabel JavaDoc tagLabel;
261     // End of variables declaration//GEN-END:variables
262

263
264     // When used as a property customizer
265
public Object JavaDoc getPropertyValue() throws IllegalStateException JavaDoc {
266         return getEditedTags();
267     }
268
269     private TaskTags getEditedTags() {
270         TaskTag[] ts = new TaskTag[model.getRowCount()];
271         for (int i = 0; i < model.getRowCount(); i++) {
272             String JavaDoc token = (String JavaDoc) model.getValueAt(i, 0);
273             SuggestionPriority prio = (SuggestionPriority) model.getValueAt(i,1);
274             TaskTag tag = new TaskTag(token, prio);
275             ts[i] = tag;
276         }
277         tags = new TaskTags();
278         tags.setTags(ts);
279         return tags;
280     }
281
282     private void updateSensitivity() {
283         int[] selected = patternsTable.getSelectedRows();
284         int count = (selected != null) ? selected.length : 0;
285         deleteButton.setEnabled(count == 1);
286         changeButton.setEnabled (count == 1);
287         addButton.setEnabled(true);
288     }
289
290     public void actionPerformed(ActionEvent JavaDoc actionEvent) {
291         Object JavaDoc source = actionEvent.getSource();
292         if (source == addButton) {
293             NotifyDescriptor.InputLine d = new NotifyDescriptor.InputLine(Util.getString("pat-col"), Util.getString("new-pat"));
294             DialogDisplayer.getDefault().notify(d);
295             if (d.getValue() == NotifyDescriptor.OK_OPTION) {
296                 String JavaDoc text = d.getInputText();
297                 if (text.length() > 0) {
298                     model.addRow(new Object JavaDoc[] {text, SuggestionPriority.MEDIUM});
299                 }
300             }
301         } else if (source == changeButton) {
302             int row = patternsTable.getSelectedRow();
303             if (row == -1) {
304                 if (patternsTable.getRowCount() > 0) {
305                     patternsTable.getSelectionModel().setSelectionInterval(0,0);
306                     row = patternsTable.getSelectedRow();
307                 } else {
308                     updateSensitivity();
309                     return;
310                 }
311             }
312             String JavaDoc pattern = (String JavaDoc) model.getValueAt(row, 0);
313             NotifyDescriptor.InputLine d = new NotifyDescriptor.InputLine(Util.getString("pat-col"), Util.getString("edit-pat"));
314             d.setInputText(pattern);
315             DialogDisplayer.getDefault().notify(d);
316             if (d.getValue() == NotifyDescriptor.OK_OPTION) {
317                 String JavaDoc text = d.getInputText();
318                 if (text.length() > 0) {
319                     model.setValueAt(text, row, 0);
320                 }
321             }
322         } else if (source == deleteButton) {
323             int row = patternsTable.getSelectedRow();
324             if (row == -1) {
325                 updateSensitivity();
326             } else {
327                 model.removeRow(row);
328             }
329         }
330         updateSensitivity();
331     }
332
333 }
334
Popular Tags