View Javadoc

1   /*
2    *  File: BooleanCellEditor.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
10   */
11  package de.jaret.util.ui.table.editor;
12  
13  import org.eclipse.swt.graphics.Rectangle;
14  import org.eclipse.swt.widgets.Control;
15  
16  import de.jaret.util.ui.table.JaretTable;
17  import de.jaret.util.ui.table.model.IColumn;
18  import de.jaret.util.ui.table.model.IRow;
19  
20  /***
21   * BooleanCellEditor is not a real editor. It toggles on double click, optional on click and on a typed SPACE.
22   * 
23   * @author Peter Kliem
24   * @version $Id: BooleanCellEditor.java 179 2007-01-07 17:37:50Z olk $
25   */
26  public class BooleanCellEditor extends CellEditorBase implements ICellEditor {
27      /*** single clickk attribute: if true react on single clicks. */
28      protected boolean _singleClick = false;
29  
30      /***
31       * Default constructor.
32       * 
33       */
34      public BooleanCellEditor() {
35      }
36  
37      /***
38       * Constructor including the singelClick property.
39       * 
40       * @param singleClick if true the editor will react on single clicks in the cell
41       */
42      public BooleanCellEditor(boolean singleClick) {
43          _singleClick = singleClick;
44      }
45  
46      /***
47       * {@inheritDoc}
48       */
49      public Control getEditorControl(JaretTable table, IRow row, IColumn column, char typedKey) {
50          if (typedKey == ' ') {
51              toggle(row, column);
52          } else if (typedKey == 0) {
53              toggle(row, column);
54          }
55          return null;
56      }
57  
58      /***
59       * {@inheritDoc}
60       */
61      public void stopEditing(boolean storeInput) {
62          // nothing to do
63      }
64  
65      /*** selection area width and height. */
66      private static final int SELECTION_DELTA = 16;
67  
68      /***
69       * {@inheritDoc}
70       */
71      public boolean handleClick(JaretTable table, IRow row, IColumn column, Rectangle drawingArea, int x, int y) {
72          if (_singleClick) {
73              Rectangle rect = new Rectangle(drawingArea.x + (drawingArea.width - SELECTION_DELTA) / 2, drawingArea.y
74                      + (drawingArea.height - SELECTION_DELTA) / 2, SELECTION_DELTA, SELECTION_DELTA);
75              if (rect.contains(x, y)) {
76                  toggle(row, column);
77                  return true;
78              }
79          }
80          return false;
81      }
82  
83      /***
84       * Toggle the boolean value.
85       * 
86       * @param row row of the cell
87       * @param column column of the cell
88       */
89      private void toggle(IRow row, IColumn column) {
90          Object value = column.getValue(row);
91          if (value instanceof Boolean) {
92              column.setValue(row, ((Boolean) value).booleanValue() ? Boolean.FALSE : Boolean.TRUE);
93          }
94      }
95  
96      /***
97       * {@inheritDoc}
98       */
99      public void dispose() {
100         super.dispose();
101     }
102 
103 }