1 18 19 package org.objectweb.jac.aspects.gui.swing; 20 21 22 import org.objectweb.jac.aspects.gui.*; 23 import org.objectweb.jac.core.rtti.CollectionItem; 24 import org.objectweb.jac.util.ExtArrays; 25 import java.awt.BorderLayout ; 26 import java.awt.Insets ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.ActionListener ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import javax.swing.JButton ; 32 import javax.swing.JLabel ; 33 import javax.swing.JPanel ; 34 35 public class CollectionItemView extends AbstractView 36 implements ActionListener , AbstractCollectionItemView 37 { 38 Object substance; 39 CollectionItem collection; 40 CollectionView collectionView; 41 CollectionModel model; 42 int current; 43 ObjectView objectView; 44 String viewType; 45 JButton parentButton; 46 View hiddenView; 47 String [] viewParams; 48 49 public CollectionItemView(View view, 50 CollectionPosition coll, 51 String viewType, String [] viewParams, 52 View hiddenView) { 53 this.objectView = (ObjectView) view; 54 this.collection = coll.getCollection(); 55 this.collectionView = coll.getCollectionView(); 56 this.model = this.collectionView.getCollectionModel(); 57 this.current = coll.getIndex(); 58 this.substance = coll.getSubstance(); 59 this.viewType = viewType; 60 this.viewParams = viewParams; 61 this.hiddenView = hiddenView; 62 63 draw(); 64 } 65 66 public View getView() { 67 return objectView; 68 } 69 70 public void close(boolean validate) { 71 super.close(validate); 72 objectView.close(validate); 73 } 74 75 protected void draw() { 76 setLayout(new BorderLayout ()); 77 78 if (GuiAC.hasSetNavBar(objectView.context.getCustomizedView() 79 .getCustomizedGUI(), 80 collection)) 81 { 82 JPanel panel = new JPanel (); 83 84 85 String prevStr = 86 (current>0) ? GuiAC.toString(model.getObject(current-1)) : null; 87 String nextStr = 88 (current<(model.getRowCount()-1)) ? 89 GuiAC.toString(model.getObject(current+1)) : 90 null; 91 92 if (prevStr != null) { 93 JButton prevButton = 94 new JButton (" ("+ prevStr +") ", 95 ResourceManager.getIconResource("previous_icon")); 96 prevButton.setToolTipText("Previous Item"); 97 prevButton.setActionCommand("previous"); 98 prevButton.addActionListener(this); 99 prevButton.setHorizontalTextPosition(JButton.LEFT); 100 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 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 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 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 } 273 | Popular Tags |