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