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: * Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
011: * - fix in bug: 195908
012: ******************************************************************************/package org.eclipse.jface.viewers;
013:
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.widgets.Event;
016: import org.eclipse.swt.widgets.Item;
017: import org.eclipse.swt.widgets.Tree;
018: import org.eclipse.swt.widgets.TreeItem;
019:
020: /**
021: * This class is responsible to provide the concept of cells for {@link Tree}.
022: * This concept is needed to provide features like editor activation with the
023: * keyboard
024: *
025: * @since 3.3
026: *
027: */
028: public class TreeViewerFocusCellManager extends SWTFocusCellManager {
029: private static final CellNavigationStrategy TREE_NAVIGATE = new CellNavigationStrategy() {
030: public void collapse(ColumnViewer viewer,
031: ViewerCell cellToCollapse, Event event) {
032: if (cellToCollapse != null) {
033: ((TreeItem) cellToCollapse.getItem())
034: .setExpanded(false);
035: }
036: }
037:
038: public void expand(ColumnViewer viewer,
039: ViewerCell cellToExpand, Event event) {
040: if (cellToExpand != null) {
041: TreeViewer v = (TreeViewer) viewer;
042: v.setExpandedState(v
043: .getTreePathFromItem((Item) cellToExpand
044: .getItem()), true);
045: }
046: }
047:
048: public boolean isCollapseEvent(ColumnViewer viewer,
049: ViewerCell cellToCollapse, Event event) {
050:
051: if (cellToCollapse == null) {
052: return false;
053: }
054:
055: return cellToCollapse != null
056: && ((TreeItem) cellToCollapse.getItem())
057: .getExpanded()
058: && event.keyCode == SWT.ARROW_LEFT
059: && isFirstColumnCell(cellToCollapse);
060: }
061:
062: public boolean isExpandEvent(ColumnViewer viewer,
063: ViewerCell cellToExpand, Event event) {
064:
065: if (cellToExpand == null) {
066: return false;
067: }
068:
069: return cellToExpand != null
070: && ((TreeItem) cellToExpand.getItem())
071: .getItemCount() > 0
072: && !((TreeItem) cellToExpand.getItem())
073: .getExpanded()
074: && event.keyCode == SWT.ARROW_RIGHT
075: && isFirstColumnCell(cellToExpand);
076: }
077:
078: private boolean isFirstColumnCell(ViewerCell cell) {
079: return cell.getViewerRow().getVisualIndex(
080: cell.getColumnIndex()) == 0;
081: }
082: };
083:
084: /**
085: * Create a new manager
086: *
087: * @param viewer
088: * the viewer the manager is bound to
089: * @param focusDrawingDelegate
090: * the delegate responsible to highlight selected cell
091: */
092: public TreeViewerFocusCellManager(TreeViewer viewer,
093: FocusCellHighlighter focusDrawingDelegate) {
094: super (viewer, focusDrawingDelegate, TREE_NAVIGATE);
095: }
096:
097: ViewerCell getInitialFocusCell() {
098: Tree tree = (Tree) getViewer().getControl();
099:
100: if (tree.getItemCount() > 0) {
101: return getViewer().getViewerRowFromItem(tree.getItem(0))
102: .getCell(0);
103: }
104:
105: return null;
106: }
107: }
|