KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ui > wizards > PanelSupportedFrameworksVisual


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.web.project.ui.wizards;
21
22 import java.awt.Component JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.swing.DefaultListModel JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30 import javax.swing.JTable JavaDoc;
31 import javax.swing.ListSelectionModel JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33 import javax.swing.event.ListSelectionListener JavaDoc;
34 import javax.swing.event.TableModelEvent JavaDoc;
35 import javax.swing.event.TableModelListener JavaDoc;
36 import javax.swing.table.AbstractTableModel JavaDoc;
37 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
38 import javax.swing.table.TableCellRenderer JavaDoc;
39
40 import org.openide.WizardDescriptor;
41 import org.openide.WizardValidationException;
42 import org.openide.util.HelpCtx;
43 import org.openide.util.NbBundle;
44
45 import org.netbeans.modules.web.api.webmodule.WebFrameworkSupport;
46 import org.netbeans.modules.web.project.WebProject;
47 import org.netbeans.modules.web.spi.webmodule.FrameworkConfigurationPanel;
48 import org.netbeans.modules.web.spi.webmodule.WebFrameworkProvider;
49
50 public class PanelSupportedFrameworksVisual extends JPanel JavaDoc implements HelpCtx.Provider, TableModelListener JavaDoc, ListSelectionListener JavaDoc, ChangeListener JavaDoc {
51     
52     /** All available web extensions */
53     public static final int ALL_FRAMEWORKS = 0;
54     
55     /** Web extensions used in the project */
56     public static final int USED_FRAMEWORKS = 1;
57     
58     /** Web extensions which are not used in the project */
59     public static final int UNUSED_FRAMEWORKS = 2;
60     
61     private List JavaDoc ignoredFrameworks;
62     private HashMap JavaDoc configPanels = new HashMap JavaDoc();
63     private FrameworksTableModel model;
64     private PanelSupportedFrameworks panel;
65     private WizardDescriptor wizardDescriptor;
66     
67     /** Creates new form PanelInitProject
68      * @param project the web project; if it is null, all available web extensions will be shown
69      * @param filter one of the options <code>ALL_FRAMEWORKS</code>, <code>USED_FRAMEWORKS</code>, <code>UNUSED_FRAMEWORKS</code>
70      * @param ignoredFrameworks the list of frameworks to be ignored when creating list; null is allowed
71      */

72     public PanelSupportedFrameworksVisual(PanelSupportedFrameworks panel, WebProject project, int filter, List JavaDoc ignoredFrameworks) {
73         this.panel = panel;
74         this.ignoredFrameworks = ignoredFrameworks;
75         initComponents();
76
77         model = new FrameworksTableModel();
78         jTableFrameworks.setModel(model);
79         createFrameworksList(project, filter);
80
81         FrameworksTableCellRenderer renderer = new FrameworksTableCellRenderer();
82         renderer.setBooleanRenderer(jTableFrameworks.getDefaultRenderer(Boolean JavaDoc.class));
83         jTableFrameworks.setDefaultRenderer(WebFrameworkProvider.class, renderer);
84         jTableFrameworks.setDefaultRenderer(Boolean JavaDoc.class, renderer);
85         initTableVisualProperties(jTableFrameworks);
86
87         this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PanelSupportedFrameworksVisual.class, "ACS_NWP2_Frameworks_A11YDesc")); // NOI18N
88

89         // Provide a name in the title bar.
90
setName(NbBundle.getMessage(PanelSupportedFrameworksVisual.class, "LBL_NWP2_Frameworks")); //NOI18N
91
putClientProperty ("NewProjectWizard_Title", NbBundle.getMessage(PanelSupportedFrameworksVisual.class, "TXT_NewWebApp")); //NOI18N
92
}
93
94     private void initTableVisualProperties(JTable JavaDoc table) {
95         table.getModel().addTableModelListener(this);
96         
97         table.setRowSelectionAllowed(true);
98         table.getSelectionModel().addListSelectionListener(this);
99         table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
100         
101         table.setTableHeader(null);
102         
103         table.setRowHeight(jTableFrameworks.getRowHeight() + 4);
104         table.setIntercellSpacing(new java.awt.Dimension JavaDoc(0, 0));
105         // set the color of the table's JViewport
106
table.getParent().setBackground(table.getBackground());
107         table.setShowHorizontalLines(false);
108         table.setShowVerticalLines(false);
109         
110         table.getColumnModel().getColumn(0).setMaxWidth(30);
111     }
112
113     private void createFrameworksList(WebProject project, int filter) {
114         List JavaDoc frameworks = WebFrameworkSupport.getFrameworkProviders();
115         configPanels.clear();
116         
117         if (project == null || filter == ALL_FRAMEWORKS) {
118             for (int i = 0; i < frameworks.size(); i++) {
119                 addFrameworkToModel((WebFrameworkProvider) frameworks.get(i));
120                 configPanels.put((WebFrameworkProvider) frameworks.get(i), ((WebFrameworkProvider) frameworks.get(i)).getConfigurationPanel(null));
121             }
122         } else if (filter == USED_FRAMEWORKS) {
123             for (int i = 0; i < frameworks.size(); i++) {
124                 WebFrameworkProvider framework = (WebFrameworkProvider) frameworks.get(i);
125                 if (framework.isInWebModule(project.getAPIWebModule())) {
126                     addFrameworkToModel(framework);
127                     configPanels.put(framework, framework.getConfigurationPanel(null));
128                 }
129             }
130         } else if (filter == UNUSED_FRAMEWORKS) {
131             for (int i = 0; i < frameworks.size(); i++) {
132                 WebFrameworkProvider framework = (WebFrameworkProvider) frameworks.get(i);
133                 if (!framework.isInWebModule(project.getAPIWebModule())) {
134                     addFrameworkToModel(framework);
135                     configPanels.put(framework, framework.getConfigurationPanel(null));
136                 }
137             }
138         }
139         
140         for (int i = 0; i < model.getRowCount(); i++) {
141             FrameworkModelItem item = model.getItem(i);
142             FrameworkConfigurationPanel configPanel = (FrameworkConfigurationPanel) configPanels.get(item.getFramework());
143             if (configPanel != null)
144                 configPanel.addChangeListener(this);
145         }
146     }
147     
148     private void addFrameworkToModel(WebFrameworkProvider framework) {
149         FrameworksTableModel model = (FrameworksTableModel) jTableFrameworks.getModel();
150         if (ignoredFrameworks == null || !ignoredFrameworks.contains(framework))
151             model.addItem(new FrameworkModelItem(framework));
152     }
153     
154     /** This method is called from within the constructor to
155      * initialize the form.
156      * WARNING: Do NOT modify this code. The content of this method is
157      * always regenerated by the Form Editor.
158      */

159     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
160
private void initComponents() {
161         jLabel1 = new javax.swing.JLabel JavaDoc();
162         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
163         jTableFrameworks = new javax.swing.JTable JavaDoc();
164         jSeparator1 = new javax.swing.JSeparator JavaDoc();
165         jLabelConfig = new javax.swing.JLabel JavaDoc();
166         jPanelConfig = new javax.swing.JPanel JavaDoc();
167
168         setPreferredSize(new java.awt.Dimension JavaDoc(500, 340));
169         setRequestFocusEnabled(false);
170         jLabel1.setDisplayedMnemonic(org.openide.util.NbBundle.getMessage(PanelSupportedFrameworksVisual.class, "MNE_Frameworks").charAt(0));
171         jLabel1.setLabelFor(jTableFrameworks);
172         jLabel1.setText(org.openide.util.NbBundle.getMessage(PanelSupportedFrameworksVisual.class, "LBL_NWP2_Select_Frameworks"));
173         jLabel1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelSupportedFrameworksVisual.class, "ACS_LBL_NWP2_FrameworksTable_A11YDesc"));
174
175         jScrollPane1.setMaximumSize(new java.awt.Dimension JavaDoc(32767, 70));
176         jScrollPane1.setMinimumSize(new java.awt.Dimension JavaDoc(22, 70));
177         jScrollPane1.setPreferredSize(new java.awt.Dimension JavaDoc(453, 70));
178         jTableFrameworks.setModel(new javax.swing.table.DefaultTableModel JavaDoc(
179             new Object JavaDoc [][] {
180                 {null, null, null, null},
181                 {null, null, null, null},
182                 {null, null, null, null},
183                 {null, null, null, null}
184             },
185             new String JavaDoc [] {
186                 "Title 1", "Title 2", "Title 3", "Title 4"
187             }
188         ));
189         jScrollPane1.setViewportView(jTableFrameworks);
190
191         jLabelConfig.setLabelFor(jPanelConfig);
192
193         jPanelConfig.setLayout(new java.awt.GridBagLayout JavaDoc());
194
195         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
196         this.setLayout(layout);
197         layout.setHorizontalGroup(
198             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
199             .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
200             .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
201             .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
202             .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabelConfig, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
203             .add(jPanelConfig, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
204         );
205         layout.setVerticalGroup(
206             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
207             .add(layout.createSequentialGroup()
208                 .add(jLabel1)
209                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
210                 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 71, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
211                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
212                 .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
213                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
214                 .add(jLabelConfig, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 21, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
215                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
216                 .add(jPanelConfig, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE))
217         );
218     }// </editor-fold>//GEN-END:initComponents
219

220     // Variables declaration - do not modify//GEN-BEGIN:variables
221
private javax.swing.JLabel JavaDoc jLabel1;
222     private javax.swing.JLabel JavaDoc jLabelConfig;
223     private javax.swing.JPanel JavaDoc jPanelConfig;
224     private javax.swing.JScrollPane JavaDoc jScrollPane1;
225     private javax.swing.JSeparator JavaDoc jSeparator1;
226     private javax.swing.JTable JavaDoc jTableFrameworks;
227     // End of variables declaration//GEN-END:variables
228

229     boolean valid(WizardDescriptor wizardDescriptor) {
230         boolean retVal = true;
231         for (int i = 0; i < model.getRowCount(); i++) {
232             if (model.getItem(i).isSelected().booleanValue()) {
233                 retVal = false;
234                 break;
235             }
236         }
237         //none framework selected, enable Finish
238
if (retVal)
239             return true;
240         
241         for (int i = 0; i < model.getRowCount(); i++) {
242             FrameworkModelItem item = model.getItem(i);
243             FrameworkConfigurationPanel configPanel = (FrameworkConfigurationPanel) configPanels.get(item.getFramework());
244             if (configPanel != null && !configPanel.isValid())
245                 return false;
246         }
247         
248         return true;
249     }
250
251     void validate (WizardDescriptor settings) throws WizardValidationException {
252 // projectLocationPanel.validate (d);
253
}
254     
255     void read (WizardDescriptor settings) {
256         
257 // if ( bottomPanel != null ) {
258
// bottomPanel.readSettings( settings );
259
// }
260
wizardDescriptor=settings;
261     }
262
263     void store(WizardDescriptor settings) {
264 // if ( bottomPanel != null ) {
265
// bottomPanel.storeSettings( settings );
266
// }
267

268         settings.putProperty(WizardProperties.FRAMEWORKS, getSelectedFrameworks()); //NOI18N
269
}
270
271     public List JavaDoc getSelectedFrameworks() {
272         List JavaDoc selectedFrameworks = new LinkedList JavaDoc();
273         FrameworksTableModel model = (FrameworksTableModel) jTableFrameworks.getModel();
274         for (int i = 0; i < model.getRowCount(); i++) {
275             FrameworkModelItem item = model.getItem(i);
276             if (item.isSelected())
277                 selectedFrameworks.add(item.getFramework());
278         }
279         
280         return selectedFrameworks;
281     }
282     
283     public Component JavaDoc[] getConfigComponents() {
284         return new Component JavaDoc[] {jSeparator1, jLabelConfig, jPanelConfig};
285     }
286     
287     /** Help context where to find more about the paste type action.
288      * @return the help context for this action
289      */

290     public HelpCtx getHelpCtx() {
291         if (jPanelConfig.getComponentCount()>0){
292             for (int i = 0; i < jPanelConfig.getComponentCount(); i++)
293                 if (jPanelConfig.getComponent(i) instanceof HelpCtx.Provider)
294                     return ((HelpCtx.Provider)jPanelConfig.getComponent(i)).getHelpCtx();
295         }
296         return null;
297     }
298     
299     public void tableChanged(TableModelEvent JavaDoc e) {
300         FrameworksTableModel model = (FrameworksTableModel) jTableFrameworks.getModel();
301         FrameworkModelItem item = model.getItem(jTableFrameworks.getSelectedRow());
302         WebFrameworkProvider framework = item.getFramework();
303         setConfigPanel(framework, item);
304     }
305     
306     public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc e) {
307         FrameworksTableModel model = (FrameworksTableModel) jTableFrameworks.getModel();
308         FrameworkModelItem item = model.getItem(jTableFrameworks.getSelectedRow());
309         WebFrameworkProvider framework = item.getFramework();
310         setConfigPanel(framework, item);
311     }
312     
313     private void setConfigPanel(WebFrameworkProvider framework, FrameworkModelItem item) {
314         if (configPanels.get(framework) != null) {
315             String JavaDoc message = MessageFormat.format(NbBundle.getMessage(PanelSupportedFrameworksVisual.class, "LBL_NWP2_ConfigureFramework"), new Object JavaDoc[] {framework.getName()}); //NOI18N
316
jLabelConfig.setText(message);
317 // jLabelConfig.setEnabled(item.isSelected().booleanValue());
318

319             jPanelConfig.removeAll();
320
321             java.awt.GridBagConstraints JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
322             gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
323             gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
324             gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
325             gridBagConstraints.weightx = 1.0;
326             gridBagConstraints.weighty = 1.0;
327
328             jPanelConfig.add(((FrameworkConfigurationPanel) configPanels.get(framework)).getComponent(), gridBagConstraints);
329             
330             jLabelConfig.setEnabled(item.isSelected().booleanValue());
331             ((FrameworkConfigurationPanel) configPanels.get(framework)).enableComponents(item.isSelected().booleanValue());
332             ((FrameworkConfigurationPanel) configPanels.get(framework)).readSettings(wizardDescriptor);
333             jPanelConfig.revalidate();
334             jPanelConfig.repaint();
335         } else {
336             jLabelConfig.setText(""); //NOI18N
337
jPanelConfig.removeAll();
338             jPanelConfig.repaint();
339             jPanelConfig.revalidate();
340         }
341         
342         if (panel != null)
343             panel.fireChangeEvent();
344     }
345
346     
347     
348     public void stateChanged(javax.swing.event.ChangeEvent JavaDoc e) {
349         if (panel != null)
350             panel.fireChangeEvent();
351     }
352     
353     public static class FrameworksTableCellRenderer extends DefaultTableCellRenderer JavaDoc {
354         private TableCellRenderer JavaDoc booleanRenderer;
355         
356         public Component JavaDoc getTableCellRendererComponent( JTable JavaDoc table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column ) {
357             if (value instanceof WebFrameworkProvider) {
358                 WebFrameworkProvider item = (WebFrameworkProvider) value;
359                 return super.getTableCellRendererComponent(table, item.getName(), isSelected, false, row, column);
360             } else {
361                 if (value instanceof Boolean JavaDoc && booleanRenderer != null)
362                     return booleanRenderer.getTableCellRendererComponent(table, value, isSelected, false, row, column);
363                 else
364                     return super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
365             }
366         }
367         
368         public void setBooleanRenderer(TableCellRenderer JavaDoc booleanRenderer) {
369             this.booleanRenderer = booleanRenderer;
370         }
371     }
372
373     /**
374      * Implements a TableModel.
375      */

376     public static final class FrameworksTableModel extends AbstractTableModel JavaDoc {
377         private DefaultListModel JavaDoc model;
378         
379         public FrameworksTableModel() {
380             model = new DefaultListModel JavaDoc();
381         }
382         
383         public int getColumnCount() {
384             return 2;
385         }
386         
387         public int getRowCount() {
388             return model.size();
389         }
390         
391         public Class JavaDoc getColumnClass(int columnIndex) {
392             if (columnIndex == 0)
393                 return Boolean JavaDoc.class;
394             else
395                 return WebFrameworkProvider.class;
396         }
397         
398         public boolean isCellEditable(int rowIndex, int columnIndex) {
399             return (columnIndex == 0);
400         }
401         
402         public Object JavaDoc getValueAt(int row, int column) {
403             FrameworkModelItem item = getItem(row);
404             switch (column) {
405                 case 0: return item.isSelected();
406                 case 1: return item.getFramework();
407             }
408             return "";
409         }
410         
411         public void setValueAt(Object JavaDoc value, int row, int column) {
412             FrameworkModelItem item = getItem(row);
413             switch (column) {
414                 case 0: item.setSelected((Boolean JavaDoc) value);break;
415                 case 1: item.setFramework((WebFrameworkProvider) value);break;
416             }
417             fireTableCellUpdated(row, column);
418         }
419         
420         private FrameworkModelItem getItem(int index) {
421             return (FrameworkModelItem) model.get(index);
422         }
423         
424         public void addItem(FrameworkModelItem item){
425             model.addElement(item);
426         }
427     }
428
429     private final class FrameworkModelItem {
430         private WebFrameworkProvider framework;
431         private Boolean JavaDoc selected;
432         
433         /** Creates a new instance of BeanFormProperty */
434         public FrameworkModelItem(WebFrameworkProvider framework) {
435             this.setFramework(framework);
436             setSelected(Boolean.FALSE);
437         }
438
439         public WebFrameworkProvider getFramework() {
440             return framework;
441         }
442
443         public void setFramework(WebFrameworkProvider framework) {
444             this.framework = framework;
445         }
446
447         public Boolean JavaDoc isSelected() {
448             return selected;
449         }
450
451         public void setSelected(Boolean JavaDoc selected) {
452             this.selected = selected;
453         }
454
455     }
456 }
457
Popular Tags