KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > 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.ejbjarproject.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.ejbjarproject.classpath.ClassPathSupport;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.util.NbBundle;
39
40
41 /**
42  *
43  * @author Petr Hrebejk
44  */

45 public class ClassPathUiSupport {
46     
47     // private ClassPathSupport cps;
48

49     /** Creates a new instance of ClassPathSupport */
50     /*
51     public ClassPathUiSupport( PropertyEvaluator evaluator,
52                                 ReferenceHelper referenceHelper,
53                                 AntProjectHelper antProjectHelper,
54                                 String wellKnownPaths[],
55                                 String libraryPrefix,
56                                 String librarySuffix,
57                                 String antArtifactPrefix ) {
58         cps = new ClassPathSupport( evaluator, referenceHelper, antProjectHelper, wellKnownPaths, libraryPrefix, librarySuffix, antArtifactPrefix );
59     }
60      */

61         
62     // Methods for working with list models ------------------------------------
63

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

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

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

150     public static int[] remove( DefaultListModel JavaDoc listModel, int[] indices ) {
151         
152         if( indices == null || indices.length == 0 ) {
153             assert false : "Remove button should be disabled"; // NOI18N
154
}
155         
156         // Remove the items
157
for( int i = indices.length - 1 ; i >= 0 ; i-- ) {
158             listModel.remove( indices[i] );
159         }
160                 
161         if ( !listModel.isEmpty() ) {
162             // Select reasonable item
163
int selectedIndex = indices[indices.length - 1] - indices.length + 1;
164             if ( selectedIndex > listModel.size() - 1) {
165                 selectedIndex = listModel.size() - 1;
166             }
167             return new int[] { selectedIndex };
168         }
169         else {
170             return new int[] {};
171         }
172         
173     }
174     
175     public static int[] addLibraries( DefaultListModel JavaDoc listModel, int[] indices, Library[] libraries, Set JavaDoc/*<Library>*/ alreadyIncludedLibs, boolean includeInDeployment ) {
176         int lastIndex = indices == null || indices.length == 0 ? listModel.getSize() - 1 : indices[indices.length - 1];
177         for (int i = 0, j=1; i < libraries.length; i++) {
178             if (!alreadyIncludedLibs.contains(libraries[i])) {
179                 listModel.add( lastIndex + j++, ClassPathSupport.Item.create( libraries[i], null, includeInDeployment ) );
180             }
181         }
182         Set JavaDoc addedLibs = new HashSet JavaDoc (Arrays.asList(libraries));
183         int[] indexes = new int[libraries.length];
184         for (int i=0, j=0; i<listModel.getSize(); i++) {
185             ClassPathSupport.Item item = (ClassPathSupport.Item)listModel.get (i);
186             if (item.getType() == ClassPathSupport.Item.TYPE_LIBRARY && !item.isBroken() ) {
187                 if (addedLibs.contains(item.getLibrary())) {
188                     indexes[j++] =i;
189                 }
190             }
191         }
192         return indexes;
193     }
194
195     public static int[] addJarFiles( DefaultListModel JavaDoc listModel, int[] indices, File JavaDoc files[], boolean includeInDeployment ) {
196         int lastIndex = indices == null || indices.length == 0 ? listModel.getSize() - 1 : indices[indices.length - 1];
197         int[] indexes = new int[files.length];
198         for( int i = 0, delta = 0; i+delta < files.length; ) {
199             int current = lastIndex + 1 + i;
200             ClassPathSupport.Item item = ClassPathSupport.Item.create( files[i+delta], null, includeInDeployment );
201             if ( !listModel.contains( item ) ) {
202                 listModel.add( current, item );
203                 indexes[delta + i] = current;
204                 i++;
205             }
206             else {
207                 indexes[i + delta] = listModel.indexOf( item );
208                 delta++;
209             }
210         }
211         return indexes;
212
213     }
214     
215     public static int[] addArtifacts( DefaultListModel JavaDoc listModel, int[] indices, AntArtifactChooser.ArtifactItem artifactItems[], boolean includeInDeployment ) {
216         int lastIndex = indices == null || indices.length == 0 ? listModel.getSize() - 1 : indices[indices.length - 1];
217         int[] indexes = new int[artifactItems.length];
218         for( int i = 0; i < artifactItems.length; i++ ) {
219             int current = lastIndex + 1 + i;
220             ClassPathSupport.Item item = ClassPathSupport.Item.create( artifactItems[i].getArtifact(), artifactItems[i].getArtifactURI(), null, includeInDeployment ) ;
221             if ( !listModel.contains( item ) ) {
222                 listModel.add( current, item );
223                 indexes[i] = current;
224             }
225             else {
226                 indexes[i] = listModel.indexOf( item );
227             }
228         }
229         return indexes;
230     }
231     
232     // Inner classes -----------------------------------------------------------
233

234     /**
235      * Implements a TableModel backed up by a DefaultListModel.
236      * This allows the TableModel's data to be used in EditMediator
237      */

238     public static final class ClassPathTableModel extends AbstractTableModel JavaDoc implements ListDataListener JavaDoc {
239         private DefaultListModel JavaDoc model;
240         
241         public ClassPathTableModel(DefaultListModel JavaDoc model) {
242             this.model = model;
243             model.addListDataListener(this);
244         }
245         
246         public DefaultListModel JavaDoc getDefaultListModel() {
247             return model;
248         }
249         
250         public int getColumnCount() {
251             return 2;
252         }
253         
254         public int getRowCount() {
255             return model.getSize();
256         }
257         
258         public String JavaDoc getColumnName(int column) {
259             if (column == 0) {
260                 return NbBundle.getMessage(ClassPathUiSupport.class, "LBL_CustomizeLibraries_TableHeader_Library");
261             } else {
262                 return NbBundle.getMessage(ClassPathUiSupport.class, "LBL_CustomizeLibraries_TableHeader_Deploy");
263             }
264         }
265         
266         public Class JavaDoc getColumnClass(int columnIndex) {
267             if (columnIndex == 0) {
268                 return ClassPathSupport.Item.class;
269             } else {
270                 return Boolean JavaDoc.class;
271             }
272         }
273         
274         public boolean isCellEditable(int rowIndex, int columnIndex) {
275             return columnIndex != 0 && getShowItemAsIncludedInDeployment(getItem(rowIndex)) instanceof Boolean JavaDoc;
276         }
277         
278         public Object JavaDoc getValueAt(int row, int column) {
279             ClassPathSupport.Item item = getItem(row);
280             if (column == 0) {
281                 return item;
282             } else {
283                 return getShowItemAsIncludedInDeployment(item);
284             }
285         }
286         
287         public void setValueAt(Object JavaDoc value, int row, int column) {
288             if (column != 1 || !(value instanceof Boolean JavaDoc))
289                 return;
290             
291             getItem(row).setIncludedInDeployment(value == Boolean.TRUE);
292             fireTableCellUpdated(row, column);
293         }
294         
295         public void contentsChanged(ListDataEvent JavaDoc e) {
296             fireTableRowsUpdated(e.getIndex0(), e.getIndex1());
297         }
298         
299         public void intervalAdded(ListDataEvent JavaDoc e) {
300             fireTableRowsInserted(e.getIndex0(), e.getIndex1());
301         }
302         
303         public void intervalRemoved(ListDataEvent JavaDoc e) {
304             fireTableRowsDeleted(e.getIndex0(), e.getIndex1());
305         }
306         
307         private ClassPathSupport.Item getItem(int index) {
308             return (ClassPathSupport.Item)model.get(index);
309         }
310         
311         private void setItem(ClassPathSupport.Item item, int index) {
312             model.set(index, item);
313         }
314         
315         private Boolean JavaDoc getShowItemAsIncludedInDeployment(ClassPathSupport.Item item) {
316             Boolean JavaDoc result = Boolean.valueOf(item.isIncludedInDeployment());
317 // if (item.getType() == ClassPathSupport.Item.TYPE_JAR) {
318
// FileObject fo = FileUtil.toFileObject(item.getFile());
319
// if (fo == null || fo.isFolder())
320
// return null;
321
// }
322
return result;
323         }
324     }
325 }
326
Popular Tags