001: /*******************************************************************************
002: * Copyright (c) 2004, 2005 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.examples.rcp.browser;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.swt.widgets.Composite;
015: import org.eclipse.ui.part.*;
016: import org.eclipse.jface.viewers.*;
017: import org.eclipse.swt.graphics.Image;
018: import org.eclipse.jface.action.*;
019: import org.eclipse.jface.dialogs.MessageDialog;
020: import org.eclipse.ui.*;
021: import org.eclipse.swt.widgets.Menu;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.core.runtime.IAdaptable;
024:
025: /**
026: * Mockup of a browser history view.
027: * For now, it's just the sample view generated by PDE.
028: */
029: public class HistoryView extends ViewPart implements ISecondaryPart {
030: private TreeViewer viewer;
031: private DrillDownAdapter drillDownAdapter;
032: private Action action1;
033: private Action action2;
034: private Action doubleClickAction;
035:
036: /*
037: * The content provider class is responsible for
038: * providing objects to the view. It can wrap
039: * existing objects in adapters or simply return
040: * objects as-is. These objects may be sensitive
041: * to the current input of the view, or ignore
042: * it and always show the same content
043: * (like Task List, for example).
044: */
045:
046: class TreeObject implements IAdaptable {
047: private String name;
048: private TreeParent parent;
049:
050: public TreeObject(String name) {
051: this .name = name;
052: }
053:
054: public String getName() {
055: return name;
056: }
057:
058: public void setParent(TreeParent parent) {
059: this .parent = parent;
060: }
061:
062: public TreeParent getParent() {
063: return parent;
064: }
065:
066: public String toString() {
067: return getName();
068: }
069:
070: public Object getAdapter(Class key) {
071: return null;
072: }
073: }
074:
075: class TreeParent extends TreeObject {
076: private ArrayList children;
077:
078: public TreeParent(String name) {
079: super (name);
080: children = new ArrayList();
081: }
082:
083: public void addChild(TreeObject child) {
084: children.add(child);
085: child.setParent(this );
086: }
087:
088: public void removeChild(TreeObject child) {
089: children.remove(child);
090: child.setParent(null);
091: }
092:
093: public TreeObject[] getChildren() {
094: return (TreeObject[]) children
095: .toArray(new TreeObject[children.size()]);
096: }
097:
098: public boolean hasChildren() {
099: return children.size() > 0;
100: }
101: }
102:
103: class ViewContentProvider implements IStructuredContentProvider,
104: ITreeContentProvider {
105: private TreeParent invisibleRoot;
106:
107: public void inputChanged(Viewer v, Object oldInput,
108: Object newInput) {
109: }
110:
111: public void dispose() {
112: }
113:
114: public Object[] getElements(Object parent) {
115: if (parent.equals(getViewSite())) {
116: if (invisibleRoot == null)
117: initialize();
118: return getChildren(invisibleRoot);
119: }
120: return getChildren(parent);
121: }
122:
123: public Object getParent(Object child) {
124: if (child instanceof TreeObject) {
125: return ((TreeObject) child).getParent();
126: }
127: return null;
128: }
129:
130: public Object[] getChildren(Object parent) {
131: if (parent instanceof TreeParent) {
132: return ((TreeParent) parent).getChildren();
133: }
134: return new Object[0];
135: }
136:
137: public boolean hasChildren(Object parent) {
138: if (parent instanceof TreeParent)
139: return ((TreeParent) parent).hasChildren();
140: return false;
141: }
142:
143: /*
144: * We will set up a dummy model to initialize tree heararchy.
145: * In a real code, you will connect to a real model and
146: * expose its hierarchy.
147: */
148: private void initialize() {
149: TreeObject to1 = new TreeObject("Leaf 1");
150: TreeObject to2 = new TreeObject("Leaf 2");
151: TreeObject to3 = new TreeObject("Leaf 3");
152: TreeParent p1 = new TreeParent("Parent 1");
153: p1.addChild(to1);
154: p1.addChild(to2);
155: p1.addChild(to3);
156:
157: TreeObject to4 = new TreeObject("Leaf 4");
158: TreeParent p2 = new TreeParent("Parent 2");
159: p2.addChild(to4);
160:
161: TreeParent root = new TreeParent("Root");
162: root.addChild(p1);
163: root.addChild(p2);
164:
165: invisibleRoot = new TreeParent("");
166: invisibleRoot.addChild(root);
167: }
168: }
169:
170: class ViewLabelProvider extends LabelProvider {
171:
172: public String getText(Object obj) {
173: return obj.toString();
174: }
175:
176: public Image getImage(Object obj) {
177: String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
178: if (obj instanceof TreeParent)
179: imageKey = ISharedImages.IMG_OBJ_FOLDER;
180: return PlatformUI.getWorkbench().getSharedImages()
181: .getImage(imageKey);
182: }
183: }
184:
185: /**
186: * The constructor.
187: */
188: public HistoryView() {
189: }
190:
191: /**
192: * This is a callback that will allow us
193: * to create the viewer and initialize it.
194: */
195: public void createPartControl(Composite parent) {
196: viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL
197: | SWT.V_SCROLL);
198: drillDownAdapter = new DrillDownAdapter(viewer);
199: viewer.setContentProvider(new ViewContentProvider());
200: viewer.setLabelProvider(new ViewLabelProvider());
201: viewer.setInput(getViewSite());
202: makeActions();
203: hookContextMenu();
204: hookDoubleClickAction();
205: }
206:
207: private void hookContextMenu() {
208: MenuManager menuMgr = new MenuManager("#PopupMenu");
209: menuMgr.setRemoveAllWhenShown(true);
210: menuMgr.addMenuListener(new IMenuListener() {
211: public void menuAboutToShow(IMenuManager manager) {
212: HistoryView.this .fillContextMenu(manager);
213: }
214: });
215: Menu menu = menuMgr.createContextMenu(viewer.getControl());
216: viewer.getControl().setMenu(menu);
217: getSite().registerContextMenu(menuMgr, viewer);
218: }
219:
220: private void fillContextMenu(IMenuManager manager) {
221: manager.add(action1);
222: manager.add(action2);
223: manager.add(new Separator());
224: drillDownAdapter.addNavigationActions(manager);
225: // Other plug-ins can contribute there actions here
226: manager.add(new Separator(
227: IWorkbenchActionConstants.MB_ADDITIONS));
228: }
229:
230: private void makeActions() {
231: action1 = new Action() {
232: public void run() {
233: showMessage("Action 1 executed");
234: }
235: };
236: action1.setText("Action 1");
237: action1.setToolTipText("Action 1 tooltip");
238: action1.setImageDescriptor(PlatformUI.getWorkbench()
239: .getSharedImages().getImageDescriptor(
240: ISharedImages.IMG_OBJS_INFO_TSK));
241:
242: action2 = new Action() {
243: public void run() {
244: showMessage("Action 2 executed");
245: }
246: };
247: action2.setText("Action 2");
248: action2.setToolTipText("Action 2 tooltip");
249: action2.setImageDescriptor(PlatformUI.getWorkbench()
250: .getSharedImages().getImageDescriptor(
251: ISharedImages.IMG_OBJS_INFO_TSK));
252: doubleClickAction = new Action() {
253: public void run() {
254: ISelection selection = viewer.getSelection();
255: Object obj = ((IStructuredSelection) selection)
256: .getFirstElement();
257: showMessage("Double-click detected on "
258: + obj.toString());
259: }
260: };
261: }
262:
263: private void hookDoubleClickAction() {
264: viewer.addDoubleClickListener(new IDoubleClickListener() {
265: public void doubleClick(DoubleClickEvent event) {
266: doubleClickAction.run();
267: }
268: });
269: }
270:
271: private void showMessage(String message) {
272: MessageDialog.openInformation(viewer.getControl().getShell(),
273: "History", message);
274: }
275:
276: /**
277: * Passing the focus request to the viewer's control.
278: */
279: public void setFocus() {
280: viewer.getControl().setFocus();
281: }
282: }
|