KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > CellNavigationStrategy


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  ******************************************************************************/

11
12 package org.eclipse.jface.viewers;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.widgets.Event;
16
17 /**
18  * This class implementation the strategy how the table is navigated using the
19  * keyboard.
20  *
21  * <p>
22  * <b>Subclasses can implement their custom navigation algorithms</b>
23  * </p>
24  *
25  * @since 3.3
26  *
27  */

28 public class CellNavigationStrategy {
29     /**
30      * is the given event an event which moves the selection to another cell
31      *
32      * @param viewer
33      * the viewer we are working for
34      * @param event
35      * the key event
36      * @return <code>true</code> if a new cell is searched
37      */

38     public boolean isNavigationEvent(ColumnViewer viewer, Event event) {
39         switch (event.keyCode) {
40         case SWT.ARROW_UP:
41         case SWT.ARROW_DOWN:
42         case SWT.ARROW_LEFT:
43         case SWT.ARROW_RIGHT:
44         case SWT.HOME:
45         case SWT.PAGE_DOWN:
46         case SWT.PAGE_UP:
47         case SWT.END:
48             return true;
49         default:
50             return false;
51         }
52     }
53
54     /**
55      * @param viewer
56      * the viewer we are working for
57      * @param cellToCollapse
58      * the cell to collapse
59      * @param event
60      * the key event
61      * @return <code>true</code> if this event triggers collapsing of a node
62      */

63     public boolean isCollapseEvent(ColumnViewer viewer,
64             ViewerCell cellToCollapse, Event event) {
65         return false;
66     }
67
68     /**
69      * @param viewer
70      * the viewer we are working for
71      * @param cellToExpand
72      * the cell to expand
73      * @param event
74      * the key event
75      * @return <code>true</code> if this event triggers expanding of a node
76      */

77     public boolean isExpandEvent(ColumnViewer viewer, ViewerCell cellToExpand,
78             Event event) {
79         return false;
80     }
81
82     /**
83      * @param viewer
84      * the viewer working for
85      * @param cellToExpand
86      * the cell the user wants to expand
87      * @param event
88      * the event triggering the expansion
89      */

90     public void expand(ColumnViewer viewer, ViewerCell cellToExpand, Event event) {
91
92     }
93
94     /**
95      * @param viewer
96      * the viewer working for
97      * @param cellToCollapse
98      * the cell the user wants to collapse
99      * @param event
100      * the event triggering the expansion
101      */

102     public void collapse(ColumnViewer viewer, ViewerCell cellToCollapse,
103             Event event) {
104
105     }
106
107     /**
108      * @param viewer
109      * the viewer we are working for
110      * @param currentSelectedCell
111      * the cell currently selected
112      * @param event
113      * the key event
114      * @return the cell which is highlighted next or <code>null</code> if the
115      * default implementation is taken. E.g. it's fairly impossible to
116      * react on PAGE_DOWN requests
117      */

118     public ViewerCell findSelectedCell(ColumnViewer viewer,
119             ViewerCell currentSelectedCell, Event event) {
120
121         switch (event.keyCode) {
122         case SWT.ARROW_UP:
123             if (currentSelectedCell != null) {
124                 return currentSelectedCell.getNeighbor(ViewerCell.ABOVE, false);
125             }
126             break;
127         case SWT.ARROW_DOWN:
128             if (currentSelectedCell != null) {
129                 return currentSelectedCell.getNeighbor(ViewerCell.BELOW, false);
130             }
131             break;
132         case SWT.ARROW_LEFT:
133             if (currentSelectedCell != null) {
134                 return currentSelectedCell.getNeighbor(ViewerCell.LEFT, true);
135             }
136             break;
137         case SWT.ARROW_RIGHT:
138             if (currentSelectedCell != null) {
139                 return currentSelectedCell.getNeighbor(ViewerCell.RIGHT, true);
140             }
141             break;
142         }
143
144         return null;
145     }
146
147     /**
148      * This method is consulted to decide whether an event has to be canceled or
149      * not. By default events who collapse/expand tree-nodes are canceled
150      *
151      * @param viewer
152      * the viewer working for
153      * @param event
154      * the event
155      * @return <code>true</code> if the event has to be canceled
156      */

157     public boolean shouldCancelEvent(ColumnViewer viewer, Event event) {
158         return event.keyCode == SWT.ARROW_LEFT
159                 || event.keyCode == SWT.ARROW_RIGHT;
160     }
161
162     /**
163      * This method is called by the framework to initialize this navigation
164      * strategy object. Subclasses may extend.
165      */

166     protected void init() {
167     }
168 }
169
Popular Tags