KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > thickclient > application > swt > ToggleButtonSelectionAdapter


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: ToggleButtonSelectionAdapter.java,v 1.3 2007/01/07 06:14:15 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21  
22 package org.opensubsystems.patterns.thickclient.application.swt;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.custom.TableEditor;
30 import org.eclipse.swt.events.SelectionAdapter;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Table;
35 import org.eclipse.swt.widgets.TableItem;
36 import org.opensubsystems.core.application.swt.ResourceManager;
37
38 /**
39  * This adapter make sure that the toggle button is displayed when user wants
40  * to edit the table cell content.
41  *
42  * @version $Id: ToggleButtonSelectionAdapter.java,v 1.3 2007/01/07 06:14:15 bastafidli Exp $
43  * @author Miro Halas
44  * @code.reviewer Miro Halas
45  * @code.reviewed 1.1 2006/02/17 03:37:04 bastafidli
46  */

47 public class ToggleButtonSelectionAdapter extends SelectionAdapter
48 {
49    // Attributes ///////////////////////////////////////////////////////////////
50

51    /**
52     * Set where all listeners listening to the event notifications specific
53     * to this object are stored.
54     */

55    protected Set JavaDoc m_setListeners = new HashSet JavaDoc();
56
57    /**
58     * Table this adapter is attached to.
59     */

60    protected Table m_table;
61    
62    /**
63     * Editor used to edit the table.
64     */

65    protected TableEditor m_editor;
66    
67    /**
68     * Which column is edited by this editor.
69     */

70    protected int m_iColumn;
71    
72    /**
73     * Button used to toggle the content.
74     */

75    protected Button m_toggleButton;
76    
77    /**
78     * Text on the toggle button.
79     */

80    protected String JavaDoc m_strButtonText;
81
82    // Constructors /////////////////////////////////////////////////////////////
83

84    /**
85     * Construct new adapter.
86     *
87     * @param editableTable - table this adapter is attached to.
88     * @param iColumn - 0 based index of column toggled by this editor.
89     */

90    public ToggleButtonSelectionAdapter(
91       Table editableTable,
92       int iColumn
93    )
94    {
95       this(editableTable, iColumn, "X");
96    }
97
98    /**
99     * Construct new adapter.
100     *
101     * @param editableTable - table this adapter is attached to.
102     * @param iColumn - 0 based index of column toggled by this editor.
103     * @param strToggleButtonText - text to display on the toggle button, if null
104     * is passed in, no button will be displayed
105     * and the toggle will occur just by clicking
106     * on the row
107     */

108    public ToggleButtonSelectionAdapter(
109       Table editableTable,
110       int iColumn,
111       String JavaDoc strToggleButtonText
112    )
113    {
114       m_table = editableTable;
115       m_editor = new TableEditor(m_table);
116       m_iColumn = iColumn;
117       m_strButtonText = strToggleButtonText;
118    }
119
120    // Public methods ///////////////////////////////////////////////////////////
121

122    /**
123     * {@inheritDoc}
124     */

125    public void widgetSelected(
126       SelectionEvent event
127    )
128    {
129       // Clean up any previous editor control
130
Control oldEditor = m_editor.getEditor();
131       if (oldEditor != null)
132       {
133          oldEditor.dispose();
134       }
135
136       // Identify the selected row
137
int iIndex = m_table.getSelectionIndex();
138       if (iIndex != -1)
139       {
140          final TableItem item = m_table.getItem(iIndex);
141
142          if ((m_strButtonText != null) && (m_strButtonText.length() > 0))
143          {
144             // The control that will be the editor must be a child of the Table
145
if (m_toggleButton != null)
146             {
147                m_toggleButton.dispose();
148             }
149             m_toggleButton = new Button(m_table, SWT.NONE);
150             m_toggleButton.setText(m_strButtonText);
151             ResourceManager.getInstance().setBigButtonFont(m_toggleButton);
152             m_toggleButton.addSelectionListener(new DelegatingSelectionAdapter()
153             {
154                public void widgetSelected(
155                   SelectionEvent event
156                )
157                {
158                   if (!m_editor.getItem().isDisposed())
159                   {
160                      notifyToggleListeners(m_editor.getItem(),
161                                            m_iColumn,
162                                            m_editor.getItem().getData(),
163                                            m_editor.getItem().getText(m_iColumn));
164                   }
165                }
166             });
167    
168             //The text editor must have the same size as the cell and must
169
//not be any smaller than 50 pixels.
170
m_editor.horizontalAlignment = SWT.RIGHT;
171             m_editor.grabHorizontal = false; // don't stretch to the whole cell
172
m_editor.minimumWidth = 40;
173    
174             // Open the text editor in the specified column of the selected row.
175
m_editor.setEditor(m_toggleButton, item, m_iColumn);
176    
177             // Assign focus to the text control
178
m_toggleButton.setFocus();
179          }
180          else
181          {
182             // Since there is no button text toggle immidiately on click
183
notifyToggleListeners(item, m_iColumn, item.getData(),
184                                   item.getText(m_iColumn));
185          }
186       }
187    }
188
189    /**
190     * Add listener for this object.
191     *
192     * @param listener - listener to add
193     */

194    public void addToggleListener(
195       ToggleButtonListener listener
196    )
197    {
198       m_setListeners.add(listener);
199    }
200    
201    /**
202     * Remove listener from this object.
203     *
204     * @param listener - listener to remove
205     */

206    public void removeToggleListener(
207       ToggleButtonListener listener
208    )
209    {
210       m_setListeners.remove(listener);
211    }
212    
213    /**
214     * Hide editor if it is displayed in current table.
215     */

216    public void hideEditor(
217    )
218    {
219       if (m_editor != null)
220       {
221          Control oldEditor = m_editor.getEditor();
222          if (oldEditor != null)
223          {
224             oldEditor.dispose();
225          }
226
227          // The control that will be the editor must be a child of the Table
228
if (m_toggleButton != null)
229          {
230             m_toggleButton.dispose();
231             m_toggleButton = null;
232          }
233       }
234    }
235    
236    /**
237     * Check if the editor is visble
238     *
239     * @return true if editor is visible
240     */

241    public boolean isVisible()
242    {
243       boolean bReturn = false;
244       
245       if (m_editor != null
246           && m_toggleButton != null
247           && !m_toggleButton.isDisposed())
248       {
249          bReturn = true;
250       }
251       
252       return bReturn;
253    }
254
255    // Helper methods ///////////////////////////////////////////////////////////
256

257    /**
258     * Notify all listeners about event.
259     *
260     * @param item - table item which was toggled
261     * @param iColumn - column which was toggled
262     * @param objData - data represented by the control
263     * @param strValue - what was the value and the moment of modification
264     */

265    protected void notifyToggleListeners(
266       TableItem item,
267       int iColumn,
268       Object JavaDoc objData,
269       String JavaDoc strValue
270    )
271    {
272       for (Iterator JavaDoc items = m_setListeners.iterator(); items.hasNext();)
273       {
274          ((ToggleButtonListener)items.next()).valueToggled(item, iColumn,
275                                                            objData, strValue);
276       }
277    }
278 }
279
Popular Tags