1 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 ; 24 import java.awt.Insets ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.awt.event.InputEvent ; 28 import java.awt.event.KeyEvent ; 29 import java.awt.event.MouseEvent ; 30 31 import javax.swing.ActionMap ; 32 import javax.swing.DefaultComboBoxModel ; 33 import javax.swing.InputMap ; 34 import javax.swing.JComponent ; 35 import javax.swing.JTable ; 36 import javax.swing.KeyStroke ; 37 import javax.swing.ListSelectionModel ; 38 import javax.swing.SwingUtilities ; 39 import javax.swing.UIManager ; 40 import javax.swing.table.TableCellEditor ; 41 42 public class RuleTable extends XObjectTable { 43 private OnLoadDialog m_onLoadDialog; 44 45 private static final String MOVE_UP_ACTION = "MoveUp"; 46 private static final String MOVE_DOWN_ACTION = "MoveDown"; 47 48 private static final KeyStroke MOVE_UP_STROKE = 49 KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.CTRL_MASK); 50 private static final KeyStroke MOVE_DOWN_STROKE = 51 KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_MASK); 52 53 public RuleTable() { 54 super(); 55 56 setDefaultRenderer(Integer .class, new RuleTypeRenderer()); 57 setDefaultEditor(Integer .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 actionMap = getActionMap(); 65 actionMap.put(MOVE_UP_ACTION, new MoveUpAction()); 66 actionMap.put(MOVE_DOWN_ACTION, new MoveDownAction()); 67 68 InputMap 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 getTableCellRendererComponent( 82 JTable table, 83 Object 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 INCLUDE_ITEM = "Include"; 100 private static final String EXCLUDE_ITEM = "Exclude"; 101 102 private final String [] MODEL_ITEMS = new String [] {INCLUDE_ITEM, EXCLUDE_ITEM}; 103 104 private int m_row; 105 private DefaultComboBoxModel m_model; 106 107 RuleTypeEditor() { 108 super(new XComboBox()); 109 m_model = new DefaultComboBoxModel (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 getCellEditorValue() { 119 Object value = super.getCellEditorValue(); 120 return new Integer (value.equals(INCLUDE_ITEM) ? Rule.INCLUDE_RULE : Rule.EXCLUDE_RULE); 121 } 122 123 public java.awt.Component getTableCellEditorComponent( 124 JTable table, 125 Object 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 )m_editorComponent; 137 } 138 } 139 140 class RuleDetailRenderer extends XTableCellRenderer { 141 protected TableCellEditor m_editor; 142 143 public RuleDetailRenderer() { 144 super(); 145 m_editor = createCellEditor(); 146 } 147 148 protected TableCellEditor createCellEditor() { 149 return new RuleDetailEditor(false); 150 } 151 152 public java.awt.Component getTableCellRendererComponent( 153 JTable table, 154 Object value, 155 boolean isSelected, 156 boolean hasFocus, 157 int row, 158 int col) 159 { 160 JComponent c = (JComponent ) 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 buildToolTip(Include include) { 170 OnLoad onLoad = include.getOnLoad(); 171 String tip = null; 172 173 if(onLoad != null) { 174 if((tip = onLoad.getMethod()) == null) { 175 if((tip = onLoad.getExecute()) != null) { 176 StringBuffer sb = new StringBuffer (); 177 String [] 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 text) { 199 super(text); 200 setMargin(new Insets (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 () { 225 public void actionPerformed(ActionEvent 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 (0,0,0,0)); 235 236 m_onLoadButton = new OnLoadButton("On load..."); 237 m_onLoadButton.addActionListener(new ActionListener () { 238 public void actionPerformed(ActionEvent 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 getTableCellEditorComponent( 250 JTable table, 251 Object 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 fg = isSelected ? table.getSelectionForeground() : table.getForeground(); 263 Color 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 )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 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 ae) { 357 moveDown(); 358 } 359 } 360 } 361 362 class RuleDetailPanel extends Container { 363 RuleDetailPanel(CheckBox checkBox, Button button) { 364 super(); 365 366 String [] constraint1 = {"-1,left:ns", "-1,top:s", "1,left:n", "-1,bottom:s", "n","n"}; 367 String [] 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 getToolTipText(MouseEvent event) { 377 setSize(-getX(), -getY()); 378 setLocation(0, 0); 379 JComponent c = (JComponent )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 |