gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.swing / org.gvsig.scripting.swing.impl / src / main / java / org / gvsig / scripting / swing / impl / DefaultJThinlet.java @ 442
History | View | Annotate | Download (3.74 KB)
1 |
package org.gvsig.scripting.swing.impl; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Dimension; |
5 |
import java.awt.event.ActionEvent; |
6 |
import java.awt.event.ActionListener; |
7 |
import java.awt.event.ComponentEvent; |
8 |
import java.awt.event.ComponentListener; |
9 |
import java.awt.event.ContainerEvent; |
10 |
import java.awt.event.ContainerListener; |
11 |
import java.io.BufferedReader; |
12 |
import java.io.File; |
13 |
import java.io.FileReader; |
14 |
import java.io.IOException; |
15 |
import java.io.InputStream; |
16 |
import java.io.StringBufferInputStream; |
17 |
import java.util.regex.Matcher; |
18 |
import java.util.regex.Pattern; |
19 |
|
20 |
import javax.swing.event.AncestorEvent; |
21 |
import javax.swing.event.AncestorListener; |
22 |
|
23 |
import org.gvsig.scripting.swing.api.JThinlet; |
24 |
|
25 |
import thinlet.Thinlet; |
26 |
|
27 |
public class DefaultJThinlet extends JThinlet implements AncestorListener{ |
28 |
|
29 |
/**
|
30 |
*
|
31 |
*/
|
32 |
private static final long serialVersionUID = 5130602761126860652L; |
33 |
|
34 |
static final Pattern pattern = Pattern.compile(" action=\\s*\"([^\"]*)\""); |
35 |
|
36 |
protected Thinlet thinlet;
|
37 |
protected ActionListener actionlistener = null; |
38 |
|
39 |
public DefaultJThinlet(){
|
40 |
super();
|
41 |
this.setLayout(new BorderLayout()); |
42 |
this.addAncestorListener(this); |
43 |
this.thinlet = null; |
44 |
} |
45 |
|
46 |
public DefaultJThinlet(File file){ |
47 |
this();
|
48 |
if (file!=null){ |
49 |
this.load(file);
|
50 |
} |
51 |
} |
52 |
|
53 |
static String readFileAsString(File file) throws java.io.IOException{ |
54 |
StringBuffer fileData = new StringBuffer(1000); |
55 |
BufferedReader reader = new BufferedReader( |
56 |
new FileReader(file)); |
57 |
char[] buf = new char[1024]; |
58 |
int numRead=0; |
59 |
while((numRead=reader.read(buf)) != -1){ |
60 |
String readData = String.valueOf(buf, 0, numRead); |
61 |
fileData.append(readData); |
62 |
buf = new char[1024]; |
63 |
} |
64 |
reader.close(); |
65 |
return fileData.toString();
|
66 |
} |
67 |
|
68 |
|
69 |
public void load(File file){ |
70 |
String xml = null; |
71 |
InputStream input=null; |
72 |
Object component=null; |
73 |
// Creamos el thinlet
|
74 |
Thinlet thinlet = new Thinlet();
|
75 |
|
76 |
try {
|
77 |
xml = readFileAsString(file); |
78 |
} catch (IOException e1) { |
79 |
// TODO Auto-generated catch block
|
80 |
e1.printStackTrace(); |
81 |
} |
82 |
|
83 |
Matcher match = pattern.matcher(xml);
|
84 |
xml = match.replaceAll(" action=\"invoke('$1')\"");
|
85 |
input = new StringBufferInputStream(xml); |
86 |
|
87 |
try {
|
88 |
// parseamos el xml, creamos los componentes y los devolvemos.
|
89 |
component = thinlet.parse(input,this);
|
90 |
} catch (IOException e) { |
91 |
e.printStackTrace(); |
92 |
} |
93 |
// Cargamos los componentes en el thinlet
|
94 |
if (component != null) |
95 |
thinlet.add(component); |
96 |
// Añadimos el thinlet al JPanel.
|
97 |
this.add(thinlet, BorderLayout.CENTER); |
98 |
this.thinlet = thinlet;
|
99 |
this.thinlet.addComponentListener(new ComponentListener() { |
100 |
public void componentShown(ComponentEvent arg0) { |
101 |
} |
102 |
public void componentResized(ComponentEvent arg0) { |
103 |
} |
104 |
public void componentMoved(ComponentEvent arg0) { |
105 |
} |
106 |
public void componentHidden(ComponentEvent arg0) { |
107 |
hideThinlet(); |
108 |
} |
109 |
}); |
110 |
} |
111 |
|
112 |
private void hideThinlet() { |
113 |
this.setVisible(false); |
114 |
} |
115 |
|
116 |
public void invoke(String name){ |
117 |
//System.out.println("JThinlet.invoke: name="+name.toString());
|
118 |
if (this.actionlistener != null){ |
119 |
ActionEvent event = new ActionEvent(this, 1, (String)name); |
120 |
this.actionlistener.actionPerformed(event);
|
121 |
} |
122 |
} |
123 |
|
124 |
public Thinlet getThinlet(){
|
125 |
return this.thinlet; |
126 |
} |
127 |
|
128 |
public Dimension getPreferredSize(){ |
129 |
return this.getThinlet().getPreferredSize(); |
130 |
} |
131 |
|
132 |
public void ancestorAdded(AncestorEvent event) { |
133 |
if (this.getThinlet() == null) |
134 |
this.getThinlet().repaint();
|
135 |
} |
136 |
|
137 |
public void ancestorMoved(AncestorEvent event) { |
138 |
if (this.getThinlet() == null) |
139 |
this.getThinlet().repaint();
|
140 |
} |
141 |
|
142 |
public void ancestorRemoved(AncestorEvent event) { |
143 |
} |
144 |
|
145 |
public void addActionListener(ActionListener actionlistener) { |
146 |
this.actionlistener = actionlistener;
|
147 |
} |
148 |
} |