001    /*
002      Copyright (C) 2002-2003 Renaud Pawlak <renaud@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017    */
018    
019    package org.objectweb.jac.aspects.gui;
020     
021    import java.applet.Applet;
022    import java.applet.AppletContext;
023    import java.applet.AppletStub;
024    import java.applet.AudioClip;
025    import java.awt.Image;
026    import java.io.IOException;
027    import java.io.InputStream;
028    import java.net.URL;
029    import java.util.Arrays;
030    import java.util.Enumeration;
031    import java.util.Iterator;
032    import java.util.Vector;
033    import javax.swing.JApplet;
034    import org.objectweb.jac.aspects.gui.Border;
035    import org.objectweb.jac.aspects.gui.View;
036    import org.objectweb.jac.core.rtti.FieldItem;
037    import org.objectweb.jac.core.rtti.MethodItem;
038    
039    /**
040     * This class allows the programmer to insert specialized object views
041     * defined as applets.
042     *
043     * <p>This is useful when views can be used by an Internet browser at
044     * client-side.
045     *
046     * <p>The programmer must subclass this class and overload the
047     * <code>init</code> method.
048     *
049     * @see #init() */
050    
051    public class AppletView extends JApplet 
052        implements View/*, ComponentListener  */
053    {
054        protected String label;
055        protected DisplayContext context;
056        protected ViewFactory factory;
057        protected Object[] parameters;
058        protected String type;
059        protected Object substance;
060    
061        /**
062         * The AppletView's constructor.
063         * 
064         * @param factory
065         * @param context
066         * @param substance the object that is represented by this view */
067    
068        public AppletView(ViewFactory factory, DisplayContext context, Object substance) {
069            this.substance = substance;
070            this.factory = factory;
071            this.context = context;
072            init();
073            start();
074        }
075    
076        Border viewBorder;
077       
078        /**
079         * Get the value of viewBorder.
080         * @return value of viewBorder.
081         */
082        public Border getViewBorder() {
083            return viewBorder;
084        }
085       
086        /**
087         * Set the value of viewBorder.
088         * @param v  Value to assign to viewBorder.
089         */
090        public void setViewBorder(Border  v) {
091            this.viewBorder = v;
092        }
093       
094        View parentView;
095       
096        /**
097         * Get the value of parent.
098         * @return value of parent.
099         */
100        public View getParentView() {
101            return parentView;
102        }
103       
104        /**
105         * Set the value of parent.
106         * @param v  Value to assign to parent.
107         */
108        public void setParentView(View  v) {
109            this.parentView = v;
110        }
111    
112        public View getRootView() {
113            if (parentView==null)
114                return this;
115            return parentView.getRootView();      
116        }
117    
118        public boolean isDescendantOf(View ancestor) {
119            if (this==ancestor)
120                return true;
121            else if (parentView==null)
122                return false;
123            else
124                return parentView.isDescendantOf(ancestor);
125        }
126    
127        // style used to change display (css for web)
128        String style;
129    
130        public void setStyle(String style) {
131            this.style = style;
132        }
133    
134        public String getStyle() {
135            return style;
136        }
137    
138    
139        // View interface
140    
141        Border border;
142       
143        /**
144         * Get the value of border.
145         * @return value of border.
146         */
147        public Border getBorder() {
148            return border;
149        }
150       
151        /**
152         * Set the value of border.
153         * @param v  Value to assign to border.
154         */
155        public void setBorder(Border  v) {
156            this.border = v;
157        }
158       
159        String description;
160       
161        /**
162         * Get the value of description.
163         * @return value of description.
164         */
165        public String getDescription() {
166            return description;
167        }
168       
169        /**
170         * Set the value of description.
171         * @param v  Value to assign to description.
172         */
173        public void setDescription(String  v) {
174            this.description = v;
175        }
176       
177    
178        public void setLabel(String label) {
179            this.label = label;
180        }
181       
182        public String getLabel() {
183            return label;
184        }
185    
186        MethodItem message;
187       
188        /**
189         * Get the value of message.
190         * @return value of message.
191         */
192        public MethodItem getMessage() {
193            return message;
194        }
195       
196        /**
197         * Set the value of message.
198         * @param v  Value to assign to message.
199         */
200        public void setMessage(MethodItem  v) {
201            this.message = v;
202        }
203    
204        public void setContext(DisplayContext context) {
205            this.context = context;
206        }
207    
208        public DisplayContext getContext() {
209            return context;
210        }
211    
212        public void setFactory(ViewFactory factory) {
213            this.factory = factory;
214        }
215    
216        public ViewFactory getFactory() {
217            return factory;
218        }
219    
220        public void setWidth(int width) {
221        }
222    
223        public void setHeight(int heigth) {
224        }
225    
226        public void setParameters(Object[] parameters) {
227            this.parameters = parameters;
228        }
229    
230        public Object[] getParameters() {
231            return parameters;
232        }
233    
234        public void setType(String type) {
235            this.type = type;
236        }
237    
238        public String getType() {
239            return type;
240        }
241    
242        public boolean equalsView(ViewIdentity view) {
243            return 
244                ( ( type!=null && 
245                    type.equals(view.getType()) )
246                  || (type==null && view.getType()==null ) )
247                && ( ( parameters!=null && 
248                       Arrays.equals(parameters,view.getParameters()) ) 
249                     || (parameters==null && view.getParameters()==null) );
250        }
251    
252        public boolean equalsView(String type, Object[] parameters) {
253            return this.type.equals(type)
254                && Arrays.equals(this.parameters,parameters);
255        }
256    
257        public void close(boolean validate) {
258            closed = true;
259        }
260    
261        boolean closed = false;
262    
263        public boolean isClosed() {
264            return closed;
265        }
266       
267        public Object getSubstance() {
268            return substance;
269        }
270       
271        public void setSubstance(Object substance) {
272            this.substance = substance;
273        }
274    
275        public void setFocus(FieldItem field, Object option) {
276        }
277    
278        /**
279         * Initializes the applet view.
280         *
281         * <p>The programmer must overload this class and start the init
282         * method code with <code>super.init()</code> so that the applet's
283         * container initialization is done. */
284    
285        public void init() {
286            setStub(new InternalStub(new InternalContext(),this));
287        }
288    
289        // The following classes simulate an applet container for the JAC
290        // Swing GUI.
291    
292        static class InternalStub implements AppletStub {
293            InternalContext context;
294            public InternalStub(InternalContext context,Applet applet) {
295                this.context = context;
296                context.setApplet(applet);
297            }
298            public void appletResize(int width, int height) {
299                //Log.trace("gui.applet","resizing applet");
300                //applet.setSize(width,height);
301            }
302            public AppletContext getAppletContext() {
303                return context;
304            }
305            public URL getCodeBase() {
306                URL ret = null;
307                try {
308                    ret = new URL("file:/");
309                } catch(Exception e) {
310                    e.printStackTrace();
311                }
312                return ret;
313            }
314            public URL getDocumentBase() {
315                URL ret = null;
316                try {
317                    ret = new URL("file:/");
318                } catch(Exception e) {
319                    e.printStackTrace();
320                }
321                return ret;
322            }
323            public String getParameter(String name) {
324                return null;
325            }
326            public boolean isActive() {
327                return true;
328            }
329        }
330    
331        static class InternalContext implements AppletContext {
332            public void setApplet(Applet a) {
333                v.add(a);
334            }
335            Vector v=new Vector();
336            public Applet getApplet(String name) {
337                return (Applet)v.get(0);
338            }
339            public Enumeration getApplets() {
340                return v.elements();
341            }
342            public AudioClip getAudioClip(URL url) {
343                return null;
344            }
345            public Image getImage(URL url) {
346                return null;
347            }
348            public void showDocument(URL url) {}
349            public void showDocument(URL url, String target) {}
350            public void showStatus(String status) {}
351            // since JDK 1.4
352            Vector streamKeys=new Vector();
353            public void setStream(String key,
354                                  InputStream stream)
355                throws IOException {}
356            public InputStream getStream(String key) {
357                return null;
358            }
359            public Iterator getStreamKeys() {
360                return streamKeys.iterator(); 
361            }
362        }
363    
364    }