2003/09/21 10:26:54
[org.ibex.core.git] / src / org / xwt / Static.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3
4 import org.xwt.js.*;
5 import org.xwt.util.*;
6
7 /** implements objects in the xwt.static.* namespace */
8 public class Static extends JS.Scope {
9
10     public static Static getStatic(String resourcename) {
11         Template t = Template.getTemplate(resourcename, null);
12         if (t != null) t.link();
13         return (Static)cache.get(resourcename);
14     }
15
16     public static Static createStatic(String resourcename, boolean isPackage) {
17         Static ret = (Static)cache.get(resourcename);
18         if (ret != null) return ret;
19         if (resourcename.indexOf('.') != -1)
20             createStatic(resourcename.substring(0, resourcename.lastIndexOf('.')), true);
21         ret = new Static(resourcename, isPackage);
22         return ret;
23     }
24
25     private static Hash cache = new Hash();
26
27     /** the resource name that this Static object corresponds to */
28     private String resourcename = null;
29
30     /** true iff this represents a directory (rather than an actual xwt) */
31     public boolean ispackage = false;
32
33     private Static(String resourcename, boolean ispackage) {
34         super(null);
35         cache.put(resourcename, this);
36         this.resourcename = resourcename;
37         this.ispackage = ispackage;
38         setSeal(ispackage);
39     }
40     
41     /** creates a new static representing a package */
42     public Static(String resourcename) { this(resourcename, true); }
43
44     public Object get(Object name_) {
45         String name = name_.toString();
46         if (!ispackage) return super.get(name);
47         return getStatic(resourcename + (resourcename.length() == 0 ? "" : ".") + name);
48     }
49
50     static { createStatic("", true); }
51
52 }