Mercurial > projects > dwt-addons
comparison dwtx/jface/viewers/SWTFocusCellManager.d @ 10:b6c35faf97c8
Viewers
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 31 Mar 2008 00:47:19 +0200 |
parents | |
children | 46a6e0e6ccd4 |
comparison
equal
deleted
inserted
replaced
9:6c14e54dfc11 | 10:b6c35faf97c8 |
---|---|
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 * Port to the D programming language: | |
13 * Frank Benoit <benoit@tionex.de> | |
14 ******************************************************************************/ | |
15 | |
16 module dwtx.jface.viewers.SWTFocusCellManager; | |
17 | |
18 import dwtx.jface.viewers.CellNavigationStrategy; | |
19 import dwtx.jface.viewers.ColumnViewer; | |
20 import dwtx.jface.viewers.ViewerCell; | |
21 import dwtx.jface.viewers.ViewerRow; | |
22 import dwtx.jface.viewers.FocusCellHighlighter; | |
23 import dwtx.jface.viewers.Viewer; | |
24 import dwtx.jface.viewers.ISelectionChangedListener; | |
25 import dwtx.jface.viewers.SelectionChangedEvent; | |
26 | |
27 import dwt.DWT; | |
28 import dwt.events.DisposeEvent; | |
29 import dwt.events.DisposeListener; | |
30 import dwt.graphics.Point; | |
31 import dwt.widgets.Event; | |
32 import dwt.widgets.Listener; | |
33 import dwtx.core.runtime.Assert; | |
34 | |
35 /** | |
36 * This class is responsible to provide cell management base features for the | |
37 * DWT-Controls {@link dwt.widgets.Table} and | |
38 * {@link dwt.widgets.Tree}. | |
39 * | |
40 * @since 3.3 | |
41 * | |
42 */ | |
43 abstract class SWTFocusCellManager { | |
44 | |
45 private CellNavigationStrategy navigationStrategy; | |
46 | |
47 private ColumnViewer viewer; | |
48 | |
49 private ViewerCell focusCell; | |
50 | |
51 private FocusCellHighlighter cellHighlighter; | |
52 | |
53 private DisposeListener itemDeletionListener; | |
54 | |
55 /** | |
56 * @param viewer | |
57 * @param focusDrawingDelegate | |
58 * @param navigationDelegate | |
59 */ | |
60 public this(ColumnViewer viewer, | |
61 FocusCellHighlighter focusDrawingDelegate, | |
62 CellNavigationStrategy navigationDelegate) { | |
63 | |
64 itemDeletionListener = new class DisposeListener { | |
65 public void widgetDisposed(DisposeEvent e) { | |
66 setFocusCell(null); | |
67 } | |
68 }; | |
69 | |
70 this.viewer = viewer; | |
71 this.cellHighlighter = focusDrawingDelegate; | |
72 this.navigationStrategy = navigationDelegate; | |
73 hookListener(viewer); | |
74 } | |
75 | |
76 /** | |
77 * This method is called by the framework to initialize this cell manager. | |
78 */ | |
79 void init() { | |
80 this.cellHighlighter.init_package(); | |
81 this.navigationStrategy.init_package(); | |
82 } | |
83 | |
84 private void handleMouseDown(Event event) { | |
85 ViewerCell cell = viewer.getCell(new Point(event.x, event.y)); | |
86 if (cell !is null) { | |
87 | |
88 if (!cell.opEquals(focusCell)) { | |
89 setFocusCell(cell); | |
90 } | |
91 } | |
92 } | |
93 | |
94 private void handleKeyDown(Event event) { | |
95 ViewerCell tmp = null; | |
96 | |
97 if (navigationStrategy.isCollapseEvent(viewer, focusCell, event)) { | |
98 navigationStrategy.collapse(viewer, focusCell, event); | |
99 } else if (navigationStrategy.isExpandEvent(viewer, focusCell, event)) { | |
100 navigationStrategy.expand(viewer, focusCell, event); | |
101 } else if (navigationStrategy.isNavigationEvent(viewer, event)) { | |
102 tmp = navigationStrategy.findSelectedCell(viewer, focusCell, event); | |
103 | |
104 if (tmp !is null) { | |
105 if (!tmp.opEquals(focusCell)) { | |
106 setFocusCell(tmp); | |
107 } | |
108 } | |
109 } | |
110 | |
111 if (navigationStrategy.shouldCancelEvent(viewer, event)) { | |
112 event.doit = false; | |
113 } | |
114 } | |
115 | |
116 private void handleSelection(Event event) { | |
117 if (focusCell !is null && focusCell.getItem() !is event.item | |
118 && event.item !is null) { | |
119 ViewerRow row = viewer.getViewerRowFromItem_package(event.item); | |
120 Assert | |
121 .isNotNull(row, | |
122 "Internal Structure invalid. Row item has no row ViewerRow assigned"); //$NON-NLS-1$ | |
123 ViewerCell tmp = row.getCell(focusCell.getColumnIndex()); | |
124 if (!focusCell.opEquals(tmp)) { | |
125 setFocusCell(tmp); | |
126 } | |
127 } | |
128 } | |
129 | |
130 private void handleFocusIn(Event event) { | |
131 if (focusCell is null) { | |
132 setFocusCell(getInitialFocusCell()); | |
133 } | |
134 } | |
135 | |
136 abstract ViewerCell getInitialFocusCell(); | |
137 | |
138 private void hookListener(ColumnViewer viewer) { | |
139 Listener listener = new class Listener { | |
140 | |
141 public void handleEvent(Event event) { | |
142 switch (event.type) { | |
143 case DWT.MouseDown: | |
144 handleMouseDown(event); | |
145 break; | |
146 case DWT.KeyDown: | |
147 handleKeyDown(event); | |
148 break; | |
149 case DWT.Selection: | |
150 handleSelection(event); | |
151 break; | |
152 case DWT.FocusIn: | |
153 handleFocusIn(event); | |
154 break; | |
155 } | |
156 } | |
157 }; | |
158 | |
159 viewer.getControl().addListener(DWT.MouseDown, listener); | |
160 viewer.getControl().addListener(DWT.KeyDown, listener); | |
161 viewer.getControl().addListener(DWT.Selection, listener); | |
162 viewer.addSelectionChangedListener(new class ISelectionChangedListener { | |
163 | |
164 public void selectionChanged(SelectionChangedEvent event) { | |
165 if( event.getSelection_package.isEmpty() ) { | |
166 setFocusCell(null); | |
167 } | |
168 } | |
169 | |
170 }); | |
171 viewer.getControl().addListener(DWT.FocusIn, listener); | |
172 } | |
173 | |
174 /** | |
175 * @return the cell with the focus | |
176 * | |
177 */ | |
178 public ViewerCell getFocusCell() { | |
179 return focusCell; | |
180 } | |
181 | |
182 void setFocusCell(ViewerCell focusCell) { | |
183 if( this.focusCell !is null && ! this.focusCell.getItem().isDisposed() ) { | |
184 this.focusCell.getItem().removeDisposeListener(itemDeletionListener); | |
185 } | |
186 | |
187 this.focusCell = focusCell; | |
188 | |
189 if( this.focusCell !is null && ! this.focusCell.getItem().isDisposed() ) { | |
190 this.focusCell.getItem().addDisposeListener(itemDeletionListener); | |
191 } | |
192 | |
193 this.cellHighlighter.focusCellChanged_package(focusCell); | |
194 } | |
195 | |
196 ColumnViewer getViewer() { | |
197 return viewer; | |
198 } | |
199 } |