1 19 20 package org.netbeans.modules.web.project.ui.wizards; 21 22 import java.awt.Component ; 23 import java.text.MessageFormat ; 24 import java.util.HashMap ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 28 import javax.swing.DefaultListModel ; 29 import javax.swing.JPanel ; 30 import javax.swing.JTable ; 31 import javax.swing.ListSelectionModel ; 32 import javax.swing.event.ChangeListener ; 33 import javax.swing.event.ListSelectionListener ; 34 import javax.swing.event.TableModelEvent ; 35 import javax.swing.event.TableModelListener ; 36 import javax.swing.table.AbstractTableModel ; 37 import javax.swing.table.DefaultTableCellRenderer ; 38 import javax.swing.table.TableCellRenderer ; 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 implements HelpCtx.Provider, TableModelListener , ListSelectionListener , ChangeListener { 51 52 53 public static final int ALL_FRAMEWORKS = 0; 54 55 56 public static final int USED_FRAMEWORKS = 1; 57 58 59 public static final int UNUSED_FRAMEWORKS = 2; 60 61 private List ignoredFrameworks; 62 private HashMap configPanels = new HashMap (); 63 private FrameworksTableModel model; 64 private PanelSupportedFrameworks panel; 65 private WizardDescriptor wizardDescriptor; 66 67 72 public PanelSupportedFrameworksVisual(PanelSupportedFrameworks panel, WebProject project, int filter, List 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 .class)); 83 jTableFrameworks.setDefaultRenderer(WebFrameworkProvider.class, renderer); 84 jTableFrameworks.setDefaultRenderer(Boolean .class, renderer); 85 initTableVisualProperties(jTableFrameworks); 86 87 this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PanelSupportedFrameworksVisual.class, "ACS_NWP2_Frameworks_A11YDesc")); 89 setName(NbBundle.getMessage(PanelSupportedFrameworksVisual.class, "LBL_NWP2_Frameworks")); putClientProperty ("NewProjectWizard_Title", NbBundle.getMessage(PanelSupportedFrameworksVisual.class, "TXT_NewWebApp")); } 93 94 private void initTableVisualProperties(JTable 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 (0, 0)); 105 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 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 159 private void initComponents() { 161 jLabel1 = new javax.swing.JLabel (); 162 jScrollPane1 = new javax.swing.JScrollPane (); 163 jTableFrameworks = new javax.swing.JTable (); 164 jSeparator1 = new javax.swing.JSeparator (); 165 jLabelConfig = new javax.swing.JLabel (); 166 jPanelConfig = new javax.swing.JPanel (); 167 168 setPreferredSize(new java.awt.Dimension (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 (32767, 70)); 176 jScrollPane1.setMinimumSize(new java.awt.Dimension (22, 70)); 177 jScrollPane1.setPreferredSize(new java.awt.Dimension (453, 70)); 178 jTableFrameworks.setModel(new javax.swing.table.DefaultTableModel ( 179 new Object [][] { 180 {null, null, null, null}, 181 {null, null, null, null}, 182 {null, null, null, null}, 183 {null, null, null, null} 184 }, 185 new String [] { 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 ()); 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 } 220 private javax.swing.JLabel jLabel1; 222 private javax.swing.JLabel jLabelConfig; 223 private javax.swing.JPanel jPanelConfig; 224 private javax.swing.JScrollPane jScrollPane1; 225 private javax.swing.JSeparator jSeparator1; 226 private javax.swing.JTable jTableFrameworks; 227 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 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 } 254 255 void read (WizardDescriptor settings) { 256 257 wizardDescriptor=settings; 261 } 262 263 void store(WizardDescriptor settings) { 264 268 settings.putProperty(WizardProperties.FRAMEWORKS, getSelectedFrameworks()); } 270 271 public List getSelectedFrameworks() { 272 List selectedFrameworks = new LinkedList (); 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 [] getConfigComponents() { 284 return new Component [] {jSeparator1, jLabelConfig, jPanelConfig}; 285 } 286 287 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 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 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 message = MessageFormat.format(NbBundle.getMessage(PanelSupportedFrameworksVisual.class, "LBL_NWP2_ConfigureFramework"), new Object [] {framework.getName()}); jLabelConfig.setText(message); 317 319 jPanelConfig.removeAll(); 320 321 java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints (); 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(""); 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 e) { 349 if (panel != null) 350 panel.fireChangeEvent(); 351 } 352 353 public static class FrameworksTableCellRenderer extends DefaultTableCellRenderer { 354 private TableCellRenderer booleanRenderer; 355 356 public Component getTableCellRendererComponent( JTable table, Object 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 && 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 booleanRenderer) { 369 this.booleanRenderer = booleanRenderer; 370 } 371 } 372 373 376 public static final class FrameworksTableModel extends AbstractTableModel { 377 private DefaultListModel model; 378 379 public FrameworksTableModel() { 380 model = new DefaultListModel (); 381 } 382 383 public int getColumnCount() { 384 return 2; 385 } 386 387 public int getRowCount() { 388 return model.size(); 389 } 390 391 public Class getColumnClass(int columnIndex) { 392 if (columnIndex == 0) 393 return Boolean .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 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 value, int row, int column) { 412 FrameworkModelItem item = getItem(row); 413 switch (column) { 414 case 0: item.setSelected((Boolean ) 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 selected; 432 433 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 isSelected() { 448 return selected; 449 } 450 451 public void setSelected(Boolean selected) { 452 this.selected = selected; 453 } 454 455 } 456 } 457 | Popular Tags |