001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.views.framelist;
011:
012: import org.eclipse.jface.util.IPropertyChangeListener;
013: import org.eclipse.jface.util.PropertyChangeEvent;
014: import org.eclipse.jface.viewers.AbstractTreeViewer;
015: import org.eclipse.jface.viewers.IStructuredSelection;
016: import org.eclipse.jface.viewers.ITreeContentProvider;
017:
018: /**
019: * Frame source for tree viewers, which uses <code>TreeFrame</code> to capture
020: * the state of the tree viewer.
021: *
022: * @see TreeFrame
023: */
024: public class TreeViewerFrameSource implements IFrameSource {
025:
026: private AbstractTreeViewer viewer;
027:
028: /**
029: * Constructs a new tree viewer frame source for the specified tree viewer.
030: *
031: * @param viewer the tree viewer
032: */
033: public TreeViewerFrameSource(AbstractTreeViewer viewer) {
034: this .viewer = viewer;
035: }
036:
037: /**
038: * Connects this source as a listener on the frame list,
039: * so that when the current frame changes, the viewer is updated.
040: */
041: public void connectTo(FrameList frameList) {
042: frameList
043: .addPropertyChangeListener(new IPropertyChangeListener() {
044: public void propertyChange(PropertyChangeEvent event) {
045: TreeViewerFrameSource.this
046: .handlePropertyChange(event);
047: }
048: });
049: }
050:
051: /**
052: * Returns a new tree frame capturing the specified input element.
053: *
054: * @param input the input element
055: * @return the tree frame
056: */
057: protected TreeFrame createFrame(Object input) {
058: return new TreeFrame(viewer, input);
059: }
060:
061: /**
062: * Updates the viewer in response to the current frame changing.
063: *
064: * @param frame the new value for the current frame
065: */
066: protected void frameChanged(TreeFrame frame) {
067: viewer.getControl().setRedraw(false);
068: viewer.setInput(frame.getInput());
069: viewer.setExpandedElements(frame.getExpandedElements());
070: viewer.setSelection(frame.getSelection(), true);
071: viewer.getControl().setRedraw(true);
072: }
073:
074: /**
075: * Returns the current frame.
076: *
077: * @param flags a bit-wise OR of the frame source flag constants
078: * @return the current frame
079: */
080: protected Frame getCurrentFrame(int flags) {
081: Object input = viewer.getInput();
082: TreeFrame frame = createFrame(input);
083: if ((flags & IFrameSource.FULL_CONTEXT) != 0) {
084: frame.setSelection(viewer.getSelection());
085: frame.setExpandedElements(viewer.getExpandedElements());
086: }
087: return frame;
088: }
089:
090: /* (non-Javadoc)
091: * Method declared on IFrameSource.
092: */
093: public Frame getFrame(int whichFrame, int flags) {
094: switch (whichFrame) {
095: case IFrameSource.CURRENT_FRAME:
096: return getCurrentFrame(flags);
097: case IFrameSource.PARENT_FRAME:
098: return getParentFrame(flags);
099: case IFrameSource.SELECTION_FRAME:
100: return getSelectionFrame(flags);
101: default:
102: return null;
103: }
104: }
105:
106: /**
107: * Returns the parent frame, or <code>null</code> if there is no parent frame.
108: *
109: * @param flags a bit-wise OR of the frame source flag constants
110: * @return the parent frame, or <code>null</code>
111: */
112: protected Frame getParentFrame(int flags) {
113: Object input = viewer.getInput();
114: ITreeContentProvider provider = (ITreeContentProvider) viewer
115: .getContentProvider();
116: Object parent = provider.getParent(input);
117: if (parent == null) {
118: return null;
119: } else {
120: TreeFrame frame = createFrame(parent);
121: if ((flags & IFrameSource.FULL_CONTEXT) != 0) {
122: frame.setSelection(viewer.getSelection());
123: // include current input in expanded set
124: Object[] expanded = viewer.getExpandedElements();
125: Object[] newExpanded = new Object[expanded.length + 1];
126: System.arraycopy(expanded, 0, newExpanded, 0,
127: expanded.length);
128: newExpanded[newExpanded.length - 1] = input;
129: frame.setExpandedElements(newExpanded);
130: }
131: return frame;
132: }
133: }
134:
135: /**
136: * Returns the frame for the selection, or <code>null</code> if there is no
137: * frame for the selection.
138: *
139: * @param flags a bit-wise OR of the frame source flag constants
140: * @return the selection frame, or <code>null</code>
141: */
142: protected Frame getSelectionFrame(int flags) {
143: IStructuredSelection sel = (IStructuredSelection) viewer
144: .getSelection();
145: if (sel.size() == 1) {
146: Object o = sel.getFirstElement();
147: if (viewer.isExpandable(o)) {
148: TreeFrame frame = createFrame(o);
149: if ((flags & IFrameSource.FULL_CONTEXT) != 0) {
150: frame.setSelection(viewer.getSelection());
151: frame.setExpandedElements(viewer
152: .getExpandedElements());
153: }
154: return frame;
155: }
156: }
157: return null;
158: }
159:
160: /**
161: * Returns the tree viewer.
162: *
163: * @return the tree viewer
164: */
165: public AbstractTreeViewer getViewer() {
166: return viewer;
167: }
168:
169: /**
170: * Handles a property change event from the frame list.
171: * Calls <code>frameChanged</code> when the current frame changes.
172: */
173: protected void handlePropertyChange(PropertyChangeEvent event) {
174: if (FrameList.P_CURRENT_FRAME.equals(event.getProperty())) {
175: frameChanged((TreeFrame) event.getNewValue());
176: }
177: }
178: }
|