KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ConfigExample


1 import java.awt.BorderLayout JavaDoc;
2 import java.io.File JavaDoc;
3 import java.net.MalformedURLException JavaDoc;
4
5 import javax.swing.JPanel JavaDoc;
6 import javax.swing.JTextField JavaDoc;
7
8 import org.columba.core.facade.Facade;
9 import org.columba.core.gui.plugin.AbstractConfigPlugin;
10 import org.columba.core.xml.XmlElement;
11 import org.columba.core.xml.XmlIO;
12
13 /**
14  * @author fdietz
15  *
16  * This example demonstrates the use of the config extension point
17  *
18  */

19 public class ConfigExample extends AbstractConfigPlugin {
20
21     JTextField JavaDoc textField;
22     XmlElement parent;
23
24     XmlIO xmlFile;
25     /**
26      *
27      */

28     public ConfigExample() {
29         super();
30
31         // open configuration file
32
File JavaDoc file = Facade.getPluginConfigFile("org.columba.example.ConfigExample");
33         try {
34             xmlFile = new XmlIO(file.toURL());
35             xmlFile.load();
36             
37             parent = xmlFile.getRoot();
38         } catch (MalformedURLException JavaDoc e) {
39             e.printStackTrace();
40         }
41             
42     }
43
44     /* (non-Javadoc)
45      * @see org.columba.core.gui.plugin.AbstractConfigPlugin#updateComponents(boolean)
46      */

47     public void updateComponents(boolean b) {
48         
49
50         if (b) {
51             // model -> view
52
// read configuration and set gui elements appropriately
53

54             // navigate to treenode "text"
55
XmlElement text = parent.getElement("/config/text");
56
57             // read attribute "value"
58
String JavaDoc attribute = text.getAttribute("value");
59
60             textField.setText(attribute);
61
62         } else {
63             // view -> model
64
// write configuration given the data the user entered in the gui
65

66             XmlElement text = parent.getElement("/config/text");
67             text.addAttribute("value", textField.getText());
68             
69             try
70             {
71                 xmlFile.save();
72                 
73             } catch ( Exception JavaDoc ex)
74             {
75                 ex.printStackTrace();
76             }
77         }
78
79     }
80
81     /* (non-Javadoc)
82      * @see org.columba.core.gui.plugin.AbstractConfigPlugin#createPanel()
83      */

84     public JPanel JavaDoc createPanel() {
85
86         JPanel JavaDoc panel = new JPanel JavaDoc();
87         panel.setLayout(new BorderLayout JavaDoc());
88
89         textField = new JTextField JavaDoc(20);
90         panel.add(textField, BorderLayout.CENTER);
91
92         return panel;
93     }
94
95 }
96
Popular Tags