001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.servers;
005:
006: import org.dijon.Button;
007: import org.dijon.Container;
008: import org.dijon.TextField;
009:
010: import com.tc.admin.common.XCellEditor;
011: import com.tc.admin.common.XObjectTable;
012: import com.tc.admin.common.XTableCellRenderer;
013: import com.tc.admin.common.XTextField;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Color;
017: import java.awt.Insets;
018: import java.awt.event.ActionEvent;
019: import java.awt.event.ActionListener;
020: import java.io.File;
021:
022: import javax.swing.JComponent;
023: import javax.swing.JFileChooser;
024: import javax.swing.JTable;
025: import javax.swing.UIManager;
026: import javax.swing.border.Border;
027: import javax.swing.border.EmptyBorder;
028: import javax.swing.table.TableCellEditor;
029: import javax.swing.table.TableCellRenderer;
030:
031: public class ServerEnvTable extends XObjectTable {
032: private TableCellRenderer m_renderer;
033: private TableCellEditor m_editor;
034: private JFileChooser m_chsr;
035: private File m_lastDir;
036:
037: protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
038:
039: public ServerEnvTable() {
040: super ();
041: }
042:
043: public TableCellRenderer getCellRenderer(int row, int col) {
044: if (col == 1) {
045: if (m_renderer == null) {
046: m_renderer = new ValueRenderer();
047: }
048: return m_renderer;
049: }
050: return super .getCellRenderer(row, col);
051: }
052:
053: public TableCellEditor getCellEditor(int row, int col) {
054: if (col == 1) {
055: if (m_editor == null) {
056: m_editor = new ValueEditor();
057: }
058: return m_editor;
059: }
060: return super .getCellEditor(row, col);
061: }
062:
063: class ValueRenderer extends XTableCellRenderer {
064: protected TableCellEditor m_rendererDelegate;
065:
066: public ValueRenderer() {
067: super ();
068: m_rendererDelegate = createCellEditor();
069: }
070:
071: protected TableCellEditor createCellEditor() {
072: return new ValueEditor(true);
073: }
074:
075: public java.awt.Component getTableCellRendererComponent(
076: JTable table, Object value, boolean isSelected,
077: boolean hasFocus, int row, int col) {
078: JComponent c = (JComponent) m_rendererDelegate
079: .getTableCellEditorComponent(table, value,
080: isSelected, row, col);
081:
082: c
083: .setBorder(hasFocus ? UIManager
084: .getBorder("Table.focusCellHighlightBorder")
085: : null);
086:
087: return c;
088: }
089: }
090:
091: class ChooserButton extends Button {
092: int m_row, m_col;
093:
094: ChooserButton(String text) {
095: super (text);
096: setMargin(new Insets(0, 2, 0, 2));
097: }
098:
099: void setCell(int row, int col) {
100: m_row = row;
101: m_col = col;
102: }
103:
104: int getRow() {
105: return m_row;
106: }
107:
108: int getCol() {
109: return m_col;
110: }
111: }
112:
113: private JFileChooser getChooser() {
114: if (m_chsr == null) {
115: m_chsr = new JFileChooser();
116: m_chsr.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
117: }
118:
119: if (m_lastDir != null) {
120: m_chsr.setCurrentDirectory(m_lastDir);
121: }
122:
123: return m_chsr;
124: }
125:
126: class ValueEditor extends XCellEditor {
127: ChooserButton m_chsrButton;
128: XTextField m_valueField;
129: boolean m_isRenderer;
130:
131: ValueEditor() {
132: this (false);
133: }
134:
135: ValueEditor(boolean isRenderer) {
136: super (new XTextField());
137:
138: m_isRenderer = isRenderer;
139:
140: m_valueField = (XTextField) m_editorComponent;
141: m_valueField.addActionListener(new ActionListener() {
142: public void actionPerformed(ActionEvent ae) {
143: int row = m_chsrButton.getRow();
144: int col = m_chsrButton.getCol();
145:
146: getServerPropertyAt(row).setValue(
147: m_valueField.getText());
148: getServerEnvTableModel().fireTableCellUpdated(row,
149: col);
150: }
151: });
152: m_valueField.setMargin(new Insets(0, 0, 0, 0));
153:
154: m_chsrButton = new ChooserButton("...");
155: m_chsrButton.addActionListener(new ActionListener() {
156: public void actionPerformed(ActionEvent ae) {
157: JFileChooser chsr = getChooser();
158:
159: if (chsr.showOpenDialog(ServerEnvTable.this ) == JFileChooser.APPROVE_OPTION) {
160: int row = m_chsrButton.getRow();
161: int col = m_chsrButton.getCol();
162: String path = chsr.getSelectedFile()
163: .getAbsolutePath();
164:
165: removeEditor();
166: getServerEnvTableModel().setValueAt(path, row,
167: col);
168: } else {
169: removeEditor();
170: }
171: m_lastDir = chsr.getCurrentDirectory();
172: }
173: });
174:
175: m_editorComponent = new PropertyValuePanel(m_valueField,
176: m_chsrButton);
177: m_clicksToStart = 1;
178: }
179:
180: public java.awt.Component getTableCellEditorComponent(
181: JTable table, Object value, boolean isSelected,
182: int row, int col) {
183: Color fg = isSelected ? table.getSelectionForeground()
184: : table.getForeground();
185: Color bg = isSelected ? table.getSelectionBackground()
186: : table.getBackground();
187:
188: ServerProperty prop = getServerPropertyAt(row);
189:
190: m_chsrButton.setCell(row, col);
191: m_chsrButton.setForeground(table.getForeground());
192: m_chsrButton.setBackground(table.getBackground());
193: m_chsrButton.setFont(table.getFont());
194:
195: m_valueField.setText(prop.getValue());
196: if (!m_isRenderer) {
197: m_valueField.setForeground(UIManager
198: .getColor("TextField.foreground"));
199: m_valueField.setBackground(UIManager
200: .getColor("TextField.background"));
201: } else {
202: m_valueField.setForeground(fg);
203: m_valueField.setBackground(bg);
204: }
205: m_valueField.setFont(table.getFont());
206:
207: if (!m_isRenderer) {
208: m_valueField.setBorder(UIManager
209: .getBorder("Table.focusCellHighlightBorder"));
210: } else {
211: m_valueField.setBorder(noFocusBorder);
212: m_chsrButton.setVisible(false);
213: }
214:
215: return (java.awt.Component) m_editorComponent;
216: }
217: }
218:
219: public ServerEnvTableModel getServerEnvTableModel() {
220: return (ServerEnvTableModel) getModel();
221: }
222:
223: public ServerProperty getServerPropertyAt(int row) {
224: return getServerEnvTableModel().getServerPropertyAt(row);
225: }
226: }
227:
228: class PropertyValuePanel extends Container {
229: PropertyValuePanel(TextField textfield, Button button) {
230: super ();
231: setLayout(new BorderLayout());
232: add(textfield);
233: add(button, BorderLayout.EAST);
234: setBorder(null);
235: }
236: }
|