001: package org.eclipse.ui.tutorials.rcp.part3;
002:
003: import java.util.ArrayList;
004:
005: import org.eclipse.jface.viewers.IStructuredContentProvider;
006: import org.eclipse.jface.viewers.ITreeContentProvider;
007: import org.eclipse.jface.viewers.LabelProvider;
008: import org.eclipse.jface.viewers.TreeViewer;
009: import org.eclipse.jface.viewers.Viewer;
010: import org.eclipse.swt.SWT;
011: import org.eclipse.swt.graphics.Image;
012: import org.eclipse.swt.widgets.Composite;
013: import org.eclipse.ui.ISharedImages;
014: import org.eclipse.ui.PlatformUI;
015: import org.eclipse.ui.part.ViewPart;
016:
017: public class NavigationView extends ViewPart {
018: public static final String ID = "org.eclipse.ui.tutorials.rcp.part3.navigationView";
019: private TreeViewer viewer;
020:
021: class TreeObject {
022: private String name;
023: private TreeParent parent;
024:
025: public TreeObject(String name) {
026: this .name = name;
027: }
028:
029: public String getName() {
030: return name;
031: }
032:
033: public void setParent(TreeParent parent) {
034: this .parent = parent;
035: }
036:
037: public TreeParent getParent() {
038: return parent;
039: }
040:
041: public String toString() {
042: return getName();
043: }
044: }
045:
046: class TreeParent extends TreeObject {
047: private ArrayList children;
048:
049: public TreeParent(String name) {
050: super (name);
051: children = new ArrayList();
052: }
053:
054: public void addChild(TreeObject child) {
055: children.add(child);
056: child.setParent(this );
057: }
058:
059: public void removeChild(TreeObject child) {
060: children.remove(child);
061: child.setParent(null);
062: }
063:
064: public TreeObject[] getChildren() {
065: return (TreeObject[]) children
066: .toArray(new TreeObject[children.size()]);
067: }
068:
069: public boolean hasChildren() {
070: return children.size() > 0;
071: }
072: }
073:
074: class ViewContentProvider implements IStructuredContentProvider,
075: ITreeContentProvider {
076:
077: public void inputChanged(Viewer v, Object oldInput,
078: Object newInput) {
079: }
080:
081: public void dispose() {
082: }
083:
084: public Object[] getElements(Object parent) {
085: return getChildren(parent);
086: }
087:
088: public Object getParent(Object child) {
089: if (child instanceof TreeObject) {
090: return ((TreeObject) child).getParent();
091: }
092: return null;
093: }
094:
095: public Object[] getChildren(Object parent) {
096: if (parent instanceof TreeParent) {
097: return ((TreeParent) parent).getChildren();
098: }
099: return new Object[0];
100: }
101:
102: public boolean hasChildren(Object parent) {
103: if (parent instanceof TreeParent)
104: return ((TreeParent) parent).hasChildren();
105: return false;
106: }
107: }
108:
109: class ViewLabelProvider extends LabelProvider {
110:
111: public String getText(Object obj) {
112: return obj.toString();
113: }
114:
115: public Image getImage(Object obj) {
116: String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
117: if (obj instanceof TreeParent)
118: imageKey = ISharedImages.IMG_OBJ_FOLDER;
119: return PlatformUI.getWorkbench().getSharedImages()
120: .getImage(imageKey);
121: }
122: }
123:
124: /**
125: * We will set up a dummy model to initialize tree heararchy. In real
126: * code, you will connect to a real model and expose its hierarchy.
127: */
128: private TreeObject createDummyModel() {
129: TreeObject to1 = new TreeObject("Inbox");
130: TreeObject to2 = new TreeObject("Drafts");
131: TreeObject to3 = new TreeObject("Sent");
132: TreeParent p1 = new TreeParent("me@this.com");
133: p1.addChild(to1);
134: p1.addChild(to2);
135: p1.addChild(to3);
136:
137: TreeObject to4 = new TreeObject("Inbox");
138: TreeParent p2 = new TreeParent("other@aol.com");
139: p2.addChild(to4);
140:
141: TreeParent root = new TreeParent("");
142: root.addChild(p1);
143: root.addChild(p2);
144: return root;
145: }
146:
147: /**
148: * This is a callback that will allow us to create the viewer and initialize
149: * it.
150: */
151: public void createPartControl(Composite parent) {
152: viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL
153: | SWT.V_SCROLL | SWT.BORDER);
154: viewer.setContentProvider(new ViewContentProvider());
155: viewer.setLabelProvider(new ViewLabelProvider());
156: viewer.setInput(createDummyModel());
157: }
158:
159: /**
160: * Passing the focus request to the viewer's control.
161: */
162: public void setFocus() {
163: viewer.getControl().setFocus();
164: }
165: }
|