KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > RuleTable


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc;
5
6 import org.apache.commons.lang.StringUtils;
7
8 import org.dijon.Button;
9 import org.dijon.CheckBox;
10 import org.dijon.Container;
11 import org.dijon.Label;
12 import org.dijon.jspring.Layout;
13
14 import com.tc.admin.common.XAbstractAction;
15 import com.tc.admin.common.XCellEditor;
16 import com.tc.admin.common.XCheckBox;
17 import com.tc.admin.common.XComboBox;
18 import com.tc.admin.common.XObjectTable;
19 import com.tc.admin.common.XTableCellRenderer;
20 import com.terracottatech.config.Include;
21 import com.terracottatech.config.OnLoad;
22
23 import java.awt.Color JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.awt.event.InputEvent JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.awt.event.MouseEvent JavaDoc;
30
31 import javax.swing.ActionMap JavaDoc;
32 import javax.swing.DefaultComboBoxModel JavaDoc;
33 import javax.swing.InputMap JavaDoc;
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.JTable JavaDoc;
36 import javax.swing.KeyStroke JavaDoc;
37 import javax.swing.ListSelectionModel JavaDoc;
38 import javax.swing.SwingUtilities JavaDoc;
39 import javax.swing.UIManager JavaDoc;
40 import javax.swing.table.TableCellEditor JavaDoc;
41
42 public class RuleTable extends XObjectTable {
43   private OnLoadDialog m_onLoadDialog;
44   
45   private static final String JavaDoc MOVE_UP_ACTION = "MoveUp";
46   private static final String JavaDoc MOVE_DOWN_ACTION = "MoveDown";
47   
48   private static final KeyStroke JavaDoc MOVE_UP_STROKE =
49     KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.CTRL_MASK);
50   private static final KeyStroke JavaDoc MOVE_DOWN_STROKE =
51     KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_MASK);
52   
53   public RuleTable() {
54     super();
55
56     setDefaultRenderer(Integer JavaDoc.class, new RuleTypeRenderer());
57     setDefaultEditor(Integer JavaDoc.class, new RuleTypeEditor());
58
59     setDefaultRenderer(RuleDetail.class, new RuleDetailRenderer());
60     setDefaultEditor(RuleDetail.class, new RuleDetailEditor(true));
61     
62     getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
63
64     ActionMap JavaDoc actionMap = getActionMap();
65     actionMap.put(MOVE_UP_ACTION, new MoveUpAction());
66     actionMap.put(MOVE_DOWN_ACTION, new MoveDownAction());
67     
68     InputMap JavaDoc inputMap = getInputMap();
69     inputMap.put(MOVE_UP_STROKE, MOVE_UP_ACTION);
70     inputMap.put(MOVE_DOWN_STROKE, MOVE_DOWN_ACTION);
71   }
72   
73   public OnLoadDialog getOnLoadDialog() {
74     if(m_onLoadDialog == null) {
75       m_onLoadDialog = new OnLoadDialog();
76     }
77     return m_onLoadDialog;
78   }
79   
80   class RuleTypeRenderer extends XTableCellRenderer {
81     public java.awt.Component JavaDoc getTableCellRendererComponent(
82       JTable JavaDoc table,
83       Object JavaDoc value,
84       boolean isSelected,
85       boolean hasFocus,
86       int row,
87       int col)
88     {
89       super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
90       
91       Rule rule = getRuleAt(row);
92       setText(rule.isExcludeRule() ? "Exclude" : "Include");
93
94       return this;
95     }
96   }
97   
98   class RuleTypeEditor extends XCellEditor {
99     private static final String JavaDoc INCLUDE_ITEM = "Include";
100     private static final String JavaDoc EXCLUDE_ITEM = "Exclude";
101
102     private final String JavaDoc[] MODEL_ITEMS = new String JavaDoc[] {INCLUDE_ITEM, EXCLUDE_ITEM};
103     
104     private int m_row;
105     private DefaultComboBoxModel JavaDoc m_model;
106     
107     RuleTypeEditor() {
108       super(new XComboBox());
109       m_model = new DefaultComboBoxModel JavaDoc(MODEL_ITEMS);
110       ((XComboBox)m_editorComponent).setModel(m_model);
111     }
112     
113     protected void fireEditingStopped() {
114       super.fireEditingStopped();
115       setRowSelectionInterval(m_row, m_row);
116     }
117     
118     public Object JavaDoc getCellEditorValue() {
119       Object JavaDoc value = super.getCellEditorValue();
120       return new Integer JavaDoc(value.equals(INCLUDE_ITEM) ? Rule.INCLUDE_RULE : Rule.EXCLUDE_RULE);
121     }
122
123     public java.awt.Component JavaDoc getTableCellEditorComponent(
124       JTable JavaDoc table,
125       Object JavaDoc value,
126       boolean isSelected,
127       int row,
128       int col)
129     {
130       super.getTableCellEditorComponent(table, value, isSelected, row, col);
131       
132       Rule rule = getRuleAt(row);
133       m_model.setSelectedItem(MODEL_ITEMS[rule.getType()]);
134       m_row = row;
135       
136       return (java.awt.Component JavaDoc)m_editorComponent;
137     }
138   }
139   
140   class RuleDetailRenderer extends XTableCellRenderer {
141     protected TableCellEditor JavaDoc m_editor;
142
143     public RuleDetailRenderer() {
144       super();
145       m_editor = createCellEditor();
146     }
147
148     protected TableCellEditor JavaDoc createCellEditor() {
149       return new RuleDetailEditor(false);
150     }
151
152     public java.awt.Component JavaDoc getTableCellRendererComponent(
153       JTable JavaDoc table,
154       Object JavaDoc value,
155       boolean isSelected,
156       boolean hasFocus,
157       int row,
158       int col)
159     {
160       JComponent JavaDoc c = (JComponent JavaDoc)
161         m_editor.getTableCellEditorComponent(table, value, isSelected, row, col);
162       
163       c.setBorder(hasFocus ? UIManager.getBorder("Table.focusCellHighlightBorder") : null);
164       
165       return c;
166     }
167   }
168   
169   private static String JavaDoc buildToolTip(Include include) {
170     OnLoad onLoad = include.getOnLoad();
171     String JavaDoc tip = null;
172     
173     if(onLoad != null) {
174       if((tip = onLoad.getMethod()) == null) {
175         if((tip = onLoad.getExecute()) != null) {
176           StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
177           String JavaDoc[] lines = StringUtils.split(onLoad.getExecute(),
178                                              System.getProperty("line.separator"));
179   
180           sb.append("<html>");
181           for(int i = 0; i < lines.length; i++) {
182             sb.append("<p>");
183             sb.append(lines[i]);
184           }
185           sb.append("</html>");
186   
187           tip = sb.toString();
188         }
189       }
190     }
191     
192     return tip;
193   }
194   
195   class OnLoadButton extends Button {
196     int m_row, m_col;
197     
198     OnLoadButton(String JavaDoc text) {
199       super(text);
200       setMargin(new Insets JavaDoc(0,2,0,2));
201     }
202     
203     void setCell(int row, int col) {
204       m_row = row;
205       m_col = col;
206     }
207
208     int getRow() {return m_row;}
209     int getCol() {return m_col;}
210   }
211   
212   class RuleDetailEditor extends XCellEditor {
213     OnLoadButton m_onLoadButton;
214     XCheckBox m_honorTransientToggle;
215     Label m_excludeRenderer;
216     boolean m_alwaysSelected;
217     
218     RuleDetailEditor(boolean alwaysSelected) {
219       super(new XCheckBox("Honor transient"));
220
221       m_alwaysSelected = alwaysSelected;
222       
223       m_honorTransientToggle = (XCheckBox)m_editorComponent;
224       m_honorTransientToggle.addActionListener(new ActionListener JavaDoc () {
225         public void actionPerformed(ActionEvent JavaDoc ae) {
226           boolean honor = m_honorTransientToggle.isSelected();
227           int row = m_onLoadButton.getRow();
228           int col = m_onLoadButton.getCol();
229           
230           getIncludeRuleAt(row).setHonorTransient(honor);
231           getRuleModel().fireTableCellUpdated(row, col);
232         }
233       });
234       m_honorTransientToggle.setMargin(new Insets JavaDoc(0,0,0,0));
235       
236       m_onLoadButton = new OnLoadButton("On load...");
237       m_onLoadButton.addActionListener(new ActionListener JavaDoc() {
238         public void actionPerformed(ActionEvent JavaDoc ae) {
239           getOnLoadDialog().edit(getIncludeAt(m_onLoadButton.getRow()));
240           RuleTable.this.repaint();
241         }
242       });
243       m_editorComponent = new RuleDetailPanel(m_honorTransientToggle, m_onLoadButton);
244       m_excludeRenderer = new Label("");
245
246       m_clicksToStart = 1;
247     }
248     
249     public java.awt.Component JavaDoc getTableCellEditorComponent(
250       JTable JavaDoc table,
251       Object JavaDoc value,
252       boolean isSelected,
253       int row,
254       int col)
255     {
256       Rule rule = getRuleAt(row);
257       
258       if(m_alwaysSelected) {
259         isSelected = true;
260       }
261       
262       Color JavaDoc fg = isSelected ? table.getSelectionForeground() : table.getForeground();
263       Color JavaDoc bg = isSelected ? table.getSelectionBackground() : table.getBackground();
264       
265       if(rule.isIncludeRule()) {
266         Include include = getIncludeAt(row);
267         
268         m_onLoadButton.setCell(row, col);
269         m_onLoadButton.setForeground(table.getForeground());
270         m_onLoadButton.setBackground(table.getBackground());
271         m_onLoadButton.setFont(table.getFont());
272         m_onLoadButton.setToolTipText(buildToolTip(include));
273         
274         m_honorTransientToggle.setSelected(include.getHonorTransient());
275         m_honorTransientToggle.setForeground(fg);
276         m_honorTransientToggle.setBackground(bg);
277         m_honorTransientToggle.setFont(table.getFont());
278         m_honorTransientToggle.setOpaque(true);
279         
280         m_editorComponent.setForeground(fg);
281         m_editorComponent.setBackground(bg);
282         m_editorComponent.setOpaque(true);
283         
284         m_editorComponent.setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
285
286         return (java.awt.Component JavaDoc)m_editorComponent;
287       }
288       else {
289         m_excludeRenderer.setForeground(fg);
290         m_excludeRenderer.setBackground(bg);
291         m_excludeRenderer.setOpaque(true);
292         m_excludeRenderer.setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
293        
294         return m_excludeRenderer;
295       }
296     }
297   }
298   
299   public RuleModel getRuleModel() {
300     return (RuleModel)getModel();
301   }
302   
303   public Include getIncludeAt(int row) {
304     return getIncludeRuleAt(row).getInclude();
305   }
306   
307   public IncludeRule getIncludeRuleAt(int row) {
308     return (IncludeRule)getRuleAt(row);
309   }
310   
311   public Rule getRuleAt(int row) {
312     return getRuleModel().getRuleAt(row);
313   }
314
315   public void moveUp() {
316     int row = getSelectedRow();
317     
318     if(isEditing()) {
319       removeEditor();
320     }
321     
322     if(row > 0) {
323       getRuleModel().moveRuleUp(row--);
324       setRowSelectionInterval(row, row);
325     }
326   }
327   
328   class MoveUpAction extends XAbstractAction {
329     MoveUpAction() {
330       super("Move up");
331     }
332     
333     public void actionPerformed(ActionEvent JavaDoc ae) {
334       moveUp();
335     }
336   }
337
338   public void moveDown() {
339     int row = getSelectedRow();
340
341     if(isEditing()) {
342       removeEditor();
343     }
344     
345     if(row != -1 && row < getRuleModel().getRowCount()-1) {
346       getRuleModel().moveRuleDown(row++);
347       setRowSelectionInterval(row, row);
348     }
349   }
350   
351   class MoveDownAction extends XAbstractAction {
352     MoveDownAction() {
353       super("Move down");
354     }
355     
356     public void actionPerformed(ActionEvent JavaDoc ae) {
357       moveDown();
358     }
359   }
360 }
361
362 class RuleDetailPanel extends Container {
363   RuleDetailPanel(CheckBox checkBox, Button button) {
364     super();
365
366     String JavaDoc[] constraint1 = {"-1,left:ns", "-1,top:s", "1,left:n", "-1,bottom:s", "n","n"};
367     String JavaDoc[] constraint2 = {"0,right:n", "-1,top:s", "-1,right:ns", "-1,bottom:s", "n","n"};
368
369     setLayout(new Layout());
370     add(checkBox, constraint1);
371     add(button, constraint2);
372     
373     setBorder(null);
374   }
375   
376   public String JavaDoc getToolTipText(MouseEvent JavaDoc event) {
377     setSize(-getX(), -getY());
378     setLocation(0, 0);
379     JComponent JavaDoc c = (JComponent JavaDoc)SwingUtilities.getDeepestComponentAt(this, event.getX(), event.getY());
380     setLocation(-getWidth(), -getHeight());
381     setSize(0, 0);
382     return c != null ? c.getToolTipText() : null;
383   }
384 }
385
Popular Tags