Statistics
| Revision:

root / trunk / extensions / extScripting / src / org / gvsig / scripting / xul / XULJPanel.java @ 24994

History | View | Annotate | Download (5.16 KB)

1
package org.gvsig.scripting.xul;
2

    
3
import java.io.IOException;
4

    
5
import javax.swing.JPanel;
6
import javax.swing.event.AncestorEvent;
7
import javax.swing.event.AncestorListener;
8

    
9
import com.iver.andami.PluginServices;
10
import com.iver.andami.ui.mdiManager.IWindow;
11
import com.iver.andami.ui.mdiManager.MDIManager;
12
import com.iver.andami.ui.mdiManager.WindowInfo;
13
import com.iver.utiles.IPersistence;
14
import com.iver.utiles.XMLEntity;
15
import com.iver.utiles.extensionPoints.ExtensionBuilder;
16

    
17
public class XULJPanel extends JPanel implements IWindow, IPersistence, IXULPanel {
18

    
19
        private static final long serialVersionUID = -8512371289780040873L;
20

    
21
        private WindowInfo windowInfo = null;
22

    
23
        private XULPanel thinlet = null;
24

    
25

    
26
        public XULJPanel() {
27
                this.addAncestorListener(new XULJPanelAncestorListener(this));
28
        }
29

    
30
        public XULJPanel(XULPanel thinlet) {
31
                this.thinlet = thinlet;
32
                this.addAncestorListener(new XULJPanelAncestorListener(this));
33
        }
34

    
35
        protected XULPanel getThinlet() {
36
                if (thinlet == null) {
37
                        thinlet = new XULPanel(this);
38
                }
39
                return thinlet;
40
        }
41

    
42
        //==============================================================
43
        // Implementacion del interface IWindow
44
        //
45
        public WindowInfo getWindowInfo() {
46

    
47
                if (windowInfo == null) {
48
                        windowInfo = new WindowInfo(WindowInfo.RESIZABLE| WindowInfo.MODELESSDIALOG);
49
                        windowInfo.setWidth(100);
50
                        windowInfo.setHeight(100);
51
                }
52

    
53
                int width = getThinlet().getPreferredSize().width;
54
                int height = getThinlet().getPreferredSize().height;
55
                if (height > 0 && width > 0) {
56
                        if (width != windowInfo.getWidth()) {
57
                                windowInfo.setWidth(width);
58
                        }
59
                        if (height != windowInfo.getHeight()) {
60
                                this.windowInfo.setHeight(height);
61
                        }
62
                }
63
                return this.windowInfo;
64
        }
65

    
66
        //==============================================================
67
        // Implementacion del interface IPersistance
68
        //
69
        public XMLEntity getXMLEntity() {
70
                XMLEntity xml = new XMLEntity();
71
                xml.putProperty("className", getClass().getName());
72
                xml.putProperty("xulFile", getThinlet().getXULFile());
73
                xml.putProperty(
74
                        "classNameOfThinlet",
75
                        getEngine().getClass().getName()
76
                );
77
                return xml;
78
        }
79

    
80
        public void setXMLEntity(XMLEntity xml) {
81
                try {
82
                        String classNameOfThinlet = xml.getStringProperty("classNameOfThinlet");
83
                        if (classNameOfThinlet != null) {
84
                                Object[] args = { this };
85
                                thinlet = (XULPanel) ExtensionBuilder.create(
86
                                        Class.forName(classNameOfThinlet),
87
                                        args
88
                                );
89
                        }
90
                        setXULFile(xml.getStringProperty("xulFile"));
91
                } catch (Exception e) {
92
                        // TODO Auto-generated catch block
93
                        e.printStackTrace();
94
                }
95
        }
96

    
97
        public String getClassName() {
98
                return this.getClassName();
99
        }
100

    
101
        //==============================================================
102
        // Implementacion del interface IXULPanel
103
        //
104
        public Object get(Object key) {
105
                return getThinlet().get(key);
106
        }
107

    
108
        public Object put(Object key, Object value) {
109
                return getThinlet().put(key,value);
110
        }
111

    
112
        public void setXULFile(String xulFile) throws IOException {
113
                getThinlet().setXULFile(xulFile);
114
        }
115

    
116
        public String getXULFile() {
117
                return getThinlet().getXULFile();
118
        }
119

    
120
        public Object getEngine() {
121
                return getThinlet();
122
        }
123

    
124
        public JPanel getJPanel() {
125
                return this;
126
        }
127

    
128
        public void close() {
129
                PluginServices.getMDIManager().closeWindow(this);
130
        }
131

    
132
        public void showWindow() {
133
                MDIManager mdim = PluginServices.getMDIManager();
134
                mdim.addWindow((IWindow) this);
135
        }
136

    
137
        public void setTitle(String title) {
138
                getWindowInfo().setTitle(title);
139
        }
140

    
141
        public void setPalette(boolean state) {
142
                getWindowInfo().toPalette(state);
143
        }
144

    
145
        public Object find(String name) {
146
                return getThinlet().find(name);
147
        }
148

    
149
        public Object createComponent(String name) {
150
                return getThinlet().find(name);
151
        }
152

    
153
        public void setString(Object component, String propertyName, String value) {
154
                getThinlet().setString(component,propertyName,value);
155
        }
156

    
157
        public void setInteger(Object component, String propertyName, int value) {
158
                getThinlet().setInteger(component,propertyName,value);
159
        }
160

    
161
        public void setBoolean(Object component, String propertyName, boolean value) {
162
                getThinlet().setBoolean(component,propertyName,value);
163
        }
164

    
165
        public Object getItem(Object component, int index) {
166
                return getThinlet().getItem(component,index);
167
        }
168

    
169
        public int getSize(Object component) {
170
                return getThinlet().getCount(component);
171
        }
172

    
173
        public String getString(Object component, String propertyName) {
174
                return getThinlet().getString(component,propertyName);
175
        }
176

    
177
        public int getInteger(Object component, String propertyName) {
178
                return getThinlet().getInteger(component,propertyName);
179
        }
180

    
181
        public boolean getBoolean(Object component, String propertyName) {
182
                return getThinlet().getBoolean(component,propertyName);
183
        }
184

    
185
        public Object getWindowProfile() {
186
                return WindowInfo.TOOL_PROFILE;
187
        }
188

    
189
}
190

    
191
class XULJPanelAncestorListener implements AncestorListener {
192

    
193
        private XULJPanel panel = null;
194

    
195
        public XULJPanelAncestorListener(XULJPanel panel) {
196
                this.panel = panel;
197
        }
198

    
199
        public void ancestorMoved(AncestorEvent event) {
200
                if (panel.getThinlet() == null)
201
                        return;
202
                panel.getThinlet().repaint();
203
        }
204

    
205
        public void ancestorRemoved(AncestorEvent event) {
206
                // Do nothing
207

    
208
        }
209

    
210
        public void ancestorAdded(AncestorEvent event) {
211
                if (panel.getThinlet() == null)
212
                        return;
213
                panel.getThinlet().repaint();
214

    
215
        }
216
}