KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > DefaultView


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but 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 library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: DefaultView.java,v 1.14 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.comp;
21
22 import java.util.*;
23
24 import org.apache.log4j.*;
25 import org.w3c.dom.*;
26
27 import org.enhydra.barracuda.core.util.dom.DOMUtil;
28
29 /**
30  * This interface defines a View for components to
31  * render themselves in. Similar to the java.awt.Graphics
32  * object in AWT and Swing.
33  *
34  * @author Christian Cryder <christianc@granitepeaks.com>
35  * @author Stephen Peterson <stephen_peterson@agilent.com>
36  * @author Jacob Kjome <hoju@visi.com>
37  * @version %I%, %G%
38  * @since 1.0
39  */

40 public class DefaultView implements View {
41
42     protected static final Logger logger = Logger.getLogger(DefaultView.class.getName());
43
44     private final static String JavaDoc DEFAULT = "~DEFAULT~";
45
46     protected Node node = null;
47     private String JavaDoc name = null;
48     protected Map templateNodes = null;
49     protected LocalElementFactory localFactory = null;
50
51     //--------------- Constructors -------------------------------
52
/**
53      * Default constructor to create a view
54      */

55     public DefaultView() {
56         this (null, null);
57     }
58
59     /**
60      * Create a view and bind it to a node
61      *
62      * @param name the name of the view
63      */

64     public DefaultView(String JavaDoc name) {
65         this (name, null);
66     }
67
68     /**
69      * Create a view and bind it to a node
70      *
71      * @param node the node to which the view is bound
72      */

73     public DefaultView(Node node) {
74         this (null, node);
75     }
76
77     /**
78      * Create a view and bind it to a node
79      *
80      * @param name the name of the view
81      * @param node the node to which the view is bound
82      */

83     public DefaultView(String JavaDoc name, Node node) {
84         if (name!=null) setName(name);
85         if (node!=null) setNode(node);
86     }
87
88
89
90     //--------------- DefaultView --------------------------------
91
/**
92      * Bind the view to a specific DOM node
93      *
94      * @param inode the specific DOM node to bind the View to
95      */

96     public void setNode(Node inode) {
97         //set the node
98
if (logger.isDebugEnabled()) logger.debug("Setting node in "+this+" to:"+node);
99         node = inode;
100
101         //serach for templates associated with the node
102
searchForTemplates(node);
103     }
104
105     /**
106      * Get the specific DOM node the View is bound to
107      *
108      * @return the specific DOM node the View is bound to
109      */

110     public Node getNode() {
111         return node;
112     }
113
114     /**
115      * Set the name for this view
116      *
117      * @param iname the name for this view
118      */

119     public void setName(String JavaDoc iname) {
120         name = iname;
121     }
122
123     /**
124      * Get the name for this view
125      *
126      * @return the name for this view
127      */

128     public String JavaDoc getName() {
129         if (name==null) name = "@"+Integer.toHexString(this.hashCode());
130         return name;
131     }
132
133     /**
134      * Get the element factory provided by this view
135      *
136      * @return the element factory provided by this view
137      */

138     public ElementFactory getElementFactory() {
139         if (localFactory==null) localFactory = new LocalElementFactory();
140         return localFactory;
141     }
142
143     /**
144      * Get a String describing the view
145      */

146     public String JavaDoc toString() {
147         return "View:"+getName()+" (bound to Node:"+DOMUtil.getID(node)+")";
148     }
149
150     /**
151      * Given a specific node, find any templates associated with it or its
152      * children.
153      */

154     protected void searchForTemplates(Node curnode) {
155         if (curnode==null) {
156             // Some (incorrect?) setups come here with curnode==null. Make this safe. -srp Agilent Technologies 11/12/2001
157
logger.warn("The current node in which templates are to be searched is null");
158             return;
159         }
160
161         if (logger.isDebugEnabled()) logger.debug("Looking for templates for Node:"+curnode+"...");
162
163         //first allow for any custom searching for templates
164
customSearchForTemplates(curnode);
165
166         //next see if we can find any template nodes
167
if (curnode.hasChildNodes()) {
168             NodeList nl = curnode.getChildNodes();
169
170             //iterate through the children
171
for (int i=0,max=nl.getLength(); i<max; i++) {
172                 Node child = nl.item(i);
173                 if (logger.isDebugEnabled()) logger.debug("Found child:"+child);
174
175                 //if its not an Element skip it
176
if (child instanceof Element) {
177                     //create the template nodes if necessary
178
if (templateNodes==null) templateNodes = new HashMap();
179 // if (templateNodes==null) templateNodes = new TreeMap();
180

181                     //set the default template value (the first one we find)
182
if (templateNodes.get(DEFAULT)==null) {
183                         if (logger.isDebugEnabled()) logger.debug("Setting default in xref!");
184                         templateNodes.put(DEFAULT, child);
185                     }
186
187                     //062501_ba_start - Bob Anderson -
188
//
189
//The effect of this addition is to allow one to do an operation such as
190
// form = (HTMLFormElement) elementFactory.getElement(MyXMLCObject.NAME_MyFormName) ;
191
//where there is a line like the following in your HTML source:
192
// <form name="MyFormName">
193
//The current ElementFactory (as of 6-6-2001) only allows you to get
194
//elements defined by an ID attribute or by tag. The ID attribute is
195
//sometimes too restrictive since only one element in an entire HTML
196
//document may use a particular ID. "name" attributes may be repeated in a
197
//document, and they are very handy for identifying various elements that
198
//you may wish to get at different places in your document. Tags may be
199
//useful sometimes, but are probably repeated too often to be of much
200
//value.
201
//
202
//added
203
//add the child's name (if it exists)
204
String JavaDoc childName = ((Element) child).getAttribute("name") ;
205                     if (childName!=null && !childName.equals("") && !templateNodes.containsKey(childName)) {
206                         if (logger.isDebugEnabled()) logger.debug("Adding child name in xref:"+childName);
207                         templateNodes.put(childName, child);
208                         templateNodes.put(childName.toUpperCase(), child);
209                         templateNodes.put(childName.toLowerCase(), child);
210                     }
211
212                     //This is simply a compromise for performance. My idea here is that
213
//accessing elements by tag name is rarely useful, as tags are so often
214
//repeated. Consider a table element. There may be many many TD tags.
215
//Which one will be returned by the ElementFactory, and how useful will it
216
//be? Also, since we've implemented suggestion #1 (above) this code would
217
//be useless overhead.
218
/*
219 //removed
220                     //add the child's tag name
221                     String tagName = ((Element) child).getTagName();
222                     if (!templateNodes.containsKey(tagName)) {
223                         if (logger.isDebugEnabled()) logger.debug("Adding tag name in xref:"+tagName);
224                         templateNodes.put(tagName.toLowerCase(), child);
225                         templateNodes.put(tagName.toUpperCase(), child);
226                     }
227
228 */

229                     //062501_ba_finish
230

231                     //add the child's id name (if it exists)
232
String JavaDoc idName = ((Element) child).getAttribute("id");
233                     if (idName!=null && !templateNodes.containsKey(idName)) {
234                         if (logger.isDebugEnabled()) logger.debug("Adding id name in xref:"+idName);
235                         templateNodes.put(idName, child);
236                         templateNodes.put(idName.toUpperCase(), child);
237                         templateNodes.put(idName.toLowerCase(), child);
238                     }
239
240                     //add the child's class name
241
String JavaDoc clname = child.getClass().getName();
242                     if (!templateNodes.containsKey(clname)) {
243                         if (logger.isDebugEnabled()) logger.debug("Adding class name in xref:"+clname);
244                         templateNodes.put(clname, child);
245
246                         //add any interfaces the child implements
247
Class JavaDoc[] cl = child.getClass().getInterfaces();
248                         for (int j=0,jmax=cl.length; j<jmax; j++) {
249                             clname = cl[j].getName();
250                             if (!templateNodes.containsKey(clname)) {
251                                 if (logger.isDebugEnabled()) logger.debug("Adding interface name in xref:"+clname);
252                                 templateNodes.put(clname, child);
253                             }
254                         }
255                     }
256                 }
257
258                 //search for templates in the child
259
searchForTemplates(child);
260
261                 if (logger.isDebugEnabled()) logger.debug("Finished check on child!");
262             }
263         }
264     }
265
266     /**
267      * If you want to identify templates based on some custom mechanism,
268      * this is the method to override. Look at DefaultTableView for an
269      * example...
270      */

271     protected void customSearchForTemplates(Node curnode) {
272         //nop
273
}
274
275
276     /**
277      * By default, the text component template factory only returns
278      * a Document; the other methods always return null.
279      */

280     class LocalElementFactory implements ElementFactory {
281
282         public Node getDefaultElement() {
283             if (templateNodes==null) return null;
284             return (Node) templateNodes.get(DEFAULT);
285         }
286
287         public Node getElement(String JavaDoc key) {
288             if (templateNodes==null) return null;
289             return (Node) templateNodes.get(key);
290         }
291
292         public List getElementKeys() {
293             if (templateNodes==null) return null;
294             return new ArrayList(templateNodes.keySet());
295         }
296
297         public Document getDocument() {
298             return node.getOwnerDocument();
299         }
300     }
301
302     //--------------- Cloneable ----------------------------------
303
//csc_092101.1_start
304
/**
305      * When a view is cloned, the underlying node that backs the view is
306      * set to null; you MUST bind the newly cloned view to a node before
307      * you can use it.
308      */

309     public Object JavaDoc clone() {
310         try {
311             DefaultView dv = (DefaultView) super.clone();
312             dv.node = null;
313             return dv;
314        } catch (Exception JavaDoc e) {
315             return null;
316        }
317     }
318     //csc_092101.1_end
319
}
Popular Tags