001 /* 002 Copyright (C) 2002-2003 Julien van Malderen <julien@aopsys.com> 003 Laurent Martelli <laurent@aopsys.com> 004 005 This program is free software; you can redistribute it and/or modify 006 it under the terms of the GNU Lesser General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This program is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 GNU Lesser General Public License for more details. 014 015 You should have received a copy of the GNU Lesser General Public License 016 along with this program; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 018 019 package org.objectweb.jac.aspects.gui.web; 020 021 import org.objectweb.jac.aspects.gui.*; 022 import org.objectweb.jac.core.rtti.CollectionItem; 023 import org.objectweb.jac.util.ExtArrays; 024 import java.io.IOException; 025 import java.io.PrintWriter; 026 import java.util.Collection; 027 import java.util.Iterator; 028 029 /** 030 * Component used to display elements of a collection, with "prev" and 031 * "next" buttons to go to the previous or next element of the 032 * collection easily. Can be useful. 033 * 034 */ 035 036 public class CollectionItemView extends AbstractView 037 implements HTMLViewer, CollectionItemViewListener, AbstractCollectionItemView 038 { 039 Object substance; 040 CollectionItem collection; 041 CollectionView collectionView; 042 CollectionModel model; 043 /** current position in collection */ 044 int current; 045 ObjectView objectView; 046 String viewType; 047 String[] viewParams; 048 View hiddenView; 049 050 /** 051 * @param view the initial embedded view 052 * @param coll the initial position in the collection 053 * @param viewType the type of the view 054 * @param hiddenView the hidden view 055 */ 056 public CollectionItemView(View view, 057 CollectionPosition coll, 058 String viewType, String[] viewParams, 059 View hiddenView) { 060 this.objectView = (ObjectView) view; 061 this.collection = coll.getCollection(); 062 this.collectionView = coll.getCollectionView(); 063 this.model = this.collectionView.getCollectionModel(); 064 this.current = coll.getIndex(); 065 this.substance = coll.getSubstance(); 066 this.viewType = viewType; 067 this.viewParams = viewParams; 068 this.hiddenView = hiddenView; 069 } 070 071 public View getView() 072 { 073 return objectView; 074 } 075 076 public void close(boolean validate) { 077 super.close(validate); 078 objectView.close(validate); 079 } 080 081 public void setCollection(CollectionItem coll) { 082 collection = coll; 083 } 084 085 public CollectionItem getCollection() { 086 return collection; 087 } 088 089 public void setCurrent(int index) { 090 current = index; 091 } 092 093 public int getCurrent() { 094 return current; 095 } 096 097 private void genPrevNext(String prev, 098 String next, 099 int total, 100 PrintWriter 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(" "); 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 out) throws IOException { 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 prevStr = 168 (current>0) ? GuiAC.toString(model.getObject(current-1)) : null; 169 String 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 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 col = collection.getActualCollection(substance); 197 if (current > 0) { 198 current--; 199 if (collectionView!=null) 200 collectionView.setSelected(current); 201 Object 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[] {"default",substance},context)); 232 } finally { 233 GuiAC.removeStartIndex(collection); 234 } 235 } 236 context.getDisplay().refresh(); 237 } 238 239 public void onRemoveInCollection() { 240 Collection 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 curr = null; 253 Iterator 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 e) { 260 e.printStackTrace(); 261 current = old; 262 context.getDisplay().refresh(); 263 return; 264 } 265 266 Iterator 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 }