1 19 20 package org.netbeans.modules.java.j2seproject.ui.customizer; 21 22 import java.awt.Component ; 23 import java.awt.event.ActionListener ; 24 import java.beans.BeanInfo ; 25 import java.io.File ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Set ; 32 import javax.swing.ButtonModel ; 33 import javax.swing.DefaultListCellRenderer ; 34 import javax.swing.DefaultListModel ; 35 import javax.swing.Icon ; 36 import javax.swing.ImageIcon ; 37 import javax.swing.JList ; 38 import javax.swing.ListCellRenderer ; 39 import javax.swing.ListSelectionModel ; 40 import org.netbeans.api.project.libraries.Library; 41 import org.netbeans.modules.java.j2seproject.classpath.ClassPathSupport; 42 import org.netbeans.spi.project.support.ant.AntProjectHelper; 43 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 44 import org.netbeans.spi.project.support.ant.ReferenceHelper; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.Repository; 47 import org.openide.loaders.DataFolder; 48 import org.openide.util.Utilities; 49 50 54 public class ClassPathUiSupport { 55 56 private ClassPathSupport cps; 57 58 59 70 71 73 public static DefaultListModel createListModel( Iterator it ) { 74 75 DefaultListModel model = new DefaultListModel (); 76 77 while( it.hasNext() ) { 78 model.addElement( it.next() ); 79 } 80 81 return model; 82 } 83 84 public static Iterator getIterator( DefaultListModel model ) { 85 return getList( model ).iterator(); 87 } 88 89 public static List getList( DefaultListModel model ) { 90 return Collections.list( model.elements() ); 91 } 92 93 94 97 public static int[] moveUp( DefaultListModel listModel, int indices[]) { 98 99 if( indices == null || indices.length == 0 ) { 100 assert false : "MoveUp button should be disabled"; } 102 103 for( int i = 0; i < indices.length; i++ ) { 105 Object item = listModel.get( indices[i] ); 106 listModel.remove( indices[i] ); 107 listModel.add( indices[i] - 1, item ); 108 } 109 110 for( int i = 0; i < indices.length; i++ ) { 112 indices[i] -= 1; 113 } 114 115 return indices; 116 117 } 118 119 public static boolean canMoveUp( ListSelectionModel selectionModel ) { 120 return selectionModel.getMinSelectionIndex() > 0; 121 } 122 123 126 public static int[] moveDown( DefaultListModel listModel, int indices[]) { 127 128 if( indices == null || indices.length == 0 ) { 129 assert false : "MoveDown button should be disabled"; } 131 132 for( int i = indices.length -1 ; i >= 0 ; i-- ) { 134 Object item = listModel.get( indices[i] ); 135 listModel.remove( indices[i] ); 136 listModel.add( indices[i] + 1, item ); 137 } 138 139 for( int i = 0; i < indices.length; i++ ) { 141 indices[i] += 1; 142 } 143 144 return indices; 145 146 } 147 148 public static boolean canMoveDown( ListSelectionModel selectionModel, int modelSize ) { 149 int iMax = selectionModel.getMaxSelectionIndex(); 150 return iMax != -1 && iMax < modelSize - 1; 151 } 152 153 155 public static int[] remove( DefaultListModel listModel, int[] indices ) { 156 157 if( indices == null || indices.length == 0 ) { 158 assert false : "Remove button should be disabled"; } 160 161 for( int i = indices.length - 1 ; i >= 0 ; i-- ) { 163 listModel.remove( indices[i] ); 164 } 165 166 if ( !listModel.isEmpty() ) { 167 int selectedIndex = indices[indices.length - 1] - indices.length + 1; 169 if ( selectedIndex > listModel.size() - 1) { 170 selectedIndex = listModel.size() - 1; 171 } 172 return new int[] { selectedIndex }; 173 } 174 else { 175 return new int[] {}; 176 } 177 178 } 179 180 public static int[] addLibraries( DefaultListModel listModel, int[] indices, Library[] libraries, Set alreadyIncludedLibs ) { 181 182 int lastIndex = indices == null || indices.length == 0 ? listModel.getSize() - 1 : indices[indices.length - 1]; 183 for (int i = 0, j=1; i < libraries.length; i++) { 184 if (!alreadyIncludedLibs.contains(libraries[i])) { 185 listModel.add( lastIndex + j++, ClassPathSupport.Item.create( libraries[i], null ) ); 186 } 187 } 188 Set addedLibs = new HashSet (Arrays.asList(libraries)); 189 int[] indexes = new int[libraries.length]; 190 for (int i=0, j=0; i<listModel.getSize(); i++) { 191 ClassPathSupport.Item item = (ClassPathSupport.Item)listModel.get (i); 192 if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY && !item.isBroken() ) { 193 if (addedLibs.contains(item.getLibrary())) { 194 indexes[j++] =i; 195 } 196 } 197 } 198 return indexes; 199 } 200 201 public static int[] addJarFiles( DefaultListModel listModel, int[] indices, File files[] ) { 202 203 int lastIndex = indices == null || indices.length == 0 ? listModel.getSize() - 1 : indices[indices.length - 1]; 204 int[] indexes = new int[files.length]; 205 for( int i = 0, delta = 0; i+delta < files.length; ) { 206 int current = lastIndex + 1 + i; 207 ClassPathSupport.Item item = ClassPathSupport.Item.create( files[i+delta], null ); 208 if ( !listModel.contains( item ) ) { 209 listModel.add( current, item ); 210 indexes[delta + i] = current; 211 i++; 212 } 213 else { 214 indexes[i + delta] = listModel.indexOf( item ); 215 delta++; 216 } 217 } 218 return indexes; 219 220 } 221 222 public static int[] addArtifacts( DefaultListModel listModel, int[] indices, AntArtifactChooser.ArtifactItem artifactItems[] ) { 223 224 int lastIndex = indices == null || indices.length == 0 ? listModel.getSize() - 1 : indices[indices.length - 1]; 225 int[] indexes = new int[artifactItems.length]; 226 for( int i = 0; i < artifactItems.length; i++ ) { 227 int current = lastIndex + 1 + i; 228 ClassPathSupport.Item item = ClassPathSupport.Item.create( artifactItems[i].getArtifact(), artifactItems[i].getArtifactURI(), null ) ; 229 if ( !listModel.contains( item ) ) { 230 listModel.add( current, item ); 231 indexes[i] = current; 232 } 233 else { 234 indexes[i] = listModel.indexOf( item ); 235 } 236 } 237 return indexes; 238 } 239 240 241 242 } 243 | Popular Tags |