KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > views > properties > tabbed > view > TabbedPropertyViewer


1 /*******************************************************************************
2  * Copyright (c) 2001, 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 package org.eclipse.ui.internal.views.properties.tabbed.view;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.StructuredViewer;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Widget;
22 import org.eclipse.ui.IWorkbenchPart;
23
24 /**
25  * Viewer representing the property sheet page. On the left side it
26  * contains a list of tabs and on the right side it contains the
27  * current selected tab.
28  *
29  * @author Anthony Hunter
30  */

31 public class TabbedPropertyViewer extends StructuredViewer {
32
33     protected TabbedPropertyList list;
34     protected List JavaDoc elements;
35     protected IWorkbenchPart part;
36
37     /**
38      * Constructor for TabbedPropertyViewer.
39      *
40      * @param list
41      * the TabbedPropertyList.
42      */

43     public TabbedPropertyViewer(TabbedPropertyList list) {
44         this.list = list;
45         hookControl(list);
46         elements = new ArrayList JavaDoc();
47     }
48
49     /**
50      * Returns the element with the given index from this list viewer.
51      * Returns <code>null</code> if the index is out of range.
52      *
53      * @param index the zero-based index
54      * @return the element at the given index, or <code>null</code> if the
55      * index is out of range
56      */

57     public Object JavaDoc getElementAt(int index) {
58         if (index >= 0 && index < elements.size()) {
59             return elements.get(index);
60         }
61         return null;
62     }
63
64     /**
65      * Returns the zero-relative index of the item which is currently
66      * selected in the receiver, or -1 if no item is selected.
67      *
68      * @return the index of the selected item
69      */

70     public int getSelectionIndex() {
71         return list.getSelectionIndex();
72     }
73
74     protected Widget doFindInputItem(Object JavaDoc element) {
75         /* not implemented */
76         return null;
77     }
78
79     protected Widget doFindItem(Object JavaDoc element) {
80         /* not implemented */
81         return null;
82     }
83
84     protected void doUpdateItem(Widget item, Object JavaDoc element, boolean fullMap) {
85         /* not implemented */
86     }
87
88     protected List JavaDoc getSelectionFromWidget() {
89         int index = list.getSelectionIndex();
90         if (index == TabbedPropertyList.NONE) {
91             return Collections.EMPTY_LIST;
92         }
93         List JavaDoc result = new ArrayList JavaDoc(1);
94         result.add(getElementAt(index));
95         return result;
96     }
97
98     protected void internalRefresh(Object JavaDoc element) {
99         /* not implemented */
100     }
101
102     public void reveal(Object JavaDoc element) {
103         /* not implemented */
104     }
105
106     /**
107      * We do not consider multiple selections. Only the first
108      * element will represent the selection.
109      */

110     protected void setSelectionToWidget(List JavaDoc l, boolean reveal) {
111         if (l == null || l.size() == 0) { // clear selection
112
list.deselectAll();
113         } else {
114             Object JavaDoc object = l.get(0);
115             int index = -1;
116             for (int i = 0; i < elements.size(); i++) {
117                 if (elements.get(i) == object) {
118                     index = i;
119                 }
120             }
121             Assert.isTrue(index != -1, "Could not set the selected tab in the tabbed property viewer");//$NON-NLS-1$
122
list.select(index);
123         }
124     }
125
126     public Control getControl() {
127         return list;
128     }
129
130     protected void inputChanged(Object JavaDoc input, Object JavaDoc oldInput) {
131         elements.clear();
132         Object JavaDoc[] children = getSortedChildren(getRoot());
133         list.removeAll();
134         for (int i = 0; i < children.length; i++) {
135             elements.add(children[i]);
136             mapElement(children[i], list);
137         }
138         list.setElements(children);
139     }
140
141     /**
142      * Set the input for viewer.
143      *
144      * @param part
145      * the workbench part.
146      * @param selection
147      * the selection in the workbench part.
148      */

149     public void setInput(IWorkbenchPart part, ISelection selection) {
150         this.part = part;
151         setInput(selection);
152     }
153
154     /**
155      * Get the current workbench part.
156      *
157      * @return the current workbench part.
158      */

159     public IWorkbenchPart getWorkbenchPart() {
160         return part;
161     }
162 }
163
Popular Tags