KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > web > CollectionItemView


1 /*
2   Copyright (C) 2002-2003 Julien van Malderen <julien@aopsys.com>
3                           Laurent Martelli <laurent@aopsys.com>
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU Lesser General Public License as
7   published by the Free Software Foundation; either version 2 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   GNU Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

18
19 package org.objectweb.jac.aspects.gui.web;
20
21 import org.objectweb.jac.aspects.gui.*;
22 import org.objectweb.jac.core.rtti.CollectionItem;
23 import org.objectweb.jac.util.ExtArrays;
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 /**
30  * Component used to display elements of a collection, with "prev" and
31  * "next" buttons to go to the previous or next element of the
32  * collection easily. Can be useful.
33  *
34  */

35
36 public class CollectionItemView extends AbstractView
37     implements HTMLViewer, CollectionItemViewListener, AbstractCollectionItemView
38 {
39     Object JavaDoc substance;
40     CollectionItem collection;
41     CollectionView collectionView;
42     CollectionModel model;
43     /** current position in collection */
44     int current;
45     ObjectView objectView;
46     String JavaDoc viewType;
47     String JavaDoc[] viewParams;
48     View hiddenView;
49
50     /**
51      * @param view the initial embedded view
52      * @param coll the initial position in the collection
53      * @param viewType the type of the view
54      * @param hiddenView the hidden view
55      */

56     public CollectionItemView(View view,
57                               CollectionPosition coll,
58                               String JavaDoc viewType, String JavaDoc[] viewParams,
59                               View hiddenView) {
60         this.objectView = (ObjectView) view;
61         this.collection = coll.getCollection();
62         this.collectionView = coll.getCollectionView();
63         this.model = this.collectionView.getCollectionModel();
64         this.current = coll.getIndex();
65         this.substance = coll.getSubstance();
66         this.viewType = viewType;
67         this.viewParams = viewParams;
68         this.hiddenView = hiddenView;
69     }
70
71     public View getView()
72     {
73         return objectView;
74     }
75
76     public void close(boolean validate) {
77         super.close(validate);
78         objectView.close(validate);
79     }
80
81     public void setCollection(CollectionItem coll) {
82         collection = coll;
83     }
84
85     public CollectionItem getCollection() {
86         return collection;
87     }
88
89     public void setCurrent(int index) {
90         current = index;
91     }
92
93     public int getCurrent() {
94         return current;
95     }
96
97     private void genPrevNext(String JavaDoc prev,
98                              String JavaDoc next,
99                              int total,
100                              PrintWriter JavaDoc out) {
101         out.println("<div class=\"navTool\">");
102
103         if (prev != null)
104         {
105             out.println("<div class=\"prev\">");
106             out.print("<a HREF=\""
107                       + eventURL("onPreviousInCollection")
108                       + "\">"
109                       + "(" + prev + ")"
110                       + iconElement(ResourceManager.getResource("previous_icon"),
111                                     "previous")
112                       + "</a>");
113             out.println("&nbsp;");
114             out.println("</div>");
115         }
116       
117         int cur = current + 1;
118         out.print("[" + cur + " / " + total + "]");
119
120         if (next != null)
121         {
122             out.println("<div class=\"next\">");
123             out.print("<a HREF=\""
124                       + eventURL("onNextInCollection")
125                       + "\">"
126                       + iconElement(ResourceManager.getResource("next_icon"),
127                                     "next")
128                       + "(" + next + ")"
129                       + "</a>");
130             out.println("</div>");
131         }
132
133         if (((View)collectionView).isClosed()) {
134             out.println("<div class=\"back\">");
135             out.print("<a HREF=\""
136                       +eventURL("onBackToCollection")
137                       +"\">");
138             out.print(iconElement(ResourceManager.getResource("up_icon"),
139                                   "back"));
140             out.print("</a>");
141             out.println("</div>");
142         }
143
144         if (GuiAC.isRemovable(collection)) {
145             out.println("<div class=\"remove\">");
146             out.print("<a HREF=\""
147                       +eventURL("onRemoveInCollection")
148                       +"\">"
149                       +iconElement(ResourceManager.getResource("remove_icon"),
150                                    "remove"));
151             out.println("</a>");
152             out.println("</div>");
153         }
154
155         out.println("</div>");
156     }
157
158     public void genHTML(PrintWriter JavaDoc out) throws IOException JavaDoc {
159         if (!GuiAC.hasSetNavBar(context.getCustomizedView().getCustomizedGUI(),
160                                 collection)) {
161             objectView.genHTML(out);
162             return;
163         }
164
165         int size = model.getRowCount();
166
167         String JavaDoc prevStr =
168             (current>0) ? GuiAC.toString(model.getObject(current-1)) : null;
169         String JavaDoc nextStr =
170             (current<(size-1)) ?
171             GuiAC.toString(model.getObject(current+1)) :
172             null;
173
174         genPrevNext(prevStr, nextStr, size, out);
175         objectView.genHTML(out);
176         genPrevNext(prevStr, nextStr, size, out);
177     }
178
179
180     public void onNextInCollection() {
181         if (current < model.getRowCount()-1)
182         {
183             current++;
184             if (collectionView!=null)
185                 collectionView.setSelected(current);
186             Object JavaDoc curr = model.getObject(current);
187             objectView.close(true);
188             objectView = (ObjectView) factory.createView("target[?]", viewType,
189                                                          ExtArrays.add(curr,viewParams),
190                                                          context);
191         }
192         context.getDisplay().refresh();
193     }
194
195     public void onPreviousInCollection() {
196         Collection JavaDoc col = collection.getActualCollection(substance);
197         if (current > 0) {
198             current--;
199             if (collectionView!=null)
200                 collectionView.setSelected(current);
201             Object JavaDoc curr = model.getObject(current);
202             objectView.close(true);
203             objectView = (ObjectView)
204                 factory.createView("target[?]", viewType,
205                                    ExtArrays.add(curr,viewParams),
206                                    context);
207         }
208         context.getDisplay().refresh();
209     }
210
211     protected CompositeView findPanel() {
212         View current = getParentView();
213         View last = null;
214         while (current!=null && !(current instanceof PanelView)) {
215             last = current;
216             current = current.getParentView();
217         }
218         return (CompositeView)last;
219     }
220
221     public void onBackToCollection() {
222         CompositeView panel = findPanel();
223         if (panel!=null) {
224             int numRows = GuiAC.getNumRowsPerPage(collection);
225             GuiAC.setStartIndex(
226                 collection,
227                 numRows>0 ? (current - current%numRows) : 0);
228             try {
229                 panel.addView(
230                     factory.createView(substance.getClass().getName(),
231                                        "Object",new Object JavaDoc[] {"default",substance},context));
232             } finally {
233                 GuiAC.removeStartIndex(collection);
234             }
235         }
236         context.getDisplay().refresh();
237     }
238
239     public void onRemoveInCollection() {
240         Collection JavaDoc col = collection.getActualCollection(substance);
241         int old = current;
242
243         if (current > 0) {
244             current--;
245         } else if (col.size() <= 1) {
246             col.clear();
247             objectView.close(true);
248             onBackToCollection();
249             return;
250         }
251
252         Object JavaDoc curr = null;
253         Iterator JavaDoc it = col.iterator();
254         for (int i=0; it.hasNext() && i<=old; i++)
255             curr = it.next();
256
257         try {
258             collection.removeThroughRemover(substance,curr);
259         } catch (Exception JavaDoc e) {
260             e.printStackTrace();
261             current = old;
262             context.getDisplay().refresh();
263             return;
264         }
265
266         Iterator JavaDoc it2 = col.iterator();
267         for (int i=0; it2.hasNext() && i<=current; i++) {
268             curr = it2.next();
269         }
270         objectView.close(true);
271         objectView = (ObjectView) factory.createView("target[?]", viewType,
272                                                      ExtArrays.add(curr,viewParams),
273                                                      context);
274
275         if (current > 0)
276             collectionView.setSelected(current);
277
278         context.getDisplay().refresh();
279     }
280
281 }
282
Popular Tags