001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023: package org.enhydra.kelp.common.deployer;
024:
025: // ToolBox
026: import org.enhydra.tool.archive.ArchiveException;
027: import org.enhydra.tool.archive.JarPlan;
028: import org.enhydra.tool.archive.wizard.ArchiveWizard;
029: import org.enhydra.tool.common.PathHandle;
030:
031: // JDK
032: import java.awt.*;
033: import java.beans.*;
034: import java.util.EventObject;
035: import javax.swing.*;
036: import javax.swing.event.CellEditorListener;
037: import javax.swing.table.TableCellEditor;
038: import javax.swing.table.DefaultTableCellRenderer;
039: import javax.swing.table.DefaultTableModel;
040:
041: //
042: public class ArchiveTablePanel extends JPanel {
043: private BorderLayout layoutMain;
044: private JScrollPane scroll;
045: private JTable table;
046: private String defWorkRoot = null;
047:
048: public ArchiveTablePanel() {
049: try {
050: jbInit();
051: pmInit();
052: } catch (Exception ex) {
053: ex.printStackTrace();
054: }
055: }
056:
057: //
058: protected void clearAll() {
059: table.getModel().removeTableModelListener(table);
060: table.setModel(new DefaultTableModel());
061: removeAll();
062: }
063:
064: protected void addRow() {
065: ArchiveWizard wizard = null;
066:
067: wizard = new ArchiveWizard();
068: wizard.setWorkingRoot(getDefWorkingRoot());
069: wizard.setMostRecentlyUsed(false);
070: if (wizard.showDialog(getTopLevelAncestor()) == JOptionPane.CANCEL_OPTION) {
071: // cancel
072: } else {
073: int index = -1;
074:
075: index = getModel().addPlan(wizard.getPlan());
076: table.setRowSelectionInterval(index, index);
077: }
078: }
079:
080: protected void editRow() throws ArchiveException {
081: ArchiveWizard wizard = null;
082: JarPlan plan = null;
083: int index = -1;
084:
085: index = table.getSelectedRow();
086: if (index > -1) {
087: wizard = new ArchiveWizard();
088: wizard.setMostRecentlyUsed(false);
089: wizard.setWorkingRoot(getDefWorkingRoot());
090: plan = getModel().getPlan(index);
091: wizard.setPlan(plan);
092: if (wizard.showDialog(getTopLevelAncestor()) == JOptionPane.CANCEL_OPTION) {
093: // cancel
094: } else {
095: getModel().setPlan(wizard.getPlan(), index);
096: table.setRowSelectionInterval(index, index);
097: }
098: }
099: }
100:
101: protected void removeSelection() {
102: int row = -1;
103:
104: row = table.getSelectedRow();
105: if (row > -1) {
106: getModel().removeRow(row);
107: }
108: }
109:
110: protected void moveSelectionDown() {
111: int count = -1;
112: int row = -1;
113:
114: count = getModel().getRowCount();
115: row = table.getSelectedRow();
116: if (row > -1 && (row < (count - 1))) {
117: getModel().moveRow(row, row, row + 1);
118: table.setRowSelectionInterval(row + 1, row + 1);
119: }
120: }
121:
122: protected void moveSelectionUp() {
123: int row = -1;
124:
125: row = table.getSelectedRow();
126: if (row > 0) {
127: getModel().moveRow(row, row, row - 1);
128: table.setRowSelectionInterval(row - 1, row - 1);
129: }
130: }
131:
132: protected void setPlans(JarPlan[] plans) {
133: getModel().setPlans(plans);
134: }
135:
136: protected JarPlan[] getPlans() {
137: return getModel().getPlans();
138: }
139:
140: protected void setDefWorkingRoot(String r) {
141: defWorkRoot = PathHandle.createPathString(r);
142: }
143:
144: protected String getDefWorkingRoot() {
145: if (defWorkRoot == null) {
146: setDefWorkingRoot(System.getProperty("user.dir"));
147: }
148: return defWorkRoot;
149: }
150:
151: //
152: private ArchiveTableModel getModel() {
153: return (ArchiveTableModel) table.getModel();
154: }
155:
156: private void initCellColors(Component comp, boolean isSelected,
157: int row) {
158: if ((row == table.getSelectedRow()) || isSelected) {
159: comp.setBackground(SystemColor.textHighlight);
160: comp.setForeground(SystemColor.textHighlightText);
161: } else {
162: comp.setBackground(SystemColor.info);
163: comp.setForeground(SystemColor.infoText);
164: }
165: }
166:
167: private void pmInit() {
168: CheckCell check = null;
169: TextCellRenderer textRenderer = null;
170:
171: table.setModel(new ArchiveTableModel());
172: table.getTableHeader().setEnabled(false);
173: table.getTableHeader().setReorderingAllowed(false);
174: table.getTableHeader().setUpdateTableInRealTime(false);
175: table.setCellSelectionEnabled(false);
176: table.setColumnSelectionAllowed(false);
177: table.setRowSelectionAllowed(true);
178: table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
179: table.getModel().addTableModelListener(table);
180: check = new CheckCell();
181: textRenderer = new TextCellRenderer();
182: table
183: .setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
184: table.getColumnModel().getColumn(0).setCellRenderer(check);
185: table.getColumnModel().getColumn(0).setCellEditor(check);
186: table.getColumnModel().getColumn(0).setPreferredWidth(40);
187: table.getColumnModel().getColumn(1).setCellRenderer(
188: textRenderer);
189: table.getColumnModel().getColumn(1).setPreferredWidth(250);
190: }
191:
192: private void jbInit() throws Exception {
193: scroll = (JScrollPane) Beans.instantiate(getClass()
194: .getClassLoader(), JScrollPane.class.getName());
195: table = (JTable) Beans.instantiate(getClass().getClassLoader(),
196: JTable.class.getName());
197: layoutMain = (BorderLayout) Beans.instantiate(getClass()
198: .getClassLoader(), BorderLayout.class.getName());
199: scroll.getViewport().add(table, null);
200: this .setForeground(UIManager.getColor("text"));
201: this .setLayout(layoutMain);
202: this .add(scroll, BorderLayout.CENTER);
203: }
204:
205: private class CheckCell extends DefaultTableCellRenderer implements
206: TableCellEditor {
207: private JCheckBox check = null;
208: private DefaultCellEditor editor = null;
209:
210: public CheckCell() {
211: super ();
212: }
213:
214: public void addCellEditorListener(CellEditorListener l) {
215: editor.addCellEditorListener(l);
216: }
217:
218: public void removeCellEditorListener(CellEditorListener l) {
219: editor.removeCellEditorListener(l);
220: }
221:
222: public Object getCellEditorValue() {
223: return editor.getCellEditorValue();
224: }
225:
226: public boolean isCellEditable(EventObject anEvent) {
227: return editor.isCellEditable(anEvent);
228: }
229:
230: public boolean shouldSelectCell(EventObject anEvent) {
231: return editor.shouldSelectCell(anEvent);
232: }
233:
234: public void cancelCellEditing() {
235: editor.cancelCellEditing();
236: }
237:
238: public boolean stopCellEditing() {
239: return editor.stopCellEditing();
240: }
241:
242: public Component getTableCellRendererComponent(JTable table,
243: Object value, boolean isSelected, boolean hasFocus,
244: int row, int column) {
245: if (isSelected) {
246: table.setRowSelectionInterval(row, row);
247: }
248: check = createCheck(table, value, isSelected, hasFocus,
249: row, column, false);
250: initCellColors(check, isSelected, row);
251: editor = new DefaultCellEditor(check);
252: return editor.getTableCellEditorComponent(table, value,
253: isSelected, row, column);
254: }
255:
256: public Component getTableCellEditorComponent(JTable table,
257: Object value, boolean isSelected, int row, int column) {
258: table.setRowSelectionInterval(row, row);
259: check = createCheck(table, value, isSelected, false, row,
260: column, true);
261: initCellColors(check, isSelected, row);
262: editor = new DefaultCellEditor(check);
263: return editor.getTableCellEditorComponent(table, value,
264: isSelected, row, column);
265: }
266:
267: protected synchronized JCheckBox createCheck(JTable table,
268: Object value, boolean isSelected, boolean hasFocus,
269: int row, int column, boolean edit) {
270: JCheckBox cb = null;
271:
272: cb = new JCheckBox();
273: cb.setFocusPainted(true);
274: cb.setHorizontalAlignment(SwingConstants.CENTER);
275: return cb;
276: }
277:
278: }
279:
280: //
281: private class TextCellRenderer extends DefaultTableCellRenderer {
282: public Component getTableCellRendererComponent(JTable table,
283: Object value, boolean isSelected, boolean hasFocus,
284: int row, int column) {
285: Component comp = null;
286:
287: if (isSelected) {
288: table.setRowSelectionInterval(row, row);
289: }
290: comp = super .getTableCellRendererComponent(table, value,
291: isSelected, hasFocus, row, column);
292: if (comp instanceof JLabel && value instanceof JarPlan) {
293: JLabel lab = (JLabel) comp;
294: JarPlan plan = (JarPlan) value;
295:
296: lab.setHorizontalAlignment(SwingConstants.LEFT);
297: lab.setText(plan.getArchivePath());
298: lab.setToolTipText(lab.getText());
299: initCellColors(lab, isSelected, row);
300: }
301: return comp;
302: }
303:
304: }
305: }
|