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: * fixes in bug 198665, 200731
012: ******************************************************************************/package org.eclipse.jface.viewers;
013:
014: import java.util.List;
015:
016: import org.eclipse.jface.viewers.CellEditor.LayoutData;
017: import org.eclipse.swt.custom.TreeEditor;
018: import org.eclipse.swt.widgets.Control;
019: import org.eclipse.swt.widgets.Item;
020: import org.eclipse.swt.widgets.Tree;
021: import org.eclipse.swt.widgets.TreeItem;
022:
023: /**
024: * This is an editor implementation for {@link Tree}
025: *
026: * @since 3.3
027: */
028: public class TreeViewerEditor extends ColumnViewerEditor {
029: /**
030: * This viewer's tree editor.
031: */
032: private TreeEditor treeEditor;
033:
034: private SWTFocusCellManager focusCellManager;
035:
036: /**
037: * @param viewer
038: * the viewer the editor is attached to
039: * @param focusCellManager
040: * the cell focus manager if one used or <code>null</code>
041: * @param editorActivationStrategy
042: * the strategy used to decide about the editor activation
043: * @param feature
044: * the feature mask
045: */
046: TreeViewerEditor(
047: TreeViewer viewer,
048: SWTFocusCellManager focusCellManager,
049: ColumnViewerEditorActivationStrategy editorActivationStrategy,
050: int feature) {
051: super (viewer, editorActivationStrategy, feature);
052: treeEditor = new TreeEditor(viewer.getTree());
053: this .focusCellManager = focusCellManager;
054: }
055:
056: /**
057: * Create a customized editor with focusable cells
058: *
059: * @param viewer
060: * the viewer the editor is created for
061: * @param focusCellManager
062: * the cell focus manager if one needed else <code>null</code>
063: * @param editorActivationStrategy
064: * activation strategy to control if an editor activated
065: * @param feature
066: * bit mask controlling the editor
067: * <ul>
068: * <li>{@link ColumnViewerEditor#DEFAULT}</li>
069: * <li>{@link ColumnViewerEditor#TABBING_CYCLE_IN_ROW}</li>
070: * <li>{@link ColumnViewerEditor#TABBING_HORIZONTAL}</li>
071: * <li>{@link ColumnViewerEditor#TABBING_MOVE_TO_ROW_NEIGHBOR}</li>
072: * <li>{@link ColumnViewerEditor#TABBING_VERTICAL}</li>
073: * </ul>
074: * @see #create(TreeViewer, ColumnViewerEditorActivationStrategy, int)
075: */
076: public static void create(
077: TreeViewer viewer,
078: SWTFocusCellManager focusCellManager,
079: ColumnViewerEditorActivationStrategy editorActivationStrategy,
080: int feature) {
081: TreeViewerEditor editor = new TreeViewerEditor(viewer,
082: focusCellManager, editorActivationStrategy, feature);
083: viewer.setColumnViewerEditor(editor);
084: if (focusCellManager != null) {
085: focusCellManager.init();
086: }
087: }
088:
089: /**
090: * Create a customized editor whose activation process is customized
091: *
092: * @param viewer
093: * the viewer the editor is created for
094: * @param editorActivationStrategy
095: * activation strategy to control if an editor activated
096: * @param feature
097: * bit mask controlling the editor
098: * <ul>
099: * <li>{@link ColumnViewerEditor#DEFAULT}</li>
100: * <li>{@link ColumnViewerEditor#TABBING_CYCLE_IN_ROW}</li>
101: * <li>{@link ColumnViewerEditor#TABBING_HORIZONTAL}</li>
102: * <li>{@link ColumnViewerEditor#TABBING_MOVE_TO_ROW_NEIGHBOR}</li>
103: * <li>{@link ColumnViewerEditor#TABBING_VERTICAL}</li>
104: * </ul>
105: */
106: public static void create(
107: TreeViewer viewer,
108: ColumnViewerEditorActivationStrategy editorActivationStrategy,
109: int feature) {
110: create(viewer, null, editorActivationStrategy, feature);
111: }
112:
113: protected void setEditor(Control w, Item item, int fColumnNumber) {
114: treeEditor.setEditor(w, (TreeItem) item, fColumnNumber);
115: }
116:
117: protected void setLayoutData(LayoutData layoutData) {
118: treeEditor.grabHorizontal = layoutData.grabHorizontal;
119: treeEditor.horizontalAlignment = layoutData.horizontalAlignment;
120: treeEditor.minimumWidth = layoutData.minimumWidth;
121: }
122:
123: public ViewerCell getFocusCell() {
124: if (focusCellManager != null) {
125: return focusCellManager.getFocusCell();
126: }
127:
128: return super .getFocusCell();
129: }
130:
131: protected void updateFocusCell(ViewerCell focusCell,
132: ColumnViewerEditorActivationEvent event) {
133: // Update the focus cell when we activated the editor with these 2
134: // events
135: if (event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC
136: || event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL) {
137:
138: List l = getViewer().getSelectionFromWidget();
139:
140: if (focusCellManager != null) {
141: focusCellManager.setFocusCell(focusCell);
142: }
143:
144: if (!l.contains(focusCell.getElement())) {
145: getViewer().setSelection(
146: new TreeSelection(focusCell.getViewerRow()
147: .getTreePath()), true);
148: }
149: }
150: }
151: }
|