KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.objectweb.jac.aspects.gui.*;
23 import org.objectweb.jac.core.rtti.FieldItem;
24 import org.objectweb.jac.util.Stack;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Insets JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import javax.swing.Box JavaDoc;
30 import javax.swing.JButton JavaDoc;
31 import javax.swing.JLabel JavaDoc;
32
33 /**
34  * This class defines a Swing component view for references in
35  * objects.
36  *
37  * <p>By default this view constructs an embedded <code>JLabel</code>
38  * containing the string representation of the referenced object. However,
39  * the field can be attributed to be displayed with a customized
40  * rendering by the GUI aspect component.
41  */

42
43 public class ReferenceView extends AbstractFieldView
44     implements FieldView, FieldUpdate, ObjectUpdate, ActionListener JavaDoc {
45
46     Object JavaDoc object;
47     JButton JavaDoc viewButton;
48     JLabel JavaDoc label = new JLabel JavaDoc();
49
50     /**
51      * Constructs a new reference view.
52      *
53      * @param substance the object the viewed field belongs to */

54
55     public ReferenceView(Object JavaDoc object, Object JavaDoc substance, FieldItem reference) {
56         super(substance,reference);
57         this.object = object;
58
59         add(label);
60         add(Box.createRigidArea(new Dimension JavaDoc(20,1)));
61
62         JButton JavaDoc b;
63         viewButton = new JButton JavaDoc (ResourceManager.getIconResource("view_icon"));
64         viewButton.setEnabled(false);
65         viewButton.setToolTipText("View");
66         viewButton.setActionCommand("open");
67         viewButton.addActionListener(this);
68         viewButton.setMargin(new Insets JavaDoc(1,1,1,1));
69         add(viewButton);
70
71         if (GuiAC.getGraphicContext()!=null)
72             contexts.addAll(GuiAC.getGraphicContext());
73         if (field!=null)
74             contexts.push(field);
75         refreshView();
76     }
77
78     Stack contexts = new Stack();
79
80     public ReferenceView() {
81         label.setFont(null);
82         add(label);
83     }
84
85     /**
86      * Handles the actions on this view.
87      *
88      * <p>On a reference view, the two default possible actions are to
89      * open a new view on the referenced object, or to edit the
90      * reference value.
91      *
92      * @param evt the user event */

93
94     public void actionPerformed(ActionEvent JavaDoc evt) {
95         if (evt.getActionCommand().equals("open")) {
96             if (object!=null) {
97                 EventHandler.get().onSelection(context,field,object,null,null,true);
98             }
99         } else if (evt.getActionCommand().equals("edit")) {
100             // GuiAC.invoke((Display)parent,setter,substance,null);
101
}
102     }
103
104     public void refreshView() {
105         Utils.registerObject(object,this);
106         String JavaDoc name;
107         if (object!=null) {
108             if (viewButton!=null)
109                 viewButton.setEnabled(true);
110             name = GuiAC.toString(object,contexts);
111         } else {
112             if (viewButton!=null)
113                 viewButton.setEnabled(false);
114             name = "";
115         }
116         label.setText(name);
117     }
118
119     // FieldView interface
120

121     public void setValue(Object JavaDoc value) {
122         Utils.unregisterObject(object,this);
123         this.object = value;
124         refreshView();
125     }
126
127     public void close(boolean validate) {
128         Utils.unregisterObject(object,this);
129         Utils.unregisterField(substance,field,this);
130     }
131
132     // FieldUpdate
133

134     public void fieldUpdated(Object JavaDoc substance, FieldItem field,
135                              Object JavaDoc value, Object JavaDoc param) {
136         setValue(value);
137     }
138
139     // ObjectUpdate interface
140

141     public void objectUpdated(Object JavaDoc object, Object JavaDoc param) {
142         refreshView();
143     }
144 }
145
Popular Tags