Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libCorePlugin / src / org / gvsig / coreplugin / preferences / general / ExtensionPage.java @ 38564

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.plugins.config.generate.Extension;
38
import org.gvsig.andami.plugins.config.generate.PluginConfig;
39
import org.gvsig.andami.preferences.AbstractPreferencePage;
40
import org.gvsig.andami.preferences.StoreException;
41

    
42

    
43
public class ExtensionPage extends AbstractPreferencePage {
44
        private Extension extension;
45
        private JPanel jPanel = null;
46
        private JScrollPane jScrollPane = null;
47
        private JTextArea jTextArea = null;
48
        private JPanel jPanel1 = null;
49
        private JPanel jPanel2 = null;
50
        private JTextField jTextField = null;
51
        private JLabel jLabel = null;
52
        private JLabel jLabel1 = null;
53
        private JCheckBox chbActivar = null;
54
        private ImageIcon icon;
55
        private boolean chkSelected;
56
        private int txtPriority;
57
        private boolean changed = false;
58
        private MyAction myAction = new MyAction();
59
        /**
60
         * This is the default constructor
61
         */
62
        public ExtensionPage(Extension extension) {
63
                super();
64
                icon = PluginServices.getIconTheme().get("edit-setup-extension");
65
                this.extension=extension;
66
                setParentID(ExtensionsPage.class.getName());
67
                initialize();
68
        }
69

    
70
        /**
71
         * This method initializes this
72
         *
73
         * @return void
74
         */
75
        private void initialize() {
76
                this.setLayout(new BorderLayout());
77
                this.setSize(451, 234);
78
                this.add(getJPanel(), java.awt.BorderLayout.NORTH);
79
                this.add(getJPanel1(), java.awt.BorderLayout.CENTER);
80
                this.add(getJPanel2(), java.awt.BorderLayout.SOUTH);
81

    
82
        }
83

    
84
        public String getID() {
85
                return extension.getClassName();
86
        }
87

    
88
        public String getTitle() {
89
                return extension.getClassName();
90
        }
91

    
92
        public JPanel getPanel() {
93
                return this;
94
        }
95

    
96
        public void initializeValues() {
97
                getChbActivar().setSelected(((Extension) Launcher.getExtension(extension.getClassName())).getActive());
98
                getJTextField().setText(String.valueOf(extension.getPriority()));
99
                getJTextArea().setText(format(((Extension) Launcher.getExtension(extension.getClassName())).getDescription(),40));
100
                chkSelected = getChbActivar().isSelected();
101
                txtPriority = extension.getPriority();
102
        }
103

    
104
        public void storeValues() throws StoreException {
105
                int pri;
106
                try {
107
                        pri = Integer.parseInt(getJTextField().getText());
108
                } catch (Exception e){
109
                        throw new StoreException( PluginServices.getText(this, "invalid_priority_value"),e);
110
                }
111
                extension.setActive(chbActivar.isSelected());
112
                extension.setPriority(pri);
113
                // Se escribe el config de los plugins
114
                marshalPlugins();
115
        }
116

    
117
        /**
118
         * Escribe sobre el config.xml, la nueva configuraci?n.
119
         */
120
        public void marshalPlugins() {
121
                HashMap pc = Launcher.getPluginConfig();
122
                Iterator iter = pc.keySet().iterator();
123

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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