KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > hints > options > AdvancedPanelImpl


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 package org.netbeans.modules.editor.hints.options;
20
21 import java.awt.Component JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.DefaultCellEditor JavaDoc;
25 import javax.swing.DefaultComboBoxModel JavaDoc;
26 import javax.swing.JComboBox JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import javax.swing.JList JavaDoc;
29 import javax.swing.JTable JavaDoc;
30 import javax.swing.ListCellRenderer JavaDoc;
31 import javax.swing.table.AbstractTableModel JavaDoc;
32 import javax.swing.table.TableCellRenderer JavaDoc;
33 import javax.swing.table.TableColumn JavaDoc;
34 import org.netbeans.spi.editor.hints.ProviderDescription;
35 import org.netbeans.spi.editor.hints.Severity;
36 import org.openide.awt.HtmlRenderer;
37
38 /**
39  *
40  * @author Jan Lahoda
41  */

42 public class AdvancedPanelImpl extends javax.swing.JPanel JavaDoc {
43     
44     private ModelImpl model;
45     
46     /** Creates new form AdvancedPanelImpl */
47     public AdvancedPanelImpl(ModelImpl model) {
48     this.model = model;
49         initComponents();
50         jTable1.setDefaultRenderer(Severity.class, new SeverityCellRenderer());
51         JComboBox JavaDoc editor = new JComboBox JavaDoc(new Object JavaDoc[] {
52             Severity.ERROR,
53             Severity.WARNING,
54             Severity.VERIFIER,
55             Severity.HINT,
56             Severity.TODO,
57             Severity.DISABLED,
58         });
59         editor.setRenderer(new SeverityComboRenderer());
60         jTable1.setDefaultEditor(Severity.class, new DefaultCellEditor JavaDoc(editor));
61         TableColumn JavaDoc c = jTable1.getColumnModel().getColumn(0);
62         c.setHeaderValue("XXXXXXXXX"); // NOI18N
63
c.setHeaderRenderer(new HeaderTableCellRenderer());
64         c.sizeWidthToFit();
65         updateForCurrentProvider();
66         eagerCombo.setSelectedIndex(2/*model.getEagerness()*/);
67     }
68     
69     /** This method is called from within the constructor to
70      * initialize the form.
71      * WARNING: Do NOT modify this code. The content of this method is
72      * always regenerated by the Form Editor.
73      */

74     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
75
private void initComponents() {
76
77         jComboBox1 = new javax.swing.JComboBox JavaDoc();
78         jCheckBox1 = new javax.swing.JCheckBox JavaDoc();
79         jTable1 = new javax.swing.JTable JavaDoc();
80         jLabel1 = new javax.swing.JLabel JavaDoc();
81         eagerCombo = new javax.swing.JComboBox JavaDoc();
82
83         jComboBox1.setModel(new ComboModelImpl());
84         jComboBox1.setSelectedIndex(jComboBox1.getModel().getSize() > 0 ? 0 : (-1));
85         jComboBox1.setRenderer(new RendererImpl());
86         jComboBox1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
87             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
88                 jComboBox1ActionPerformed(evt);
89             }
90         });
91
92         jCheckBox1.setText("Enabled");
93         jCheckBox1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
94         jCheckBox1.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
95         jCheckBox1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
96             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
97                 jCheckBox1ActionPerformed(evt);
98             }
99         });
100
101         jTable1.setModel(new OptionsTableModel());
102         jTable1.setRowSelectionAllowed(false);
103         jTable1.setShowHorizontalLines(false);
104         jTable1.setShowVerticalLines(false);
105
106         jLabel1.setText("Eagerness:");
107
108         eagerCombo.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(new String JavaDoc[] { "<html>On Project Open <i>requires restart</i>", "<html>On Demand", "<html>Lazy" }));
109         eagerCombo.addActionListener(new java.awt.event.ActionListener JavaDoc() {
110             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
111                 eagerComboActionPerformed(evt);
112             }
113         });
114
115         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
116         this.setLayout(layout);
117         layout.setHorizontalGroup(
118             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
119             .add(layout.createSequentialGroup()
120                 .addContainerGap()
121                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
122                     .add(jTable1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE)
123                     .add(jCheckBox1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE)
124                     .add(layout.createSequentialGroup()
125                         .add(jLabel1)
126                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
127                         .add(eagerCombo, 0, 375, Short.MAX_VALUE))
128                     .add(jComboBox1, 0, 454, Short.MAX_VALUE))
129                 .addContainerGap())
130         );
131         layout.setVerticalGroup(
132             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
133             .add(layout.createSequentialGroup()
134                 .addContainerGap()
135                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
136                     .add(jLabel1)
137                     .add(eagerCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
138                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
139                 .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
140                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
141                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
142                     .add(jTable1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
143                     .add(jCheckBox1))
144                 .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
145         );
146     }// </editor-fold>//GEN-END:initComponents
147

148     private void eagerComboActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_eagerComboActionPerformed
149
/*model.setEagerness(eagerCombo.getSelectedIndex());*/
150     }//GEN-LAST:event_eagerComboActionPerformed
151

152     private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
153
model.setProviderEnabled(getProviderDescription().getKey(), jCheckBox1.isSelected());
154         
155         int index = jComboBox1.getSelectedIndex();
156         
157         ((ComboModelImpl) jComboBox1.getModel()).fireContentsChanged(index, index);
158     }//GEN-LAST:event_jCheckBox1ActionPerformed
159

160     private void jComboBox1ActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
161
updateForCurrentProvider();
162     }//GEN-LAST:event_jComboBox1ActionPerformed
163

164     
165     // Variables declaration - do not modify//GEN-BEGIN:variables
166
public javax.swing.JComboBox JavaDoc eagerCombo;
167     public javax.swing.JCheckBox JavaDoc jCheckBox1;
168     public javax.swing.JComboBox JavaDoc jComboBox1;
169     public javax.swing.JLabel JavaDoc jLabel1;
170     public javax.swing.JTable JavaDoc jTable1;
171     // End of variables declaration//GEN-END:variables
172

173     private void updateForCurrentProvider() {
174         ProviderDescription desc = getProviderDescription();
175         
176         if (desc != null) {
177             jCheckBox1.setSelected(model.isProviderEnabled(desc.getKey()));
178             jCheckBox1.setEnabled(true);
179         } else {
180             jCheckBox1.setSelected(false);
181             jCheckBox1.setEnabled(false);
182         }
183         
184         ((OptionsTableModel) jTable1.getModel()).fireTableDataChanged();
185     }
186     
187     private ProviderDescription getProviderDescription() {
188         return (ProviderDescription) jComboBox1.getSelectedItem();
189     }
190     
191     private final class ComboModelImpl extends DefaultComboBoxModel JavaDoc {
192
193         public int getSize() {
194             return model.getDescriptions().size();
195         }
196
197         public Object JavaDoc getElementAt(int index) {
198             return model.getDescriptions().get(index);
199         }
200         
201         public void fireContentsChanged(int start, int end) {
202             super.fireContentsChanged(this, start, end);
203         }
204
205     }
206     
207     private static final class HeaderTableCellRenderer extends JLabel JavaDoc implements TableCellRenderer JavaDoc {
208         
209         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
210             setText((String JavaDoc) value);
211             return this;
212         }
213         
214     }
215     
216     private final class RendererImpl implements ListCellRenderer JavaDoc {
217     
218     private ListCellRenderer JavaDoc delegateTo;
219     
220     public RendererImpl() {
221         delegateTo = HtmlRenderer.createRenderer();
222     }
223     
224         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
225             String JavaDoc text;
226             
227             if (value == null) {
228                 text = "<none>";
229             } else {
230                 text = ((ProviderDescription) value).getDisplayName();
231                 
232                 if (!model.isProviderEnabled(((ProviderDescription) value).getKey())) {
233                     text = "<html><s>" + text;
234                 }
235             }
236             
237             return delegateTo.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus);
238         }
239     }
240     
241     private final class OptionsTableModel extends AbstractTableModel JavaDoc {
242         
243         public int getRowCount() {
244             ProviderDescription desc = getProviderDescription();
245             
246             if (desc == null)
247                 return 0;
248             
249             return desc.getSupportedErrorKeys().size();
250         }
251
252         public int getColumnCount() {
253             return 2;
254         }
255
256         public String JavaDoc getColumnName(int columnIndex) {
257             switch (columnIndex) {
258                 case 0:
259                     return "Severity";
260                 case 1:
261                     return "Name";
262                 default:
263                     throw new IllegalArgumentException JavaDoc("columnIndex=" + columnIndex);
264             }
265         }
266
267         public Class JavaDoc getColumnClass(int columnIndex) {
268             switch (columnIndex) {
269                 case 0:
270                     return Severity.class;
271                 case 1:
272                     return String JavaDoc.class;
273                 default:
274                     throw new IllegalArgumentException JavaDoc("columnIndex=" + columnIndex);
275             }
276         }
277
278         public boolean isCellEditable(int rowIndex, int columnIndex) {
279             switch (columnIndex) {
280                 case 0:
281                     return true;
282                 case 1:
283                     return false;
284                 default:
285                     throw new IllegalArgumentException JavaDoc("columnIndex=" + columnIndex);
286             }
287         }
288
289         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
290             ProviderDescription desc = getProviderDescription();
291             
292             switch (columnIndex) {
293                 case 0:
294                     return model.getErrorSeverity(desc.getKey(), (String JavaDoc) desc.getSupportedErrorKeys().get(rowIndex));
295                 case 1:
296                     return desc.getErrorDisplayName((String JavaDoc) desc.getSupportedErrorKeys().get(rowIndex));
297                 default:
298                     throw new IllegalArgumentException JavaDoc("columnIndex=" + columnIndex);
299             }
300         }
301
302         public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex) {
303             ProviderDescription desc = getProviderDescription();
304             
305             switch (columnIndex) {
306                 case 0:
307                     model.setSeverity(desc.getKey(), (String JavaDoc) desc.getSupportedErrorKeys().get(rowIndex), (Severity) aValue);
308                     return ;
309                 default:
310                     throw new IllegalArgumentException JavaDoc("columnIndex=" + columnIndex);
311             }
312         }
313
314     }
315     
316     private static final class SeverityCellRenderer implements TableCellRenderer JavaDoc {
317         
318     private TableCellRenderer JavaDoc delegateTo;
319     
320     public SeverityCellRenderer() {
321         delegateTo = HtmlRenderer.createRenderer();
322     }
323     
324         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
325             return delegateTo.getTableCellRendererComponent(table, SEVERITIES.get(((Severity) value).ordinal()), isSelected, hasFocus, row, column);
326         }
327     }
328
329     private static final String JavaDoc[] SEVERITIES_ARRAY = new String JavaDoc[] {
330         "<html><font color=\"#808080\">disabled",
331         "<html><font color=\"#800000\">error",
332         "<html><font color=\"#808000\">warning",
333         "<html><font color=\"#000080\">verifier",
334         "<html><font color=\"#000080\">hint",
335         "<html><font color=\"#000080\">todo",
336     };
337     
338     static final List JavaDoc SEVERITIES = Arrays.asList(SEVERITIES_ARRAY);
339     
340 }
341
Popular Tags