root / branches / v10 / libraries / libCorePlugin / src / com / iver / core / preferences / general / ExtensionPage.java @ 11142
History | View | Annotate | Download (10.3 KB)
1 |
package com.iver.core.preferences.general; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Dimension; |
5 |
import java.awt.FlowLayout; |
6 |
import java.awt.event.ActionEvent; |
7 |
import java.awt.event.ActionListener; |
8 |
import java.awt.event.KeyEvent; |
9 |
import java.awt.event.KeyListener; |
10 |
import java.io.BufferedReader; |
11 |
import java.io.BufferedWriter; |
12 |
import java.io.File; |
13 |
import java.io.FileInputStream; |
14 |
import java.io.FileOutputStream; |
15 |
import java.io.FileReader; |
16 |
import java.io.IOException; |
17 |
import java.io.OutputStreamWriter; |
18 |
import java.io.Reader; |
19 |
import java.io.Writer; |
20 |
import java.util.HashMap; |
21 |
import java.util.Iterator; |
22 |
import java.util.Map.Entry; |
23 |
|
24 |
import javax.swing.BorderFactory; |
25 |
import javax.swing.ImageIcon; |
26 |
import javax.swing.JCheckBox; |
27 |
import javax.swing.JLabel; |
28 |
import javax.swing.JPanel; |
29 |
import javax.swing.JScrollPane; |
30 |
import javax.swing.JTextArea; |
31 |
import javax.swing.JTextField; |
32 |
import javax.swing.border.TitledBorder; |
33 |
|
34 |
import org.exolab.castor.xml.MarshalException; |
35 |
import org.exolab.castor.xml.ValidationException; |
36 |
|
37 |
import com.iver.andami.Launcher; |
38 |
import com.iver.andami.PluginServices; |
39 |
import com.iver.andami.plugins.config.generate.Extension; |
40 |
import com.iver.andami.plugins.config.generate.PluginConfig; |
41 |
import com.iver.andami.preferences.AbstractPreferencePage; |
42 |
import com.iver.andami.preferences.StoreException; |
43 |
|
44 |
public class ExtensionPage extends AbstractPreferencePage { |
45 |
private Extension extension;
|
46 |
private JPanel jPanel = null; |
47 |
private JScrollPane jScrollPane = null; |
48 |
private JTextArea jTextArea = null; |
49 |
private JPanel jPanel1 = null; |
50 |
private JPanel jPanel2 = null; |
51 |
private JTextField jTextField = null; |
52 |
private JLabel jLabel = null; |
53 |
private JLabel jLabel1 = null; |
54 |
private JCheckBox chbActivar = null; |
55 |
private ImageIcon icon; |
56 |
private boolean chkSelected; |
57 |
private int txtPriority; |
58 |
private boolean changed = false; |
59 |
private MyAction myAction = new MyAction(); |
60 |
/**
|
61 |
* This is the default constructor
|
62 |
*/
|
63 |
public ExtensionPage(Extension extension) {
|
64 |
super();
|
65 |
icon = new ImageIcon(this.getClass().getClassLoader().getResource("images/emblem-work.png")); |
66 |
this.extension=extension;
|
67 |
setParentID(ExtensionsPage.class.getName()); |
68 |
initialize(); |
69 |
} |
70 |
|
71 |
/**
|
72 |
* This method initializes this
|
73 |
*
|
74 |
* @return void
|
75 |
*/
|
76 |
private void initialize() { |
77 |
this.setLayout(new BorderLayout()); |
78 |
this.setSize(451, 234); |
79 |
this.add(getJPanel(), java.awt.BorderLayout.NORTH);
|
80 |
this.add(getJPanel1(), java.awt.BorderLayout.CENTER);
|
81 |
this.add(getJPanel2(), java.awt.BorderLayout.SOUTH);
|
82 |
|
83 |
} |
84 |
|
85 |
public String getID() { |
86 |
return extension.getClassName();
|
87 |
} |
88 |
|
89 |
public String getTitle() { |
90 |
return extension.getClassName();
|
91 |
} |
92 |
|
93 |
public JPanel getPanel() { |
94 |
return this; |
95 |
} |
96 |
|
97 |
public void initializeValues() { |
98 |
getChbActivar().setSelected(((Extension) Launcher.getExtension(extension.getClassName())).getActive()); |
99 |
getJTextField().setText(String.valueOf(extension.getPriority()));
|
100 |
getJTextArea().setText(format(((Extension) Launcher.getExtension(extension.getClassName())).getDescription(),40));
|
101 |
chkSelected = getChbActivar().isSelected(); |
102 |
txtPriority = extension.getPriority(); |
103 |
} |
104 |
|
105 |
public void storeValues() throws StoreException { |
106 |
int pri;
|
107 |
try {
|
108 |
pri = Integer.parseInt(getJTextField().getText());
|
109 |
} catch (Exception e){ |
110 |
throw new StoreException( PluginServices.getText(this, "invalid_priority_value"),e); |
111 |
} |
112 |
extension.setActive(chbActivar.isSelected()); |
113 |
extension.setPriority(pri); |
114 |
// Se escribe el config de los plugins
|
115 |
marshalPlugins(); |
116 |
} |
117 |
|
118 |
/**
|
119 |
* Escribe sobre el config.xml, la nueva configuraci?n.
|
120 |
*/
|
121 |
public void marshalPlugins() { |
122 |
HashMap pc = Launcher.getPluginConfig();
|
123 |
Iterator iter = pc.entrySet().iterator();
|
124 |
|
125 |
Entry entry; |
126 |
File configFile;
|
127 |
PluginConfig pconfig; |
128 |
String pluginName;
|
129 |
Writer writer;
|
130 |
FileOutputStream fos;
|
131 |
|
132 |
|
133 |
while (iter.hasNext()) {
|
134 |
entry =(Entry)iter.next(); |
135 |
pluginName = (String) entry.getKey();
|
136 |
pconfig = (PluginConfig) entry.getValue(); |
137 |
|
138 |
configFile = new File(Launcher.getAndamiConfig().getPluginsDirectory() + |
139 |
File.separator + pluginName + File.separator + |
140 |
"config.xml");
|
141 |
if (!configFile.canWrite()){
|
142 |
continue;
|
143 |
} |
144 |
|
145 |
try {
|
146 |
fos = new FileOutputStream(configFile); |
147 |
// castor uses xerces, and xerces uses UTF-8 by default, so we should use
|
148 |
// UTF-8 to create the writer, as long as we continue using castor+xerces
|
149 |
writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8")); |
150 |
|
151 |
try {
|
152 |
pconfig.marshal(writer); |
153 |
} catch (MarshalException e1) { |
154 |
e1.printStackTrace(); |
155 |
} catch (ValidationException e1) {
|
156 |
e1.printStackTrace(); |
157 |
} |
158 |
} catch (IOException e) { |
159 |
e.printStackTrace(); |
160 |
} |
161 |
|
162 |
//hay que refrescar la aplicaci?n
|
163 |
///((App)App.instance).run();
|
164 |
} |
165 |
} |
166 |
/**
|
167 |
* Lee del config.xml la configuraci?n.
|
168 |
*/
|
169 |
public void unmarshalPlugins() { |
170 |
HashMap pc = Launcher.getPluginConfig();
|
171 |
Iterator iter = pc.keySet().iterator();
|
172 |
|
173 |
while (iter.hasNext()) {
|
174 |
Object obj = iter.next();
|
175 |
PluginConfig pconfig = (PluginConfig) pc.get(obj); |
176 |
|
177 |
try {
|
178 |
String fileName = Launcher.getAndamiConfig().getPluginsDirectory() + File.separator + (String) obj + File.separator + "config.xml"; |
179 |
FileInputStream is = new FileInputStream(fileName); |
180 |
Reader reader = com.iver.utiles.xml.XMLEncodingUtils.getReader(is);
|
181 |
if (reader==null) { |
182 |
// the encoding was not correctly detected, use system default
|
183 |
reader = new FileReader(fileName); |
184 |
} |
185 |
else {
|
186 |
// use a buffered reader to improve performance
|
187 |
reader = new BufferedReader(reader); |
188 |
} |
189 |
pconfig=(PluginConfig)PluginConfig.unmarshal(reader); |
190 |
Launcher.getPluginConfig().put(obj,pconfig); |
191 |
} catch (Exception e) { |
192 |
System.out.println("Exception unmarshalPlugin " + e); |
193 |
} |
194 |
} |
195 |
|
196 |
//hay que refrescar la aplicaci?n
|
197 |
///((App)App.instance).run();
|
198 |
} |
199 |
public void initializeDefaults() { |
200 |
unmarshalPlugins(); |
201 |
getChbActivar().setSelected(((Extension) Launcher.getExtension(extension.getClassName())).getActive()); |
202 |
getJTextField().setText(String.valueOf(extension.getPriority()));
|
203 |
getJTextArea().setText(format(((Extension) Launcher.getExtension(extension.getClassName())).getDescription(),40));
|
204 |
} |
205 |
|
206 |
public ImageIcon getIcon() { |
207 |
return icon;
|
208 |
} |
209 |
|
210 |
/**
|
211 |
* This method initializes jPanel
|
212 |
*
|
213 |
* @return javax.swing.JPanel
|
214 |
*/
|
215 |
private JPanel getJPanel() { |
216 |
if (jPanel == null) { |
217 |
jPanel = new JPanel(); |
218 |
jPanel.setLayout(new FlowLayout()); |
219 |
jPanel.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "descripcion"), TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null)); |
220 |
jPanel.add(getJScrollPane(), null);
|
221 |
} |
222 |
return jPanel;
|
223 |
} |
224 |
|
225 |
/**
|
226 |
* This method initializes jScrollPane
|
227 |
*
|
228 |
* @return javax.swing.JScrollPane
|
229 |
*/
|
230 |
private JScrollPane getJScrollPane() { |
231 |
if (jScrollPane == null) { |
232 |
jScrollPane = new JScrollPane(); |
233 |
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
|
234 |
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
235 |
jScrollPane.setViewportView(getJTextArea()); |
236 |
} |
237 |
return jScrollPane;
|
238 |
} |
239 |
|
240 |
/**
|
241 |
* This method initializes jTextArea
|
242 |
*
|
243 |
* @return javax.swing.JTextArea
|
244 |
*/
|
245 |
private JTextArea getJTextArea() { |
246 |
if (jTextArea == null) { |
247 |
jTextArea = new JTextArea(); |
248 |
jTextArea.setEnabled(false);
|
249 |
jTextArea.setText("");
|
250 |
jTextArea.setEditable(false);
|
251 |
jTextArea.setLineWrap(true);
|
252 |
jTextArea.setWrapStyleWord(true);
|
253 |
jTextArea.setPreferredSize(new Dimension(320, 170)); |
254 |
jTextArea.addKeyListener(myAction); |
255 |
} |
256 |
return jTextArea;
|
257 |
} |
258 |
/**
|
259 |
* This method initializes chbActivar
|
260 |
*
|
261 |
* @return javax.swing.JCheckBox
|
262 |
*/
|
263 |
private JCheckBox getChbActivar() { |
264 |
if (chbActivar == null) { |
265 |
chbActivar = new JCheckBox(); |
266 |
chbActivar.setSelected(true);
|
267 |
chbActivar.setText(PluginServices.getText(this, "extension_activada")); |
268 |
chbActivar.addActionListener(myAction); |
269 |
} |
270 |
|
271 |
return chbActivar;
|
272 |
} |
273 |
/**
|
274 |
* This method initializes jPanel1
|
275 |
*
|
276 |
* @return javax.swing.JPanel
|
277 |
*/
|
278 |
private JPanel getJPanel1() { |
279 |
if (jPanel1 == null) { |
280 |
jLabel = new JLabel(); |
281 |
jLabel.setText(PluginServices.getText(this, "prioridad")); |
282 |
jPanel1 = new JPanel(); |
283 |
jPanel1.add(getChbActivar(), null);
|
284 |
jPanel1.add(getJTextField(), null);
|
285 |
jPanel1.add(jLabel, null);
|
286 |
} |
287 |
return jPanel1;
|
288 |
} |
289 |
/**
|
290 |
* This method initializes jPanel1
|
291 |
*
|
292 |
* @return javax.swing.JPanel
|
293 |
*/
|
294 |
private JPanel getJPanel2() { |
295 |
if (jPanel2 == null) { |
296 |
jLabel1 = new JLabel(); |
297 |
jLabel1.setText(PluginServices.getText(this, "Los_cambios_efectuados_sobre_estos_valores_se_aplicaran_al_reiniciar_la_aplicacion")); |
298 |
jPanel2 = new JPanel(); |
299 |
jPanel2.add(jLabel1, null);
|
300 |
} |
301 |
return jPanel2;
|
302 |
} |
303 |
|
304 |
/**
|
305 |
* This method initializes jTextField
|
306 |
*
|
307 |
* @return javax.swing.JTextField
|
308 |
*/
|
309 |
private JTextField getJTextField() { |
310 |
if (jTextField == null) { |
311 |
jTextField = new JTextField(); |
312 |
jTextField.setPreferredSize(new Dimension(40, 20)); |
313 |
jTextField.addKeyListener(myAction); |
314 |
} |
315 |
return jTextField;
|
316 |
} |
317 |
/**
|
318 |
* Cuts the message text to force its lines to be shorter or equal to
|
319 |
* lineLength.
|
320 |
* @param message, the message.
|
321 |
* @param lineLength, the max line length in number of characters.
|
322 |
* @return the formated message.
|
323 |
*/
|
324 |
private static String format(String message, int lineLength){ |
325 |
if (message.length() <= lineLength) return message; |
326 |
String[] lines = message.split(" "); |
327 |
String theMessage = ""; |
328 |
String line=""; |
329 |
int i=0; |
330 |
while (i<lines.length) {
|
331 |
while (i<lines.length && lineLength>line.length()) {
|
332 |
line=line.concat(lines[i]+" ");
|
333 |
i++; |
334 |
} |
335 |
theMessage=theMessage.concat(line+"\n");
|
336 |
line="";
|
337 |
} |
338 |
return theMessage;
|
339 |
} |
340 |
|
341 |
private class MyAction implements ActionListener, KeyListener { |
342 |
public void actionPerformed(ActionEvent e) { changed = true; System.out.println("actionperformed");} |
343 |
public void keyPressed(KeyEvent e) { changed = true; System.out.println("keypressed"); } |
344 |
public void keyReleased(KeyEvent e) { changed = true; System.out.println("keyreleased");} |
345 |
public void keyTyped(KeyEvent e) { changed = true; System.out.println("keytyped");} |
346 |
} |
347 |
|
348 |
public boolean isValueChanged() { |
349 |
return changed;
|
350 |
} |
351 |
|
352 |
public void setChangesApplied() { |
353 |
changed = false;
|
354 |
} |
355 |
|
356 |
public void setReadOnly(boolean readOnly){ |
357 |
getChbActivar().setEnabled(!readOnly); |
358 |
getJTextField().setEnabled(!readOnly); |
359 |
} |
360 |
} // @jve:decl-index=0:visual-constraint="10,10"
|