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 @ 443
History | View | Annotate | Download (3.58 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.io.BufferedReader; |
8 |
import java.io.File; |
9 |
import java.io.FileReader; |
10 |
import java.io.IOException; |
11 |
import java.io.InputStream; |
12 |
import java.io.StringBufferInputStream; |
13 |
import java.util.regex.Matcher; |
14 |
import java.util.regex.Pattern; |
15 |
|
16 |
import javax.swing.event.AncestorEvent; |
17 |
import javax.swing.event.AncestorListener; |
18 |
|
19 |
import org.gvsig.scripting.swing.api.JThinlet; |
20 |
import org.slf4j.Logger; |
21 |
import org.slf4j.LoggerFactory; |
22 |
|
23 |
import thinlet.Thinlet; |
24 |
|
25 |
public class DefaultJThinlet extends JThinlet implements AncestorListener{ |
26 |
|
27 |
/**
|
28 |
*
|
29 |
*/
|
30 |
private static final long serialVersionUID = 5130602761126860652L; |
31 |
|
32 |
private static final Logger logger = LoggerFactory.getLogger(DefaultJThinlet.class); |
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 |
class ThinletWrapper extends Thinlet { |
69 |
public void setVisible(boolean b) { |
70 |
super.setVisible(b);
|
71 |
if( !b ) {
|
72 |
hideThinlet(); |
73 |
} |
74 |
} |
75 |
} |
76 |
|
77 |
public void load(File file){ |
78 |
String xml = null; |
79 |
InputStream input=null; |
80 |
Object component=null; |
81 |
// Creamos el thinlet
|
82 |
Thinlet thinlet = new ThinletWrapper();
|
83 |
|
84 |
try {
|
85 |
xml = readFileAsString(file); |
86 |
} catch (IOException e1) { |
87 |
// TODO Auto-generated catch block
|
88 |
e1.printStackTrace(); |
89 |
} |
90 |
|
91 |
Matcher match = pattern.matcher(xml);
|
92 |
xml = match.replaceAll(" action=\"invoke('$1')\"");
|
93 |
input = new StringBufferInputStream(xml); |
94 |
|
95 |
try {
|
96 |
// parseamos el xml, creamos los componentes y los devolvemos.
|
97 |
component = thinlet.parse(input,this);
|
98 |
} catch (IOException e) { |
99 |
e.printStackTrace(); |
100 |
} |
101 |
// Cargamos los componentes en el thinlet
|
102 |
if (component != null) |
103 |
thinlet.add(component); |
104 |
// A?adimos el thinlet al JPanel.
|
105 |
this.add(thinlet, BorderLayout.CENTER); |
106 |
this.thinlet = thinlet;
|
107 |
} |
108 |
|
109 |
private void hideThinlet() { |
110 |
this.setVisible(false); |
111 |
} |
112 |
|
113 |
public void invoke(String name){ |
114 |
//System.out.println("JThinlet.invoke: name="+name.toString());
|
115 |
if (this.actionlistener != null){ |
116 |
ActionEvent event = new ActionEvent(this, 1, (String)name); |
117 |
this.actionlistener.actionPerformed(event);
|
118 |
} |
119 |
} |
120 |
|
121 |
public Thinlet getThinlet(){
|
122 |
return this.thinlet; |
123 |
} |
124 |
|
125 |
public Dimension getPreferredSize(){ |
126 |
return this.getThinlet().getPreferredSize(); |
127 |
} |
128 |
|
129 |
public void ancestorAdded(AncestorEvent event) { |
130 |
if (this.getThinlet() == null) |
131 |
this.getThinlet().repaint();
|
132 |
} |
133 |
|
134 |
public void ancestorMoved(AncestorEvent event) { |
135 |
if (this.getThinlet() == null) |
136 |
this.getThinlet().repaint();
|
137 |
} |
138 |
|
139 |
public void ancestorRemoved(AncestorEvent event) { |
140 |
} |
141 |
|
142 |
public void addActionListener(ActionListener actionlistener) { |
143 |
this.actionlistener = actionlistener;
|
144 |
} |
145 |
} |