KickJava   Java API By Example, From Geeks To Geeks.

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


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, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18   USA */

19
20 package org.objectweb.jac.aspects.gui.swing;
21
22 import java.awt.Dimension JavaDoc;
23 import java.awt.Insets JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.awt.event.ItemEvent JavaDoc;
27 import java.awt.event.ItemListener JavaDoc;
28 import javax.swing.JButton JavaDoc;
29 import javax.swing.JComboBox JavaDoc;
30 import org.apache.log4j.Logger;
31 import org.objectweb.jac.aspects.gui.*;
32 import org.objectweb.jac.core.Collaboration;
33 import org.objectweb.jac.core.Wrappee;
34 import org.objectweb.jac.core.rtti.FieldItem;
35
36 /**
37  * This is a special value editor that allows the user to choose a
38  * value within a set of object of a given type. */

39
40 public class ObjectChooser extends AbstractFieldEditor
41     implements ActionListener JavaDoc, ItemListener JavaDoc, ReferenceEditor
42 {
43     static Logger logger = Logger.getLogger("gui.chooser");
44
45     JComboBox JavaDoc choice;
46     ComboBoxModel model;
47     JButton JavaDoc viewButton;
48     JButton JavaDoc newButton;
49
50     /**
51      * Constructs a new object chooser.
52      */

53
54     public ObjectChooser(Object JavaDoc substance, FieldItem reference,
55                          ComboBoxModel model, boolean isEditable)
56     {
57         super(substance,reference);
58
59         this.model = model;
60         this.isEditable = isEditable;
61         choice = new JComboBox JavaDoc(model);
62
63         boolean isWrappee = model.getType()!=null &&
64             Wrappee.class.isAssignableFrom(model.getType().getActualClass());
65         choice.setEditable(isEditable && !isWrappee);
66
67         // This is a very dirty hack to get the real data component since
68
// JComboBox does not fire the focus events (certainly a bug in
69
// JDK 1.3
70

71         /*
72           if(isEditable) {
73           choice.getComponent(2).addFocusListener(this);
74           choice.addFocusListener(this);
75           } else {
76         */

77       
78         choice.getComponent(0).addFocusListener(this);
79         choice.addFocusListener(this);
80       
81         /*
82           }
83         */

84
85         add(choice);
86         //choice.addActionListener(this);
87
choice.addItemListener(this);
88       
89         // Fill stupid height
90
Dimension JavaDoc minSize = choice.getPreferredSize();
91         Dimension JavaDoc maxSize = choice.getMaximumSize();
92         choice.setMaximumSize(new Dimension JavaDoc(maxSize.width,minSize.height));
93
94         Boolean JavaDoc small_view = (Boolean JavaDoc) Collaboration.get().
95             getAttribute(GuiAC.SMALL_VIEW);
96
97         if ((small_view == null) || (!small_view.booleanValue())) {
98             if (isWrappee) {
99                 viewButton = new JButton JavaDoc (ResourceManager.getIconResource("view_icon"));
100                 viewButton.setToolTipText("View");
101                 viewButton.setActionCommand("open");
102                 viewButton.addActionListener(this);
103                 viewButton.setMargin(new Insets JavaDoc(1,1,1,1));
104                 add(viewButton);
105             }
106
107             if (isWrappee && isEditable && model.getType()!=null &&
108                 GuiAC.isCreatable(model.getType())) {
109                 newButton = new JButton JavaDoc(ResourceManager.getIconResource("new_icon"));
110                 newButton.setToolTipText("New");
111                 newButton.setActionCommand("new");
112                 newButton.addActionListener(this);
113                 newButton.setMargin(new Insets JavaDoc(1,1,1,1));
114                 add(newButton);
115             }
116         }
117     }
118
119     public void setFocus(FieldItem field, Object JavaDoc extraOption) {}
120
121
122     // FieldEditor interface
123

124     public void setValue(Object JavaDoc value) {
125         super.setValue(value);
126         model.setSelectedObject(value);
127       
128         if (value==null && viewButton!=null)
129             viewButton.setEnabled(false);
130     }
131
132     public Object JavaDoc getValue() {
133         logger.debug("selectedItem = "+model.getSelectedObject());
134         return model.getSelectedObject();
135     }
136
137     public void close(boolean validate) {
138         super.close(validate);
139         model.close();
140     }
141
142     /**
143      * Handles the actions performed by the users on this view.
144      *
145      * <p>On an object chooser, a "new" action can be performed to
146      * allow the user to add a new object to the choices it not present
147      * yet.
148      *
149      * @param event the performed action
150      */

151     public void actionPerformed(ActionEvent JavaDoc event) {
152         loggerEvents.debug("actionPerformed: "+event.getActionCommand());
153         setContext();
154         if (event.getActionCommand().equals("new")) {
155             Object JavaDoc instance =
156                 EventHandler.get().onCreateObject(
157                     context,model.getType(),substance,field);
158             if (instance!=null) {
159                 requestFocus();
160                 model.addObject(instance);
161                 model.setSelectedObject(instance);
162             }
163         } else if (event.getActionCommand().equals("open")) {
164             Object JavaDoc object = model.getSelectedObject();
165             if (object == null)
166                 return;
167             if (object!=null) {
168                 EventHandler.get().onView(context,field,object,null,null);
169             }
170         }
171     }
172
173     /**
174      * Set the focus on the JComboBox
175      */

176     public void requestFocus() {
177         choice.requestFocus();
178         loggerFocus.debug("focusing "+choice.getClass().getName());
179     }
180
181     boolean isEditable = false;
182     public boolean isEditable() {
183         return isEditable;
184     }
185     public void setEditable(boolean editable) {
186         this.isEditable = editable;
187     }
188
189     public ComboBoxModel getModel() {
190         return model;
191     }
192    
193     // ItemListener interface
194

195     public void itemStateChanged(ItemEvent JavaDoc event) {
196         loggerEvents.debug("itemStateChanged on "+this);
197         if (field!=null && isEmbedded) {
198             invokeInContext(this,"commit", new Object JavaDoc[]{});
199         } else {
200             loggerEvents.debug("ignoring item event");
201         }
202     }
203 }
204
Popular Tags