001: /*******************************************************************************
002: * Copyright (c) 2001, 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: *******************************************************************************/package org.eclipse.ui.internal.views.properties.tabbed.view;
011:
012: import java.util.ArrayList;
013: import java.util.Collections;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.Assert;
017: import org.eclipse.jface.viewers.ISelection;
018: import org.eclipse.jface.viewers.StructuredViewer;
019: import org.eclipse.swt.widgets.Control;
020: import org.eclipse.swt.widgets.Widget;
021: import org.eclipse.ui.IWorkbenchPart;
022:
023: /**
024: * Viewer representing the property sheet page. On the left side it
025: * contains a list of tabs and on the right side it contains the
026: * current selected tab.
027: *
028: * @author Anthony Hunter
029: */
030: public class TabbedPropertyViewer extends StructuredViewer {
031:
032: protected TabbedPropertyList list;
033: protected List elements;
034: protected IWorkbenchPart part;
035:
036: /**
037: * Constructor for TabbedPropertyViewer.
038: *
039: * @param list
040: * the TabbedPropertyList.
041: */
042: public TabbedPropertyViewer(TabbedPropertyList list) {
043: this .list = list;
044: hookControl(list);
045: elements = new ArrayList();
046: }
047:
048: /**
049: * Returns the element with the given index from this list viewer.
050: * Returns <code>null</code> if the index is out of range.
051: *
052: * @param index the zero-based index
053: * @return the element at the given index, or <code>null</code> if the
054: * index is out of range
055: */
056: public Object getElementAt(int index) {
057: if (index >= 0 && index < elements.size()) {
058: return elements.get(index);
059: }
060: return null;
061: }
062:
063: /**
064: * Returns the zero-relative index of the item which is currently
065: * selected in the receiver, or -1 if no item is selected.
066: *
067: * @return the index of the selected item
068: */
069: public int getSelectionIndex() {
070: return list.getSelectionIndex();
071: }
072:
073: protected Widget doFindInputItem(Object element) {
074: /* not implemented */
075: return null;
076: }
077:
078: protected Widget doFindItem(Object element) {
079: /* not implemented */
080: return null;
081: }
082:
083: protected void doUpdateItem(Widget item, Object element,
084: boolean fullMap) {
085: /* not implemented */
086: }
087:
088: protected List getSelectionFromWidget() {
089: int index = list.getSelectionIndex();
090: if (index == TabbedPropertyList.NONE) {
091: return Collections.EMPTY_LIST;
092: }
093: List result = new ArrayList(1);
094: result.add(getElementAt(index));
095: return result;
096: }
097:
098: protected void internalRefresh(Object element) {
099: /* not implemented */
100: }
101:
102: public void reveal(Object 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 l, boolean reveal) {
111: if (l == null || l.size() == 0) { // clear selection
112: list.deselectAll();
113: } else {
114: Object 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
122: .isTrue(index != -1,
123: "Could not set the selected tab in the tabbed property viewer");//$NON-NLS-1$
124: list.select(index);
125: }
126: }
127:
128: public Control getControl() {
129: return list;
130: }
131:
132: protected void inputChanged(Object input, Object oldInput) {
133: elements.clear();
134: Object[] children = getSortedChildren(getRoot());
135: list.removeAll();
136: for (int i = 0; i < children.length; i++) {
137: elements.add(children[i]);
138: mapElement(children[i], list);
139: }
140: list.setElements(children);
141: }
142:
143: /**
144: * Set the input for viewer.
145: *
146: * @param part
147: * the workbench part.
148: * @param selection
149: * the selection in the workbench part.
150: */
151: public void setInput(IWorkbenchPart part, ISelection selection) {
152: this .part = part;
153: setInput(selection);
154: }
155:
156: /**
157: * Get the current workbench part.
158: *
159: * @return the current workbench part.
160: */
161: public IWorkbenchPart getWorkbenchPart() {
162: return part;
163: }
164: }
|