KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > 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.web.project.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
30 import javax.swing.DefaultListModel JavaDoc;
31 import javax.swing.ListSelectionModel JavaDoc;
32 import javax.swing.event.ListDataEvent JavaDoc;
33 import javax.swing.event.ListDataListener JavaDoc;
34 import javax.swing.table.AbstractTableModel JavaDoc;
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 /**
43  *
44  * @author Petr Hrebejk, Radko Najman
45  */

46 public class ClassPathUiSupport {
47     
48     // Methods for working with list models ------------------------------------
49

50     public static DefaultListModel JavaDoc createListModel( Iterator JavaDoc it ) {
51         
52         DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
53         
54         while( it.hasNext() ) {
55             model.addElement( it.next() );
56         }
57         
58         return model;
59     }
60     
61     public static ClassPathTableModel createTableModel ( Iterator JavaDoc it ) {
62         return new ClassPathTableModel( createListModel( it ) );
63     }
64     
65     public static Iterator JavaDoc getIterator( DefaultListModel JavaDoc model ) {
66         // XXX Better performing impl. would be nice
67
return getList( model ).iterator();
68     }
69     
70     public static List JavaDoc getList( DefaultListModel JavaDoc model ) {
71         return Collections.list( model.elements() );
72     }
73         
74     
75     /** Moves items up in the list. The indices array will contain
76      * indices to be selected after the change was done.
77      */

78     public static int[] moveUp( DefaultListModel JavaDoc listModel, int indices[]) {
79                 
80         if( indices == null || indices.length == 0 ) {
81             assert false : "MoveUp button should be disabled"; // NOI18N
82
}
83         
84         // Move the items up
85
for( int i = 0; i < indices.length; i++ ) {
86             Object JavaDoc item = listModel.get( indices[i] );
87             listModel.remove( indices[i] );
88             listModel.add( indices[i] - 1, item );
89         }
90         
91         // Keep the selection a before
92
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 JavaDoc selectionModel ) {
101         return selectionModel.getMinSelectionIndex() > 0;
102     }
103     
104     /** Moves items down in the list. The indices array will contain
105      * indices to be selected after the change was done.
106      */

107     public static int[] moveDown( DefaultListModel JavaDoc listModel, int indices[]) {
108         
109         if( indices == null || indices.length == 0 ) {
110             assert false : "MoveDown button should be disabled"; // NOI18N
111
}
112         
113         // Move the items up
114
for( int i = indices.length -1 ; i >= 0 ; i-- ) {
115             Object JavaDoc item = listModel.get( indices[i] );
116             listModel.remove( indices[i] );
117             listModel.add( indices[i] + 1, item );
118         }
119         
120         // Keep the selection a before
121
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 JavaDoc selectionModel, int modelSize ) {
130         int iMax = selectionModel.getMaxSelectionIndex();
131         return iMax != -1 && iMax < modelSize - 1;
132     }
133     
134     /** Removes selected indices from the model. Returns the index to be selected
135      */

136     public static int[] remove( DefaultListModel JavaDoc listModel, int[] indices ) {
137         
138         if( indices == null || indices.length == 0 ) {
139             assert false : "Remove button should be disabled"; // NOI18N
140
}
141         
142         // Remove the items
143
for( int i = indices.length - 1 ; i >= 0 ; i-- ) {
144             listModel.remove( indices[i] );
145         }
146                 
147         if ( !listModel.isEmpty() ) {
148             // Select reasonable item
149
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 JavaDoc listModel, int[] indices, Library[] libraries, Set JavaDoc/*<Library>*/ 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 JavaDoc addedLibs = new HashSet JavaDoc (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 JavaDoc listModel, int[] indices, File JavaDoc 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 JavaDoc f = files[i+delta];
187             String JavaDoc 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 JavaDoc 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     // Inner classes -----------------------------------------------------------
221

222     /**
223      * Implements a TableModel backed up by a DefaultListModel.
224      * This allows the TableModel's data to be used in EditMediator
225      */

226     public static final class ClassPathTableModel extends AbstractTableModel JavaDoc implements ListDataListener JavaDoc {
227         private DefaultListModel JavaDoc model;
228         
229         public ClassPathTableModel(DefaultListModel JavaDoc model) {
230             this.model = model;
231             model.addListDataListener(this);
232         }
233         
234         public DefaultListModel JavaDoc 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 JavaDoc 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 JavaDoc getColumnClass(int columnIndex) {
255             if (columnIndex == 0) {
256                 return ClassPathSupport.Item.class;
257             } else {
258                 return Boolean JavaDoc.class;
259             }
260         }
261         
262         public boolean isCellEditable(int rowIndex, int columnIndex) {
263             return (columnIndex != 0);
264         }
265         
266         public Object JavaDoc getValueAt(int row, int column) {
267             if (column == 0)
268                 return getItem(row);
269             else {
270                 String JavaDoc 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 JavaDoc value, int row, int column) {
276             if (column != 1 || !(value instanceof Boolean JavaDoc))
277                 return;
278             
279             if (value == Boolean.TRUE) {
280                 ClassPathSupport.Item item = getItem(row);
281                 String JavaDoc 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 JavaDoc e) {
289             fireTableRowsUpdated(e.getIndex0(), e.getIndex1());
290         }
291         
292         public void intervalAdded(ListDataEvent JavaDoc e) {
293             fireTableRowsInserted(e.getIndex0(), e.getIndex1());
294         }
295         
296         public void intervalRemoved(ListDataEvent JavaDoc 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