1 19 20 package org.netbeans.modules.j2ee.earproject.ui.wizards; 21 22 import java.awt.Component ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.io.File ; 26 import java.util.EventObject ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Set ; 32 import java.util.Vector ; 33 import javax.swing.JComboBox ; 34 import javax.swing.JFileChooser ; 35 import javax.swing.JPanel ; 36 import javax.swing.JTable ; 37 import javax.swing.event.CellEditorListener ; 38 import javax.swing.event.ChangeEvent ; 39 import javax.swing.event.ChangeListener ; 40 import javax.swing.event.EventListenerList ; 41 import javax.swing.table.DefaultTableModel ; 42 import javax.swing.table.TableCellEditor ; 43 import javax.swing.table.TableCellRenderer ; 44 import javax.swing.table.TableColumn ; 45 import javax.swing.table.TableColumnModel ; 46 import org.netbeans.modules.j2ee.earproject.ModuleType; 47 import org.netbeans.spi.project.support.ant.PropertyUtils; 48 import org.openide.WizardDescriptor; 49 import org.openide.filesystems.FileObject; 50 import org.openide.filesystems.FileUtil; 51 import org.openide.util.NbBundle; 52 53 56 public class PanelModuleDetectionVisual extends JPanel { 57 58 private final Vector <Vector <String >> modules = new Vector <Vector <String >>(); 59 private static final int REL_PATH_INDEX = 0; 60 private static final int TYPE_INDEX = 1; 61 private final Set <ChangeListener > listeners = new HashSet <ChangeListener >(1); 62 63 private File eaLocation; 65 66 public PanelModuleDetectionVisual() { 67 initComponents(); 68 initModuleTable(); 69 setName(getMessage("LBL_IW_ApplicationModulesStep")); 71 putClientProperty("NewProjectWizard_Title", getMessage("TXT_ImportProject")); 72 getAccessibleContext().setAccessibleDescription(getMessage("ACS_NWP1_NamePanel_A11YDesc")); 73 } 74 75 public void addChangeListener(ChangeListener l) { 76 synchronized (listeners) { 77 listeners.add(l); 78 } 79 } 80 81 public void removeChangeListener(ChangeListener l) { 82 synchronized (listeners) { 83 listeners.remove(l); 84 } 85 } 86 87 protected void fireChangeEvent() { 88 Iterator <ChangeListener > it; 89 synchronized (listeners) { 90 it = new HashSet <ChangeListener >(listeners).iterator(); 91 } 92 ChangeEvent ev = new ChangeEvent (this); 93 while (it.hasNext()) { 94 it.next().stateChanged(ev); 95 } 96 } 97 98 private void initModuleTable() { 99 Vector <String > colNames = new Vector <String >(); 100 colNames.add(getMessage("LBL_IW_Module")); 101 colNames.add(getMessage("LBL_IW_Type")); 102 DefaultTableModel moduleTableModel = new DefaultTableModel (modules, colNames); 103 moduleTable.setModel(moduleTableModel); 104 TableColumnModel tcm = moduleTable.getColumnModel(); 105 TableColumn tc = tcm.getColumn(1); 106 ModuleTypeRenderer renderer = new ModuleTypeRenderer(); 107 tc.setCellRenderer(renderer); 108 tc.setCellEditor(new ModuleTypeEditor()); 109 moduleTable.setRowHeight((int) renderer.getPreferredSize().getHeight()); 110 moduleSP.getViewport().setBackground(moduleTable.getBackground()); 111 } 112 113 void read(WizardDescriptor settings) { 114 File newEALocation = (File ) settings.getProperty(WizardProperties.SOURCE_ROOT); 115 assert newEALocation != null : "Location is not available!"; 116 if (!newEALocation.equals(eaLocation)) { 117 this.modules.removeAllElements(); 119 } 120 eaLocation = newEALocation; 121 FileObject eaLocationFO = FileUtil.toFileObject(eaLocation); 122 Map <FileObject, ModuleType> modules = ModuleType.detectModules(eaLocationFO); 123 for (FileObject moduleDir : modules.keySet()) { 124 addModuleToTable(FileUtil.toFile(moduleDir)); 125 } 126 getModuleTableModel().fireTableDataChanged(); 127 } 128 129 boolean valid(WizardDescriptor wizardDescriptor) { 130 File project = getProject(wizardDescriptor); 131 for (Vector <String > module : modules) { 132 if (isModuleForbidden(project, module.get(REL_PATH_INDEX))) { 133 wizardDescriptor.putProperty("WizardPanel_errorMessage", getMessage("MSG_ModuleLocationAlreadyExists")); return false; 136 } 137 } 138 wizardDescriptor.putProperty("WizardPanel_errorMessage", null); return true; 140 } 141 142 143 private File getProject(WizardDescriptor wizardDescriptor) { 144 return (File ) wizardDescriptor.getProperty(WizardProperties.PROJECT_DIR); 145 } 146 147 149 private boolean isModuleForbidden(File project, String module) { 150 String moduleName = new File (project, module).getName(); 151 File forbiddenLocation = new File (project, moduleName); 152 return forbiddenLocation.exists(); 153 } 154 155 void store(WizardDescriptor wd) { 156 Map <FileObject, ModuleType> userModules = 157 new HashMap <FileObject, ModuleType>(); 158 for (Vector <String > module : modules) { 159 String description = module.get(TYPE_INDEX); 160 for (ModuleType type : ModuleType.values()) { 161 if (type.getDescription().equals(description)) { 162 File moduleDir = new File (eaLocation, module.get(REL_PATH_INDEX)); 163 FileObject moduleDirFO = FileUtil.toFileObject(FileUtil.normalizeFile(moduleDir)); 164 assert moduleDirFO != null; 165 userModules.put(moduleDirFO, type); 166 break; 167 } 168 } 169 } 170 wd.putProperty(WizardProperties.USER_MODULES, userModules); 171 } 172 173 private DefaultTableModel getModuleTableModel() { 174 return (DefaultTableModel ) moduleTable.getModel(); 175 } 176 177 private void addModuleToTable(final File moduleF) { 178 String relPath = PropertyUtils.relativizeFile(eaLocation, moduleF); 179 for (Vector <String > module : modules) { 180 if (relPath.equals(module.get(REL_PATH_INDEX))) { 181 return; 183 } 184 } 185 Vector <String > row = new Vector <String >(); 186 row.add(relPath); 187 row.add(getModuleType(relPath).getDescription()); 188 modules.add(row); 189 fireChangeEvent(); 190 } 191 192 private static final String getMessage(String bundleKey) { 193 return NbBundle.getMessage(PanelModuleDetectionVisual.class, bundleKey); 194 } 195 196 201 private void initComponents() { 203 appModulesLabel = new javax.swing.JLabel (); 204 moduleSP = new javax.swing.JScrollPane (); 205 moduleTable = new javax.swing.JTable (); 206 addModuleButton = new javax.swing.JButton (); 207 removeModuleButton = new javax.swing.JButton (); 208 209 appModulesLabel.setLabelFor(moduleTable); 210 org.openide.awt.Mnemonics.setLocalizedText(appModulesLabel, org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "LBL_IW_ApplicationModules")); 211 appModulesLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "ACSD_LBL_IW_ApplicationModules")); 212 213 moduleSP.setViewportView(moduleTable); 214 215 moduleSP.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "ACSN_CTL_AppModules")); 216 moduleSP.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "ACSD_CTL_AppModules")); 217 218 org.openide.awt.Mnemonics.setLocalizedText(addModuleButton, org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "LBL_IW_Add")); 219 addModuleButton.addActionListener(new java.awt.event.ActionListener () { 220 public void actionPerformed(java.awt.event.ActionEvent evt) { 221 addModuleButtonActionPerformed(evt); 222 } 223 }); 224 225 addModuleButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "ACSD_LBL_IW_Add")); 226 227 org.openide.awt.Mnemonics.setLocalizedText(removeModuleButton, org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "LBL_IW_Remove")); 228 removeModuleButton.addActionListener(new java.awt.event.ActionListener () { 229 public void actionPerformed(java.awt.event.ActionEvent evt) { 230 removeModuleButtonActionPerformed(evt); 231 } 232 }); 233 234 removeModuleButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "ACSD_LBL_IW_Remove")); 235 236 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 237 this.setLayout(layout); 238 layout.setHorizontalGroup( 239 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 240 .add(layout.createSequentialGroup() 241 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 242 .add(appModulesLabel) 243 .add(moduleSP, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)) 244 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 245 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 246 .add(addModuleButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE) 247 .add(removeModuleButton))) 248 ); 249 250 layout.linkSize(new java.awt.Component [] {addModuleButton, removeModuleButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL); 251 252 layout.setVerticalGroup( 253 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 254 .add(layout.createSequentialGroup() 255 .add(appModulesLabel) 256 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 257 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 258 .add(layout.createSequentialGroup() 259 .add(addModuleButton) 260 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 261 .add(removeModuleButton) 262 .addContainerGap()) 263 .add(moduleSP, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE))) 264 ); 265 } 267 private void removeModuleButtonActionPerformed(java.awt.event.ActionEvent evt) { int row = moduleTable.getSelectedRow(); 269 if (row != -1) { 270 modules.remove(row); 271 getModuleTableModel().fireTableRowsDeleted(row, row); 272 fireChangeEvent(); 273 } 274 } 276 private void addModuleButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (eaLocation); 278 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 279 if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 280 if (eaLocation.equals(chooser.getSelectedFile())) { 281 return; 284 } 285 addModuleToTable(chooser.getSelectedFile()); 286 getModuleTableModel().fireTableDataChanged(); 287 } 288 } 290 private ModuleType getModuleType(final String relPath) { 291 ModuleType type = null; 292 File dir = FileUtil.normalizeFile(new File (eaLocation, relPath)); 293 FileObject dirFO = FileUtil.toFileObject(dir); 294 if (dirFO != null) { 295 type = ModuleType.detectModuleType(dirFO); 296 } 297 return type == null ? ModuleType.WEB : type; } 299 300 private javax.swing.JButton addModuleButton; 302 private javax.swing.JLabel appModulesLabel; 303 private javax.swing.JScrollPane moduleSP; 304 private javax.swing.JTable moduleTable; 305 private javax.swing.JButton removeModuleButton; 306 308 private static final class ModuleTypeRenderer extends JComboBox 309 implements TableCellRenderer { 310 311 ModuleTypeRenderer() { 312 for (ModuleType type : ModuleType.values()) { 313 addItem(type.getDescription()); 314 } 315 } 316 317 public Component getTableCellRendererComponent(JTable table, Object value, 318 boolean isSelected, boolean hasFocus, int row, int column) { 319 320 if (isSelected) { 321 setForeground(table.getSelectionForeground()); 322 super.setBackground(table.getSelectionBackground()); 323 } else { 324 setForeground(table.getForeground()); 325 setBackground(table.getBackground()); 326 } 327 328 String moduleType = (String ) value; 329 setSelectedItem(moduleType); 330 return this; 331 } 332 333 } 334 335 private class ModuleTypeEditor extends JComboBox implements TableCellEditor { 336 337 protected EventListenerList listenerList = new EventListenerList (); 338 protected ChangeEvent changeEvent = new ChangeEvent (this); 339 340 ModuleTypeEditor() { 341 for (ModuleType type : ModuleType.values()) { 342 addItem(type.getDescription()); 343 } 344 addActionListener(new ActionListener () { 345 public void actionPerformed(ActionEvent event) { 346 fireEditingStopped(); 347 } 348 }); 349 } 350 351 public void addCellEditorListener(CellEditorListener listener) { 352 listenerList.add(CellEditorListener .class, listener); 353 } 354 355 public void removeCellEditorListener(CellEditorListener listener) { 356 listenerList.remove(CellEditorListener .class, listener); 357 } 358 359 protected void fireEditingStopped() { 360 CellEditorListener listener; 361 Object [] listeners = listenerList.getListenerList(); 362 for (int i = 0; i < listeners.length; i++) { 363 if (listeners[i] == CellEditorListener .class) { 364 listener = (CellEditorListener ) listeners[i + 1]; 365 listener.editingStopped(changeEvent); 366 } 367 } 368 } 369 370 protected void fireEditingCanceled() { 371 CellEditorListener listener; 372 Object [] listeners = listenerList.getListenerList(); 373 for (int i = 0; i < listeners.length; i++) { 374 if (listeners[i] == CellEditorListener .class) { 375 listener = (CellEditorListener ) listeners[i + 1]; 376 listener.editingCanceled(changeEvent); 377 } 378 } 379 } 380 381 public void cancelCellEditing() { 382 fireEditingCanceled(); 383 } 384 385 public boolean stopCellEditing() { 386 fireEditingStopped(); 387 return true; 388 } 389 390 public boolean isCellEditable(EventObject event) { 391 return true; 392 } 393 394 public boolean shouldSelectCell(EventObject event) { 395 return true; 396 } 397 398 public Object getCellEditorValue() { 399 return getSelectedItem(); 400 } 401 402 public Component getTableCellEditorComponent(JTable table, Object value, 403 boolean isSelected, int row, int column) { 404 String moduleType = (String ) value; 405 setSelectedItem(moduleType); 406 return this; 407 } 408 409 } 410 411 } 412 | Popular Tags |