comparison org.eclipse.jface/src/org/eclipse/jface/viewers/TreeViewerFocusCellManager.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 * - fix in bug: 195908, 210752
12 * Port to the D programming language:
13 * Frank Benoit <benoit@tionex.de>
14 *******************************************************************************/
15
16 module org.eclipse.jface.viewers.TreeViewerFocusCellManager;
17
18 import org.eclipse.jface.viewers.SWTFocusCellManager;
19 import org.eclipse.jface.viewers.CellNavigationStrategy;
20 import org.eclipse.jface.viewers.TreeViewer;
21 import org.eclipse.jface.viewers.FocusCellHighlighter;
22 import org.eclipse.jface.viewers.ViewerCell;
23 import org.eclipse.jface.viewers.ColumnViewer;
24
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.widgets.Event;
27 import org.eclipse.swt.widgets.Item;
28 import org.eclipse.swt.widgets.Tree;
29 import org.eclipse.swt.widgets.TreeItem;
30
31 import java.lang.all;
32 import java.util.Set;
33
34 /**
35 * This class is responsible to provide the concept of cells for {@link Tree}.
36 * This concept is needed to provide features like editor activation with the
37 * keyboard
38 *
39 * @since 3.3
40 *
41 */
42 public class TreeViewerFocusCellManager : SWTFocusCellManager {
43 private static /+final+/ CellNavigationStrategy TREE_NAVIGATE;
44 static this(){
45 TREE_NAVIGATE = new class CellNavigationStrategy {
46 public void collapse(ColumnViewer viewer, ViewerCell cellToCollapse,
47 Event event) {
48 if (cellToCollapse !is null) {
49 (cast(TreeItem) cellToCollapse.getItem()).setExpanded(false);
50 }
51 }
52
53 public void expand(ColumnViewer viewer, ViewerCell cellToExpand,
54 Event event) {
55 if (cellToExpand !is null) {
56 TreeViewer v = cast(TreeViewer) viewer;
57 v.setExpandedState(v.getTreePathFromItem_package(cast(Item)cellToExpand
58 .getItem()), true);
59 }
60 }
61
62 public bool isCollapseEvent(ColumnViewer viewer,
63 ViewerCell cellToCollapse, Event event) {
64
65 if (cellToCollapse is null) {
66 return false;
67 }
68
69 return cellToCollapse !is null
70 && (cast(TreeItem) cellToCollapse.getItem()).getExpanded()
71 && event.keyCode is SWT.ARROW_LEFT
72 && isFirstColumnCell(cellToCollapse);
73 }
74
75 public bool isExpandEvent(ColumnViewer viewer,
76 ViewerCell cellToExpand, Event event) {
77
78 if (cellToExpand is null) {
79 return false;
80 }
81
82 return cellToExpand !is null
83 && (cast(TreeItem) cellToExpand.getItem()).getItemCount() > 0
84 && !(cast(TreeItem) cellToExpand.getItem()).getExpanded()
85 && event.keyCode is SWT.ARROW_RIGHT
86 && isFirstColumnCell(cellToExpand);
87 }
88
89 private bool isFirstColumnCell(ViewerCell cell) {
90 return cell.getViewerRow().getVisualIndex_package(cell.getColumnIndex()) is 0;
91 }
92 };
93 }
94
95 /**
96 * Create a new manager using a default navigation strategy:
97 * <ul>
98 * <li><code>SWT.ARROW_UP</code>: navigate to cell above</li>
99 * <li><code>SWT.ARROW_DOWN</code>: navigate to cell below</li>
100 * <li><code>SWT.ARROW_RIGHT</code>: on first column (collapses if item
101 * is expanded) else navigate to next visible cell on the right</li>
102 * <li><code>SWT.ARROW_LEFT</code>: on first column (expands if item is
103 * collapsed) else navigate to next visible cell on the left</li>
104 * </ul>
105 *
106 * @param viewer
107 * the viewer the manager is bound to
108 * @param focusDrawingDelegate
109 * the delegate responsible to highlight selected cell
110 */
111 public this(TreeViewer viewer,
112 FocusCellHighlighter focusDrawingDelegate) {
113 this(viewer, focusDrawingDelegate, TREE_NAVIGATE);
114 }
115
116 /**
117 * Create a new manager with a custom navigation strategy
118 *
119 * @param viewer
120 * the viewer the manager is bound to
121 * @param focusDrawingDelegate
122 * the delegate responsible to highlight selected cell
123 * @param navigationStrategy
124 * the strategy used to navigate the cells
125 * @since 3.4
126 */
127 public this(TreeViewer viewer,
128 FocusCellHighlighter focusDrawingDelegate,
129 CellNavigationStrategy navigationStrategy) {
130 super(viewer, focusDrawingDelegate, navigationStrategy);
131 }
132
133 override ViewerCell getInitialFocusCell() {
134 Tree tree = cast(Tree) getViewer().getControl();
135
136 if (! tree.isDisposed() && tree.getItemCount() > 0 && ! tree.getItem(0).isDisposed()) {
137 return getViewer().getViewerRowFromItem_package(tree.getItem(0)).getCell(0);
138 }
139
140 return null;
141 }
142
143 public ViewerCell getFocusCell() {
144 ViewerCell cell = super.getFocusCell();
145 Tree t = cast(Tree) getViewer().getControl();
146
147 // It is possible that the selection has changed under the hood
148 if (cell !is null) {
149 if (t.getSelection().length is 1
150 && t.getSelection()[0] !is cell.getItem()) {
151 setFocusCell(getViewer().getViewerRowFromItem_package(
152 t.getSelection()[0]).getCell(cell.getColumnIndex()));
153 }
154 }
155
156 return super.getFocusCell();
157 }
158 }