001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.jface.viewers;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.widgets.Event;
014:
015: /**
016: * This class implementation the strategy how the table is navigated using the
017: * keyboard.
018: *
019: * <p>
020: * <b>Subclasses can implement their custom navigation algorithms</b>
021: * </p>
022: *
023: * @since 3.3
024: *
025: */
026: public class CellNavigationStrategy {
027: /**
028: * is the given event an event which moves the selection to another cell
029: *
030: * @param viewer
031: * the viewer we are working for
032: * @param event
033: * the key event
034: * @return <code>true</code> if a new cell is searched
035: */
036: public boolean isNavigationEvent(ColumnViewer viewer, Event event) {
037: switch (event.keyCode) {
038: case SWT.ARROW_UP:
039: case SWT.ARROW_DOWN:
040: case SWT.ARROW_LEFT:
041: case SWT.ARROW_RIGHT:
042: case SWT.HOME:
043: case SWT.PAGE_DOWN:
044: case SWT.PAGE_UP:
045: case SWT.END:
046: return true;
047: default:
048: return false;
049: }
050: }
051:
052: /**
053: * @param viewer
054: * the viewer we are working for
055: * @param cellToCollapse
056: * the cell to collapse
057: * @param event
058: * the key event
059: * @return <code>true</code> if this event triggers collapsing of a node
060: */
061: public boolean isCollapseEvent(ColumnViewer viewer,
062: ViewerCell cellToCollapse, Event event) {
063: return false;
064: }
065:
066: /**
067: * @param viewer
068: * the viewer we are working for
069: * @param cellToExpand
070: * the cell to expand
071: * @param event
072: * the key event
073: * @return <code>true</code> if this event triggers expanding of a node
074: */
075: public boolean isExpandEvent(ColumnViewer viewer,
076: ViewerCell cellToExpand, Event event) {
077: return false;
078: }
079:
080: /**
081: * @param viewer
082: * the viewer working for
083: * @param cellToExpand
084: * the cell the user wants to expand
085: * @param event
086: * the event triggering the expansion
087: */
088: public void expand(ColumnViewer viewer, ViewerCell cellToExpand,
089: Event event) {
090:
091: }
092:
093: /**
094: * @param viewer
095: * the viewer working for
096: * @param cellToCollapse
097: * the cell the user wants to collapse
098: * @param event
099: * the event triggering the expansion
100: */
101: public void collapse(ColumnViewer viewer,
102: ViewerCell cellToCollapse, Event event) {
103:
104: }
105:
106: /**
107: * @param viewer
108: * the viewer we are working for
109: * @param currentSelectedCell
110: * the cell currently selected
111: * @param event
112: * the key event
113: * @return the cell which is highlighted next or <code>null</code> if the
114: * default implementation is taken. E.g. it's fairly impossible to
115: * react on PAGE_DOWN requests
116: */
117: public ViewerCell findSelectedCell(ColumnViewer viewer,
118: ViewerCell currentSelectedCell, Event event) {
119:
120: switch (event.keyCode) {
121: case SWT.ARROW_UP:
122: if (currentSelectedCell != null) {
123: return currentSelectedCell.getNeighbor(
124: ViewerCell.ABOVE, false);
125: }
126: break;
127: case SWT.ARROW_DOWN:
128: if (currentSelectedCell != null) {
129: return currentSelectedCell.getNeighbor(
130: ViewerCell.BELOW, false);
131: }
132: break;
133: case SWT.ARROW_LEFT:
134: if (currentSelectedCell != null) {
135: return currentSelectedCell.getNeighbor(ViewerCell.LEFT,
136: true);
137: }
138: break;
139: case SWT.ARROW_RIGHT:
140: if (currentSelectedCell != null) {
141: return currentSelectedCell.getNeighbor(
142: ViewerCell.RIGHT, true);
143: }
144: break;
145: }
146:
147: return null;
148: }
149:
150: /**
151: * This method is consulted to decide whether an event has to be canceled or
152: * not. By default events who collapse/expand tree-nodes are canceled
153: *
154: * @param viewer
155: * the viewer working for
156: * @param event
157: * the event
158: * @return <code>true</code> if the event has to be canceled
159: */
160: public boolean shouldCancelEvent(ColumnViewer viewer, Event event) {
161: return event.keyCode == SWT.ARROW_LEFT
162: || event.keyCode == SWT.ARROW_RIGHT;
163: }
164:
165: /**
166: * This method is called by the framework to initialize this navigation
167: * strategy object. Subclasses may extend.
168: */
169: protected void init() {
170: }
171: }
|