001    /*
002      Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>, 
003                              Laurent Martelli <laurent@aopsys.com>
004      
005      This program is free software; you can redistribute it and/or modify
006      it under the terms of the GNU Lesser General Public License as
007      published by the Free Software Foundation; either version 2 of the
008      License, or (at your option) any later version.
009    
010      This program is distributed in the hope that it will be useful, but
011      WITHOUT ANY WARRANTY; without even the implied warranty of
012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013      Lesser General Public License for more details.
014    
015      You should have received a copy of the GNU Lesser General Public
016      License along with this program; if not, write to the Free Software
017      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
018      USA */
019    
020    package org.objectweb.jac.aspects.gui.swing;
021    
022    import org.objectweb.jac.aspects.gui.*;
023    import org.objectweb.jac.core.rtti.FieldItem;
024    import org.objectweb.jac.util.Stack;
025    import java.awt.Dimension;
026    import java.awt.Insets;
027    import java.awt.event.ActionEvent;
028    import java.awt.event.ActionListener;
029    import javax.swing.Box;
030    import javax.swing.JButton;
031    import javax.swing.JLabel;
032    
033    /**
034     * This class defines a Swing component view for references in
035     * objects.
036     *
037     * <p>By default this view constructs an embedded <code>JLabel</code>
038     * containing the string representation of the referenced object. However,
039     * the field can be attributed to be displayed with a customized
040     * rendering by the GUI aspect component.
041     */
042    
043    public class ReferenceView extends AbstractFieldView 
044        implements FieldView, FieldUpdate, ObjectUpdate, ActionListener {
045    
046        Object object;
047        JButton viewButton;
048        JLabel label = new JLabel();
049    
050        /**
051         * Constructs a new reference view.
052         *
053         * @param substance the object the viewed field belongs to */
054    
055        public ReferenceView(Object object, Object substance, FieldItem reference) {
056            super(substance,reference);
057            this.object = object;
058    
059            add(label);
060            add(Box.createRigidArea(new Dimension(20,1)));
061    
062            JButton b;
063            viewButton = new JButton (ResourceManager.getIconResource("view_icon"));
064            viewButton.setEnabled(false);
065            viewButton.setToolTipText("View");
066            viewButton.setActionCommand("open");
067            viewButton.addActionListener(this);
068            viewButton.setMargin(new Insets(1,1,1,1));
069            add(viewButton);
070    
071            if (GuiAC.getGraphicContext()!=null)
072                contexts.addAll(GuiAC.getGraphicContext());
073            if (field!=null)
074                contexts.push(field);
075            refreshView();
076        }
077    
078        Stack contexts = new Stack();
079    
080        public ReferenceView() {
081            label.setFont(null);
082            add(label);
083        }
084    
085        /**
086         * Handles the actions on this view.
087         *
088         * <p>On a reference view, the two default possible actions are to
089         * open a new view on the referenced object, or to edit the
090         * reference value.
091         *
092         * @param evt the user event */
093    
094        public void actionPerformed(ActionEvent evt) {
095            if (evt.getActionCommand().equals("open")) {
096                if (object!=null) {
097                    EventHandler.get().onSelection(context,field,object,null,null,true);
098                }
099            } 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 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 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 substance, FieldItem field, 
135                                 Object value, Object param) {
136            setValue(value);
137        }
138    
139        // ObjectUpdate interface
140    
141        public void objectUpdated(Object object, Object param) {
142            refreshView();
143        }
144    }