KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > SWTFocusCellManager


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
11  * - bug fix for bug 187189
12  ******************************************************************************/

13
14 package org.eclipse.jface.viewers;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.DisposeEvent;
19 import org.eclipse.swt.events.DisposeListener;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.widgets.Event;
22 import org.eclipse.swt.widgets.Listener;
23
24 /**
25  * This class is responsible to provide cell management base features for the
26  * SWT-Controls {@link org.eclipse.swt.widgets.Table} and
27  * {@link org.eclipse.swt.widgets.Tree}.
28  *
29  * @since 3.3
30  *
31  */

32 abstract class SWTFocusCellManager {
33
34     private CellNavigationStrategy navigationStrategy;
35
36     private ColumnViewer viewer;
37
38     private ViewerCell focusCell;
39
40     private FocusCellHighlighter cellHighlighter;
41     
42     private DisposeListener itemDeletionListener = new DisposeListener() {
43
44         public void widgetDisposed(DisposeEvent e) {
45             setFocusCell(null);
46         }
47         
48     };
49
50     /**
51      * @param viewer
52      * @param focusDrawingDelegate
53      * @param navigationDelegate
54      */

55     public SWTFocusCellManager(ColumnViewer viewer,
56             FocusCellHighlighter focusDrawingDelegate,
57             CellNavigationStrategy navigationDelegate) {
58         this.viewer = viewer;
59         this.cellHighlighter = focusDrawingDelegate;
60         this.navigationStrategy = navigationDelegate;
61         hookListener(viewer);
62     }
63
64     /**
65      * This method is called by the framework to initialize this cell manager.
66      */

67     void init() {
68         this.cellHighlighter.init();
69         this.navigationStrategy.init();
70     }
71
72     private void handleMouseDown(Event event) {
73         ViewerCell cell = viewer.getCell(new Point(event.x, event.y));
74         if (cell != null) {
75
76             if (!cell.equals(focusCell)) {
77                 setFocusCell(cell);
78             }
79         }
80     }
81
82     private void handleKeyDown(Event event) {
83         ViewerCell tmp = null;
84
85         if (navigationStrategy.isCollapseEvent(viewer, focusCell, event)) {
86             navigationStrategy.collapse(viewer, focusCell, event);
87         } else if (navigationStrategy.isExpandEvent(viewer, focusCell, event)) {
88             navigationStrategy.expand(viewer, focusCell, event);
89         } else if (navigationStrategy.isNavigationEvent(viewer, event)) {
90             tmp = navigationStrategy.findSelectedCell(viewer, focusCell, event);
91
92             if (tmp != null) {
93                 if (!tmp.equals(focusCell)) {
94                     setFocusCell(tmp);
95                 }
96             }
97         }
98
99         if (navigationStrategy.shouldCancelEvent(viewer, event)) {
100             event.doit = false;
101         }
102     }
103
104     private void handleSelection(Event event) {
105         if (focusCell != null && focusCell.getItem() != event.item
106                 && event.item != null) {
107             ViewerRow row = viewer.getViewerRowFromItem(event.item);
108             Assert
109                     .isNotNull(row,
110                             "Internal Structure invalid. Row item has no row ViewerRow assigned"); //$NON-NLS-1$
111
ViewerCell tmp = row.getCell(focusCell.getColumnIndex());
112             if (!focusCell.equals(tmp)) {
113                 setFocusCell(tmp);
114             }
115         }
116     }
117
118     private void handleFocusIn(Event event) {
119         if (focusCell == null) {
120             setFocusCell(getInitialFocusCell());
121         }
122     }
123
124     abstract ViewerCell getInitialFocusCell();
125
126     private void hookListener(ColumnViewer viewer) {
127         Listener listener = new Listener() {
128
129             public void handleEvent(Event event) {
130                 switch (event.type) {
131                 case SWT.MouseDown:
132                     handleMouseDown(event);
133                     break;
134                 case SWT.KeyDown:
135                     handleKeyDown(event);
136                     break;
137                 case SWT.Selection:
138                     handleSelection(event);
139                     break;
140                 case SWT.FocusIn:
141                     handleFocusIn(event);
142                     break;
143                 }
144             }
145         };
146
147         viewer.getControl().addListener(SWT.MouseDown, listener);
148         viewer.getControl().addListener(SWT.KeyDown, listener);
149         viewer.getControl().addListener(SWT.Selection, listener);
150         viewer.addSelectionChangedListener(new ISelectionChangedListener() {
151
152             public void selectionChanged(SelectionChangedEvent event) {
153                 if( event.selection.isEmpty() ) {
154                     setFocusCell(null);
155                 }
156             }
157             
158         });
159         viewer.getControl().addListener(SWT.FocusIn, listener);
160     }
161
162     /**
163      * @return the cell with the focus
164      *
165      */

166     public ViewerCell getFocusCell() {
167         return focusCell;
168     }
169
170     void setFocusCell(ViewerCell focusCell) {
171         if( this.focusCell != null && ! this.focusCell.getItem().isDisposed() ) {
172             this.focusCell.getItem().removeDisposeListener(itemDeletionListener);
173         }
174         
175         this.focusCell = focusCell;
176         
177         if( this.focusCell != null && ! this.focusCell.getItem().isDisposed() ) {
178             this.focusCell.getItem().addDisposeListener(itemDeletionListener);
179         }
180         
181         this.cellHighlighter.focusCellChanged(focusCell);
182     }
183
184     ColumnViewer getViewer() {
185         return viewer;
186     }
187 }
188
Popular Tags