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