Statistics
| Revision:

gvsig-projects-pool / org.gvsig.winmgr / trunk / org.gvsig.winmgr.app / org.gvsig.winmgr.app.mainplugin / src / main / java / org / gvsig / coreplugin / preferences / general / ExtensionPage.java @ 682

History | View | Annotate | Download (11 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.coreplugin.preferences.general;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Dimension;
28
import java.awt.FlowLayout;
29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
31
import java.awt.event.KeyEvent;
32
import java.awt.event.KeyListener;
33
import java.io.BufferedReader;
34
import java.io.BufferedWriter;
35
import java.io.File;
36
import java.io.FileInputStream;
37
import java.io.FileOutputStream;
38
import java.io.FileReader;
39
import java.io.IOException;
40
import java.io.OutputStreamWriter;
41
import java.io.Reader;
42
import java.io.Writer;
43
import java.util.HashMap;
44
import java.util.Iterator;
45

    
46
import javax.swing.BorderFactory;
47
import javax.swing.ImageIcon;
48
import javax.swing.JCheckBox;
49
import javax.swing.JLabel;
50
import javax.swing.JPanel;
51
import javax.swing.JScrollPane;
52
import javax.swing.JTextArea;
53
import javax.swing.JTextField;
54
import javax.swing.border.TitledBorder;
55

    
56
import org.exolab.castor.xml.MarshalException;
57
import org.exolab.castor.xml.ValidationException;
58
import org.gvsig.andami.Launcher;
59
import org.gvsig.andami.PluginServices;
60
import org.gvsig.andami.plugins.config.generate.Extension;
61
import org.gvsig.andami.plugins.config.generate.PluginConfig;
62
import org.gvsig.andami.preferences.AbstractPreferencePage;
63
import org.gvsig.andami.preferences.StoreException;
64

    
65

    
66
public class ExtensionPage extends AbstractPreferencePage {
67
        private Extension extension;
68
        private JPanel jPanel = null;
69
        private JScrollPane jScrollPane = null;
70
        private JTextArea jTextArea = null;
71
        private JPanel jPanel1 = null;
72
        private JPanel jPanel2 = null;
73
        private JTextField jTextField = null;
74
        private JLabel jLabel = null;
75
        private JLabel jLabel1 = null;
76
        private JCheckBox chbActivar = null;
77
        private ImageIcon icon;
78
        private boolean chkSelected;
79
        private int txtPriority;
80
        private boolean changed = false;
81
        private MyAction myAction = new MyAction();
82
        /**
83
         * This is the default constructor
84
         */
85
        public ExtensionPage(Extension extension) {
86
                super();
87
                icon = PluginServices.getIconTheme().get("edit-setup-extension");
88
                this.extension=extension;
89
                setParentID(ExtensionsPage.class.getName());
90
                initialize();
91
        }
92

    
93
        /**
94
         * This method initializes this
95
         *
96
         * @return void
97
         */
98
        private void initialize() {
99
                this.setLayout(new BorderLayout());
100
                this.setSize(451, 234);
101
                this.add(getJPanel(), java.awt.BorderLayout.NORTH);
102
                this.add(getJPanel1(), java.awt.BorderLayout.CENTER);
103
                this.add(getJPanel2(), java.awt.BorderLayout.SOUTH);
104

    
105
        }
106

    
107
        public String getID() {
108
                return extension.getClassName();
109
        }
110

    
111
        public String getTitle() {
112
                return extension.getClassName();
113
        }
114

    
115
        public JPanel getPanel() {
116
                return this;
117
        }
118

    
119
        public void initializeValues() {
120
                getChbActivar().setSelected(((Extension) Launcher.getExtension(extension.getClassName())).getActive());
121
                getJTextField().setText(String.valueOf(extension.getPriority()));
122
                getJTextArea().setText(format(((Extension) Launcher.getExtension(extension.getClassName())).getDescription(),40));
123
                chkSelected = getChbActivar().isSelected();
124
                txtPriority = extension.getPriority();
125
        }
126

    
127
        public void storeValues() throws StoreException {
128
//                int pri;
129
//                try {
130
//                        pri = Integer.parseInt(getJTextField().getText());
131
//                } catch (Exception e){
132
//                        throw new StoreException( PluginServices.getText(this, "invalid_priority_value"),e);
133
//                }
134
//                extension.setActive(chbActivar.isSelected());
135
//                extension.setPriority(pri);
136
//                // Se escribe el config de los plugins
137
//                marshalPlugins();
138
        }
139

    
140
        /**
141
         * Escribe sobre el config.xml, la nueva configuraci?n.
142
         */
143
        public void marshalPlugins() {
144
                HashMap pc = Launcher.getPluginConfig();
145
                Iterator iter = pc.keySet().iterator();
146

    
147
                while (iter.hasNext()) {
148
                        Object obj = iter.next();
149
                        PluginConfig pconfig = (PluginConfig) pc.get(obj);
150
                        Writer writer;
151

    
152
                        try {
153
                                FileOutputStream fos = new FileOutputStream(Launcher.getAndamiConfig().getPluginsDirectory() +
154
                                                File.separator + (String) obj + File.separator +
155
                                                "config.xml");
156
                                // castor uses xerces, and xerces uses UTF-8 by default, so we should use
157
                                // UTF-8 to create the writer, as long as we continue using castor+xerces
158
                                writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
159

    
160
                                try {
161
                                        pconfig.marshal(writer);
162
                                } catch (MarshalException e1) {
163
                                        e1.printStackTrace();
164
                                } catch (ValidationException e1) {
165
                                        e1.printStackTrace();
166
                                }
167
                        } catch (IOException e) {
168
                                e.printStackTrace();
169
                        }
170

    
171
                        //hay que refrescar la aplicaci?n
172
                        ///((App)App.instance).run();
173
                }
174
        }
175
        /**
176
         * Lee del config.xml la configuraci?n.
177
         */
178
        public void unmarshalPlugins() {
179
                HashMap pc = Launcher.getPluginConfig();
180
                Iterator iter = pc.keySet().iterator();
181

    
182
                while (iter.hasNext()) {
183
                        Object obj = iter.next();
184
                        PluginConfig pconfig = (PluginConfig) pc.get(obj);
185

    
186
                        try {
187
                                String fileName = Launcher.getAndamiConfig().getPluginsDirectory() + File.separator + (String) obj + File.separator +        "config.xml";
188
                                FileInputStream is = new FileInputStream(fileName);
189
                                Reader reader = org.gvsig.utils.xml.XMLEncodingUtils.getReader(is);
190
                                if (reader==null) {
191
                                        // the encoding was not correctly detected, use system default
192
                                        reader = new FileReader(fileName);
193
                                }
194
                                else {
195
                                        // use a buffered reader to improve performance
196
                                        reader = new BufferedReader(reader);
197
                                }
198
                                pconfig=(PluginConfig)PluginConfig.unmarshal(reader);
199
                                Launcher.getPluginConfig().put(obj,pconfig);
200
                        } catch (Exception e) {
201
                                System.out.println("Exception unmarshalPlugin " + e);
202
                        }
203
                }
204

    
205
                //hay que refrescar la aplicaci?n
206
                ///((App)App.instance).run();
207
        }
208
        public void initializeDefaults() {
209
                unmarshalPlugins();
210
                getChbActivar().setSelected(((Extension) Launcher.getExtension(extension.getClassName())).getActive());
211
                getJTextField().setText(String.valueOf(extension.getPriority()));
212
                getJTextArea().setText(format(((Extension) Launcher.getExtension(extension.getClassName())).getDescription(),40));
213
        }
214

    
215
        public ImageIcon getIcon() {
216
                return icon;
217
        }
218

    
219
        /**
220
         * This method initializes jPanel
221
         *
222
         * @return javax.swing.JPanel
223
         */
224
        private JPanel getJPanel() {
225
                if (jPanel == null) {
226
                        jPanel = new JPanel();
227
                        jPanel.setLayout(new FlowLayout());
228
                        jPanel.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "descripcion"), TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
229
                        jPanel.add(getJScrollPane(), null);
230
                }
231
                return jPanel;
232
        }
233

    
234
        /**
235
         * This method initializes jScrollPane
236
         *
237
         * @return javax.swing.JScrollPane
238
         */
239
        private JScrollPane getJScrollPane() {
240
                if (jScrollPane == null) {
241
                        jScrollPane = new JScrollPane();
242
                        jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
243
                        jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
244
                        jScrollPane.setViewportView(getJTextArea());
245
                }
246
                return jScrollPane;
247
        }
248

    
249
        /**
250
         * This method initializes jTextArea
251
         *
252
         * @return javax.swing.JTextArea
253
         */
254
        private JTextArea getJTextArea() {
255
                if (jTextArea == null) {
256
                        jTextArea = new JTextArea();
257
                        jTextArea.setEnabled(false);
258
                        jTextArea.setText("");
259
                        jTextArea.setEditable(false);
260
                        jTextArea.setLineWrap(true);
261
                        jTextArea.setWrapStyleWord(true);
262
                        jTextArea.setPreferredSize(new Dimension(320, 170));
263
                        jTextArea.addKeyListener(myAction);
264
                }
265
                return jTextArea;
266
        }
267
        /**
268
         * This method initializes chbActivar
269
         *
270
         * @return javax.swing.JCheckBox
271
         */
272
        private JCheckBox getChbActivar() {
273
                if (chbActivar == null) {
274
                        chbActivar = new JCheckBox();
275
                        chbActivar.setSelected(true);
276
                        chbActivar.setEnabled(false);
277
                        chbActivar.setText(PluginServices.getText(this, "extension_activada"));
278
                        chbActivar.addActionListener(myAction);
279
                }
280

    
281
                return chbActivar;
282
        }
283
        /**
284
         * This method initializes jPanel1
285
         *
286
         * @return javax.swing.JPanel
287
         */
288
        private JPanel getJPanel1() {
289
                if (jPanel1 == null) {
290
                        jLabel = new JLabel();
291
                        jLabel.setText(PluginServices.getText(this, "prioridad"));
292
                        jPanel1 = new JPanel();
293
                        jPanel1.add(getChbActivar(), null);
294
                        jPanel1.add(getJTextField(), null);
295
                        jPanel1.add(jLabel, null);
296
                }
297
                return jPanel1;
298
        }
299
        /**
300
         * This method initializes jPanel1
301
         *
302
         * @return javax.swing.JPanel
303
         */
304
        private JPanel getJPanel2() {
305
                if (jPanel2 == null) {
306
                        jLabel1 = new JLabel();
307
                        jLabel1.setText(PluginServices.getText(this, "Los_cambios_efectuados_sobre_estos_valores_se_aplicaran_al_reiniciar_la_aplicacion"));
308
                        jPanel2 = new JPanel();
309
                        jPanel2.add(jLabel1, null);                        
310
                }
311
                return jPanel2;
312
        }
313

    
314
        /**
315
         * This method initializes jTextField
316
         *
317
         * @return javax.swing.JTextField
318
         */
319
        private JTextField getJTextField() {
320
                if (jTextField == null) {
321
                        jTextField = new JTextField();
322
                        jTextField.setEnabled(false);
323
                        jTextField.setPreferredSize(new Dimension(40, 20));
324
                        jTextField.addKeyListener(myAction);
325
                }
326
                return jTextField;
327
        }
328
        /**
329
     * Cuts the message text to force its lines to be shorter or equal to
330
     * lineLength.
331
     * @param message, the message.
332
     * @param lineLength, the max line length in number of characters.
333
     * @return the formated message.
334
     */
335
    private static String format(String message, int lineLength){
336
            if (message==null)
337
                 return "";
338
            if (message.length() <= lineLength) return message;
339
        String[] lines = message.split(" ");
340
        String theMessage = "";
341
        String line="";
342
        int i=0;
343
        while (i<lines.length) {
344
                while (i<lines.length && lineLength>line.length()) {
345
                        line=line.concat(lines[i]+" ");
346
                        i++;
347
                }
348
                theMessage=theMessage.concat(line+"\n");
349
                line="";
350
        }
351
        return theMessage;
352
    }
353

    
354
    private class MyAction implements ActionListener, KeyListener {
355
                public void actionPerformed(ActionEvent e) { changed = true; System.out.println("actionperformed");}
356
                public void keyPressed(KeyEvent e) { changed = true; System.out.println("keypressed"); }
357
                public void keyReleased(KeyEvent e) { changed = true; System.out.println("keyreleased");}
358
                public void keyTyped(KeyEvent e) { changed = true; System.out.println("keytyped");}
359
        }
360

    
361
        public boolean isValueChanged() {
362
                return changed;
363
        }
364

    
365
        public void setChangesApplied() {
366
                changed = false;
367
        }
368
}  //  @jve:decl-index=0:visual-constraint="10,10"