KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.commands.common.EventManager;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.jface.util.IPropertyChangeListener;
19 import org.eclipse.jface.util.PropertyChangeEvent;
20
21 /**
22  * Supports a web-browser style of navigation by maintaining a list
23  * of frames. Each frame holds a snapshot of a view at some point
24  * in time.
25  * <p>
26  * The frame list obtains a snapshot of the current frame from a frame source
27  * on creation, and whenever switching to a different frame.
28  * </p>
29  * <p>
30  * A property change notification is sent whenever the current page changes.
31  * </p>
32  */

33 public class FrameList extends EventManager {
34
35     /** Property name constant for the current frame. */
36     public static final String JavaDoc P_CURRENT_FRAME = "currentFrame"; //$NON-NLS-1$
37

38     private IFrameSource source;
39
40     private List JavaDoc frames;
41
42     private int current;
43
44     /**
45      * Creates a new frame list with the given source.
46      *
47      * @param source the frame source
48      */

49     public FrameList(IFrameSource source) {
50         this.source = source;
51         Frame frame = source.getFrame(IFrameSource.CURRENT_FRAME, 0);
52         frame.setParent(this);
53         frame.setIndex(0);
54         frames = new ArrayList JavaDoc();
55         frames.add(frame);
56         current = 0;
57     }
58
59     /**
60      * Adds a property change listener.
61      * Has no effect if an identical listener is already registered.
62      *
63      * @param listener a property change listener
64      */

65     public void addPropertyChangeListener(IPropertyChangeListener listener) {
66         addListenerObject(listener);
67     }
68
69     /**
70      * Moves the frame pointer back by one.
71      * Has no effect if there is no frame before the current one.
72      * Fires a <code>P_CURRENT_FRAME</code> property change event.
73      */

74     public void back() {
75         if (current > 0) {
76             setCurrent(current - 1);
77         }
78     }
79
80     /**
81      * Notifies any property change listeners that a property has changed.
82      * Only listeners registered at the time this method is called are notified.
83      *
84      * @param event the property change event
85      *
86      * @see IPropertyChangeListener#propertyChange
87      */

88     protected void firePropertyChange(PropertyChangeEvent event) {
89         Object JavaDoc[] listeners = getListeners();
90         for (int i = 0; i < listeners.length; ++i) {
91             ((IPropertyChangeListener) listeners[i]).propertyChange(event);
92         }
93     }
94
95     /**
96      * Moves the frame pointer forward by one.
97      * Has no effect if there is no frame after the current one.
98      * Fires a <code>P_CURRENT_FRAME</code> property change event.
99      */

100     public void forward() {
101         if (current < frames.size() - 1) {
102             setCurrent(current + 1);
103         }
104     }
105
106     /**
107      * Returns the current frame.
108      * Returns <code>null</code> if there is no current frame.
109      *
110      * @return the current frame, or <code>null</code>
111      */

112     public Frame getCurrentFrame() {
113         return getFrame(current);
114     }
115
116     /**
117      * Returns the index of the current frame.
118      *
119      * @return the index of the current frame
120      */

121     public int getCurrentIndex() {
122         return current;
123     }
124
125     /**
126      * Returns the frame at the given index, or <code>null</code>
127      * if the index is &le; 0 or &ge; <code>size()</code>.
128      *
129      * @param index the index of the requested frame
130      * @return the frame at the given index or <code>null</code>
131      */

132     public Frame getFrame(int index) {
133         if (index < 0 || index >= frames.size()) {
134             return null;
135         }
136         return (Frame) frames.get(index);
137     }
138
139     /**
140      * Returns the frame source.
141      */

142     public IFrameSource getSource() {
143         return source;
144     }
145
146     /**
147      * Adds the given frame after the current frame,
148      * and advances the pointer to the new frame.
149      * Before doing so, updates the current frame, and removes any frames following the current frame.
150      * Fires a <code>P_CURRENT_FRAME</code> property change event.
151      *
152      * @param frame the frame to add
153      */

154     public void gotoFrame(Frame frame) {
155         for (int i = frames.size(); --i > current;) {
156             frames.remove(i);
157         }
158         frame.setParent(this);
159         int index = frames.size();
160         frame.setIndex(index);
161         frames.add(frame);
162         setCurrent(index);
163     }
164
165     /**
166      * Removes a property change listener.
167      * Has no effect if an identical listener is not registered.
168      *
169      * @param listener a property change listener
170      */

171     public void removePropertyChangeListener(IPropertyChangeListener listener) {
172         removeListenerObject(listener);
173     }
174
175     /**
176      * Sets the current frame to the one with the given index.
177      * Updates the old current frame, and fires a <code>P_CURRENT_FRAME</code> property change event
178      * if the current frame changes.
179      *
180      * @param newCurrent the index of the frame
181      */

182     void setCurrent(int newCurrent) {
183         Assert.isTrue(newCurrent >= 0 && newCurrent < frames.size());
184         int oldCurrent = this.current;
185         if (oldCurrent != newCurrent) {
186             updateCurrentFrame();
187             this.current = newCurrent;
188             firePropertyChange(new PropertyChangeEvent(this, P_CURRENT_FRAME,
189                     getFrame(oldCurrent), getFrame(newCurrent)));
190         }
191     }
192
193     /**
194      * Sets the current frame to the frame with the given index.
195      * Fires a <code>P_CURRENT_FRAME</code> property change event
196      * if the current frame changes.
197      */

198     public void setCurrentIndex(int index) {
199         if (index != -1 && index != current) {
200             setCurrent(index);
201         }
202     }
203
204     /**
205      * Returns the number of frames in the frame list.
206      */

207     public int size() {
208         return frames.size();
209     }
210
211     /**
212      * Replaces the current frame in this list with the current frame
213      * from the frame source. No event is fired.
214      */

215     public void updateCurrentFrame() {
216         Assert.isTrue(current >= 0);
217         Frame frame = source.getFrame(IFrameSource.CURRENT_FRAME,
218                 IFrameSource.FULL_CONTEXT);
219         frame.setParent(this);
220         frame.setIndex(current);
221         frames.set(current, frame);
222     }
223 }
224
Popular Tags