KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > framelist > TreeViewerFrameSource


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.views.framelist;
12
13 import org.eclipse.jface.util.IPropertyChangeListener;
14 import org.eclipse.jface.util.PropertyChangeEvent;
15 import org.eclipse.jface.viewers.AbstractTreeViewer;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.viewers.ITreeContentProvider;
18
19 /**
20  * Frame source for tree viewers, which uses <code>TreeFrame</code> to capture
21  * the state of the tree viewer.
22  *
23  * @see TreeFrame
24  */

25 public class TreeViewerFrameSource implements IFrameSource {
26
27     private AbstractTreeViewer viewer;
28
29     /**
30      * Constructs a new tree viewer frame source for the specified tree viewer.
31      *
32      * @param viewer the tree viewer
33      */

34     public TreeViewerFrameSource(AbstractTreeViewer viewer) {
35         this.viewer = viewer;
36     }
37
38     /**
39      * Connects this source as a listener on the frame list,
40      * so that when the current frame changes, the viewer is updated.
41      */

42     public void connectTo(FrameList frameList) {
43         frameList.addPropertyChangeListener(new IPropertyChangeListener() {
44             public void propertyChange(PropertyChangeEvent event) {
45                 TreeViewerFrameSource.this.handlePropertyChange(event);
46             }
47         });
48     }
49
50     /**
51      * Returns a new tree frame capturing the specified input element.
52      *
53      * @param input the input element
54      * @return the tree frame
55      */

56     protected TreeFrame createFrame(Object JavaDoc input) {
57         return new TreeFrame(viewer, input);
58     }
59
60     /**
61      * Updates the viewer in response to the current frame changing.
62      *
63      * @param frame the new value for the current frame
64      */

65     protected void frameChanged(TreeFrame frame) {
66         viewer.getControl().setRedraw(false);
67         viewer.setInput(frame.getInput());
68         viewer.setExpandedElements(frame.getExpandedElements());
69         viewer.setSelection(frame.getSelection(), true);
70         viewer.getControl().setRedraw(true);
71     }
72
73     /**
74      * Returns the current frame.
75      *
76      * @param flags a bit-wise OR of the frame source flag constants
77      * @return the current frame
78      */

79     protected Frame getCurrentFrame(int flags) {
80         Object JavaDoc input = viewer.getInput();
81         TreeFrame frame = createFrame(input);
82         if ((flags & IFrameSource.FULL_CONTEXT) != 0) {
83             frame.setSelection(viewer.getSelection());
84             frame.setExpandedElements(viewer.getExpandedElements());
85         }
86         return frame;
87     }
88
89     /* (non-Javadoc)
90      * Method declared on IFrameSource.
91      */

92     public Frame getFrame(int whichFrame, int flags) {
93         switch (whichFrame) {
94         case IFrameSource.CURRENT_FRAME:
95             return getCurrentFrame(flags);
96         case IFrameSource.PARENT_FRAME:
97             return getParentFrame(flags);
98         case IFrameSource.SELECTION_FRAME:
99             return getSelectionFrame(flags);
100         default:
101             return null;
102         }
103     }
104
105     /**
106      * Returns the parent frame, or <code>null</code> if there is no parent frame.
107      *
108      * @param flags a bit-wise OR of the frame source flag constants
109      * @return the parent frame, or <code>null</code>
110      */

111     protected Frame getParentFrame(int flags) {
112         Object JavaDoc input = viewer.getInput();
113         ITreeContentProvider provider = (ITreeContentProvider) viewer
114                 .getContentProvider();
115         Object JavaDoc parent = provider.getParent(input);
116         if (parent == null) {
117             return null;
118         } else {
119             TreeFrame frame = createFrame(parent);
120             if ((flags & IFrameSource.FULL_CONTEXT) != 0) {
121                 frame.setSelection(viewer.getSelection());
122                 // include current input in expanded set
123
Object JavaDoc[] expanded = viewer.getExpandedElements();
124                 Object JavaDoc[] newExpanded = new Object JavaDoc[expanded.length + 1];
125                 System.arraycopy(expanded, 0, newExpanded, 0, expanded.length);
126                 newExpanded[newExpanded.length - 1] = input;
127                 frame.setExpandedElements(newExpanded);
128             }
129             return frame;
130         }
131     }
132
133     /**
134      * Returns the frame for the selection, or <code>null</code> if there is no
135      * frame for the selection.
136      *
137      * @param flags a bit-wise OR of the frame source flag constants
138      * @return the selection frame, or <code>null</code>
139      */

140     protected Frame getSelectionFrame(int flags) {
141         IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
142         if (sel.size() == 1) {
143             Object JavaDoc o = sel.getFirstElement();
144             if (viewer.isExpandable(o)) {
145                 TreeFrame frame = createFrame(o);
146                 if ((flags & IFrameSource.FULL_CONTEXT) != 0) {
147                     frame.setSelection(viewer.getSelection());
148                     frame.setExpandedElements(viewer.getExpandedElements());
149                 }
150                 return frame;
151             }
152         }
153         return null;
154     }
155
156     /**
157      * Returns the tree viewer.
158      *
159      * @return the tree viewer
160      */

161     public AbstractTreeViewer getViewer() {
162         return viewer;
163     }
164
165     /**
166      * Handles a property change event from the frame list.
167      * Calls <code>frameChanged</code> when the current frame changes.
168      */

169     protected void handlePropertyChange(PropertyChangeEvent event) {
170         if (FrameList.P_CURRENT_FRAME.equals(event.getProperty())) {
171             frameChanged((TreeFrame) event.getNewValue());
172         }
173     }
174 }
175
Popular Tags