KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.eclipse.swt.widgets.Item;
17 import org.eclipse.swt.widgets.Tree;
18 import org.eclipse.swt.widgets.TreeItem;
19
20 /**
21  * This class is responsible to provide the concept of cells for {@link Tree}.
22  * This concept is needed to provide features like editor activation with the
23  * keyboard
24  *
25  * @since 3.3
26  *
27  */

28 public class TreeViewerFocusCellManager extends SWTFocusCellManager {
29     private static final CellNavigationStrategy TREE_NAVIGATE = new CellNavigationStrategy() {
30         public void collapse(ColumnViewer viewer, ViewerCell cellToCollapse,
31                 Event event) {
32             if (cellToCollapse != null) {
33                 ((TreeItem) cellToCollapse.getItem()).setExpanded(false);
34             }
35         }
36
37         public void expand(ColumnViewer viewer, ViewerCell cellToExpand,
38                 Event event) {
39             if (cellToExpand != null) {
40                 TreeViewer v = (TreeViewer) viewer;
41                 v.setExpandedState(v
42                         .getTreePathFromItem((Item)cellToExpand.getItem()), true);
43             }
44         }
45
46         public boolean isCollapseEvent(ColumnViewer viewer,
47                 ViewerCell cellToCollapse, Event event) {
48             return cellToCollapse != null
49                     && ((TreeItem) cellToCollapse.getItem()).getExpanded()
50                     && cellToCollapse.getColumnIndex() == 0
51                     && event.keyCode == SWT.ARROW_LEFT;
52         }
53
54         public boolean isExpandEvent(ColumnViewer viewer,
55                 ViewerCell cellToExpand, Event event) {
56             return cellToExpand != null
57                     && ((TreeItem) cellToExpand.getItem()).getItemCount() > 0
58                     && !((TreeItem) cellToExpand.getItem()).getExpanded()
59                     && cellToExpand.getColumnIndex() == 0
60                     && event.keyCode == SWT.ARROW_RIGHT;
61         }
62     };
63     
64     /**
65      * Create a new manager
66      *
67      * @param viewer
68      * the viewer the manager is bound to
69      * @param focusDrawingDelegate
70      * the delegate responsible to highlight selected cell
71      */

72     public TreeViewerFocusCellManager(TreeViewer viewer,
73             FocusCellHighlighter focusDrawingDelegate) {
74         super(viewer, focusDrawingDelegate,TREE_NAVIGATE);
75     }
76
77     ViewerCell getInitialFocusCell() {
78         Tree tree = (Tree) getViewer().getControl();
79         
80         if( tree.getItemCount() > 0 ) {
81             return getViewer().getViewerRowFromItem(tree.getItem(0)).getCell(0);
82         }
83         
84         return null;
85     }
86 }
87
Popular Tags