1 19 20 package org.netbeans.modules.web.project.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 30 import javax.swing.DefaultListModel ; 31 import javax.swing.ListSelectionModel ; 32 import javax.swing.event.ListDataEvent ; 33 import javax.swing.event.ListDataListener ; 34 import javax.swing.table.AbstractTableModel ; 35 36 import org.openide.util.NbBundle; 37 38 import org.netbeans.api.project.libraries.Library; 39 40 import org.netbeans.modules.web.project.classpath.ClassPathSupport; 41 42 46 public class ClassPathUiSupport { 47 48 50 public static DefaultListModel createListModel( Iterator it ) { 51 52 DefaultListModel model = new DefaultListModel (); 53 54 while( it.hasNext() ) { 55 model.addElement( it.next() ); 56 } 57 58 return model; 59 } 60 61 public static ClassPathTableModel createTableModel ( Iterator it ) { 62 return new ClassPathTableModel( createListModel( it ) ); 63 } 64 65 public static Iterator getIterator( DefaultListModel model ) { 66 return getList( model ).iterator(); 68 } 69 70 public static List getList( DefaultListModel model ) { 71 return Collections.list( model.elements() ); 72 } 73 74 75 78 public static int[] moveUp( DefaultListModel listModel, int indices[]) { 79 80 if( indices == null || indices.length == 0 ) { 81 assert false : "MoveUp button should be disabled"; } 83 84 for( int i = 0; i < indices.length; i++ ) { 86 Object item = listModel.get( indices[i] ); 87 listModel.remove( indices[i] ); 88 listModel.add( indices[i] - 1, item ); 89 } 90 91 for( int i = 0; i < indices.length; i++ ) { 93 indices[i] -= 1; 94 } 95 96 return indices; 97 98 } 99 100 public static boolean canMoveUp( ListSelectionModel selectionModel ) { 101 return selectionModel.getMinSelectionIndex() > 0; 102 } 103 104 107 public static int[] moveDown( DefaultListModel listModel, int indices[]) { 108 109 if( indices == null || indices.length == 0 ) { 110 assert false : "MoveDown button should be disabled"; } 112 113 for( int i = indices.length -1 ; i >= 0 ; i-- ) { 115 Object item = listModel.get( indices[i] ); 116 listModel.remove( indices[i] ); 117 listModel.add( indices[i] + 1, item ); 118 } 119 120 for( int i = 0; i < indices.length; i++ ) { 122 indices[i] += 1; 123 } 124 125 return indices; 126 127 } 128 129 public static boolean canMoveDown( ListSelectionModel selectionModel, int modelSize ) { 130 int iMax = selectionModel.getMaxSelectionIndex(); 131 return iMax != -1 && iMax < modelSize - 1; 132 } 133 134 136 public static int[] remove( DefaultListModel listModel, int[] indices ) { 137 138 if( indices == null || indices.length == 0 ) { 139 assert false : "Remove button should be disabled"; } 141 142 for( int i = indices.length - 1 ; i >= 0 ; i-- ) { 144 listModel.remove( indices[i] ); 145 } 146 147 if ( !listModel.isEmpty() ) { 148 int selectedIndex = indices[indices.length - 1] - indices.length + 1; 150 if ( selectedIndex > listModel.size() - 1) { 151 selectedIndex = listModel.size() - 1; 152 } 153 return new int[] { selectedIndex }; 154 } 155 else { 156 return new int[] {}; 157 } 158 159 } 160 161 public static int[] addLibraries( DefaultListModel listModel, int[] indices, Library[] libraries, Set alreadyIncludedLibs) { 162 int lastIndex = indices == null || indices.length == 0 ? listModel.getSize() - 1 : indices[indices.length - 1]; 163 for (int i = 0, j=1; i < libraries.length; i++) { 164 if (!alreadyIncludedLibs.contains(libraries[i])) { 165 listModel.add( lastIndex + j++, ClassPathSupport.Item.create( libraries[i], null, ClassPathSupport.Item.PATH_IN_WAR_LIB) ); 166 } 167 } 168 Set addedLibs = new HashSet (Arrays.asList(libraries)); 169 int[] indexes = new int[libraries.length]; 170 for (int i=0, j=0; i<listModel.getSize(); i++) { 171 ClassPathSupport.Item item = (ClassPathSupport.Item)listModel.get (i); 172 if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY && !item.isBroken() ) { 173 if (addedLibs.contains(item.getLibrary())) { 174 indexes[j++] =i; 175 } 176 } 177 } 178 return indexes; 179 } 180 181 public static int[] addJarFiles( DefaultListModel listModel, int[] indices, File files[]) { 182 int lastIndex = indices == null || indices.length == 0 ? listModel.getSize() - 1 : indices[indices.length - 1]; 183 int[] indexes = new int[files.length]; 184 for( int i = 0, delta = 0; i+delta < files.length; ) { 185 int current = lastIndex + 1 + i; 186 File f = files[i+delta]; 187 String pathInWar = (f.isDirectory() ? ClassPathSupport.Item.PATH_IN_WAR_DIR : ClassPathSupport.Item.PATH_IN_WAR_LIB); 188 ClassPathSupport.Item item = ClassPathSupport.Item.create( f, null, pathInWar); 189 if ( !listModel.contains( item ) ) { 190 listModel.add( current, item ); 191 indexes[delta + i] = current; 192 i++; 193 } 194 else { 195 indexes[i + delta] = listModel.indexOf( item ); 196 delta++; 197 } 198 } 199 return indexes; 200 201 } 202 203 public static int[] addArtifacts( DefaultListModel listModel, int[] indices, AntArtifactChooser.ArtifactItem artifactItems[]) { 204 int lastIndex = indices == null || indices.length == 0 ? listModel.getSize() - 1 : indices[indices.length - 1]; 205 int[] indexes = new int[artifactItems.length]; 206 for( int i = 0; i < artifactItems.length; i++ ) { 207 int current = lastIndex + 1 + i; 208 ClassPathSupport.Item item = ClassPathSupport.Item.create( artifactItems[i].getArtifact(), artifactItems[i].getArtifactURI(), null, ClassPathSupport.Item.PATH_IN_WAR_LIB) ; 209 if ( !listModel.contains( item ) ) { 210 listModel.add( current, item ); 211 indexes[i] = current; 212 } 213 else { 214 indexes[i] = listModel.indexOf( item ); 215 } 216 } 217 return indexes; 218 } 219 220 222 226 public static final class ClassPathTableModel extends AbstractTableModel implements ListDataListener { 227 private DefaultListModel model; 228 229 public ClassPathTableModel(DefaultListModel model) { 230 this.model = model; 231 model.addListDataListener(this); 232 } 233 234 public DefaultListModel getDefaultListModel() { 235 return model; 236 } 237 238 public int getColumnCount() { 239 return 2; 240 } 241 242 public int getRowCount() { 243 return model.getSize(); 244 } 245 246 public String getColumnName(int column) { 247 if (column == 0) { 248 return NbBundle.getMessage(ClassPathUiSupport.class, "LBL_CustomizeCompile_TableHeader_Name"); 249 } else { 250 return NbBundle.getMessage(ClassPathUiSupport.class, "LBL_CustomizeCompile_TableHeader_Deploy"); 251 } 252 } 253 254 public Class getColumnClass(int columnIndex) { 255 if (columnIndex == 0) { 256 return ClassPathSupport.Item.class; 257 } else { 258 return Boolean .class; 259 } 260 } 261 262 public boolean isCellEditable(int rowIndex, int columnIndex) { 263 return (columnIndex != 0); 264 } 265 266 public Object getValueAt(int row, int column) { 267 if (column == 0) 268 return getItem(row); 269 else { 270 String pathInWar = getItem(row).getPathInWAR(); 271 return (ClassPathSupport.Item.PATH_IN_WAR_LIB.equals(pathInWar) || ClassPathSupport.Item.PATH_IN_WAR_DIR.equals(pathInWar)) ? Boolean.TRUE : Boolean.FALSE; 272 } 273 } 274 275 public void setValueAt(Object value, int row, int column) { 276 if (column != 1 || !(value instanceof Boolean )) 277 return; 278 279 if (value == Boolean.TRUE) { 280 ClassPathSupport.Item item = getItem(row); 281 String pathInWar = (item.getType() == ClassPathSupport.Item.TYPE_JAR && item.getFile().isDirectory()) ? ClassPathSupport.Item.PATH_IN_WAR_DIR : ClassPathSupport.Item.PATH_IN_WAR_LIB; 282 item.setPathInWAR(pathInWar); 283 } else 284 getItem(row).setPathInWAR(ClassPathSupport.Item.PATH_IN_WAR_NONE); 285 fireTableCellUpdated(row, column); 286 } 287 288 public void contentsChanged(ListDataEvent e) { 289 fireTableRowsUpdated(e.getIndex0(), e.getIndex1()); 290 } 291 292 public void intervalAdded(ListDataEvent e) { 293 fireTableRowsInserted(e.getIndex0(), e.getIndex1()); 294 } 295 296 public void intervalRemoved(ListDataEvent e) { 297 fireTableRowsDeleted(e.getIndex0(), e.getIndex1()); 298 } 299 300 private ClassPathSupport.Item getItem(int index) { 301 return (ClassPathSupport.Item)model.get(index); 302 } 303 } 304 } 305 | Popular Tags |