KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2002-2003 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.aspects.gui.web;
20
21 import org.objectweb.jac.aspects.gui.*;
22 import org.objectweb.jac.core.rtti.FieldItem;
23 import java.io.PrintWriter JavaDoc;
24
25 /**
26  * This class defines a component view for references in
27  * objects.
28  *
29  * <p>By default this view constructs an embedded <code>JLabel</code>
30  * containing the string representation of the referenced object. However,
31  * the field can be attributed to be displayed with a customized
32  * rendering by the GUI aspect component.
33  *
34  * @see GuiAC
35  * @see FieldView
36  */

37
38 public class ReferenceView extends AbstractFieldView
39     implements FieldView, FieldUpdate, ObjectUpdate,
40               HTMLViewer, SelectionListener, LinkGenerator
41 {
42     protected Object JavaDoc object;
43     protected String JavaDoc text;
44     protected String JavaDoc eventURL;
45
46     /**
47      * Constructs a new reference view.
48      *
49      * @param substance the object the viewed field belongs to */

50
51     public ReferenceView(Object JavaDoc value, Object JavaDoc substance, FieldItem reference) {
52         super(substance,reference);
53         this.object = value;
54
55         if (autoUpdate)
56             Utils.registerField(substance,reference,this);
57
58         refreshView();
59     }
60
61     public ReferenceView() {
62     }
63
64     boolean enableLinks = true;
65     public void setEnableLinks(boolean enable) {
66         this.enableLinks = enable;
67     }
68     public boolean areLinksEnabled() {
69         return enableLinks;
70     }
71
72     public void refreshView() {
73         if (autoUpdate)
74             Utils.registerObject(object,this);
75         if (object!=null) {
76             text = GuiAC.toString(object,contexts);
77         } else {
78             text = "";
79         }
80     }
81
82     /**
83      * Set the URL to link to.
84      */

85     public void setEventURL(String JavaDoc eventURL) {
86         this.eventURL = eventURL;
87     }
88
89     // FieldView interface
90

91     public void setValue(Object JavaDoc value) {
92         if (autoUpdate)
93             Utils.unregisterObject(object,this);
94         this.object = value;
95         refreshView();
96     }
97
98     public void close(boolean validate) {
99         super.close(validate);
100         if (autoUpdate)
101             Utils.unregisterObject(object,this);
102     }
103
104     // FieldUpdate interface
105

106     public void fieldUpdated(Object JavaDoc substance, FieldItem field, Object JavaDoc value, Object JavaDoc param) {
107         setValue(value);
108     }
109
110     // ObjectUpdate interface
111

112     public void objectUpdated(Object JavaDoc object, Object JavaDoc param) {
113         refreshView();
114     }
115
116     // HTMLViewer interface
117
public void genHTML(PrintWriter JavaDoc out) {
118         if (enableLinks)
119             out.print("<a HREF=\""+
120                       (eventURL!=null?eventURL:eventURL("onSelection"))+"\">"+text+"</a>");
121         else
122             out.print(text);
123     }
124
125     // SelectionListener interface
126

127     public void onSelection() {
128         EventHandler.get().onSelection(context,field,object,null,null,true);
129     }
130
131 }
132
Popular Tags