KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > archive > wizard > WarSelector


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.archive.wizard;
25
26 // ToolBox
27
import org.enhydra.tool.archive.WebApplication;
28 import org.enhydra.tool.common.PathHandle;
29
30 // JDK
31
import java.awt.Component JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import javax.swing.JTable JavaDoc;
34 import javax.swing.table.AbstractTableModel JavaDoc;
35
36 //
37
public class WarSelector extends OneColumnFileSelector {
38     private JTable JavaDoc table = null;
39
40     public WarSelector() {
41         super();
42         warInit();
43     }
44
45     // override OneColumnFileSelector
46
protected void removeSelection() {
47         int index = table.getSelectedRow();
48
49         if ((index > -1) && (index < getTableModel().getRowCount())) {
50             getTableModel().removeRow(index);
51         }
52     }
53
54     // override OneColumnFileSelector
55
protected void addToModel(PathHandle path) {
56         int count = getTableModel().getRowCount();
57         ArrayList JavaDoc list = new ArrayList JavaDoc();
58
59         for (int i = 0; i < count; i++) {
60             list.add(getTableModel().getValueAt(i, 1).toString());
61         }
62         if (!list.contains(path.getPath())) {
63             WebApplication webApp = null;
64
65             webApp = new WebApplication(path.getPath());
66             getTableModel().addWebApplication(webApp);
67         }
68     }
69
70     protected void setWebApplications(WebApplication[] webApps) {
71         int count = getTableModel().getRowCount();
72
73         for (int i = 0; i < count; i++) {
74             getTableModel().removeRow(i);
75         }
76         for (int i = 0; i < webApps.length; i++) {
77             getTableModel().addWebApplication(webApps[i]);
78         }
79     }
80
81     protected WebApplication[] getWebApplications() {
82         int count = getTableModel().getRowCount();
83         WebApplication[] webApps = new WebApplication[count];
84
85         for (int i = 0; i < count; i++) {
86             webApps[i] = new WebApplication(getTableModel().getValueAt(i,
87                     1).toString());
88             webApps[i].setContextRoot(getTableModel().getValueAt(i,
89                     0).toString());
90         }
91         return webApps;
92     }
93
94     private void warInit() {
95         Component JavaDoc[] comps = getScrollPane().getViewport().getComponents();
96
97         for (int i = 0; i < comps.length; i++) {
98             getScrollPane().getViewport().remove(comps[i]);
99         }
100         table = new JTable JavaDoc();
101         table.setModel(new WarTableModel());
102         getScrollPane().getViewport().add(table, null);
103         table.getColumnModel().getColumn(0).setPreferredWidth(40);
104         table.getColumnModel().getColumn(1).setPreferredWidth(250);
105         getTableModel().addTableModelListener(table);
106     }
107
108     private WarTableModel getTableModel() {
109         return (WarTableModel) table.getModel();
110     }
111
112     private class WarTableModel extends AbstractTableModel JavaDoc {
113         ArrayList JavaDoc tableList = new ArrayList JavaDoc();
114
115         public WarTableModel() {
116             super();
117         }
118
119         public String JavaDoc getColumnName(int column) {
120             String JavaDoc cn = "Unknown";
121
122             switch (column) {
123             case 0:
124                 cn = "Context Root";
125                 break;
126             case 1:
127                 cn = "Web Application";
128                 break;
129             }
130             return cn;
131         }
132
133         public void removeRow(int index) {
134             tableList.remove(index);
135             fireTableRowsDeleted(index, index);
136         }
137
138         public Object JavaDoc getValueAt(int row, int column) {
139             WebApplication webApp = (WebApplication) tableList.get(row);
140             Object JavaDoc value = "Unknown";
141
142             switch (column) {
143             case 0:
144                 value = webApp.getContextRoot();
145                 break;
146             case 1:
147                 value = webApp.getArchive();
148                 break;
149             }
150             return value;
151         }
152
153         public void setValueAt(Object JavaDoc value, int row, int column) {
154             WebApplication webApp = (WebApplication) tableList.get(row);
155
156             switch (column) {
157             case 0:
158                 webApp.setContextRoot(value.toString());
159                 break;
160             case 1:
161                 webApp.setArchive(value.toString());
162                 break;
163             }
164         }
165
166
167         public int getColumnCount() {
168             return 2;
169         }
170
171         public boolean isCellEditable(int row, int col) {
172             return (col == 0);
173         }
174
175         public int getRowCount() {
176             return tableList.size();
177         }
178
179         protected void addWebApplication(WebApplication webApp) {
180             int index = tableList.size();
181
182             tableList.add(webApp);
183             fireTableRowsInserted(index, index);
184         }
185
186     }
187 }
188
Popular Tags