KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > model > History


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.internal.intro.impl.model;
12
13 import java.util.List JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import org.eclipse.ui.internal.intro.impl.model.url.IntroURLParser;
17
18 /*
19  * Intro History Model.
20  */

21 public class History {
22     // History of Intro Pages and real URL visited by Intro Browser. All
23
// elements are all of type HistoryObject.
24
private Vector JavaDoc history = new Vector JavaDoc();
25     private int navigationLocation = 0;
26
27
28     // Model class for history objects. A history object may be a url or an
29
// Intro page. A url is a regular URL navigated to from a fully qualified
30
// link. An intro page may be an IFrame page. IFrame pages are not created
31
// for every Help topic navigated in an embedded IFrame. Instead the same
32
// IFrame is stored in history as a different object with the IFrameURL set.
33
// This way the model actually creates one page for every embedded Help
34
// Topic target but the navigation history updates the IFrame accordingly.
35
class HistoryObject {
36         AbstractIntroPage page;
37         String JavaDoc iframeUrl;
38         String JavaDoc url;
39
40         HistoryObject(Object JavaDoc location) {
41             if (location instanceof String JavaDoc)
42                 this.url = (String JavaDoc) location;
43
44             if (location instanceof AbstractIntroPage) {
45                 this.page = (AbstractIntroPage) location;
46                 // will be set to null if the page is not an IFrame page.
47
this.iframeUrl = this.page.getIFrameURL();
48             }
49         }
50
51         /**
52          * returns the history page. If iframe page, updated to correct url.
53          *
54          * @return
55          */

56         AbstractIntroPage getPage() {
57             if (page.isIFramePage())
58                 // when page was stored, the IFrame url was also stored. Make
59
// sure to return the same state. The page is the same, only the
60
// IFrame url changes.
61
page.setIFrameURL(getIFrameUrl());
62             return page;
63         }
64
65         String JavaDoc getPageId() {
66             return page.getId();
67         }
68
69         String JavaDoc getIFrameUrl() {
70             return iframeUrl;
71         }
72
73         String JavaDoc getUrl() {
74             return url;
75         }
76
77         boolean isURL() {
78             return (url != null) ? true : false;
79         }
80
81         boolean isIntroPage() {
82             return (page != null) ? true : false;
83         }
84
85         boolean isIFramePage() {
86             return (iframeUrl != null) ? true : false;
87         }
88
89     }
90
91
92     /**
93      * Updates the UI navigation history with either a real URL, or a page ID.
94      *
95      * @param pageId
96      */

97     public void updateHistory(String JavaDoc location) {
98         // quick exit.
99
if (!history.isEmpty() && isSameLocation(location))
100             // resetting the same location is useless.
101
return;
102         doUpdateHistory(location);
103     }
104
105     /**
106      * Updates the UI navigation history with either a real URL, or a page ID.
107      *
108      * @param page
109      */

110     public void updateHistory(AbstractIntroPage page) {
111         // quick exit.
112
if (!history.isEmpty() && isSameLocation(page))
113             // resetting the same location is useless.
114
return;
115         doUpdateHistory(page);
116     }
117
118     private void doUpdateHistory(Object JavaDoc location) {
119         // we got here due to an intro URL listener or an SWT Form hyperlink
120
// listener. location may be a url or an IntroPage.
121
if (navigationLocation == getHistoryEndPosition())
122             // we are at the end of the vector, just push.
123
pushToHistory(location);
124         else
125             // we already navigated. add item at current location, and clear
126
// rest of history. (Same as browser behavior.)
127
trimHistory(location);
128     }
129
130
131     private boolean isSameLocation(Object JavaDoc location) {
132         HistoryObject currentLocation = getCurrentLocation();
133         if (location instanceof String JavaDoc && currentLocation.isURL())
134             return currentLocation.getUrl().equals(location);
135
136         if (location instanceof AbstractIntroPage
137                 && currentLocation.isIntroPage()) {
138
139             AbstractIntroPage locationPage = (AbstractIntroPage) location;
140             // be carefull here with calling getPage on historyOvject.
141
if (!currentLocation.getPageId().equals(locationPage.getId()))
142                 return false;
143
144             // both pages have same ids, they are either both regular pages or
145
// both are Iframe pages. check if they have the same IFrame urls
146
if (currentLocation.isIFramePage() && locationPage.isIFramePage())
147                 return currentLocation.getIFrameUrl().equals(
148                     locationPage.getIFrameURL());
149
150             // both pages are not IFrame pages, and they have same id.
151
return true;
152         }
153
154         return false;
155     }
156
157
158
159
160     private void pushToHistory(Object JavaDoc location) {
161         history.add(new HistoryObject(location));
162         // point the nav location to the end of the vector.
163
navigationLocation = getHistoryEndPosition();
164     }
165     
166      public void removeLastHistory() {
167         history.remove(getHistoryEndPosition());
168         // point the nav location to the end of the vector.
169
navigationLocation = getHistoryEndPosition();
170     }
171
172     private void trimHistory(Object JavaDoc location) {
173         List JavaDoc newHistory = history.subList(0, navigationLocation + 1);
174         history = new Vector JavaDoc(newHistory);
175         history.add(new HistoryObject(location));
176         // point the nav location to the end of the vector.
177
navigationLocation = getHistoryEndPosition();
178     }
179
180     /**
181      * Return the position of the last element in the navigation history. If
182      * vector is empty, return 0.
183      *
184      * @param vector
185      * @return
186      */

187     private int getHistoryEndPosition() {
188         if (history.isEmpty())
189             return 0;
190         return history.size() - 1;
191     }
192
193     public void navigateHistoryBackward() {
194         if (badNavigationLocation(navigationLocation - 1))
195             // do nothing. We are at the begining.
196
return;
197         --navigationLocation;
198     }
199
200     /**
201      * Navigate forward in the history.
202      *
203      * @return
204      */

205     public void navigateHistoryForward() {
206         if (badNavigationLocation(navigationLocation + 1))
207             // do nothing. We are at the begining.
208
return;
209         ++navigationLocation;
210     }
211
212
213     private boolean badNavigationLocation(int navigationLocation) {
214         if (navigationLocation < 0 || navigationLocation >= history.size())
215             // bad nav location.
216
return true;
217         return false;
218     }
219
220
221     /**
222      * Returns true if the current location in the navigation history represents
223      * a URL. False if the location is an Intro Page id.
224      *
225      * @return Returns the locationIsURL.
226      */

227     private HistoryObject getCurrentLocation() {
228         return (HistoryObject) history.elementAt(navigationLocation);
229     }
230
231     public boolean canNavigateForward() {
232         return navigationLocation != getHistoryEndPosition() ? true : false;
233     }
234
235     public boolean canNavigateBackward() {
236         return navigationLocation == 0 ? false : true;
237     }
238
239     public boolean currentLocationIsUrl() {
240         return getCurrentLocation().isURL();
241     }
242
243     public String JavaDoc getCurrentLocationAsUrl() {
244         return getCurrentLocation().getUrl();
245     }
246
247     public AbstractIntroPage getCurrentLocationAsPage() {
248         return getCurrentLocation().getPage();
249     }
250
251     public static boolean isURL(String JavaDoc aString) {
252         IntroURLParser parser = new IntroURLParser(aString);
253         if (parser.hasProtocol())
254             return true;
255         return false;
256     }
257
258
259     public void clear() {
260         history.clear();
261         navigationLocation = 0;
262     }
263
264
265
266
267 }
268
Popular Tags