KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > AbstractCollection


1 /*
2   Copyright (C) 2001-2003 Renaud Pawlak <renaud@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 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Insets JavaDoc;
23 import java.awt.Point JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import java.awt.event.KeyListener JavaDoc;
28 import java.awt.event.MouseEvent JavaDoc;
29 import java.awt.event.MouseListener JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.swing.DefaultListSelectionModel JavaDoc;
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JComponent JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.JScrollPane JavaDoc;
36 import javax.swing.ListSelectionModel JavaDoc;
37 import javax.swing.event.ListSelectionEvent JavaDoc;
38 import javax.swing.event.ListSelectionListener JavaDoc;
39 import org.objectweb.jac.aspects.gui.*;
40 import org.objectweb.jac.aspects.session.SessionAC;
41 import org.objectweb.jac.core.Collaboration;
42 import org.objectweb.jac.core.Wrappee;
43 import org.objectweb.jac.core.rtti.ClassRepository;
44 import org.objectweb.jac.core.rtti.CollectionItem;
45 import org.objectweb.jac.core.rtti.FieldItem;
46 import org.objectweb.jac.core.rtti.MethodItem;
47 import org.objectweb.jac.core.rtti.NamingConventions;
48 import org.objectweb.jac.util.ExtArrays;
49
50 /**
51  * Base class to implement ListView and TableView
52  */

53 public abstract class AbstractCollection extends AbstractView
54     implements ListSelectionListener JavaDoc, ActionListener JavaDoc,
55               MouseListener JavaDoc, CollectionView, KeyListener JavaDoc
56 {
57     CollectionItem collection;
58     Object JavaDoc substance;
59     CollectionModel model;
60     protected org.objectweb.jac.aspects.gui.CollectionItemView itemView;
61     JComponent JavaDoc component;
62
63     JButton JavaDoc viewButton;
64     JButton JavaDoc removeButton;
65     JButton JavaDoc moveupButton;
66     JButton JavaDoc movedownButton;
67
68     public CollectionModel getCollectionModel() {
69         return model;
70     }
71
72     public AbstractCollection(ViewFactory factory, DisplayContext context,
73                               CollectionItem collection, Object JavaDoc substance,
74                               CollectionModel model,
75                               org.objectweb.jac.aspects.gui.CollectionItemView itemView) {
76         super(factory,context);
77         this.collection = collection;
78         this.substance = substance;
79         this.model = model;
80         this.itemView = itemView;
81
82         setLayout(new BorderLayout JavaDoc());
83
84         component = getInnerComponent(model);
85         component.addKeyListener(this);
86         JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(component);
87         component.addMouseListener(this);
88         add(scrollPane, BorderLayout.CENTER);
89       
90         // add, remove and view buttons
91
JPanel JavaDoc lowerCont = new JPanel JavaDoc();
92         JButton JavaDoc b = null;
93
94         CustomizedGUI customized = null;
95         List targets = null;
96         if (context.getCustomizedView()!=null) {
97             customized = context.getCustomizedView().getCustomizedGUI();
98             targets = customized.getFieldTargets(collection);
99         }
100
101         if (targets==null || targets.size()==0) {
102             viewButton = createButton("view_icon","View","open");
103             viewButton.setEnabled(false);
104             lowerCont.add(viewButton);
105         }
106       
107         if (GuiAC.isAddable(substance,collection)) {
108             b = createButton("new_icon",GuiAC.getLabelAdd(),"add");
109             lowerCont.add(b);
110         }
111       
112         if (GuiAC.isRemovable(substance,collection)) {
113             removeButton = createButton("remove_icon","Remove","remove");
114             removeButton.setEnabled(false);
115             lowerCont.add(removeButton);
116         }
117
118         if (collection.isList() && !collection.isCalculated()) {
119             moveupButton = createButton("up_icon","Move up","moveup");
120             moveupButton.setEnabled(false);
121             lowerCont.add(moveupButton);
122
123             movedownButton = createButton("down_icon","Move up","movedown");
124             movedownButton.setEnabled(false);
125             lowerCont.add(movedownButton);
126         }
127
128         MethodItem[] directMeths = (MethodItem[])
129             collection.getAttribute(GuiAC.DIRECT_COLLECTION_METHODS);
130         if (directMeths != null) {
131             for (int i=0; i<directMeths.length; i++) {
132                 // We should use SwingMethodView for more consistency
133
lowerCont.add(new DirectButton(directMeths[i]));
134             }
135         }
136
137         add(lowerCont,BorderLayout.SOUTH);
138
139         // Utils.registerCollection(substance,collection,this);
140
}
141
142     protected JButton JavaDoc createButton(String JavaDoc icon, String JavaDoc tooltip, String JavaDoc action) {
143         JButton JavaDoc button = new JButton JavaDoc(ResourceManager.getIconResource(icon));
144         button.setToolTipText(tooltip);
145         button.setActionCommand(action);
146         button.addActionListener(this);
147         button.setMargin(new Insets JavaDoc(1,5,1,5));
148         return button;
149     }
150
151     protected abstract JComponent JavaDoc getInnerComponent(Model JavaDoc model);
152
153     boolean isEditor;
154     public boolean isEditor() {
155         return isEditor;
156     }
157     public void setEditor(boolean isEditor) {
158         this.isEditor = isEditor;
159     }
160
161     public void setAutoUpdate(boolean autoUpdate) {
162         // TODO ...
163
}
164
165     public void close(boolean validate) {
166         closed = true;
167         model.close();
168     }
169
170     /**
171      * Defines what happens when the selection changes.
172      */

173
174     public void valueChanged(ListSelectionEvent JavaDoc event) {
175         loggerEvents.debug("valueChanged "+event);
176
177         ListSelectionModel JavaDoc lsm = (ListSelectionModel JavaDoc)event.getSource();
178         if (lsm.isSelectionEmpty()) {
179             if (viewButton!=null)
180                 viewButton.setEnabled(false);
181             if (removeButton!=null)
182                 removeButton.setEnabled(false);
183             if (moveupButton!=null)
184                 moveupButton.setEnabled(false);
185             if (movedownButton!=null)
186                 movedownButton.setEnabled(false);
187             return;
188         } else if (event.getValueIsAdjusting()) {
189             return;
190         } else {
191             if (removeButton!=null)
192                 removeButton.setEnabled(true);
193             if (viewButton!=null)
194                 viewButton.setEnabled(selectionContainsAWrappee());
195             int indice = getSelectedIndices()[0];
196             CollectionPosition collpos = new CollectionPosition(
197                 this,
198                 collection,
199                 indice,
200                 substance);
201             EventHandler.get().onSelection(context,
202                                            collection,
203                                            getSelected()[0],
204                                            null,
205                                            collpos);
206             //null);
207

208             if (moveupButton!=null)
209                 moveupButton.setEnabled(indice>0);
210             if (movedownButton!=null)
211                 movedownButton.setEnabled(indice<model.getRowCount()-1);
212         }
213     }
214
215     /**
216      * Tells wether the selection contains at least on Wrappee object
217      */

218     boolean selectionContainsAWrappee() {
219         Object JavaDoc[] selection = getSelected();
220         for (int i=0; i<selection.length; i++) {
221             if (selection[i] instanceof Wrappee) {
222                 return true;
223             }
224         }
225         return false;
226     }
227
228     // KeyListener interface
229

230     public void keyTyped(KeyEvent JavaDoc event) {}
231
232     public void keyPressed(KeyEvent JavaDoc event) {}
233
234     public void keyReleased(KeyEvent JavaDoc event) {
235         if (event.getKeyCode()==KeyEvent.VK_DELETE &&
236             removeButton!=null
237             /* GuiAC.isRemovable(collection) // this does not work
238                because the session's context is not initialized */
) {
239             doDelete();
240         }
241     }
242
243     void doDelete() {
244         Object JavaDoc[] selected = getSelected();
245         MethodItem removeMethod = collection.getRemover();
246         loggerEvents.debug("remover="+removeMethod);
247         if (removeMethod!=null) {
248             // try with the removing method
249
for (int i=0; i<selected.length; i++) {
250                 EventHandler.get().onRemoveFromCollection(
251                     context,
252                     new RemoveEvent(
253                         this,
254                         substance,
255                         collection,
256                         selected[i]),
257                     false);
258             }
259         } else {
260             // call the collection's method directly
261
/*
262               getMethod = collection.getGetter();
263               if (getMethod!=null) {
264               MethodItem method = null;
265               Object c;
266               try {
267               c = getMethod.invoke(substance,ExtArrays.emptyObjectArray);
268               } catch (Exception e) {
269               Log.error("Getting collection with "+getMethod.getName()+
270               " failed : "+e);
271               return;
272               }
273               ClassItem cl = ClassRepository.get().getClass(c.getClass());
274               if (!collection.isMap()) {
275               method = cl.getMethod("remove(java.lang.Object)");
276               Log.trace("gui","Invoking "+method.getName()+" on collection itself");
277               for (int i=0; i<selected.length; i++) {
278               try {
279               method.invoke(c,new Object[] {selected[i]});
280               } catch (Exception e) {
281               Log.error("Removing from collection with "+method.getName()+
282               " failed : "+e);
283               }
284               }
285               }
286             */

287         }
288         onRemove();
289     }
290
291     /**
292      * Handles the actions on this view.
293      *
294      * <p>On a collection view, the three default possible actions are
295      * to open a new view on the selected item, to add a new item to
296      * the collection, and to remove the selected item from the
297      * collection.
298      *
299      * @param event the user event
300      */

301     public void actionPerformed(ActionEvent JavaDoc event) {
302         setContext();
303         loggerEvents.debug("action performed on collection : "+event.getActionCommand()+
304                   "; modifiers = "+event.getModifiers());
305         if (event.getActionCommand().equals("open")) {
306             Object JavaDoc[] selected = getSelected();
307             for (int i=0; i<selected.length; i++) {
308                 try {
309                     EventHandler.get().onSelection(
310                         context,collection,selected[i],null,null,true);
311                 } catch (Exception JavaDoc e) {
312                     loggerEvents.error("failed to view "+selected[i],e);
313                 }
314             }
315         } else if (event.getActionCommand().equals("add")) {
316             System.out.println(event. getModifiers()+"&"+ActionEvent.CTRL_MASK+"="+
317                                (event. getModifiers()& ActionEvent.CTRL_MASK));
318             EventHandler.get().onAddToCollection(
319                 context,
320                 new AddEvent(this,substance,collection,null),
321                 (event. getModifiers()& ActionEvent.CTRL_MASK) != 0);
322         } else if (event.getActionCommand().equals("remove")) {
323             doDelete();
324         } else if (event.getActionCommand().equals("movedown")) {
325             Object JavaDoc selected = getSelected()[0];
326             int index = getSelectedIndices()[0];
327             if (index<model.getRowCount()-1) {
328                 Integer JavaDoc i = new Integer JavaDoc(index);
329                 collection.remove(substance,selected,i);
330                 CollectionUpdate update = getCollectionUpdate();
331                 update.onRemove(substance, collection, null, i , null);
332                 i = new Integer JavaDoc(index+1);
333                 collection.add(substance,selected,i);
334                 update.onAdd(substance, collection, null, i, null);
335                 setSelected(index+1);
336             }
337         } else if (event.getActionCommand().equals("moveup")) {
338             Object JavaDoc selected = getSelected()[0];
339             int index = getSelectedIndices()[0];
340             if (index>0) {
341                 Integer JavaDoc i = new Integer JavaDoc(index);
342                 collection.remove(substance,selected,i);
343                 CollectionUpdate update = getCollectionUpdate();
344                 update.onRemove(substance, collection, null, i, null);
345                 i = new Integer JavaDoc(index-1);
346                 collection.add(substance,selected,i);
347                 update.onAdd(substance, collection, null, i, null);
348                 setSelected(index-1);
349             }
350         }
351     }
352
353     // button for direct method
354
class DirectButton extends JButton JavaDoc
355         implements ListSelectionListener JavaDoc, ActionListener JavaDoc
356     {
357         MethodItem method;
358         DirectButton(MethodItem method) {
359             super(GuiAC.getLabel(method));
360             this.method = method;
361             setIcon(ResourceManager.getIcon(GuiAC.getIcon(method)));
362             addActionListener(this);
363             setEnabled(false);
364             getSelectionModel().addListSelectionListener(this);
365         }
366         public void valueChanged(ListSelectionEvent JavaDoc event) {
367             ListSelectionModel JavaDoc lsm = (ListSelectionModel JavaDoc)event.getSource();
368             setEnabled(!lsm.isSelectionEmpty());
369         }
370         public void actionPerformed(ActionEvent JavaDoc event) {
371             Collaboration.get().addAttribute(
372                 SessionAC.SESSION_ID, GuiAC.getLocalSessionID());
373
374             Object JavaDoc[] selected = getSelected();
375             for (int i=0; i<selected.length; i++) {
376                 try {
377                     EventHandler.get().onInvoke(
378                         context,
379                         new InvokeEvent(AbstractCollection.this,selected[i],method)).join();
380                 } catch(InterruptedException JavaDoc e) {
381                     context.getDisplay().showModal(
382                         e,
383                         "Error: "+e,
384                         "An error occured during the invocation of "+
385                             method.getCompactFullName()+
386                             " on "+selected[i].getClass().getName()+
387                             " \""+GuiAC.toString(selected[i])+"\"",
388                         context.getWindow(),
389                         false,false,true);
390                 }
391             }
392         }
393     }
394
395     // should call clear selection on the component's selection model
396
protected abstract void onRemove();
397
398     protected abstract CollectionUpdate getCollectionUpdate();
399
400     /**
401      * Returns an array of the selected objects. The array is empty if
402      * no object is selected, but not null.
403      **/

404
405     protected Object JavaDoc[] getSelected() {
406         Object JavaDoc[] selected = null;
407         int[] indices = getSelectedIndices();
408         if (indices != null) {
409             selected = new Object JavaDoc[indices.length];
410             for (int i=0; i<indices.length; i++)
411             {
412                 selected[i]= model.getObject(indices[i]);
413             }
414         } else {
415             selected = ExtArrays.emptyObjectArray;
416         }
417         return selected;
418     }
419
420     /**
421      * Returns the indices of selected objects.
422      */

423     protected abstract int[] getSelectedIndices();
424
425     protected abstract ListSelectionModel JavaDoc getSelectionModel();
426
427     protected void setNoRefresh(boolean norefresh) {
428         if (norefresh==false) {
429             repaint();
430         }
431     }
432
433     static class BetterListSelectionModel extends DefaultListSelectionModel JavaDoc {
434         JComponent JavaDoc list = null;
435         public BetterListSelectionModel( JComponent JavaDoc list ) {
436             super();
437             this.list = list;
438         }
439         public JComponent JavaDoc getList() {
440             return list;
441         }
442     }
443
444     // MouseListener interface
445

446     public void mouseClicked(MouseEvent JavaDoc me){
447         loggerEvents.debug("mouseClicked");
448         if (model.getRowCount()==1) {
449             loggerEvents.debug("rowCount==1 => forceRefresh");
450             if (getSelectedIndices().length>0) {
451                 CollectionPosition collpos = new CollectionPosition(
452                     this,
453                     collection,
454                     getSelectedIndices()[0],
455                     substance);
456                 EventHandler.get().onSelection(context,
457                                                collection,
458                                                getSelected()[0],
459                                                null,
460                                                collpos);
461             }
462         }
463     }
464
465     public void mousePressed(MouseEvent JavaDoc e) {
466         maybeShowPopup(e);
467     }
468     public void mouseReleased(MouseEvent JavaDoc e) {
469         maybeShowPopup(e);
470     }
471     public void mouseExited(MouseEvent JavaDoc me){}
472     public void mouseEntered(MouseEvent JavaDoc me){}
473     void maybeShowPopup(MouseEvent JavaDoc event) {
474         loggerEvents.debug("maybeShowPopup");
475         if (event.isPopupTrigger()) {
476             int index = locationToIndex(event.getPoint());
477             if (index!=-1) {
478                 Object JavaDoc o = model.getObject(index);
479                 if (o instanceof Wrappee)
480                     SwingEvents.showObjectMenu(context, o, event);
481             }
482         }
483     }
484     /**
485      * Returns the index of the element at the given location, or -1 if
486      * if no element is under this location . */

487     abstract int locationToIndex(Point JavaDoc location);
488
489     public void setField(FieldItem field) {
490         collection = (CollectionItem)field;
491     }
492
493     public void setSubstance(Object JavaDoc substance) {
494         this.substance = substance;
495     }
496
497     public Object JavaDoc getSubstance() {
498         return substance;
499     }
500
501     public FieldItem getField() {
502         return collection;
503     }
504
505     public void setValue(Object JavaDoc value) {
506     }
507
508     public void updateModel(Object JavaDoc substance) {
509     }
510
511 }
512
Popular Tags