KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > 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.j2ee.clientproject.ui.customizer;
21
22 import java.io.File JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.DefaultListModel JavaDoc;
30 import javax.swing.ListSelectionModel JavaDoc;
31 import javax.swing.event.ListDataEvent JavaDoc;
32 import javax.swing.event.ListDataListener JavaDoc;
33 import javax.swing.table.AbstractTableModel JavaDoc;
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 /**
39  *
40  * @author Petr Hrebejk
41  */

42 public class ClassPathUiSupport {
43     
44     private ClassPathSupport cps;
45     
46     /** Creates a new instance of ClassPathSupport */
47     /*
48     public ClassPathUiSupport( PropertyEvaluator evaluator,
49                                 ReferenceHelper referenceHelper,
50                                 AntProjectHelper antProjectHelper,
51                                 String wellKnownPaths[],
52                                 String libraryPrefix,
53                                 String librarySuffix,
54                                 String antArtifactPrefix ) {
55         cps = new ClassPathSupport( evaluator, referenceHelper, antProjectHelper, wellKnownPaths, libraryPrefix, librarySuffix, antArtifactPrefix );
56     }
57      */

58     
59     // Methods for working with list models ------------------------------------
60

61     public static DefaultListModel JavaDoc createListModel( Iterator JavaDoc it ) {
62         
63         DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
64         
65         while( it.hasNext() ) {
66             model.addElement( it.next() );
67         }
68         
69         return model;
70     }
71     
72     public static ClassPathTableModel createTableModel( Iterator JavaDoc it ) {
73         return new ClassPathTableModel( createListModel( it ) );
74     }
75     
76     
77     public static Iterator JavaDoc getIterator( DefaultListModel JavaDoc model ) {
78         // XXX Better performing impl. would be nice
79
return getList( model ).iterator();
80     }
81     
82     @SuppressWarnings JavaDoc("unchecked")
83     public static List JavaDoc<ClassPathSupport.Item> getList( DefaultListModel JavaDoc model ) {
84         return (List JavaDoc<ClassPathSupport.Item>)Collections.list( model.elements() );
85     }
86     
87     
88     /** Moves items up in the list. The indices array will contain
89      * indices to be selected after the change was done.
90      */

91     public static int[] moveUp( DefaultListModel JavaDoc listModel, int indices[]) {
92         
93         if( indices == null || indices.length == 0 ) {
94             assert false : "MoveUp button should be disabled"; // NOI18N
95
}
96         
97         // Move the items up
98
for( int i = 0; i < indices.length; i++ ) {
99             Object JavaDoc item = listModel.get( indices[i] );
100             listModel.remove( indices[i] );
101             listModel.add( indices[i] - 1, item );
102         }
103         
104         // Keep the selection a before
105
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 JavaDoc selectionModel ) {
114         return selectionModel.getMinSelectionIndex() > 0;
115     }
116     
117     /** Moves items down in the list. The indices array will contain
118      * indices to be selected after the change was done.
119      */

120     public static int[] moveDown( DefaultListModel JavaDoc listModel, int indices[]) {
121         
122         if( indices == null || indices.length == 0 ) {
123             assert false : "MoveDown button should be disabled"; // NOI18N
124
}
125         
126         // Move the items up
127
for( int i = indices.length -1 ; i >= 0 ; i-- ) {
128             Object JavaDoc item = listModel.get( indices[i] );
129             listModel.remove( indices[i] );
130             listModel.add( indices[i] + 1, item );
131         }
132         
133         // Keep the selection a before
134
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 JavaDoc selectionModel, int modelSize ) {
143         int iMax = selectionModel.getMaxSelectionIndex();
144         return iMax != -1 && iMax < modelSize - 1;
145     }
146     
147     /** Removes selected indices from the model. Returns the index to be selected
148      */

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

231     /**
232      * Implements a TableModel backed up by a DefaultListModel.
233      * This allows the TableModel's data to be used in EditMediator
234      */

235     public static final class ClassPathTableModel extends AbstractTableModel JavaDoc implements ListDataListener JavaDoc {
236         private DefaultListModel JavaDoc model;
237         
238         public ClassPathTableModel(DefaultListModel JavaDoc model) {
239             this.model = model;
240             model.addListDataListener(this);
241         }
242         
243         public DefaultListModel JavaDoc 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 JavaDoc 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 JavaDoc getColumnClass(int columnIndex) {
264             if (columnIndex == 0) {
265                 return ClassPathSupport.Item.class;
266             } else {
267                 return Boolean JavaDoc.class;
268             }
269         }
270         
271         public boolean isCellEditable(int rowIndex, int columnIndex) {
272             return columnIndex != 0 && getShowItemAsIncludedInDeployment(getItem(rowIndex)) instanceof Boolean JavaDoc;
273         }
274         
275         public Object JavaDoc 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 JavaDoc value, int row, int column) {
285             if (column != 1 || !(value instanceof Boolean JavaDoc))
286                 return;
287             
288             getItem(row).setIncludedInDeployment(value == Boolean.TRUE);
289             fireTableCellUpdated(row, column);
290         }
291         
292         public void contentsChanged(ListDataEvent JavaDoc e) {
293             fireTableRowsUpdated(e.getIndex0(), e.getIndex1());
294         }
295         
296         public void intervalAdded(ListDataEvent JavaDoc e) {
297             fireTableRowsInserted(e.getIndex0(), e.getIndex1());
298         }
299         
300         public void intervalRemoved(ListDataEvent JavaDoc 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 JavaDoc getShowItemAsIncludedInDeployment(ClassPathSupport.Item item) {
313             Boolean JavaDoc result = Boolean.valueOf(item.isIncludedInDeployment());
314             // if (item.getType() == ClassPathSupport.Item.TYPE_JAR) {
315
// FileObject fo = FileUtil.toFileObject(item.getFile());
316
// if (fo == null || fo.isFolder())
317
// return null;
318
// }
319
return result;
320         }
321     }
322     
323 }
324
Popular Tags