KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > ui > customizer > ClassPathUiSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.j2seproject.ui.customizer;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.beans.BeanInfo JavaDoc;
25 import java.io.File JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.swing.ButtonModel JavaDoc;
33 import javax.swing.DefaultListCellRenderer JavaDoc;
34 import javax.swing.DefaultListModel JavaDoc;
35 import javax.swing.Icon JavaDoc;
36 import javax.swing.ImageIcon JavaDoc;
37 import javax.swing.JList JavaDoc;
38 import javax.swing.ListCellRenderer JavaDoc;
39 import javax.swing.ListSelectionModel JavaDoc;
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 /**
51  *
52  * @author Petr Hrebejk
53  */

54 public class ClassPathUiSupport {
55     
56     private ClassPathSupport cps;
57      
58     /** Creates a new instance of ClassPathSupport */
59     /*
60     public ClassPathUiSupport( PropertyEvaluator evaluator,
61                                 ReferenceHelper referenceHelper,
62                                 AntProjectHelper antProjectHelper,
63                                 String wellKnownPaths[],
64                                 String libraryPrefix,
65                                 String librarySuffix,
66                                 String antArtifactPrefix ) {
67         cps = new ClassPathSupport( evaluator, referenceHelper, antProjectHelper, wellKnownPaths, libraryPrefix, librarySuffix, antArtifactPrefix );
68     }
69      */

70         
71     // Methods for working with list models ------------------------------------
72

73     public static DefaultListModel JavaDoc createListModel( Iterator JavaDoc it ) {
74         
75         DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
76         
77         while( it.hasNext() ) {
78             model.addElement( it.next() );
79         }
80         
81         return model;
82     }
83     
84     public static Iterator JavaDoc getIterator( DefaultListModel JavaDoc model ) {
85         // XXX Better performing impl. would be nice
86
return getList( model ).iterator();
87     }
88     
89     public static List JavaDoc getList( DefaultListModel JavaDoc model ) {
90         return Collections.list( model.elements() );
91     }
92         
93     
94     /** Moves items up in the list. The indices array will contain
95      * indices to be selected after the change was done.
96      */

97     public static int[] moveUp( DefaultListModel JavaDoc listModel, int indices[]) {
98                 
99         if( indices == null || indices.length == 0 ) {
100             assert false : "MoveUp button should be disabled"; // NOI18N
101
}
102         
103         // Move the items up
104
for( int i = 0; i < indices.length; i++ ) {
105             Object JavaDoc item = listModel.get( indices[i] );
106             listModel.remove( indices[i] );
107             listModel.add( indices[i] - 1, item );
108         }
109         
110         // Keep the selection a before
111
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 JavaDoc selectionModel ) {
120         return selectionModel.getMinSelectionIndex() > 0;
121     }
122     
123     /** Moves items down in the list. The indices array will contain
124      * indices to be selected after the change was done.
125      */

126     public static int[] moveDown( DefaultListModel JavaDoc listModel, int indices[]) {
127         
128         if( indices == null || indices.length == 0 ) {
129             assert false : "MoveDown button should be disabled"; // NOI18N
130
}
131         
132         // Move the items up
133
for( int i = indices.length -1 ; i >= 0 ; i-- ) {
134             Object JavaDoc item = listModel.get( indices[i] );
135             listModel.remove( indices[i] );
136             listModel.add( indices[i] + 1, item );
137         }
138         
139         // Keep the selection a before
140
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 JavaDoc selectionModel, int modelSize ) {
149         int iMax = selectionModel.getMaxSelectionIndex();
150         return iMax != -1 && iMax < modelSize - 1;
151     }
152     
153     /** Removes selected indices from the model. Returns the index to be selected
154      */

155     public static int[] remove( DefaultListModel JavaDoc listModel, int[] indices ) {
156         
157         if( indices == null || indices.length == 0 ) {
158             assert false : "Remove button should be disabled"; // NOI18N
159
}
160         
161         // Remove the items
162
for( int i = indices.length - 1 ; i >= 0 ; i-- ) {
163             listModel.remove( indices[i] );
164         }
165                 
166         if ( !listModel.isEmpty() ) {
167             // Select reasonable item
168
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 JavaDoc listModel, int[] indices, Library[] libraries, Set JavaDoc/*<Library>*/ 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 JavaDoc addedLibs = new HashSet JavaDoc (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 JavaDoc listModel, int[] indices, File JavaDoc 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 JavaDoc 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