1 19 20 package org.netbeans.modules.j2ee.ejbjarproject.ui.customizer; 21 22 import java.io.File ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Set ; 29 import javax.swing.DefaultListModel ; 30 import javax.swing.ListSelectionModel ; 31 import javax.swing.event.ListDataEvent ; 32 import javax.swing.event.ListDataListener ; 33 import javax.swing.table.AbstractTableModel ; 34 import org.netbeans.api.project.libraries.Library; 35 import org.netbeans.modules.j2ee.ejbjarproject.classpath.ClassPathSupport; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.util.NbBundle; 39 40 41 45 public class ClassPathUiSupport { 46 47 49 50 61 62 64 public static DefaultListModel createListModel( Iterator it ) { 65 66 DefaultListModel model = new DefaultListModel (); 67 68 while( it.hasNext() ) { 69 model.addElement( it.next() ); 70 } 71 72 return model; 73 } 74 75 public static ClassPathTableModel createTableModel ( Iterator it ) { 76 return new ClassPathTableModel( createListModel( it ) ); 77 } 78 79 public static Iterator getIterator( DefaultListModel model ) { 80 return getList( model ).iterator(); 82 } 83 84 public static List getList( DefaultListModel model ) { 85 return Collections.list( model.elements() ); 86 } 87 88 89 92 public static int[] moveUp( DefaultListModel listModel, int indices[]) { 93 94 if( indices == null || indices.length == 0 ) { 95 assert false : "MoveUp button should be disabled"; } 97 98 for( int i = 0; i < indices.length; i++ ) { 100 Object item = listModel.get( indices[i] ); 101 listModel.remove( indices[i] ); 102 listModel.add( indices[i] - 1, item ); 103 } 104 105 for( int i = 0; i < indices.length; i++ ) { 107 indices[i] -= 1; 108 } 109 110 return indices; 111 112 } 113 114 public static boolean canMoveUp( ListSelectionModel selectionModel ) { 115 return selectionModel.getMinSelectionIndex() > 0; 116 } 117 118 121 public static int[] moveDown( DefaultListModel listModel, int indices[]) { 122 123 if( indices == null || indices.length == 0 ) { 124 assert false : "MoveDown button should be disabled"; } 126 127 for( int i = indices.length -1 ; i >= 0 ; i-- ) { 129 Object item = listModel.get( indices[i] ); 130 listModel.remove( indices[i] ); 131 listModel.add( indices[i] + 1, item ); 132 } 133 134 for( int i = 0; i < indices.length; i++ ) { 136 indices[i] += 1; 137 } 138 139 return indices; 140 141 } 142 143 public static boolean canMoveDown( ListSelectionModel selectionModel, int modelSize ) { 144 int iMax = selectionModel.getMaxSelectionIndex(); 145 return iMax != -1 && iMax < modelSize - 1; 146 } 147 148 150 public static int[] remove( DefaultListModel listModel, int[] indices ) { 151 152 if( indices == null || indices.length == 0 ) { 153 assert false : "Remove button should be disabled"; } 155 156 for( int i = indices.length - 1 ; i >= 0 ; i-- ) { 158 listModel.remove( indices[i] ); 159 } 160 161 if ( !listModel.isEmpty() ) { 162 int selectedIndex = indices[indices.length - 1] - indices.length + 1; 164 if ( selectedIndex > listModel.size() - 1) { 165 selectedIndex = listModel.size() - 1; 166 } 167 return new int[] { selectedIndex }; 168 } 169 else { 170 return new int[] {}; 171 } 172 173 } 174 175 public static int[] addLibraries( DefaultListModel listModel, int[] indices, Library[] libraries, Set alreadyIncludedLibs, boolean includeInDeployment ) { 176 int lastIndex = indices == null || indices.length == 0 ? listModel.getSize() - 1 : indices[indices.length - 1]; 177 for (int i = 0, j=1; i < libraries.length; i++) { 178 if (!alreadyIncludedLibs.contains(libraries[i])) { 179 listModel.add( lastIndex + j++, ClassPathSupport.Item.create( libraries[i], null, includeInDeployment ) ); 180 } 181 } 182 Set addedLibs = new HashSet (Arrays.asList(libraries)); 183 int[] indexes = new int[libraries.length]; 184 for (int i=0, j=0; i<listModel.getSize(); i++) { 185 ClassPathSupport.Item item = (ClassPathSupport.Item)listModel.get (i); 186 if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY && !item.isBroken() ) { 187 if (addedLibs.contains(item.getLibrary())) { 188 indexes[j++] =i; 189 } 190 } 191 } 192 return indexes; 193 } 194 195 public static int[] addJarFiles( DefaultListModel listModel, int[] indices, File files[], boolean includeInDeployment ) { 196 int lastIndex = indices == null || indices.length == 0 ? listModel.getSize() - 1 : indices[indices.length - 1]; 197 int[] indexes = new int[files.length]; 198 for( int i = 0, delta = 0; i+delta < files.length; ) { 199 int current = lastIndex + 1 + i; 200 ClassPathSupport.Item item = ClassPathSupport.Item.create( files[i+delta], null, includeInDeployment ); 201 if ( !listModel.contains( item ) ) { 202 listModel.add( current, item ); 203 indexes[delta + i] = current; 204 i++; 205 } 206 else { 207 indexes[i + delta] = listModel.indexOf( item ); 208 delta++; 209 } 210 } 211 return indexes; 212 213 } 214 215 public static int[] addArtifacts( DefaultListModel listModel, int[] indices, AntArtifactChooser.ArtifactItem artifactItems[], boolean includeInDeployment ) { 216 int lastIndex = indices == null || indices.length == 0 ? listModel.getSize() - 1 : indices[indices.length - 1]; 217 int[] indexes = new int[artifactItems.length]; 218 for( int i = 0; i < artifactItems.length; i++ ) { 219 int current = lastIndex + 1 + i; 220 ClassPathSupport.Item item = ClassPathSupport.Item.create( artifactItems[i].getArtifact(), artifactItems[i].getArtifactURI(), null, includeInDeployment ) ; 221 if ( !listModel.contains( item ) ) { 222 listModel.add( current, item ); 223 indexes[i] = current; 224 } 225 else { 226 indexes[i] = listModel.indexOf( item ); 227 } 228 } 229 return indexes; 230 } 231 232 234 238 public static final class ClassPathTableModel extends AbstractTableModel implements ListDataListener { 239 private DefaultListModel model; 240 241 public ClassPathTableModel(DefaultListModel model) { 242 this.model = model; 243 model.addListDataListener(this); 244 } 245 246 public DefaultListModel getDefaultListModel() { 247 return model; 248 } 249 250 public int getColumnCount() { 251 return 2; 252 } 253 254 public int getRowCount() { 255 return model.getSize(); 256 } 257 258 public String getColumnName(int column) { 259 if (column == 0) { 260 return NbBundle.getMessage(ClassPathUiSupport.class, "LBL_CustomizeLibraries_TableHeader_Library"); 261 } else { 262 return NbBundle.getMessage(ClassPathUiSupport.class, "LBL_CustomizeLibraries_TableHeader_Deploy"); 263 } 264 } 265 266 public Class getColumnClass(int columnIndex) { 267 if (columnIndex == 0) { 268 return ClassPathSupport.Item.class; 269 } else { 270 return Boolean .class; 271 } 272 } 273 274 public boolean isCellEditable(int rowIndex, int columnIndex) { 275 return columnIndex != 0 && getShowItemAsIncludedInDeployment(getItem(rowIndex)) instanceof Boolean ; 276 } 277 278 public Object getValueAt(int row, int column) { 279 ClassPathSupport.Item item = getItem(row); 280 if (column == 0) { 281 return item; 282 } else { 283 return getShowItemAsIncludedInDeployment(item); 284 } 285 } 286 287 public void setValueAt(Object value, int row, int column) { 288 if (column != 1 || !(value instanceof Boolean )) 289 return; 290 291 getItem(row).setIncludedInDeployment(value == Boolean.TRUE); 292 fireTableCellUpdated(row, column); 293 } 294 295 public void contentsChanged(ListDataEvent e) { 296 fireTableRowsUpdated(e.getIndex0(), e.getIndex1()); 297 } 298 299 public void intervalAdded(ListDataEvent e) { 300 fireTableRowsInserted(e.getIndex0(), e.getIndex1()); 301 } 302 303 public void intervalRemoved(ListDataEvent e) { 304 fireTableRowsDeleted(e.getIndex0(), e.getIndex1()); 305 } 306 307 private ClassPathSupport.Item getItem(int index) { 308 return (ClassPathSupport.Item)model.get(index); 309 } 310 311 private void setItem(ClassPathSupport.Item item, int index) { 312 model.set(index, item); 313 } 314 315 private Boolean getShowItemAsIncludedInDeployment(ClassPathSupport.Item item) { 316 Boolean result = Boolean.valueOf(item.isIncludedInDeployment()); 317 return result; 323 } 324 } 325 } 326 | Popular Tags |