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: *******************************************************************************/package org.eclipse.ui.tests.views.properties.tabbed.dynamic.views;
011:
012: import org.eclipse.jface.action.Action;
013: import org.eclipse.jface.action.IAction;
014: import org.eclipse.jface.action.IToolBarManager;
015: import org.eclipse.jface.resource.ImageDescriptor;
016: import org.eclipse.jface.viewers.LabelProvider;
017: import org.eclipse.jface.viewers.StructuredSelection;
018: import org.eclipse.jface.viewers.TreeViewer;
019: import org.eclipse.swt.SWT;
020: import org.eclipse.swt.graphics.Image;
021: import org.eclipse.swt.widgets.Composite;
022: import org.eclipse.ui.ISharedImages;
023: import org.eclipse.ui.PlatformUI;
024: import org.eclipse.ui.part.ViewPart;
025: import org.eclipse.ui.tests.views.properties.tabbed.Activator;
026: import org.eclipse.ui.tests.views.properties.tabbed.dynamic.model.DynamicTestsElement;
027: import org.eclipse.ui.views.properties.IPropertySheetPage;
028: import org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor;
029: import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
030:
031: /**
032: * A view to test dynamic contributions to the tabbed properties view. The view
033: * has three modes of providing tabs and sections to the tabbed properties view:
034: * use static contributions from plugin.xml, use dynamic section contributions
035: * from code, or use dynamic tab (and section) contributions from code.
036: *
037: * @author Anthony Hunter
038: */
039: public class DynamicTestsView extends ViewPart implements
040: ITabbedPropertySheetPageContributor {
041:
042: class DynamicTestsViewLabelProvider extends LabelProvider {
043:
044: public Image getImage(Object obj) {
045: DynamicTestsElement element = ((DynamicTestsTreeNode) obj)
046: .getDynamicTestsElement();
047: return element.getImage();
048: }
049:
050: public String getText(Object obj) {
051: DynamicTestsElement element = ((DynamicTestsTreeNode) obj)
052: .getDynamicTestsElement();
053: return element.getName();
054: }
055: }
056:
057: public static final String DYNAMIC_TESTS_VIEW_ID = "org.eclipse.ui.tests.views.properties.tabbed.dynamic.views.DynamicTestsView"; //$NON-NLS-1$
058:
059: public static final String DYNAMIC_TESTS_VIEW_STATIC = "org.eclipse.ui.tests.views.properties.tabbed.static"; //$NON-NLS-1$
060:
061: public static final String DYNAMIC_TESTS_VIEW_DYNAMIC_TABS = "org.eclipse.ui.tests.views.properties.tabbed.dynamic.tab"; //$NON-NLS-1$
062:
063: public static final String DYNAMIC_TESTS_VIEW_DYNAMIC_SECTIONS = "org.eclipse.ui.tests.views.properties.tabbed.dynamic.section"; //$NON-NLS-1$
064: private Action dynamicSectionsAction;
065:
066: private Action dynamicTabsAction;
067:
068: private Action staticAction;
069:
070: private TabbedPropertySheetPage tabbedPropertySheetPage;
071:
072: private TreeViewer viewer;
073:
074: public void createPartControl(Composite parent) {
075: viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL
076: | SWT.V_SCROLL);
077: viewer.setContentProvider(new DynamicTestsViewContentProvider(
078: this ));
079: viewer.setLabelProvider(new DynamicTestsViewLabelProvider());
080: viewer.setInput(getViewSite());
081: initToolBar();
082: getSite().setSelectionProvider(viewer);
083: }
084:
085: public Object getAdapter(Class adapter) {
086: if (adapter == IPropertySheetPage.class) {
087: if (tabbedPropertySheetPage == null) {
088: tabbedPropertySheetPage = new TabbedPropertySheetPage(
089: this );
090: }
091: return tabbedPropertySheetPage;
092: }
093: return super .getAdapter(adapter);
094: }
095:
096: public String getContributorId() {
097: if (staticAction.isChecked()) {
098: return DYNAMIC_TESTS_VIEW_STATIC;
099: } else if (dynamicSectionsAction.isChecked()) {
100: return DYNAMIC_TESTS_VIEW_DYNAMIC_SECTIONS;
101: } else if (dynamicTabsAction.isChecked()) {
102: return DYNAMIC_TESTS_VIEW_DYNAMIC_TABS;
103: } else {
104: return null;
105: }
106: }
107:
108: public void setContributorId(String contributorId) {
109: if (DYNAMIC_TESTS_VIEW_STATIC.equals(contributorId)) {
110: staticAction.setChecked(true);
111: dynamicSectionsAction.setChecked(false);
112: dynamicTabsAction.setChecked(false);
113: } else if (DYNAMIC_TESTS_VIEW_DYNAMIC_SECTIONS
114: .equals(contributorId)) {
115: staticAction.setChecked(false);
116: dynamicSectionsAction.setChecked(true);
117: dynamicTabsAction.setChecked(false);
118: } else if (DYNAMIC_TESTS_VIEW_DYNAMIC_TABS
119: .equals(contributorId)) {
120: staticAction.setChecked(false);
121: dynamicSectionsAction.setChecked(false);
122: dynamicTabsAction.setChecked(true);
123: }
124: }
125:
126: public TabbedPropertySheetPage getTabbedPropertySheetPage() {
127: return tabbedPropertySheetPage;
128: }
129:
130: public TreeViewer getViewer() {
131: return viewer;
132: }
133:
134: /**
135: * Create the tool bar for the view. The tool bar has three toggle buttons
136: * which enable one of the three property contributors.
137: */
138: private void initToolBar() {
139: final String staticText = "Use static contributions from plugin.xml"; //$NON-NLS-1$
140: final String dynamicSectionsText = "Use dynamic section contributions from code"; //$NON-NLS-1$
141: final String dynamicTabsText = "Use dynamic tab (and section) contributions from code"; //$NON-NLS-1$
142: ImageDescriptor imageDescriptor = Activator
143: .getImageDescriptor("icons/sample.gif");//$NON-NLS-1$
144: if (imageDescriptor == null) {
145: imageDescriptor = PlatformUI.getWorkbench()
146: .getSharedImages().getImageDescriptor(
147: ISharedImages.IMG_OBJS_ERROR_TSK);
148: }
149:
150: staticAction = new Action(staticText, IAction.AS_CHECK_BOX) {
151: public void run() {
152: if (isChecked()) {
153: getViewSite().getActionBars()
154: .getStatusLineManager().setMessage(
155: staticText);
156: dynamicSectionsAction.setChecked(false);
157: dynamicTabsAction.setChecked(false);
158: viewer.setSelection(StructuredSelection.EMPTY);
159: }
160: }
161: };
162: staticAction.setToolTipText(staticText);
163: staticAction.setImageDescriptor(imageDescriptor);
164: staticAction.setDisabledImageDescriptor(imageDescriptor);
165:
166: dynamicSectionsAction = new Action(dynamicSectionsText,
167: IAction.AS_CHECK_BOX) {
168: public void run() {
169: if (isChecked()) {
170: getViewSite().getActionBars()
171: .getStatusLineManager().setMessage(
172: dynamicSectionsText);
173: staticAction.setChecked(false);
174: dynamicTabsAction.setChecked(false);
175: viewer.setSelection(StructuredSelection.EMPTY);
176: }
177: }
178: };
179: dynamicSectionsAction.setToolTipText(dynamicSectionsText);
180: dynamicSectionsAction.setImageDescriptor(imageDescriptor);
181: dynamicSectionsAction
182: .setDisabledImageDescriptor(imageDescriptor);
183:
184: dynamicTabsAction = new Action(dynamicTabsText,
185: IAction.AS_CHECK_BOX) {
186: public void run() {
187: if (isChecked()) {
188: getViewSite().getActionBars()
189: .getStatusLineManager().setMessage(
190: dynamicTabsText);
191: staticAction.setChecked(false);
192: dynamicSectionsAction.setChecked(false);
193: viewer.setSelection(StructuredSelection.EMPTY);
194: }
195: }
196: };
197: dynamicTabsAction.setToolTipText(dynamicTabsText);
198: dynamicTabsAction.setImageDescriptor(imageDescriptor);
199: dynamicTabsAction.setDisabledImageDescriptor(imageDescriptor);
200: staticAction.setChecked(true);
201:
202: IToolBarManager toolBarManager = getViewSite().getActionBars()
203: .getToolBarManager();
204: toolBarManager.add(staticAction);
205: toolBarManager.add(dynamicSectionsAction);
206: toolBarManager.add(dynamicTabsAction);
207:
208: getViewSite().getActionBars().getStatusLineManager()
209: .setMessage(staticText);
210: }
211:
212: public void setFocus() {
213: viewer.getControl().setFocus();
214: }
215: }
|