KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > deployer > ArchiveTablePanel


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

23 package org.enhydra.kelp.common.deployer;
24
25 // ToolBox
26
import org.enhydra.tool.archive.ArchiveException;
27 import org.enhydra.tool.archive.JarPlan;
28 import org.enhydra.tool.archive.wizard.ArchiveWizard;
29 import org.enhydra.tool.common.PathHandle;
30
31 // JDK
32
import java.awt.*;
33 import java.awt.event.*;
34 import java.beans.*;
35 import java.util.EventObject JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Arrays JavaDoc;
38 import javax.swing.*;
39 import javax.swing.event.*;
40 import javax.swing.event.CellEditorListener JavaDoc;
41 import javax.swing.table.TableCellEditor JavaDoc;
42 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
43 import javax.swing.table.DefaultTableModel JavaDoc;
44
45 //
46
public class ArchiveTablePanel extends JPanel {
47     private BorderLayout layoutMain;
48     private JScrollPane scroll;
49     private JTable table;
50     private String JavaDoc defWorkRoot = null;
51
52     public ArchiveTablePanel() {
53         try {
54             jbInit();
55             pmInit();
56         } catch (Exception JavaDoc ex) {
57             ex.printStackTrace();
58         }
59     }
60
61     //
62
protected void clearAll() {
63         table.getModel().removeTableModelListener(table);
64         table.setModel(new DefaultTableModel JavaDoc());
65         removeAll();
66     }
67
68     protected void addRow() {
69         ArchiveWizard wizard = null;
70
71         wizard = new ArchiveWizard();
72         wizard.setWorkingRoot(getDefWorkingRoot());
73         wizard.setMostRecentlyUsed(false);
74         if (wizard.showDialog(getTopLevelAncestor()) == JOptionPane.CANCEL_OPTION) {
75            // cancel
76
} else {
77             int index = -1;
78
79             index = getModel().addPlan(wizard.getPlan());
80             table.setRowSelectionInterval(index, index);
81         }
82     }
83
84     protected void editRow() throws ArchiveException {
85         ArchiveWizard wizard = null;
86         JarPlan plan = null;
87         int index = -1;
88
89         index = table.getSelectedRow();
90         if (index > -1) {
91             wizard = new ArchiveWizard();
92             wizard.setMostRecentlyUsed(false);
93             wizard.setWorkingRoot(getDefWorkingRoot());
94             plan = getModel().getPlan(index);
95             wizard.setPlan(plan);
96             if (wizard.showDialog(getTopLevelAncestor()) == JOptionPane.CANCEL_OPTION) {
97                 // cancel
98
} else {
99                 getModel().setPlan(wizard.getPlan(), index);
100                 table.setRowSelectionInterval(index, index);
101             }
102         }
103     }
104
105     protected void removeSelection() {
106         int row = -1;
107
108         row = table.getSelectedRow();
109         if (row > -1) {
110             getModel().removeRow(row);
111         }
112     }
113
114     protected void moveSelectionDown() {
115         int count = -1;
116         int row = -1;
117
118         count = getModel().getRowCount();
119         row = table.getSelectedRow();
120         if (row > -1 && (row < (count - 1))) {
121             getModel().moveRow(row, row, row + 1);
122             table.setRowSelectionInterval(row + 1, row + 1);
123         }
124     }
125
126     protected void moveSelectionUp() {
127         int row = -1;
128
129         row = table.getSelectedRow();
130         if (row > 0) {
131             getModel().moveRow(row, row, row - 1);
132             table.setRowSelectionInterval(row - 1, row - 1);
133         }
134     }
135
136     protected void setPlans(JarPlan[] plans) {
137         getModel().setPlans(plans);
138     }
139
140     protected JarPlan[] getPlans() {
141         return getModel().getPlans();
142     }
143
144     protected void setDefWorkingRoot(String JavaDoc r) {
145         defWorkRoot = PathHandle.createPathString(r);
146     }
147
148     protected String JavaDoc getDefWorkingRoot() {
149         if (defWorkRoot == null) {
150             setDefWorkingRoot(System.getProperty("user.dir"));
151         }
152         return defWorkRoot;
153     }
154
155     //
156
private ArchiveTableModel getModel() {
157         return (ArchiveTableModel) table.getModel();
158     }
159
160     private void initCellColors(Component comp, boolean isSelected, int row) {
161         if ((row == table.getSelectedRow()) || isSelected) {
162             comp.setBackground(SystemColor.textHighlight);
163             comp.setForeground(SystemColor.textHighlightText);
164         } else {
165             comp.setBackground(SystemColor.info);
166             comp.setForeground(SystemColor.infoText);
167         }
168     }
169
170     private void pmInit() {
171         CheckCell check = null;
172         TextCellRenderer textRenderer = null;
173
174         table.setModel(new ArchiveTableModel());
175         table.getTableHeader().setEnabled(false);
176         table.getTableHeader().setReorderingAllowed(false);
177         table.getTableHeader().setUpdateTableInRealTime(false);
178         table.setCellSelectionEnabled(false);
179         table.setColumnSelectionAllowed(false);
180         table.setRowSelectionAllowed(true);
181         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
182         table.getModel().addTableModelListener(table);
183         check = new CheckCell();
184         textRenderer = new TextCellRenderer();
185         table.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
186         table.getColumnModel().getColumn(0).setCellRenderer(check);
187         table.getColumnModel().getColumn(0).setCellEditor(check);
188         table.getColumnModel().getColumn(0).setPreferredWidth(40);
189         table.getColumnModel().getColumn(1).setCellRenderer(textRenderer);
190         table.getColumnModel().getColumn(1).setPreferredWidth(250);
191     }
192
193     private void jbInit() throws Exception JavaDoc {
194         scroll = (JScrollPane) Beans.instantiate(getClass().getClassLoader(),
195                                                  JScrollPane.class.getName());
196         table = (JTable) Beans.instantiate(getClass().getClassLoader(),
197                                            JTable.class.getName());
198         layoutMain =
199             (BorderLayout) Beans.instantiate(getClass().getClassLoader(),
200                                              BorderLayout.class.getName());
201         scroll.getViewport().add(table, null);
202         this.setForeground(UIManager.getColor("text"));
203         this.setLayout(layoutMain);
204         this.add(scroll, BorderLayout.CENTER);
205     }
206
207     private class CheckCell extends DefaultTableCellRenderer JavaDoc
208         implements TableCellEditor JavaDoc {
209         private JCheckBox check = null;
210         private DefaultCellEditor editor = null;
211
212         public CheckCell() {
213             super();
214         }
215
216         public void addCellEditorListener(CellEditorListener JavaDoc l) {
217             editor.addCellEditorListener(l);
218         }
219
220         public void removeCellEditorListener(CellEditorListener JavaDoc l) {
221             editor.removeCellEditorListener(l);
222         }
223
224         public Object JavaDoc getCellEditorValue() {
225             return editor.getCellEditorValue();
226         }
227
228         public boolean isCellEditable(EventObject JavaDoc anEvent) {
229             return editor.isCellEditable(anEvent);
230         }
231
232         public boolean shouldSelectCell(EventObject JavaDoc anEvent) {
233             return editor.shouldSelectCell(anEvent);
234         }
235
236         public void cancelCellEditing() {
237             editor.cancelCellEditing();
238         }
239
240         public boolean stopCellEditing() {
241             return editor.stopCellEditing();
242         }
243
244         public Component getTableCellRendererComponent(JTable table,
245                 Object JavaDoc value, boolean isSelected, boolean hasFocus, int row,
246                 int column) {
247             if (isSelected) {
248                 table.setRowSelectionInterval(row, row);
249             }
250             check = createCheck(table, value, isSelected, hasFocus, row,
251                                 column, false);
252             initCellColors(check, isSelected, row);
253             editor = new DefaultCellEditor(check);
254             return editor.getTableCellEditorComponent(table, value,
255                                                       isSelected, row,
256                                                       column);
257         }
258
259         public Component getTableCellEditorComponent(JTable table,
260                 Object JavaDoc value, boolean isSelected, int row, int column) {
261             table.setRowSelectionInterval(row, row);
262             check = createCheck(table, value, isSelected, false, row, column,
263                                 true);
264             initCellColors(check, isSelected, row);
265             editor = new DefaultCellEditor(check);
266             return editor.getTableCellEditorComponent(table, value,
267                                                       isSelected, row,
268                                                       column);
269         }
270
271         protected synchronized JCheckBox createCheck(JTable table,
272                 Object JavaDoc value, boolean isSelected, boolean hasFocus, int row,
273                 int column, boolean edit) {
274             JCheckBox cb = null;
275
276             cb = new JCheckBox();
277             cb.setFocusPainted(true);
278             cb.setHorizontalAlignment(SwingConstants.CENTER);
279             return cb;
280         }
281
282     }
283
284     //
285
private class TextCellRenderer extends DefaultTableCellRenderer JavaDoc {
286         public Component getTableCellRendererComponent(JTable table,
287                 Object JavaDoc value, boolean isSelected, boolean hasFocus, int row,
288                 int column) {
289             Component comp = null;
290
291             if (isSelected) {
292                 table.setRowSelectionInterval(row, row);
293             }
294             comp = super.getTableCellRendererComponent(table, value,
295                                                        isSelected, hasFocus,
296                                                        row, column);
297             if (comp instanceof JLabel && value instanceof JarPlan) {
298                 JLabel lab = (JLabel) comp;
299                 JarPlan plan = (JarPlan) value;
300
301                 lab.setHorizontalAlignment(SwingConstants.LEFT);
302                 lab.setText(plan.getArchivePath());
303                 lab.setToolTipText(lab.getText());
304                 initCellColors(lab, isSelected, row);
305             }
306             return comp;
307         }
308
309     }
310 }
311
Popular Tags