KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > suggestions > TypesCustomizer


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.suggestions;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30 import java.util.Vector JavaDoc;
31 import javax.swing.AbstractListModel JavaDoc;
32 import javax.swing.event.ListSelectionListener JavaDoc;
33 import javax.swing.ListModel JavaDoc;
34 import javax.swing.DefaultListModel JavaDoc;
35 import javax.swing.JCheckBox JavaDoc;
36 import javax.swing.JComboBox JavaDoc;
37 import javax.swing.JList JavaDoc;
38 import javax.swing.JTable JavaDoc;
39 import javax.swing.JTextField JavaDoc;
40 import javax.swing.ListCellRenderer JavaDoc;
41 import javax.swing.ListSelectionModel JavaDoc;
42 import javax.swing.UIManager JavaDoc;
43 import javax.swing.border.Border JavaDoc;
44 import javax.swing.border.EmptyBorder JavaDoc;
45 import javax.swing.table.AbstractTableModel JavaDoc;
46 import javax.swing.table.TableColumn JavaDoc;
47 import javax.swing.table.TableColumnModel JavaDoc;
48 import org.netbeans.modules.tasklist.client.SuggestionManager;
49 import org.openide.util.NbBundle;
50 import org.openide.awt.Mnemonics;
51
52 /**
53  * Panel used to customize which types are active, which are disabled,
54  * and which of the active ones get confirmations.
55  *
56  * <p>
57  * @todo Also consider adding a default priority assigned to suggestions
58  * of this type (if it's interesting to the user to see it, or edit it)
59  * <p>
60  *
61  * @author Tor Norbye
62  * @author Tim Lebedkov
63  */

64 public final class TypesCustomizer extends javax.swing.JPanel JavaDoc
65     implements ListSelectionListener JavaDoc {
66
67     private static final long serialVersionUID = 1;
68
69     /**
70      * Creates new form TypesCustomizer
71      */

72     public TypesCustomizer() {
73         initComponents();
74         initA11y();
75         
76         typesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
77         typesTable.setModel(new TypesCustomizer.SuggTypesTableModel());
78         TableColumnModel JavaDoc cm = typesTable.getColumnModel();
79         cm.getColumn(0).setPreferredWidth(50);
80         cm.getColumn(1).setPreferredWidth(50);
81         cm.getColumn(2).setPreferredWidth(300);
82         typesTable.getSelectionModel().addListSelectionListener(this);
83         typesTable.getSelectionModel().setSelectionInterval(0, 0);
84     }
85
86     /**
87      * This method is called from within the constructor to
88      * initialize the form.
89      * WARNING: Do NOT modify this code. The content of this method is
90      * always regenerated by the Form Editor.
91      */

92     private void initComponents() {//GEN-BEGIN:initComponents
93
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
94
95         activeLabel = new javax.swing.JLabel JavaDoc();
96         typeDescLabel = new javax.swing.JLabel JavaDoc();
97         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
98         descTextArea = new javax.swing.JTextArea JavaDoc();
99         jScrollPane3 = new javax.swing.JScrollPane JavaDoc();
100         typesTable = new javax.swing.JTable JavaDoc();
101
102         setLayout(new java.awt.GridBagLayout JavaDoc());
103
104         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(12, 12, 11, 11)));
105         activeLabel.setLabelFor(typesTable);
106         /*
107         activeLabel.setText(NbBundle.getMessage(TypesCustomizer.class, "ActiveTypes")); // NOI18N();
108         */

109         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
110         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
111         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
112         add(activeLabel, gridBagConstraints);
113
114         typeDescLabel.setLabelFor(descTextArea);
115         /*
116         typeDescLabel.setText(NbBundle.getMessage(TypesCustomizer.class, "TypeDesc")); // NOI18N();
117         */

118         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
119         gridBagConstraints.gridx = 0;
120         gridBagConstraints.gridy = 3;
121         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 2, 0);
122         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
123         add(typeDescLabel, gridBagConstraints);
124
125         descTextArea.setEditable(false);
126         descTextArea.setLineWrap(true);
127         descTextArea.setWrapStyleWord(true);
128         descTextArea.setPreferredSize(new java.awt.Dimension JavaDoc(400, 100));
129         jScrollPane1.setViewportView(descTextArea);
130
131         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
132         gridBagConstraints.gridx = 0;
133         gridBagConstraints.gridy = 4;
134         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
135         gridBagConstraints.weightx = 1.0;
136         gridBagConstraints.weighty = 1.0;
137         add(jScrollPane1, gridBagConstraints);
138
139         jScrollPane3.setViewportView(typesTable);
140
141         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
142         gridBagConstraints.gridx = 0;
143         gridBagConstraints.gridy = 1;
144         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
145         gridBagConstraints.weighty = 1.0;
146         add(jScrollPane3, gridBagConstraints);
147
148     }//GEN-END:initComponents
149

150     
151     /** Initialize accessibility settings on the panel */
152     private void initA11y() {
153         /*
154           I couldn't figure out how to use Mnemonics.setLocalizedText
155           to set labels and checkboxes with a mnemonic using the
156           form builder, so the closest I got was to use "/*" and "* /
157           as code pre-init/post-init blocks, such that I don't actually
158           execute the bundle lookup code - and then call it explicitly
159           below. (I wanted to keep the text on the components so that
160           I can see them when visually editing the GUI.
161         */

162
163         Mnemonics.setLocalizedText(activeLabel,
164           NbBundle.getMessage(TypesCustomizer.class, "ActiveTypes")); // NOI18N
165
Mnemonics.setLocalizedText(typeDescLabel,
166           NbBundle.getMessage(TypesCustomizer.class, "TypeDesc")); // NOI18N
167

168         this.getAccessibleContext().setAccessibleDescription(
169                 NbBundle.getMessage(TypesCustomizer.class, "ACSD_TypesCustomizer")); // NOI18N
170
typesTable.getAccessibleContext().setAccessibleDescription(
171                 NbBundle.getMessage(TypesCustomizer.class, "ACSD_Enabled")); // NOI18N
172
descTextArea.getAccessibleContext().setAccessibleDescription(
173                 NbBundle.getMessage(TypesCustomizer.class, "ACSD_TypeDesc")); // NOI18N
174
}
175     
176     
177     // Variables declaration - do not modify//GEN-BEGIN:variables
178
private javax.swing.JTable JavaDoc typesTable;
179     private javax.swing.JScrollPane JavaDoc jScrollPane1;
180     private javax.swing.JTextArea JavaDoc descTextArea;
181     private javax.swing.JLabel JavaDoc activeLabel;
182     private javax.swing.JLabel JavaDoc typeDescLabel;
183     private javax.swing.JScrollPane JavaDoc jScrollPane3;
184     // End of variables declaration//GEN-END:variables
185

186     /**
187      * Apply changes in the dialog
188      */

189     public void apply() {
190         SuggTypesTableModel model = (SuggTypesTableModel) typesTable.getModel();
191         model.save();
192     }
193    
194     public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc event) {
195         int selected = typesTable.getSelectedRow();
196         if (selected < 0) {
197             descTextArea.setText("");
198         } else {
199             SuggTypesTableModel m = (SuggTypesTableModel) typesTable.getModel();
200             descTextArea.setText(m.getType(selected).getDescription());
201         }
202     }
203     
204     /**
205      * TableModel for SuggestionTypes
206      */

207     private static class SuggTypesTableModel extends AbstractTableModel JavaDoc {
208
209         private static final long serialVersionUID = 1;
210
211         private static String JavaDoc[] columnNames;
212         
213         static {
214             ResourceBundle JavaDoc rb = NbBundle.getBundle(SuggTypesTableModel.class);
215             columnNames = new String JavaDoc[] {
216                 rb.getString("Active"),
217                 rb.getString("Confirmation"),
218                 rb.getString("Name")
219             };
220         }
221         
222         private SuggestionType[] types;
223         private boolean[] enabled;
224         private boolean[] confirm;
225         
226         /**
227          * Constructor. Creates TableModel for all registered SuggestionTypes.
228          */

229         public SuggTypesTableModel() {
230             Collection JavaDoc cl = SuggestionTypes.getDefault().getAllTypes();
231             Iterator JavaDoc it = cl.iterator();
232             SuggestionManagerImpl manager =
233                 (SuggestionManagerImpl) SuggestionManager.getDefault();
234             ArrayList JavaDoc types = new ArrayList JavaDoc();
235             while (it.hasNext()) {
236                 types.add(it.next());
237             }
238             
239             
240             this.types = (SuggestionType[]) types.toArray(
241                 new SuggestionType[types.size()]);
242             this.enabled = new boolean[types.size()];
243             this.confirm = new boolean[types.size()];
244             for (int i = 0; i < types.size(); i++) {
245                 SuggestionType type = (SuggestionType) types.get(i);
246                 enabled[i] = manager.isEnabled(type.getName());
247                 confirm[i] = manager.isConfirm(type);
248             }
249         }
250
251         /**
252          * Saves options.
253          */

254         public void save() {
255             SuggestionManagerImpl manager =
256                 (SuggestionManagerImpl) SuggestionManager.getDefault();
257             for (int i = 0; i < types.length; i++) {
258                 manager.setEnabled(types[i].getName(), enabled[i], true);
259                 manager.setConfirm(types[i], confirm[i],
260                     i != types.length - 1);
261             }
262         }
263
264         /**
265          * Returns suggestion type for the specified row.
266          * @param index row number
267          * @return suggestion type
268          */

269         public SuggestionType getType(int index) {
270             return types[index];
271         }
272         
273         /**
274          * Returns "confirm before fix" property for the specified row.
275          * @param index row number
276          * @return true = confirm
277          */

278         public boolean getConfirmation(int index) {
279             return confirm[index];
280         }
281
282         /**
283          * Sets "confirm before fix" property for the specified row.
284          * @param index row number
285          * @param c true = confirm
286          */

287         public void setConfirmation(int index, boolean c) {
288             confirm[index] = c;
289             fireTableCellUpdated(index, 1);
290         }
291         
292         public int getRowCount() {
293             return types.length;
294         }
295
296         public int getColumnCount() {
297             return 3;
298         }
299
300         public String JavaDoc getColumnName(int columnIndex) {
301             return columnNames[columnIndex];
302         }
303
304         public Class JavaDoc getColumnClass(int columnIndex) {
305             if (columnIndex == 2)
306                 return String JavaDoc.class;
307             else
308                 return Boolean JavaDoc.class;
309         }
310
311         public boolean isCellEditable(int rowIndex, int columnIndex) {
312             return columnIndex != 2;
313         }
314
315         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
316             switch (columnIndex) {
317                 case 0:
318                     return enabled[rowIndex] ? Boolean.TRUE : Boolean.FALSE;
319                 case 1:
320                     return confirm[rowIndex] ? Boolean.TRUE : Boolean.FALSE;
321                 case 2:
322                     return types[rowIndex].getLocalizedName();
323             }
324             return null;
325         }
326
327         public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex) {
328             switch (columnIndex) {
329                 case 0:
330                     enabled[rowIndex] = ((Boolean JavaDoc) aValue).booleanValue();
331                     fireTableCellUpdated(rowIndex, columnIndex);
332                     break;
333                 case 1:
334                     confirm[rowIndex] = ((Boolean JavaDoc) aValue).booleanValue();
335                     fireTableCellUpdated(rowIndex, columnIndex);
336                     break;
337             }
338         }
339     }
340 }
341
Popular Tags