1 19 20 package org.netbeans.modules.j2ee.earproject.ui.customizer; 21 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.io.File ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Collection ; 30 import java.util.List ; 31 import javax.swing.DefaultListSelectionModel ; 32 import javax.swing.JButton ; 33 import javax.swing.JFileChooser ; 34 import javax.swing.JTable ; 35 import javax.swing.ListSelectionModel ; 36 import javax.swing.event.ListSelectionEvent ; 37 import javax.swing.event.ListSelectionListener ; 38 import javax.swing.event.TableModelEvent ; 39 import javax.swing.event.TableModelListener ; 40 import javax.swing.table.AbstractTableModel ; 41 import javax.swing.table.DefaultTableCellRenderer ; 42 import org.netbeans.api.java.project.JavaProjectConstants; 43 import org.netbeans.api.project.Project; 44 import org.netbeans.api.project.ant.AntArtifact; 45 import org.netbeans.api.project.libraries.Library; 46 import org.netbeans.modules.j2ee.api.ejbjar.EjbProjectConstants; 47 import org.netbeans.modules.web.api.webmodule.WebProjectConstants; 48 import org.openide.DialogDescriptor; 49 import org.openide.DialogDisplayer; 50 import org.openide.util.NbBundle; 51 52 54 final class VisualArchiveIncludesSupport { 55 56 final Project master; 57 final String j2eePlatform; 58 final JTable classpathTable; 59 final JButton addJarButton; 60 final JButton addLibraryButton; 61 final JButton addArtifactButton; 62 final JButton removeButton; 63 64 private final ClasspathTableModel classpathModel; 65 private Object [][] data; 66 67 private final List <ActionListener > actionListeners = new ArrayList <ActionListener >(); 68 69 public VisualArchiveIncludesSupport(Project master, 70 String j2eePlatform, 71 JTable classpathTable, 72 JButton addJarButton, 73 JButton addLibraryButton, 74 JButton addArtifactButton, 75 JButton removeButton) { 76 77 this.classpathTable = classpathTable; 79 this.classpathModel = new ClasspathTableModel(); 80 this.classpathTable.setModel(classpathModel); 81 this.classpathTable.getColumnModel().getColumn(0).setHeaderValue(NbBundle.getMessage(VisualArchiveIncludesSupport.class, "TXT_Archive_Item")); 82 this.classpathTable.getColumnModel().getColumn(1).setHeaderValue(NbBundle.getMessage(VisualArchiveIncludesSupport.class, "TXT_Archive_PathInArchive")); 83 this.classpathTable.getColumnModel().getColumn(0).setCellRenderer(new ClassPathCellRenderer()); 84 this.classpathTable.getColumnModel().getColumn(1).setCellRenderer(new DefaultTableCellRenderer () { 85 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, 86 boolean hasFocus, int row, int column) { 87 if (value != null) { 88 setToolTipText(value.toString()); 89 } 90 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 91 } 92 }); 93 94 this.classpathTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 95 96 this.addJarButton = addJarButton; 97 this.addLibraryButton = addLibraryButton; 98 this.addArtifactButton = addArtifactButton; 99 this.removeButton = removeButton; 100 101 this.master = master; 102 this.j2eePlatform = j2eePlatform; 103 104 ClasspathSupportListener csl = new ClasspathSupportListener(); 106 107 addJarButton.addActionListener(csl); 109 addLibraryButton.addActionListener(csl); 110 addArtifactButton.addActionListener(csl); 111 removeButton.addActionListener(csl); 112 classpathTable.getSelectionModel().addListSelectionListener(csl); 114 115 classpathModel.addTableModelListener(csl); 116 117 csl.valueChanged(null); 119 } 120 121 public void setVisualWarItems(List <VisualClassPathItem> items) { 122 Object data[][] = new Object [items.size()][2]; 123 this.data = data; 124 for (int i = 0; i < items.size(); i++) { 125 classpathModel.setValueAt(items.get(i), i, 0); 126 String pathInWAR = items.get(i).getPathInEAR(); 127 classpathModel.setValueAt(pathInWAR, i, 1); 128 } 129 130 classpathModel.fireTableDataChanged(); 131 } 132 133 public List <VisualClassPathItem> getVisualWarItems() { 134 List <VisualClassPathItem> items = new ArrayList <VisualClassPathItem>(); 135 for (int i = 0; i < data.length; i++) { 136 items.add((VisualClassPathItem) classpathModel.getValueAt(i, 0)); 137 } 138 return items; 139 } 140 141 public void addTableModelListener(TableModelListener tml) { 142 classpathModel.addTableModelListener(tml); 143 } 144 145 public void removeTableModelListener(TableModelListener tml) { 146 classpathModel.removeTableModelListener(tml); 147 } 148 149 152 public void addActionListener( ActionListener listener ) { 153 actionListeners.add( listener ); 154 } 155 156 public void removeActionListener( ActionListener listener ) { 157 actionListeners.remove( listener ); 158 } 159 160 private void fireActionPerformed() { 161 List <ActionListener > listeners; 162 163 synchronized (this) { 164 listeners = new ArrayList <ActionListener >( actionListeners ); 165 } 166 167 ActionEvent ae = new ActionEvent ( this, 0, null ); 168 169 for (ActionListener al : listeners) { 170 al.actionPerformed( ae ); 171 } 172 } 173 174 176 private Collection <Object > getLibraries () { 177 List <Object > list = new ArrayList <Object >(); 178 for (VisualClassPathItem vcpi : getVisualWarItems()) { 179 if (vcpi.getType() == VisualClassPathItem.Type.LIBRARY) { 180 list.add(vcpi.getObject()); 181 } 182 } 183 return list; 184 } 185 186 private void addLibraries (Library[] libraries) { 187 if (libraries.length > 0) { 188 List <Library> newLibList = new ArrayList <Library>(Arrays.asList(libraries)); 189 classpathTable.clearSelection(); 190 int n0 = data.length; 191 for (int i = 0; i < n0; i++) { 192 VisualClassPathItem item = (VisualClassPathItem) data[i][0]; 193 if(item.getType() == VisualClassPathItem.Type.LIBRARY && 194 newLibList.remove(item.getObject())) { 195 classpathTable.addRowSelectionInterval(i, i); 196 } 197 } 198 int n = newLibList.size(); 199 if (n > 0) { 200 Object [][] newData = new Object [n0 + n][2]; 201 for (int i = 0; i < n0; i++) { 202 newData[i] = data[i]; 203 } 204 for (int i = 0; i < n; i++) { 205 Library library = newLibList.get(i); 206 VisualClassPathItem item = VisualClassPathItem.createLibrary(library); 207 newData[n0 + i][0] = item; 208 newData[n0 + i][1] = VisualClassPathItem.PATH_IN_EAR; 209 } 210 211 data = newData; 212 classpathModel.fireTableRowsInserted(n0, n0 + n - 1); 213 classpathTable.addRowSelectionInterval(n0, n0 + n - 1); 214 } 215 216 fireActionPerformed(); 217 } 218 219 } 220 221 private void addJarFiles( File files[] ) { 222 Object [][] newData = new Object [data.length + files.length][2]; 223 for (int i = 0; i < data.length; i++) { 224 newData[i] = data[i]; 225 } 226 for (int i = 0; i < files.length; i++) { 227 VisualClassPathItem jarFile = VisualClassPathItem.createJAR(files[i]); 228 newData[data.length + i][0] = jarFile; 229 newData[data.length + i][1] = jarFile.getPathInEAR(); 230 } 231 232 data = newData; 233 classpathModel.fireTableRowsInserted(data.length, data.length + files.length - 1); 234 235 fireActionPerformed(); 236 } 237 238 private void addArtifacts( AntArtifact artifacts[] ) { 239 Object [][] newData = new Object [data.length + artifacts.length][2]; 240 for (int i = 0; i < data.length; i++) { 241 newData[i] = data[i]; 242 } 243 for (int i = 0; i < artifacts.length; i++) { 244 VisualClassPathItem vcpi = VisualClassPathItem.createArtifact(artifacts[i]); 245 newData[data.length + i][0] = vcpi; 246 newData[data.length + i][1] = vcpi.getPathInEAR(); 247 } 248 249 data = newData; 250 classpathModel.fireTableRowsInserted(data.length, data.length + artifacts.length - 1); 251 252 fireActionPerformed(); 253 } 254 255 private void removeElements() { 256 ListSelectionModel sm = classpathTable.getSelectionModel(); 257 int index = sm.getMinSelectionIndex(); 258 if (sm.isSelectionEmpty()) { 259 assert false : "Remove button should be disabled"; } 261 Collection <Object > elements = new ArrayList <Object >(); 262 final int n0 = data.length; 263 for (int i = 0; i < n0; i++) { 264 if (!sm.isSelectedIndex(i)) { 265 elements.add(data[i]); 266 } 267 } 268 final int n = elements.size(); 269 data = elements.toArray(new Object [n][2]); 270 classpathModel.fireTableRowsDeleted(elements.size(), n0 - 1); 271 272 if (index >= n) { 273 index = n - 1; 274 } 275 sm.setSelectionInterval(index, index); 276 277 fireActionPerformed(); 278 } 279 280 282 private class ClasspathSupportListener implements ActionListener , ListSelectionListener , TableModelListener { 283 285 287 public void actionPerformed( ActionEvent e ) { 288 Object source = e.getSource(); 289 if ( source == addJarButton ) { 290 JFileChooser chooser = new JFileChooser (); 292 chooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); 293 chooser.setMultiSelectionEnabled( true ); 294 chooser.setDialogTitle( NbBundle.getMessage( VisualArchiveIncludesSupport.class, "LBL_CustomizeCompile_Classpath_AddJar_JButton" ) ); chooser.setAcceptAllFileFilterUsed( false ); 297 298 int option = chooser.showOpenDialog( null ); 300 if ( option == JFileChooser.APPROVE_OPTION ) { 301 File files[] = chooser.getSelectedFiles(); 302 addJarFiles( files ); 303 } 304 } else if ( source == addLibraryButton ) { 305 LibrariesChooser panel = new LibrariesChooser(getLibraries(), j2eePlatform); 306 Object [] options = new Object [] { 307 NbBundle.getMessage (VisualArchiveIncludesSupport.class,"LBL_AddLibrary"), 308 DialogDescriptor.CANCEL_OPTION 309 }; 310 DialogDescriptor desc = new DialogDescriptor(panel,NbBundle.getMessage( VisualArchiveIncludesSupport.class, "LBL_CustomizeCompile_Classpath_AddLibrary" ), 311 true, options, options[0], DialogDescriptor.DEFAULT_ALIGN,null,null); 312 Dialog dlg = DialogDisplayer.getDefault().createDialog(desc); 313 dlg.setVisible(true); 314 if (desc.getValue() == options[0]) { 315 addLibraries (panel.getSelectedLibraries()); 316 } 317 dlg.dispose(); 318 } else if ( source == addArtifactButton ) { 319 AntArtifact artifacts[] = AntArtifactChooser.showDialog(master, 322 new String [] { 323 EjbProjectConstants.ARTIFACT_TYPE_J2EE_MODULE_IN_EAR_ARCHIVE, 324 WebProjectConstants.ARTIFACT_TYPE_WAR_EAR_ARCHIVE, 325 JavaProjectConstants.ARTIFACT_TYPE_JAR }); 326 if ( artifacts != null ) { 328 addArtifacts( artifacts ); 329 } 330 } else if ( source == removeButton ) { 331 removeElements(); 332 } 333 } 334 335 337 339 public void valueChanged( ListSelectionEvent e ) { 340 DefaultListSelectionModel sm = (DefaultListSelectionModel ) classpathTable.getSelectionModel(); 341 int index = sm.getMinSelectionIndex(); 342 343 boolean remove = index != -1; 345 if (remove) { 347 VisualClassPathItem vcpi = (VisualClassPathItem) classpathModel.getValueAt(index, 0); 348 if (!vcpi.canDelete()) { 349 remove = false; 350 } 351 } 352 353 removeButton.setEnabled(remove); 354 } 355 356 public void tableChanged(TableModelEvent e) { 358 if (e.getColumn() == 1) { 359 VisualClassPathItem cpItem = (VisualClassPathItem) classpathModel.getValueAt(e.getFirstRow(), 0); 360 cpItem.setPathInEAR((String ) classpathModel.getValueAt(e.getFirstRow(), 1)); 361 362 fireActionPerformed(); 363 } 364 } 365 366 } 367 368 private static class ClassPathCellRenderer extends DefaultTableCellRenderer { 369 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { 370 if (value instanceof VisualClassPathItem) { 371 final VisualClassPathItem item = (VisualClassPathItem) value; 372 setIcon(item.getIcon()); 373 } 376 final String s = value == null ? null : value.toString(); 377 return super.getTableCellRendererComponent(table, s, isSelected, false, row, column); 378 } 379 } 380 381 class ClasspathTableModel extends AbstractTableModel { 382 public int getColumnCount() { 383 return 2; } 385 386 public int getRowCount() { 387 if (data == null) { 388 return 0; 389 } 390 return data.length; 391 } 392 393 public Object getValueAt(int row, int col) { 394 return data[row][col]; 395 } 396 397 public boolean isCellEditable(int row, int col) { 398 return col == 1; 399 } 400 401 public void setValueAt(Object value, int row, int col) { 402 data[row][col] = value; 403 fireTableCellUpdated(row, col); 404 } 405 } 406 407 } 408 | Popular Tags |