Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libCorePlugin / src / org / gvsig / coreplugin / preferences / general / ExtensionPage.java @ 29630

History | View | Annotate | Download (9.94 KB)

1
package org.gvsig.coreplugin.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

    
23
import javax.swing.BorderFactory;
24
import javax.swing.ImageIcon;
25
import javax.swing.JCheckBox;
26
import javax.swing.JLabel;
27
import javax.swing.JPanel;
28
import javax.swing.JScrollPane;
29
import javax.swing.JTextArea;
30
import javax.swing.JTextField;
31
import javax.swing.border.TitledBorder;
32

    
33
import org.exolab.castor.xml.MarshalException;
34
import org.exolab.castor.xml.ValidationException;
35
import org.gvsig.andami.Launcher;
36
import org.gvsig.andami.PluginServices;
37
import org.gvsig.andami.config.generate.Plugin;
38
import org.gvsig.andami.plugins.config.generate.Extension;
39
import org.gvsig.andami.plugins.config.generate.PluginConfig;
40
import org.gvsig.andami.preferences.AbstractPreferencePage;
41
import org.gvsig.andami.preferences.StoreException;
42

    
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 = PluginServices.getIconTheme().get("emblem-work");
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.keySet().iterator();
124

    
125
                while (iter.hasNext()) {
126
                        Object obj = iter.next();
127
                        PluginConfig pconfig = (PluginConfig) pc.get(obj);
128
                        Writer writer;
129

    
130
                        try {
131
                                FileOutputStream fos = new FileOutputStream(Launcher.getAndamiConfig().getPluginsDirectory() +
132
                                                File.separator + (String) obj + File.separator +
133
                                                "config.xml");
134
                                // castor uses xerces, and xerces uses UTF-8 by default, so we should use
135
                                // UTF-8 to create the writer, as long as we continue using castor+xerces
136
                                writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
137

    
138
                                try {
139
                                        pconfig.marshal(writer);
140
                                } catch (MarshalException e1) {
141
                                        e1.printStackTrace();
142
                                } catch (ValidationException e1) {
143
                                        e1.printStackTrace();
144
                                }
145
                        } catch (IOException e) {
146
                                e.printStackTrace();
147
                        }
148

    
149
                        //hay que refrescar la aplicaci?n
150
                        ///((App)App.instance).run();
151
                }
152
        }
153
        /**
154
         * Lee del config.xml la configuraci?n.
155
         */
156
        public void unmarshalPlugins() {
157
                HashMap pc = Launcher.getPluginConfig();
158
                Iterator iter = pc.keySet().iterator();
159

    
160
                while (iter.hasNext()) {
161
                        Object obj = iter.next();
162
                        PluginConfig pconfig = (PluginConfig) pc.get(obj);
163

    
164
                        try {
165
                                String fileName = Launcher.getAndamiConfig().getPluginsDirectory() + File.separator + (String) obj + File.separator +        "config.xml";
166
                                FileInputStream is = new FileInputStream(fileName);
167
                                Reader reader = org.gvsig.utils.xml.XMLEncodingUtils.getReader(is);
168
                                if (reader==null) {
169
                                        // the encoding was not correctly detected, use system default
170
                                        reader = new FileReader(fileName);
171
                                }
172
                                else {
173
                                        // use a buffered reader to improve performance
174
                                        reader = new BufferedReader(reader);
175
                                }
176
                                pconfig=(PluginConfig)PluginConfig.unmarshal(reader);
177
                                Launcher.getPluginConfig().put(obj,pconfig);
178
                        } catch (Exception e) {
179
                                System.out.println("Exception unmarshalPlugin " + e);
180
                        }
181
                }
182

    
183
                //hay que refrescar la aplicaci?n
184
                ///((App)App.instance).run();
185
        }
186
        public void initializeDefaults() {
187
                unmarshalPlugins();
188
                getChbActivar().setSelected(((Extension) Launcher.getExtension(extension.getClassName())).getActive());
189
                getJTextField().setText(String.valueOf(extension.getPriority()));
190
                getJTextArea().setText(format(((Extension) Launcher.getExtension(extension.getClassName())).getDescription(),40));
191
        }
192

    
193
        public ImageIcon getIcon() {
194
                return icon;
195
        }
196

    
197
        /**
198
         * This method initializes jPanel
199
         *
200
         * @return javax.swing.JPanel
201
         */
202
        private JPanel getJPanel() {
203
                if (jPanel == null) {
204
                        jPanel = new JPanel();
205
                        jPanel.setLayout(new FlowLayout());
206
                        jPanel.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "descripcion"), TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
207
                        jPanel.add(getJScrollPane(), null);
208
                }
209
                return jPanel;
210
        }
211

    
212
        /**
213
         * This method initializes jScrollPane
214
         *
215
         * @return javax.swing.JScrollPane
216
         */
217
        private JScrollPane getJScrollPane() {
218
                if (jScrollPane == null) {
219
                        jScrollPane = new JScrollPane();
220
                        jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
221
                        jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
222
                        jScrollPane.setViewportView(getJTextArea());
223
                }
224
                return jScrollPane;
225
        }
226

    
227
        /**
228
         * This method initializes jTextArea
229
         *
230
         * @return javax.swing.JTextArea
231
         */
232
        private JTextArea getJTextArea() {
233
                if (jTextArea == null) {
234
                        jTextArea = new JTextArea();
235
                        jTextArea.setEnabled(false);
236
                        jTextArea.setText("");
237
                        jTextArea.setEditable(false);
238
                        jTextArea.setLineWrap(true);
239
                        jTextArea.setWrapStyleWord(true);
240
                        jTextArea.setPreferredSize(new Dimension(320, 170));
241
                        jTextArea.addKeyListener(myAction);
242
                }
243
                return jTextArea;
244
        }
245
        /**
246
         * This method initializes chbActivar
247
         *
248
         * @return javax.swing.JCheckBox
249
         */
250
        private JCheckBox getChbActivar() {
251
                if (chbActivar == null) {
252
                        chbActivar = new JCheckBox();
253
                        chbActivar.setSelected(true);
254
                        chbActivar.setText(PluginServices.getText(this, "extension_activada"));
255
                        chbActivar.addActionListener(myAction);
256
                }
257

    
258
                return chbActivar;
259
        }
260
        /**
261
         * This method initializes jPanel1
262
         *
263
         * @return javax.swing.JPanel
264
         */
265
        private JPanel getJPanel1() {
266
                if (jPanel1 == null) {
267
                        jLabel = new JLabel();
268
                        jLabel.setText(PluginServices.getText(this, "prioridad"));
269
                        jPanel1 = new JPanel();
270
                        jPanel1.add(getChbActivar(), null);
271
                        jPanel1.add(getJTextField(), null);
272
                        jPanel1.add(jLabel, null);
273
                }
274
                return jPanel1;
275
        }
276
        /**
277
         * This method initializes jPanel1
278
         *
279
         * @return javax.swing.JPanel
280
         */
281
        private JPanel getJPanel2() {
282
                if (jPanel2 == null) {
283
                        jLabel1 = new JLabel();
284
                        jLabel1.setText(PluginServices.getText(this, "Los_cambios_efectuados_sobre_estos_valores_se_aplicaran_al_reiniciar_la_aplicacion"));
285
                        jPanel2 = new JPanel();
286
                        jPanel2.add(jLabel1, null);                        
287
                }
288
                return jPanel2;
289
        }
290

    
291
        /**
292
         * This method initializes jTextField
293
         *
294
         * @return javax.swing.JTextField
295
         */
296
        private JTextField getJTextField() {
297
                if (jTextField == null) {
298
                        jTextField = new JTextField();
299
                        jTextField.setPreferredSize(new Dimension(40, 20));
300
                        jTextField.addKeyListener(myAction);
301
                }
302
                return jTextField;
303
        }
304
        /**
305
     * Cuts the message text to force its lines to be shorter or equal to
306
     * lineLength.
307
     * @param message, the message.
308
     * @param lineLength, the max line length in number of characters.
309
     * @return the formated message.
310
     */
311
    private static String format(String message, int lineLength){
312
        if (message.length() <= lineLength) return message;
313
        String[] lines = message.split(" ");
314
        String theMessage = "";
315
        String line="";
316
        int i=0;
317
        while (i<lines.length) {
318
                while (i<lines.length && lineLength>line.length()) {
319
                        line=line.concat(lines[i]+" ");
320
                        i++;
321
                }
322
                theMessage=theMessage.concat(line+"\n");
323
                line="";
324
        }
325
        return theMessage;
326
    }
327

    
328
    private class MyAction implements ActionListener, KeyListener {
329
                public void actionPerformed(ActionEvent e) { changed = true; System.out.println("actionperformed");}
330
                public void keyPressed(KeyEvent e) { changed = true; System.out.println("keypressed"); }
331
                public void keyReleased(KeyEvent e) { changed = true; System.out.println("keyreleased");}
332
                public void keyTyped(KeyEvent e) { changed = true; System.out.println("keytyped");}
333
        }
334

    
335
        public boolean isValueChanged() {
336
                return changed;
337
        }
338

    
339
        public void setChangesApplied() {
340
                changed = false;
341
        }
342
}  //  @jve:decl-index=0:visual-constraint="10,10"