KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
11  * fixes in bug 198665
12  ******************************************************************************/

13
14 package org.eclipse.jface.viewers;
15
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jface.viewers.CellEditor.LayoutData;
19 import org.eclipse.swt.custom.TreeEditor;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Item;
22 import org.eclipse.swt.widgets.Tree;
23 import org.eclipse.swt.widgets.TreeItem;
24
25 /**
26  * This is an editor implementation for {@link Tree}
27  *
28  * @since 3.3
29  */

30 public class TreeViewerEditor extends ColumnViewerEditor {
31     /**
32      * This viewer's tree editor.
33      */

34     private TreeEditor treeEditor;
35
36     private SWTFocusCellManager focusCellManager;
37
38     /**
39      * @param viewer
40      * the viewer the editor is attached to
41      * @param focusCellManager
42      * the cell focus manager if one used or <code>null</code>
43      * @param editorActivationStrategy
44      * the strategy used to decide about the editor activation
45      * @param feature
46      * the feature mask
47      */

48     TreeViewerEditor(TreeViewer viewer, SWTFocusCellManager focusCellManager,
49             ColumnViewerEditorActivationStrategy editorActivationStrategy,
50             int feature) {
51         super(viewer, editorActivationStrategy, feature);
52         treeEditor = new TreeEditor(viewer.getTree());
53         this.focusCellManager = focusCellManager;
54     }
55
56     /**
57      * Create a customized editor with focusable cells
58      *
59      * @param viewer
60      * the viewer the editor is created for
61      * @param focusCellManager
62      * the cell focus manager if one needed else <code>null</code>
63      * @param editorActivationStrategy
64      * activation strategy to control if an editor activated
65      * @param feature
66      * bit mask controlling the editor
67      * <ul>
68      * <li>{@link ColumnViewerEditor#DEFAULT}</li>
69      * <li>{@link ColumnViewerEditor#TABBING_CYCLE_IN_ROW}</li>
70      * <li>{@link ColumnViewerEditor#TABBING_HORIZONTAL}</li>
71      * <li>{@link ColumnViewerEditor#TABBING_MOVE_TO_ROW_NEIGHBOR}</li>
72      * <li>{@link ColumnViewerEditor#TABBING_VERTICAL}</li>
73      * </ul>
74      * @see #create(TreeViewer, ColumnViewerEditorActivationStrategy, int)
75      */

76     public static void create(TreeViewer viewer,
77             SWTFocusCellManager focusCellManager,
78             ColumnViewerEditorActivationStrategy editorActivationStrategy,
79             int feature) {
80         TreeViewerEditor editor = new TreeViewerEditor(viewer,
81                 focusCellManager, editorActivationStrategy, feature);
82         viewer.setColumnViewerEditor(editor);
83         if (focusCellManager != null) {
84             focusCellManager.init();
85         }
86     }
87
88     /**
89      * Create a customized editor whose activation process is customized
90      *
91      * @param viewer
92      * the viewer the editor is created for
93      * @param editorActivationStrategy
94      * activation strategy to control if an editor activated
95      * @param feature
96      * bit mask controlling the editor
97      * <ul>
98      * <li>{@link ColumnViewerEditor#DEFAULT}</li>
99      * <li>{@link ColumnViewerEditor#TABBING_CYCLE_IN_ROW}</li>
100      * <li>{@link ColumnViewerEditor#TABBING_HORIZONTAL}</li>
101      * <li>{@link ColumnViewerEditor#TABBING_MOVE_TO_ROW_NEIGHBOR}</li>
102      * <li>{@link ColumnViewerEditor#TABBING_VERTICAL}</li>
103      * </ul>
104      */

105     public static void create(TreeViewer viewer,
106             ColumnViewerEditorActivationStrategy editorActivationStrategy,
107             int feature) {
108         create(viewer, null, editorActivationStrategy, feature);
109     }
110
111     protected void setEditor(Control w, Item item, int fColumnNumber) {
112         treeEditor.setEditor(w, (TreeItem) item, fColumnNumber);
113     }
114
115     protected void setLayoutData(LayoutData layoutData) {
116         treeEditor.grabHorizontal = layoutData.grabHorizontal;
117         treeEditor.horizontalAlignment = layoutData.horizontalAlignment;
118         treeEditor.minimumWidth = layoutData.minimumWidth;
119     }
120
121     public ViewerCell getFocusCell() {
122         if (focusCellManager != null) {
123             return focusCellManager.getFocusCell();
124         }
125
126         return super.getFocusCell();
127     }
128
129     protected void updateFocusCell(ViewerCell focusCell,
130             ColumnViewerEditorActivationEvent event) {
131         // Update the focus cell when we activated the editor with these 2
132
// events
133
if (event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC
134                 || event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL) {
135
136             List JavaDoc l = getViewer().getSelectionFromWidget();
137
138             if (focusCellManager != null) {
139                 focusCellManager.setFocusCell(focusCell);
140             }
141
142             if (!l.contains(focusCell.getElement())) {
143                 getViewer().setSelection(
144                         new TreeSelection(focusCell.getViewerRow()
145                                 .getTreePath()),true);
146             }
147         }
148     }
149 }
150
Popular Tags