Statistics
| Revision:

root / trunk / libraries / libCorePlugin / src / com / iver / core / configExtensions / ConfigPlugins.java @ 719

History | View | Annotate | Download (16.6 KB)

1
package com.iver.core.configExtensions;
2

    
3
import com.iver.andami.Launcher;
4
import com.iver.andami.PluginServices;
5
import com.iver.andami.plugins.config.generate.Extension;
6
import com.iver.andami.plugins.config.generate.Extensions;
7
import com.iver.andami.plugins.config.generate.PluginConfig;
8
import com.iver.andami.ui.mdiManager.View;
9
import com.iver.andami.ui.mdiManager.ViewInfo;
10

    
11
import com.iver.utiles.GenericFileFilter;
12

    
13
import org.exolab.castor.xml.MarshalException;
14
import org.exolab.castor.xml.ValidationException;
15

    
16
import java.awt.BorderLayout;
17
import java.awt.Component;
18

    
19
import java.io.File;
20
import java.io.FileReader;
21
import java.io.FileWriter;
22
import java.io.IOException;
23

    
24
import java.util.ArrayList;
25
import java.util.HashMap;
26
import java.util.Iterator;
27

    
28
import javax.swing.BoxLayout;
29
import javax.swing.JButton;
30
import javax.swing.JCheckBox;
31
import javax.swing.JDialog;
32
import javax.swing.JFileChooser;
33
import javax.swing.JLabel;
34
import javax.swing.JPanel;
35
import javax.swing.JScrollPane;
36
import javax.swing.JSplitPane;
37
import javax.swing.JTabbedPane;
38
import javax.swing.JTextArea;
39
import javax.swing.JTextField;
40
import javax.swing.JTree;
41
import javax.swing.tree.DefaultMutableTreeNode;
42

    
43

    
44
/**
45
 * Di?logo de Configuraci?n de ANDAMI.
46
 *
47
 * @author Vicente Caballero Navarro
48
 */
49
public class ConfigPlugins extends JPanel implements View {
50
        private JTree jTree = null;
51
        private JPanel jPanel = null; //  @jve:decl-index=0:visual-constraint="113,10"
52
        private JPanel jPanel1 = null;
53
        private JSplitPane jSplitPane = null;
54
        private JCheckBox chbActivar = null;
55
        private JTextField jTextField = null;
56
        private JPanel pGeneral = null;
57
        private JButton jButton = null;
58
        private JPanel pOpciones = null;
59
        private JTabbedPane jTabbedPane1 = null;
60
        private JPanel pDescripcion = null;
61
        private JPanel pDirectorio = null;
62
        private JPanel jPanel2 = null;
63
        private JScrollPane jScrollPane1 = null;
64
        private ViewInfo m_viewinfo = null;
65
        private JPanel pBotones = null;
66
        private JButton bAceptar = null;
67
        private JButton bCancelar = null;
68
        private HashMap listExt = new HashMap();
69
        private HashMap listPlugins = new HashMap();
70
        private JTextArea taDescripcion = null;
71
        private JScrollPane jScrollPane = null;
72
        private Extension ext = null;
73
        private JTextField jTextField1 = null;
74
        private JLabel jLabel = null;
75
        private JPanel jPanel3 = null;
76

    
77
        /**
78
         * This is the default constructor
79
         */
80
        public ConfigPlugins() {
81
                super();
82
                initialize();
83
        }
84

    
85
        /**
86
         * A?ade los nodes al arbol que representa a todos los plugins y extesiones que hay.
87
         *
88
         * @param top 
89
         */
90
        private void addNodes(DefaultMutableTreeNode top) {
91
                DefaultMutableTreeNode nameExtension = null;
92
                DefaultMutableTreeNode namePlugin = null;
93
                HashMap pc = Launcher.getPluginConfig();
94
                ArrayList array = new ArrayList();
95
                Iterator iter = pc.values().iterator();
96

    
97
                while (iter.hasNext()) {
98
                        array.add(((PluginConfig) iter.next()).getExtensions());
99
                }
100

    
101
                Extensions[] exts = (Extensions[]) array.toArray(new Extensions[0]);
102

    
103
                for (int i = 0; i < exts.length; i++) {
104
                        if (exts[i].getSkinExtension() != null) {
105
                        }
106

    
107
                        for (int j = 0; j < exts[i].getExtensionCount(); j++) {
108
                                Extension ext = exts[i].getExtension(j);
109
                                String pn = null;
110
                                String sExt = ext.getClassName().toString();
111
                                pn = sExt.substring(0, sExt.lastIndexOf("."));
112
                                namePlugin = new DefaultMutableTreeNode(pn);
113

    
114
                                if (!listPlugins.containsKey(namePlugin.getUserObject()
115
                                                                                                                   .toString())) {
116
                                        listPlugins.put(namePlugin.getUserObject().toString(),
117
                                                namePlugin);
118
                                        top.add(namePlugin);
119
                                        nameExtension = new DefaultMutableTreeNode(sExt.substring(sExt.lastIndexOf(
120
                                                                        ".") + 1, sExt.length())); //replaceFirst(namePlugin.getUserObject().toString()+".",""));
121
                                        listExt.put(sExt, ext);
122
                                        namePlugin.add(nameExtension);
123
                                } else {
124
                                        nameExtension = new DefaultMutableTreeNode(sExt.substring(sExt.lastIndexOf(
125
                                                                        ".") + 1, sExt.length())); //sExt.replaceFirst(namePlugin.getUserObject().toString()+".",""));
126
                                        listExt.put(sExt, ext);
127
                                        ((DefaultMutableTreeNode) listPlugins.get(namePlugin.getUserObject()
128
                                                                                                                                                .toString())).add(nameExtension);
129
                                }
130
                        }
131
                }
132
        }
133

    
134
        /**
135
         * This method initializes jTree
136
         *
137
         * @return javax.swing.JTree
138
         */
139
        private JTree getJTree() {
140
                if (jTree == null) {
141
                        DefaultMutableTreeNode root = new DefaultMutableTreeNode("ANDAMI");
142
                        addNodes(root);
143
                        jTree = new JTree(root);
144

    
145
                        jTree.setName("ANDAMI");
146
                        jTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
147
                                        public void valueChanged(
148
                                                javax.swing.event.TreeSelectionEvent e) {
149
                                                if (e.getPath().getParentPath() != null) {
150
                                                        ext = (Extension) listExt.get(e.getPath()
151
                                                                                                                   .getParentPath()
152
                                                                                                                   .getLastPathComponent()
153
                                                                                                                   .toString() + "." +
154
                                                                        e.getPath().getLastPathComponent().toString());
155

    
156
                                                        if (ext == null) {
157
                                                                getChbActivar().setVisible(false);
158
                                                                getTaDescripcion().setText("Plugin : " +
159
                                                                        e.getNewLeadSelectionPath().toString());
160
                                                        } else {
161
                                                                try {
162
                                                                        getChbActivar().setSelected(((Extension) Launcher.getExtension(
163
                                                                                        ext.getClassName())).getActive());
164
                                                                        getChbActivar().setVisible(true);
165
                                                                        getTaDescripcion().setText(((Extension) Launcher.getExtension(
166
                                                                                        ext.getClassName())).getDescription());
167
                                                                        getJTextField1().setText(String.valueOf(
168
                                                                                        ext.getPriority()));
169
                                                                } catch (NullPointerException npe) {
170
                                                                        getChbActivar().setVisible(false);
171
                                                                        getTaDescripcion().setText("Plugin : " +
172
                                                                                e.getNewLeadSelectionPath().toString());
173
                                                                }
174
                                                        }
175
                                                }
176
                                        }
177
                                });
178
                }
179

    
180
                return jTree;
181
        }
182

    
183
        /**
184
         * This method initializes jPanel
185
         *
186
         * @return javax.swing.JPanel
187
         */
188
        private JPanel getJPanel() {
189
                if (jPanel == null) {
190
                        jPanel = new JPanel();
191
                        jPanel.setLayout(new BorderLayout());
192
                        jPanel.setName("jPanel");
193
                        jPanel.setPreferredSize(new java.awt.Dimension(150, 150));
194
                        jPanel.setSize(435, 299);
195
                        jPanel.add(getJSplitPane(), java.awt.BorderLayout.CENTER);
196
                        jPanel.add(getPBotones(), java.awt.BorderLayout.SOUTH);
197
                }
198

    
199
                return jPanel;
200
        }
201

    
202
        /**
203
         * This method initializes jPanel1
204
         *
205
         * @return javax.swing.JPanel
206
         */
207
        private JPanel getJPanel1() {
208
                if (jPanel1 == null) {
209
                        jPanel1 = new JPanel();
210
                        jPanel1.setLayout(new BoxLayout(jPanel1, BoxLayout.Y_AXIS));
211
                        jPanel1.setName("jPanel1");
212
                        jPanel1.setPreferredSize(new java.awt.Dimension(200, 100));
213
                        jPanel1.add(getJTabbedPane1(), null);
214
                }
215

    
216
                return jPanel1;
217
        }
218

    
219
        /**
220
         * This method initializes jSplitPane
221
         *
222
         * @return javax.swing.JSplitPane
223
         */
224
        private JSplitPane getJSplitPane() {
225
                if (jSplitPane == null) {
226
                        jSplitPane = new JSplitPane();
227
                        jSplitPane.setRightComponent(getJPanel1());
228
                        jSplitPane.setLeftComponent(getJPanel2());
229
                }
230

    
231
                return jSplitPane;
232
        }
233

    
234
        /**
235
         * This method initializes chbActivar
236
         *
237
         * @return javax.swing.JCheckBox
238
         */
239
        private JCheckBox getChbActivar() {
240
                if (chbActivar == null) {
241
                        chbActivar = new JCheckBox();
242
                        chbActivar.setSelected(true);
243
                        chbActivar.setVisible(false);
244
                        chbActivar.setText(PluginServices.getText(this, "extension_activada"));
245
                        chbActivar.addActionListener(new java.awt.event.ActionListener() {
246
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
247
                                                ext.setActive(chbActivar.isSelected());
248
                                        }
249
                                });
250
                }
251

    
252
                return chbActivar;
253
        }
254

    
255
        /**
256
         * This method initializes jTextField
257
         *
258
         * @return javax.swing.JTextField
259
         */
260
        private JTextField getJTextField() {
261
                if (jTextField == null) {
262
                        jTextField = new JTextField();
263
                        jTextField.setPreferredSize(new java.awt.Dimension(200, 20));
264
                        jTextField.setText(Launcher.getPluginsDirectory());
265
                }
266

    
267
                return jTextField;
268
        }
269

    
270
        /**
271
         * This method initializes pGeneral
272
         *
273
         * @return javax.swing.JPanel
274
         */
275
        private JPanel getPGeneral() {
276
                if (pGeneral == null) {
277
                        pGeneral = new JPanel();
278
                        pGeneral.add(getPDirectorio(), null);
279
                }
280

    
281
                return pGeneral;
282
        }
283

    
284
        /**
285
         * This method initializes jButton
286
         *
287
         * @return javax.swing.JButton
288
         */
289
        private JButton getJButton() {
290
                if (jButton == null) {
291
                        jButton = new JButton();
292
                        jButton.setText(PluginServices.getText(this, "examinar"));
293
                        jButton.addActionListener(new java.awt.event.ActionListener() {
294
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
295
                                                JFileChooser jfc = new JFileChooser();
296
                                                jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
297
                                                jfc.addChoosableFileFilter(new GenericFileFilter("",
298
                                                                PluginServices.getText(this,
299
                                                                        "directorio_extensiones")));
300

    
301
                                                if (jfc.showOpenDialog(
302
                                                                        (Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
303
                                                        getJTextField().setText(jfc.getSelectedFile()
304
                                                                                                           .getAbsolutePath());
305
                                                }
306
                                        }
307
                                });
308
                }
309

    
310
                return jButton;
311
        }
312

    
313
        /**
314
         * This method initializes pOpciones
315
         *
316
         * @return javax.swing.JPanel
317
         */
318
        private JPanel getPOpciones() {
319
                if (pOpciones == null) {
320
                        jLabel = new JLabel();
321
                        pOpciones = new JPanel();
322
                        pOpciones.setLayout(new BorderLayout());
323
                        jLabel.setText(PluginServices.getText(this, "prioridad"));
324
                        pOpciones.add(getChbActivar(), java.awt.BorderLayout.NORTH);
325
                        pOpciones.add(getPDescripcion(), java.awt.BorderLayout.CENTER);
326
                        pOpciones.add(getJPanel3(), java.awt.BorderLayout.SOUTH);
327
                }
328

    
329
                return pOpciones;
330
        }
331

    
332
        /**
333
         * This method initializes jTabbedPane1
334
         *
335
         * @return javax.swing.JTabbedPane
336
         */
337
        private JTabbedPane getJTabbedPane1() {
338
                if (jTabbedPane1 == null) {
339
                        jTabbedPane1 = new JTabbedPane();
340
                        jTabbedPane1.addTab(PluginServices.getText(this, "opciones"), null,
341
                                getPOpciones(), null);
342
                        jTabbedPane1.addTab(PluginServices.getText(this, "general"), null,
343
                                getPGeneral(), null);
344
                }
345

    
346
                return jTabbedPane1;
347
        }
348

    
349
        /**
350
         * This method initializes pDescripcion
351
         *
352
         * @return javax.swing.JPanel
353
         */
354
        private JPanel getPDescripcion() {
355
                if (pDescripcion == null) {
356
                        pDescripcion = new JPanel();
357
                        pDescripcion.setLayout(new BorderLayout());
358
                        pDescripcion.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
359
                        pDescripcion.setBorder(javax.swing.BorderFactory.createTitledBorder(
360
                                        null, PluginServices.getText(this, "descripcion"),
361
                                        javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
362
                                        javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
363
                }
364

    
365
                return pDescripcion;
366
        }
367

    
368
        /**
369
         * This method initializes pDirectorio
370
         *
371
         * @return javax.swing.JPanel
372
         */
373
        private JPanel getPDirectorio() {
374
                if (pDirectorio == null) {
375
                        pDirectorio = new JPanel();
376
                        pDirectorio.setBorder(javax.swing.BorderFactory.createTitledBorder(
377
                                        null, PluginServices.getText(this, "directorio"),
378
                                        javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
379
                                        javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
380
                        pDirectorio.add(getJTextField(), null);
381
                        pDirectorio.add(getJButton(), null);
382
                }
383

    
384
                return pDirectorio;
385
        }
386

    
387
        /**
388
         * This method initializes jPanel2
389
         *
390
         * @return javax.swing.JPanel
391
         */
392
        private JPanel getJPanel2() {
393
                if (jPanel2 == null) {
394
                        jPanel2 = new JPanel();
395
                        jPanel2.setLayout(new BorderLayout());
396
                        jPanel2.setPreferredSize(new java.awt.Dimension(120, 200));
397
                        jPanel2.add(getJScrollPane1(), java.awt.BorderLayout.CENTER);
398
                }
399

    
400
                return jPanel2;
401
        }
402

    
403
        /**
404
         * This method initializes jScrollPane1
405
         *
406
         * @return javax.swing.JScrollPane
407
         */
408
        private JScrollPane getJScrollPane1() {
409
                if (jScrollPane1 == null) {
410
                        jScrollPane1 = new JScrollPane();
411
                        jScrollPane1.setViewportView(getJTree());
412
                }
413

    
414
                return jScrollPane1;
415
        }
416

    
417
        /**
418
         * This method initializes pBotones
419
         *
420
         * @return javax.swing.JPanel
421
         */
422
        private JPanel getPBotones() {
423
                if (pBotones == null) {
424
                        pBotones = new JPanel();
425
                        pBotones.add(getBAceptar(), null);
426
                        pBotones.add(getBCancelar(), null);
427
                }
428

    
429
                return pBotones;
430
        }
431

    
432
        /**
433
         * This method initializes bAceptar
434
         *
435
         * @return javax.swing.JButton
436
         */
437
        private JButton getBAceptar() {
438
                if (bAceptar == null) {
439
                        bAceptar = new JButton();
440
                        bAceptar.setText(PluginServices.getText(this, "aceptar"));
441
                        bAceptar.addActionListener(new java.awt.event.ActionListener() {
442
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
443
                                                marshalPlugins();
444
                                                Launcher.setPluginsDirectory(getJTextField().getText());
445
                                                PluginServices.getMDIManager().closeView(ConfigPlugins.this);
446
                                        }
447
                                });
448
                }
449

    
450
                return bAceptar;
451
        }
452

    
453
        /**
454
         * This method initializes bCancelar
455
         *
456
         * @return javax.swing.JButton
457
         */
458
        private JButton getBCancelar() {
459
                if (bCancelar == null) {
460
                        bCancelar = new JButton();
461
                        bCancelar.setText(PluginServices.getText(this, "cancelar"));
462
                        bCancelar.addActionListener(new java.awt.event.ActionListener() {
463
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
464
                                                unmarshalPlugins();
465
                                                PluginServices.getMDIManager().closeView(ConfigPlugins.this);
466
                                        }
467
                                });
468
                }
469

    
470
                return bCancelar;
471
        }
472

    
473
        /**
474
         * This method initializes jTextArea1
475
         *
476
         * @return javax.swing.JTextArea
477
         */
478
        private JTextArea getTaDescripcion() {
479
                if (taDescripcion == null) {
480
                        taDescripcion = new JTextArea();
481
                        taDescripcion.setPreferredSize(new java.awt.Dimension(200, 100));
482
                        taDescripcion.setText("");
483
                        taDescripcion.setEditable(false);
484
                        taDescripcion.setEnabled(false);
485
                        taDescripcion.setLineWrap(true);
486
                }
487

    
488
                return taDescripcion;
489
        }
490

    
491
        /**
492
         * This method initializes jScrollPane
493
         *
494
         * @return javax.swing.JScrollPane
495
         */
496
        private JScrollPane getJScrollPane() {
497
                if (jScrollPane == null) {
498
                        jScrollPane = new JScrollPane();
499
                        jScrollPane.setViewportView(getTaDescripcion());
500
                        jScrollPane.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
501
                        jScrollPane.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
502
                }
503

    
504
                return jScrollPane;
505
        }
506

    
507
        /**
508
         * This method initializes jTextField1
509
         *
510
         * @return javax.swing.JTextField
511
         */
512
        private JTextField getJTextField1() {
513
                if (jTextField1 == null) {
514
                        jTextField1 = new JTextField();
515
                        jTextField1.setPreferredSize(new java.awt.Dimension(20, 20));
516

    
517
                        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
518
                                        public void keyTyped(java.awt.event.KeyEvent e) {
519
                                                ext.setPriority(Integer.parseInt(jTextField1.getText()));
520
                                        }
521
                                });
522
                }
523

    
524
                return jTextField1;
525
        }
526

    
527
        /**
528
         * This method initializes jPanel3
529
         *
530
         * @return javax.swing.JPanel
531
         */
532
        private JPanel getJPanel3() {
533
                if (jPanel3 == null) {
534
                        jPanel3 = new JPanel();
535
                        jPanel3.add(getJTextField1(), null);
536
                        jPanel3.add(jLabel, null);
537
                }
538

    
539
                return jPanel3;
540
        }
541

    
542
        /**
543
         * DOCUMENT ME!
544
         *
545
         * @param args DOCUMENT ME!
546
         */
547
        public static void main(String[] args) {
548
                ConfigPlugins cp = new ConfigPlugins();
549
                cp.setVisible(true);
550

    
551
                JDialog dialog = new JDialog();
552
                dialog.getContentPane().add(cp);
553
                dialog.setSize(400, 300);
554
                dialog.show(true);
555
        }
556

    
557
        /**
558
         * This method initializes this
559
         */
560
        private void initialize() {
561
                this.setLayout(new BorderLayout());
562
                this.setSize(443, 299);
563
                this.setLocation(1, 1);
564
                this.add(getJPanel(), java.awt.BorderLayout.CENTER);
565
        }
566

    
567
        /**
568
         * @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
569
         */
570
        public ViewInfo getViewInfo() {
571
                if (m_viewinfo == null) {
572
                        m_viewinfo = new ViewInfo(ViewInfo.MODALDIALOG);
573
                        m_viewinfo.setTitle(PluginServices.getText(this, "configurar_ANDAMI"));
574
                }
575

    
576
                return m_viewinfo;
577
        }
578

    
579
        /**
580
         * @see com.iver.mdiApp.ui.MDIManager.View#viewActivated()
581
         */
582
        public void viewActivated() {
583
        }
584

    
585
        /**
586
         * Escribe sobre el config.xml, la nueva configuraci?n.
587
         */
588
        public void marshalPlugins() {
589
                HashMap pc = Launcher.getPluginConfig();
590
                ArrayList array = new ArrayList();
591
                Iterator iter = pc.keySet().iterator();
592

    
593
                while (iter.hasNext()) {
594
                        Object obj = iter.next();
595
                        PluginConfig pconfig = (PluginConfig) pc.get(obj);
596
                        FileWriter writer;
597

    
598
                        try {
599
                                writer = new FileWriter(Launcher.getPluginsDirectory() +
600
                                                File.separator + (String) obj + File.separator +
601
                                                "config.xml");
602

    
603
                                try {
604
                                        pconfig.marshal(writer);
605
                                } catch (MarshalException e1) {
606
                                        e1.printStackTrace();
607
                                } catch (ValidationException e1) {
608
                                        e1.printStackTrace();
609
                                }
610
                        } catch (IOException e) {
611
                                e.printStackTrace();
612
                        }
613

    
614
                        //hay que refrescar la aplicaci?n
615
                        ///((App)App.instance).run();
616
                }
617
        }
618

    
619
        /**
620
         * Lee del config.xml la configuraci?n.
621
         */
622
        public void unmarshalPlugins() {
623
                HashMap pc = Launcher.getPluginConfig();
624
                ArrayList array = new ArrayList();
625
                Iterator iter = pc.keySet().iterator();
626

    
627
                while (iter.hasNext()) {
628
                        Object obj = iter.next();
629
                        PluginConfig pconfig = (PluginConfig) pc.get(obj);
630

    
631
                        try {
632
                                FileReader reader = new FileReader(Launcher.getPluginsDirectory() +
633
                                                File.separator + (String) obj + File.separator +
634
                                                "config.xml");
635
                                PluginConfig.unmarshal(reader);
636
                        } catch (Exception e) {
637
                                System.out.println("Exception unmarshalPlugin " + e);
638
                        }
639
                }
640

    
641
                //hay que refrescar la aplicaci?n
642
                ///((App)App.instance).run();
643
        }
644
} //  @jve:decl-index=0:visual-constraint="10,10"