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.swing; 020 021 022 import org.objectweb.jac.aspects.gui.*; 023 import org.objectweb.jac.core.rtti.CollectionItem; 024 import org.objectweb.jac.util.ExtArrays; 025 import java.awt.BorderLayout; 026 import java.awt.Insets; 027 import java.awt.event.ActionEvent; 028 import java.awt.event.ActionListener; 029 import java.util.Collection; 030 import java.util.Iterator; 031 import javax.swing.JButton; 032 import javax.swing.JLabel; 033 import javax.swing.JPanel; 034 035 public class CollectionItemView extends AbstractView 036 implements ActionListener, AbstractCollectionItemView 037 { 038 Object substance; 039 CollectionItem collection; 040 CollectionView collectionView; 041 CollectionModel model; 042 int current; 043 ObjectView objectView; 044 String viewType; 045 JButton parentButton; 046 View hiddenView; 047 String[] viewParams; 048 049 public CollectionItemView(View view, 050 CollectionPosition coll, 051 String viewType, String[] viewParams, 052 View hiddenView) { 053 this.objectView = (ObjectView) view; 054 this.collection = coll.getCollection(); 055 this.collectionView = coll.getCollectionView(); 056 this.model = this.collectionView.getCollectionModel(); 057 this.current = coll.getIndex(); 058 this.substance = coll.getSubstance(); 059 this.viewType = viewType; 060 this.viewParams = viewParams; 061 this.hiddenView = hiddenView; 062 063 draw(); 064 } 065 066 public View getView() { 067 return objectView; 068 } 069 070 public void close(boolean validate) { 071 super.close(validate); 072 objectView.close(validate); 073 } 074 075 protected void draw() { 076 setLayout(new BorderLayout()); 077 078 if (GuiAC.hasSetNavBar(objectView.context.getCustomizedView() 079 .getCustomizedGUI(), 080 collection)) 081 { 082 JPanel panel = new JPanel(); 083 084 085 String prevStr = 086 (current>0) ? GuiAC.toString(model.getObject(current-1)) : null; 087 String nextStr = 088 (current<(model.getRowCount()-1)) ? 089 GuiAC.toString(model.getObject(current+1)) : 090 null; 091 092 if (prevStr != null) { 093 JButton prevButton = 094 new JButton(" ("+ prevStr +") ", 095 ResourceManager.getIconResource("previous_icon")); 096 prevButton.setToolTipText("Previous Item"); 097 prevButton.setActionCommand("previous"); 098 prevButton.addActionListener(this); 099 prevButton.setHorizontalTextPosition(JButton.LEFT); 100 //prevButton.setMargin(new Insets(1,1,1,1)); 101 panel.add(prevButton); 102 } 103 104 int cur = current + 1; 105 JLabel counter = new JLabel("["+cur+" / "+model.getRowCount()+"]"); 106 panel.add(counter); 107 108 if (nextStr != null) { 109 JButton nextButton = 110 new JButton(" ("+ nextStr +") ", 111 ResourceManager.getIconResource("next_icon")); 112 nextButton.setToolTipText("Next Item"); 113 nextButton.setActionCommand("next"); 114 nextButton.addActionListener(this); 115 //nextButton.setMargin(new Insets(1,1,1,1)); 116 panel.add(nextButton); 117 } 118 119 if (((View)collectionView).isClosed()) { 120 parentButton = 121 new JButton(ResourceManager.getIconResource("up_icon")); 122 parentButton.setToolTipText("Back to Collection"); 123 parentButton.setActionCommand("back"); 124 parentButton.addActionListener(this); 125 //parentButton.setMargin(new Insets(1,1,1,1)); 126 panel.add(parentButton); 127 } 128 129 if (GuiAC.isRemovable(collection)) { 130 JButton removeButton = 131 new JButton(ResourceManager.getIconResource("remove_icon")); 132 removeButton.setToolTipText("Remove Item"); 133 removeButton.setActionCommand("remove"); 134 removeButton.addActionListener(this); 135 removeButton.setMargin(new Insets(1,1,1,1)); 136 panel.add(removeButton); 137 } 138 139 add(panel,BorderLayout.NORTH); 140 } 141 142 add((java.awt.Component) objectView,BorderLayout.CENTER); 143 144 } 145 146 public void onNextInCollection() { 147 if (current < model.getRowCount() - 1) 148 { 149 current++; 150 if (collectionView!=null) 151 collectionView.setSelected(current); 152 Object curr = model.getObject(current); 153 objectView.close(true); 154 objectView = (ObjectView) factory.createView("target[?]", viewType, 155 ExtArrays.add(curr,viewParams), 156 context); 157 removeAll(); 158 draw(); 159 validate(); 160 } 161 } 162 163 public void onPreviousInCollection() { 164 if (current > 0) { 165 current--; 166 if (collectionView!=null) 167 collectionView.setSelected(current); 168 Object curr = model.getObject(current); 169 objectView.close(true); 170 objectView = (ObjectView) factory.createView("target[?]", viewType, 171 ExtArrays.add(curr,viewParams), 172 context); 173 removeAll(); 174 draw(); 175 validate(); 176 } 177 } 178 179 public void onRemoveInCollection() { 180 Collection col = collection.getActualCollection(substance); 181 int old = current; 182 183 if (current > 0) { 184 current--; 185 } else if (col.size() <= 1) { 186 col.clear(); 187 objectView.close(true); 188 onBackToCollection(); 189 return; 190 } 191 192 Object curr = null; 193 Iterator it = col.iterator(); 194 for (int i=0; it.hasNext() && i<=old; i++) { 195 curr = it.next(); 196 } 197 198 try { 199 collection.removeThroughRemover(substance,curr); 200 } catch (Exception e) { 201 e.printStackTrace(); 202 current = old; 203 context.getDisplay().refresh(); 204 return; 205 } 206 207 Iterator it2 = col.iterator(); 208 for (int i=0; it2.hasNext() && i<=current; i++) { 209 curr = it2.next(); 210 } 211 objectView.close(true); 212 objectView = (ObjectView) factory.createView("target[?]", viewType, 213 ExtArrays.add(curr,viewParams), 214 context); 215 System.out.println("model="+collectionView.getCollectionModel()); 216 if (current>0) 217 collectionView.setSelected(current); 218 removeAll(); 219 draw(); 220 validate(); 221 } 222 223 protected CompositeView findPanel() { 224 View current = getParentView(); 225 View last = null; 226 while (current!=null && !(current instanceof PanelView)) { 227 last = current; 228 current = current.getParentView(); 229 } 230 return (CompositeView)last; 231 } 232 233 public void onBackToCollection() { 234 CompositeView panel = findPanel(); 235 if (panel!=null) { 236 panel.addView( 237 factory.createView(substance.getClass().getName(), 238 "Object",new Object[] {"default",substance},context)); 239 } 240 validate(); 241 } 242 243 public void actionPerformed(ActionEvent evt) { 244 //setContext(); 245 if (evt.getActionCommand().equals("previous")) { 246 onPreviousInCollection(); 247 } else if (evt.getActionCommand().equals("next")) { 248 onNextInCollection(); 249 } else if (evt.getActionCommand().equals("remove")) { 250 onRemoveInCollection(); 251 } else if (evt.getActionCommand().equals("back")) { 252 onBackToCollection(); 253 } 254 repaint(); 255 } 256 257 public void setCollection(CollectionItem coll) { 258 collection = coll; 259 } 260 261 public CollectionItem getCollection() { 262 return collection; 263 } 264 265 public void setCurrent(int index) { 266 current = index; 267 } 268 269 public int getCurrent() { 270 return current; 271 } 272 }