Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b1d562f8f9ecfd85c243ac37a9ba781d5a6a1647 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 ******************************************************************************/

package org.eclipse.jface.viewers;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;

/**
 * This class implementation the strategy how the table is navigated using the
 * keyboard.
 * 
 * <p>
 * <b>Subclasses can implement their custom navigation algorithms</b>
 * </p>
 * 
 * @since 3.3
 * 
 */
public class CellNavigationStrategy {
	/**
	 * is the given event an event which moves the selection to another cell
	 * 
	 * @param viewer
	 *            the viewer we are working for
	 * @param event
	 *            the key event
	 * @return <code>true</code> if a new cell is searched
	 */
	public boolean isNavigationEvent(ColumnViewer viewer, Event event) {
		switch (event.keyCode) {
		case SWT.ARROW_UP:
		case SWT.ARROW_DOWN:
		case SWT.ARROW_LEFT:
		case SWT.ARROW_RIGHT:
		case SWT.HOME:
		case SWT.PAGE_DOWN:
		case SWT.PAGE_UP:
		case SWT.END:
			return true;
		default:
			return false;
		}
	}

	/**
	 * @param viewer
	 *            the viewer we are working for
	 * @param cellToCollapse
	 *            the cell to collapse
	 * @param event
	 *            the key event
	 * @return <code>true</code> if this event triggers collapsing of a node
	 */
	public boolean isCollapseEvent(ColumnViewer viewer,
			ViewerCell cellToCollapse, Event event) {
		return false;
	}

	/**
	 * @param viewer
	 *            the viewer we are working for
	 * @param cellToExpand
	 *            the cell to expand
	 * @param event
	 *            the key event
	 * @return <code>true</code> if this event triggers expanding of a node
	 */
	public boolean isExpandEvent(ColumnViewer viewer, ViewerCell cellToExpand,
			Event event) {
		return false;
	}

	/**
	 * @param viewer
	 *            the viewer working for
	 * @param cellToExpand
	 *            the cell the user wants to expand
	 * @param event
	 *            the event triggering the expansion
	 */
	public void expand(ColumnViewer viewer, ViewerCell cellToExpand, Event event) {

	}

	/**
	 * @param viewer
	 *            the viewer working for
	 * @param cellToCollapse
	 *            the cell the user wants to collapse
	 * @param event
	 *            the event triggering the expansion
	 */
	public void collapse(ColumnViewer viewer, ViewerCell cellToCollapse,
			Event event) {

	}

	/**
	 * @param viewer
	 *            the viewer we are working for
	 * @param currentSelectedCell
	 *            the cell currently selected
	 * @param event
	 *            the key event
	 * @return the cell which is highlighted next or <code>null</code> if the
	 *         default implementation is taken. E.g. it's fairly impossible to
	 *         react on PAGE_DOWN requests
	 */
	public ViewerCell findSelectedCell(ColumnViewer viewer,
			ViewerCell currentSelectedCell, Event event) {

		switch (event.keyCode) {
		case SWT.ARROW_UP:
			if (currentSelectedCell != null) {
				return currentSelectedCell.getNeighbor(ViewerCell.ABOVE, false);
			}
			break;
		case SWT.ARROW_DOWN:
			if (currentSelectedCell != null) {
				return currentSelectedCell.getNeighbor(ViewerCell.BELOW, false);
			}
			break;
		case SWT.ARROW_LEFT:
			if (currentSelectedCell != null) {
				return currentSelectedCell.getNeighbor(ViewerCell.LEFT, true);
			}
			break;
		case SWT.ARROW_RIGHT:
			if (currentSelectedCell != null) {
				return currentSelectedCell.getNeighbor(ViewerCell.RIGHT, true);
			}
			break;
		}

		return null;
	}

	/**
	 * This method is consulted to decide whether an event has to be canceled or
	 * not. By default events who collapse/expand tree-nodes are canceled
	 * 
	 * @param viewer
	 *            the viewer working for
	 * @param event
	 *            the event
	 * @return <code>true</code> if the event has to be canceled
	 */
	public boolean shouldCancelEvent(ColumnViewer viewer, Event event) {
		return event.keyCode == SWT.ARROW_LEFT
				|| event.keyCode == SWT.ARROW_RIGHT;
	}

	/**
	 * This method is called by the framework to initialize this navigation
	 * strategy object. Subclasses may extend.
	 */
	protected void init() {
	}
}

Back to the top