KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ui > customizer > WarIncludesUiSupport


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.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.swing.JTable JavaDoc;
32 import javax.swing.ListSelectionModel JavaDoc;
33 import javax.swing.table.AbstractTableModel JavaDoc;
34
35 import org.netbeans.api.project.libraries.Library;
36
37 import org.netbeans.modules.web.project.classpath.ClassPathSupport;
38
39 /**
40  *
41  * @author Petr Hrebejk, Radko Najman
42  */

43 public class WarIncludesUiSupport {
44     
45     static private Object JavaDoc[][] data;
46
47     // Methods for working with list models ------------------------------------
48

49     public static ClasspathTableModel createTableModel( List JavaDoc items ) {
50         
51         ClasspathTableModel model = new ClasspathTableModel();
52         
53         data = new Object JavaDoc[items.size()][2];
54         for (int i = 0; i < items.size(); i++) {
55             model.setValueAt((ClassPathSupport.Item) items.get(i), i, 0);
56             String JavaDoc pathInWAR = ((ClassPathSupport.Item) items.get(i)).getPathInWAR();
57             model.setValueAt(pathInWAR, i, 1);
58         }
59         
60         model.fireTableDataChanged();
61         
62         return model;
63     }
64     
65     public static List JavaDoc getList( ClasspathTableModel model ) {
66         List JavaDoc items= new LinkedList JavaDoc();
67         for (int i = 0; i < data.length; i++)
68             items.add(data[i][0]);
69         return items;
70     }
71     
72     public static Iterator JavaDoc getIterator( ClasspathTableModel model ) {
73         return getList(model).iterator();
74     }
75     
76     /** Removes selected indices from the model. Returns the index to be selected
77      */

78     public static void remove( JTable JavaDoc list ) {
79         ListSelectionModel JavaDoc sm = list.getSelectionModel();
80         int index = sm.getMinSelectionIndex();
81         if (sm.isSelectionEmpty()) {
82             assert false : "Remove button should be disabled"; // NOI18N
83
}
84         Collection JavaDoc elements = new ArrayList JavaDoc();
85         final int n0 = data.length;
86         for (int i = 0; i < n0; i++) {
87             if (!sm.isSelectedIndex(i)) {
88                 elements.add(data[i]);
89             }
90         }
91         final int n = elements.size();
92         data = (Object JavaDoc[][]) elements.toArray(new Object JavaDoc[n][2]);
93         ((ClasspathTableModel) list.getModel()).fireTableRowsDeleted(elements.size(), n0 - 1);
94
95         if (index >= n) {
96             index = n - 1;
97         }
98         sm.setSelectionInterval(index, index);
99     }
100     
101     public static void addLibraries(Library[] libraries, Set JavaDoc/*<Library>*/ alreadyIncludedLibs, JTable JavaDoc table) {
102         if (libraries.length > 0) {
103             List JavaDoc newLibList = new ArrayList JavaDoc(Arrays.asList(libraries));
104             table.clearSelection();
105             int n0 = data.length;
106             for (int i = 0; i < n0; i++) {
107                 ClassPathSupport.Item item = (ClassPathSupport.Item) data[i][0];
108                 if(item.getType() == ClassPathSupport.Item.TYPE_LIBRARY) {
109                     if(newLibList.remove(item.getObject()))
110                         table.addRowSelectionInterval(i, i);
111                 }
112             }
113             int n = newLibList.size();
114             if (n > 0) {
115                 Object JavaDoc[][] newData = new Object JavaDoc[n0 + n][2];
116                 for (int i = 0; i < n0; i++)
117                     newData[i] = data[i];
118                 for (int i = 0; i < n; i++) {
119                     newData[n0 + i][0] = ClassPathSupport.Item.create((Library) newLibList.get(i), null, ClassPathSupport.Item.PATH_IN_WAR_APPLET);
120                     newData[n0 + i][1] = ClassPathSupport.Item.PATH_IN_WAR_APPLET;
121                 }
122
123                 data = newData;
124                 ((ClasspathTableModel) table.getModel()).fireTableRowsInserted(n0, n0 + n - 1);
125                 table.addRowSelectionInterval(n0, n0 + n - 1);
126             }
127         }
128     }
129
130     public static void addJarFiles(File JavaDoc files[], ClasspathTableModel tableModel) {
131         Object JavaDoc[][] newData = new Object JavaDoc[data.length + files.length][2];
132         for (int i = 0; i < data.length; i++)
133             newData[i] = data[i];
134         for (int i = 0; i < files.length; i++) {
135             newData[data.length + i][0] = ClassPathSupport.Item.create (files[i], null, ClassPathSupport.Item.PATH_IN_WAR_APPLET);
136             newData[data.length + i][1] = ClassPathSupport.Item.PATH_IN_WAR_APPLET;
137         }
138         
139         data = newData;
140         tableModel.fireTableRowsInserted(data.length, data.length + files.length - 1);
141     }
142     
143     public static void addArtifacts(AntArtifactChooser.ArtifactItem artifactItems[], ClasspathTableModel tableModel) {
144         Object JavaDoc[][] newData = new Object JavaDoc[data.length + artifactItems.length][2];
145         for (int i = 0; i < data.length; i++)
146             newData[i] = data[i];
147         for (int i = 0; i < artifactItems.length; i++) {
148             newData[data.length + i][0] = ClassPathSupport.Item.create (artifactItems[i].getArtifact(), artifactItems[i].getArtifactURI(), null, ClassPathSupport.Item.PATH_IN_WAR_APPLET);
149             newData[data.length + i][1] = ClassPathSupport.Item.PATH_IN_WAR_APPLET;
150         }
151         
152         data = newData;
153         tableModel.fireTableRowsInserted(data.length, data.length + artifactItems.length - 1);
154     }
155     
156     static class ClasspathTableModel extends AbstractTableModel JavaDoc {
157         public int getColumnCount() {
158             return 2; //classpath item name, item location within WAR
159
}
160
161         public int getRowCount() {
162             if (data == null)
163                 return 0;
164             return data.length;
165         }
166
167         public Object JavaDoc getValueAt(int row, int col) {
168             return data[row][col];
169         }
170
171         public boolean isCellEditable(int row, int col) {
172             if (col == 1)
173                 return true;
174             else
175                 return false;
176         }
177
178         public void setValueAt(Object JavaDoc value, int row, int col) {
179             data[row][col] = value;
180             fireTableCellUpdated(row, col);
181         }
182     }
183
184 }
185
Popular Tags