KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > 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.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 JavaDoc;
26 import java.awt.Insets JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.JLabel JavaDoc;
33 import javax.swing.JPanel JavaDoc;
34
35 public class CollectionItemView extends AbstractView
36     implements ActionListener JavaDoc, AbstractCollectionItemView
37 {
38     Object JavaDoc substance;
39     CollectionItem collection;
40     CollectionView collectionView;
41     CollectionModel model;
42     int current;
43     ObjectView objectView;
44     String JavaDoc viewType;
45     JButton JavaDoc parentButton;
46     View hiddenView;
47     String JavaDoc[] viewParams;
48
49     public CollectionItemView(View view,
50                               CollectionPosition coll,
51                               String JavaDoc viewType, String JavaDoc[] 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 JavaDoc());
77
78         if (GuiAC.hasSetNavBar(objectView.context.getCustomizedView()
79                                .getCustomizedGUI(),
80                                collection))
81         {
82             JPanel JavaDoc panel = new JPanel JavaDoc();
83          
84
85             String JavaDoc prevStr =
86                 (current>0) ? GuiAC.toString(model.getObject(current-1)) : null;
87             String JavaDoc nextStr =
88                 (current<(model.getRowCount()-1)) ?
89                 GuiAC.toString(model.getObject(current+1)) :
90                 null;
91
92             if (prevStr != null) {
93                 JButton JavaDoc prevButton =
94                     new JButton JavaDoc(" ("+ 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                 //prevButton.setMargin(new Insets(1,1,1,1));
101
panel.add(prevButton);
102             }
103          
104             int cur = current + 1;
105             JLabel JavaDoc counter = new JLabel JavaDoc("["+cur+" / "+model.getRowCount()+"]");
106             panel.add(counter);
107
108             if (nextStr != null) {
109                 JButton JavaDoc nextButton =
110                     new JButton JavaDoc(" ("+ 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 JavaDoc(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 JavaDoc removeButton =
131                     new JButton JavaDoc(ResourceManager.getIconResource("remove_icon"));
132                 removeButton.setToolTipText("Remove Item");
133                 removeButton.setActionCommand("remove");
134                 removeButton.addActionListener(this);
135                 removeButton.setMargin(new Insets JavaDoc(1,1,1,1));
136                 panel.add(removeButton);
137             }
138
139             add(panel,BorderLayout.NORTH);
140         }
141
142         add((java.awt.Component JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc curr = null;
193         Iterator JavaDoc 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 JavaDoc e) {
201             e.printStackTrace();
202             current = old;
203             context.getDisplay().refresh();
204             return;
205         }
206
207         Iterator JavaDoc 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 JavaDoc[] {"default",substance},context));
239         }
240         validate();
241     }
242
243     public void actionPerformed(ActionEvent JavaDoc 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 }
273
Popular Tags