KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > components > Border


1 // Copyright 2004 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.vlib.components;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.tapestry.BaseComponent;
19 import org.apache.tapestry.IAsset;
20 import org.apache.tapestry.IBinding;
21 import org.apache.tapestry.IEngine;
22 import org.apache.tapestry.IPage;
23 import org.apache.tapestry.IRequestCycle;
24 import org.apache.tapestry.Tapestry;
25 import org.apache.tapestry.callback.ICallback;
26 import org.apache.tapestry.callback.PageCallback;
27 import org.apache.tapestry.vlib.IActivate;
28 import org.apache.tapestry.vlib.IMessageProperty;
29 import org.apache.tapestry.vlib.VirtualLibraryEngine;
30 import org.apache.tapestry.vlib.Visit;
31 import org.apache.tapestry.vlib.pages.Login;
32
33 /**
34  * The standard Border component, which provides the title of the page,
35  * the link to {@link org.apache.tapestry.vlib.pages.MyLibrary},
36  * the {@link org.apache.tapestry.vlib.pages.Login} page and the
37  * Logout page.
38  *
39  * @author Howard Lewis Ship
40  * @version $Id: Border.java,v 1.15 2004/10/31 22:57:46 hlship Exp $
41  *
42  **/

43
44 public abstract class Border extends BaseComponent
45 {
46     public static final String JavaDoc WINDOW_TITLE = "Tapestry Virtual Library";
47
48     // Determined by finishLoad(), static threafter.
49

50     private String JavaDoc _pageType;
51     private IAsset _titleImage;
52     private IAsset _searchImage;
53     private IAsset _searchRolloverImage;
54     private IAsset _myLibraryImage;
55     private IAsset _myLibraryRolloverImage;
56     private IAsset _borrowedBooksImage;
57     private IAsset _borrowedBooksRolloverImage;
58     private IAsset _loginImage;
59     private IAsset _loginRolloverImage;
60     private IAsset _newBookImage;
61     private IAsset _newBookRolloverImage;
62     private IAsset _editProfileImage;
63     private IAsset _editProfileRolloverImage;
64     private IAsset _giveAwayImage;
65     private IAsset _giveAwayRolloverImage;
66     private IAsset _editUsersImage;
67     private IAsset _editUsersRolloverImage;
68     private IAsset _editPublishersImage;
69     private IAsset _editPublishersRolloverImage;
70     private IAsset _transferBooksImage;
71     private IAsset _transferBooksRolloverImage;
72
73     public abstract String JavaDoc getSubtitle();
74
75     public void finishLoad()
76     {
77         IPage page = getPage();
78
79         String JavaDoc pageName = page.getPageName();
80
81         _pageType = page.getSpecification().getProperty("page-type");
82
83         if (_pageType == null)
84             _pageType = pageName;
85
86         _titleImage = getAsset("title_" + pageName);
87
88         if (_titleImage == null)
89             _titleImage = getAsset("title_" + _pageType);
90
91         if (_titleImage == null)
92             throw new ApplicationRuntimeException(
93                 "Cannot find title image for " + pageName + " or " + _pageType + ".",
94                 this,
95                 null,
96                 null);
97
98         // Based on the type, select the images to use on this instance of Border
99
// in this particular page.
100

101         _searchImage = selectImage("Search", "search");
102         _searchRolloverImage = selectImage("Search", "search_h");
103
104         _myLibraryImage = selectImage("MyLibrary", "mylibrary");
105         _myLibraryRolloverImage = selectImage("MyLibrary", "mylibrary_h");
106
107         _borrowedBooksImage = selectImage("BorrowedBooks", "borrowedbooks");
108         _borrowedBooksRolloverImage = selectImage("BorrowedBooks", "borrowedbooks_h");
109
110         _newBookImage = selectImage("NewBook", "newbook");
111         _newBookRolloverImage = selectImage("NewBook", "newbook_h");
112
113         _editProfileImage = selectImage("EditProfile", "editprofile");
114         _editProfileRolloverImage = selectImage("EditProfile", "editprofile_h");
115
116         _giveAwayImage = selectImage("GiveAwayBooks", "giveaway");
117         _giveAwayRolloverImage = selectImage("GiveAwayBooks", "giveaway_h");
118
119         _editUsersImage = selectImage("EditUsers", "editusers");
120         _editUsersRolloverImage = selectImage("EditUsers", "editusers_h");
121
122         _editPublishersImage = selectImage("EditPublishers", "editpublishers");
123         _editPublishersRolloverImage = selectImage("EditPublishers", "editpublishers_h");
124
125         _transferBooksImage = selectImage("TransferBooks", "transferbooks");
126         _transferBooksRolloverImage = selectImage("TransferBooks", "transferbooks_h");
127
128         _loginImage = selectImage("Login", "login");
129         _loginRolloverImage = selectImage("Login", "login_h");
130     }
131
132     private IAsset selectImage(String JavaDoc type, String JavaDoc baseName)
133     {
134         String JavaDoc key = _pageType.equals(type) ? baseName + "_s" : baseName;
135
136         return getAsset(key);
137     }
138
139     public String JavaDoc getWindowTitle()
140     {
141         String JavaDoc subtitle = getSubtitle();
142
143         if (subtitle == null)
144             return WINDOW_TITLE;
145
146         return WINDOW_TITLE + ": " + subtitle;
147     }
148
149     public boolean isLoggedIn()
150     {
151         IEngine engine = getPage().getEngine();
152         Visit visit = (Visit) engine.getVisit();
153
154         if (visit == null)
155             return false;
156
157         return visit.isUserLoggedIn();
158     }
159
160     public boolean isAdmin()
161     {
162         IEngine engine = getPage().getEngine();
163         Visit visit = (Visit) engine.getVisit();
164
165         if (visit == null)
166             return false;
167
168         IRequestCycle cycle = getPage().getRequestCycle();
169
170         return visit.isUserLoggedIn() && visit.getUser(cycle).isAdmin();
171     }
172
173     public void editProfile(IRequestCycle cycle)
174     {
175         activate("EditProfile", cycle);
176     }
177
178     public void viewBorrowedBooks(IRequestCycle cycle)
179     {
180         activate("BorrowedBooks", cycle);
181     }
182
183     public void viewMyLibrary(IRequestCycle cycle)
184     {
185         activate("MyLibrary", cycle);
186     }
187
188     private void activate(String JavaDoc pageName, IRequestCycle cycle)
189     {
190         IActivate page = (IActivate) cycle.getPage(pageName);
191
192         page.validate(cycle);
193
194         page.activate(cycle);
195     }
196
197     public void login(IRequestCycle cycle)
198     {
199         Visit visit = (Visit) cycle.getEngine().getVisit();
200
201         if (visit != null && visit.isUserLoggedIn())
202             return;
203
204         ICallback callback = new PageCallback(getPage().getPageName());
205         Login loginPage = (Login) cycle.getPage("Login");
206         loginPage.setCallback(callback);
207         cycle.activate(loginPage);
208     }
209
210     public void logout(IRequestCycle cycle)
211     {
212         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getPage().getEngine();
213
214         vengine.logout();
215
216         IMessageProperty home = (IMessageProperty) cycle.getPage("Home");
217
218         home.setMessage(getMessage("goodbye"));
219
220         cycle.activate(home);
221     }
222
223     public void selectBrowserPage(IRequestCycle cycle)
224     {
225         Integer JavaDoc page = (Integer JavaDoc) cycle.getServiceParameters()[0];
226
227         // Not rendering, so have to access the browser the hard way.
228

229         IBinding browserBinding = getBrowserBinding();
230
231         Browser browser = (Browser) browserBinding.getObject("browser", Browser.class);
232
233         if (browser == null)
234             throw Tapestry.createRequiredParameterException(this, "browser");
235
236         browser.jump(page.intValue());
237     }
238
239     public IAsset getBorrowedBooksImage()
240     {
241         return _borrowedBooksImage;
242     }
243
244     public IAsset getBorrowedBooksRolloverImage()
245     {
246         return _borrowedBooksRolloverImage;
247     }
248
249     public IAsset getTitleImage()
250     {
251         return _titleImage;
252     }
253
254     public IAsset getSearchImage()
255     {
256         return _searchImage;
257     }
258
259     public IAsset getSearchRolloverImage()
260     {
261         return _searchRolloverImage;
262     }
263
264     public IAsset getMyLibraryImage()
265     {
266         return _myLibraryImage;
267     }
268
269     public IAsset getMyLibraryRolloverImage()
270     {
271         return _myLibraryRolloverImage;
272     }
273
274     public IAsset getLoginImage()
275     {
276         return _loginImage;
277     }
278
279     public IAsset getLoginRolloverImage()
280     {
281         return _loginRolloverImage;
282     }
283
284     public String JavaDoc getPageType()
285     {
286         return _pageType;
287     }
288
289     public IAsset getNewBookImage()
290     {
291         return _newBookImage;
292     }
293
294     public IAsset getNewBookRolloverImage()
295     {
296         return _newBookRolloverImage;
297     }
298
299     public IAsset getEditProfileImage()
300     {
301         return _editProfileImage;
302     }
303
304     public IAsset getEditProfileRolloverImage()
305     {
306         return _editProfileRolloverImage;
307     }
308
309     public IAsset getGiveAwayImage()
310     {
311         return _giveAwayImage;
312     }
313
314     public IAsset getGiveAwayRolloverImage()
315     {
316         return _giveAwayRolloverImage;
317     }
318
319     public IAsset getEditPublishersImage()
320     {
321         return _editPublishersImage;
322     }
323
324     public IAsset getEditPublishersRolloverImage()
325     {
326         return _editPublishersRolloverImage;
327     }
328
329     public IAsset getEditUsersImage()
330     {
331         return _editUsersImage;
332     }
333
334     public IAsset getEditUsersRolloverImage()
335     {
336         return _editUsersRolloverImage;
337     }
338
339     public IAsset getTransferBooksImage()
340     {
341         return _transferBooksImage;
342     }
343
344     public IAsset getTransferBooksRolloverImage()
345     {
346         return _transferBooksRolloverImage;
347     }
348
349 }
Popular Tags