1 package net.logAnalyzer.gui.config;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.event.KeyEvent;
6 import java.io.File;
7 import java.io.IOException;
8 import java.util.Hashtable;
9
10 import javax.swing.Icon;
11 import javax.swing.JEditorPane;
12 import javax.swing.JScrollPane;
13 import javax.swing.JTabbedPane;
14
15 import net.logAnalyzer.gui.LAView;
16 import net.logAnalyzer.gui.ViewDefinition;
17 import net.logAnalyzer.resources.LAResourceBundle;
18 import net.logAnalyzer.utils.XMLUtils;
19 import net.logAnalyzer.utils.gui.GraphicsAttributes;
20 import net.logAnalyzer.utils.gui.treetable.JTreeTable;
21
22 import org.apache.log4j.Logger;
23 import org.apache.xerces.parsers.DOMParser;
24 import org.w3c.dom.Document;
25 import org.xml.sax.InputSource;
26 import org.xml.sax.SAXException;
27
28 public class XMLConfigurationView extends LAView {
29
30 /***
31 * Default configuration file.
32 */
33 private static String xmlFilename = "logAnalyzer.xml";
34 /***
35 * Class logger.
36 */
37 private static Logger logger = Logger.getLogger(XMLConfigurationView.class
38 .getName());
39
40 public XMLConfigurationView(ViewDefinition definition, Hashtable attributes) {
41 super(definition, attributes);
42 }
43 public XMLConfigurationView(ViewDefinition definition, Hashtable attributes,String xmlFilename) {
44 super(definition, attributes);
45 this.xmlFilename = xmlFilename;
46 }
47 public XMLConfigurationView(String xmlFilename) {
48 super(null, null);
49 this.xmlFilename = xmlFilename;
50 }
51
52 protected void addComponents() {
53 setLayout(new BorderLayout());
54
55 Document doc = getDocument();
56 JTreeTable treeTable = new JTreeTable(new XMLDocumentModel(doc));
57 treeTable.setSelectionBackground(GraphicsAttributes.BACKGROUND_COLOR);
58 treeTable.setGridColor(GraphicsAttributes.BACKGROUND_COLOR);
59 treeTable.setShowGrid(true);
60 treeTable.setTreeCellRenderer(new XMLTreeCellRenderer());
61
62 JTabbedPane tabbedPane = new JTabbedPane();
63 tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
64 tabbedPane.addTab("Design", getIcon(), new JScrollPane(treeTable),
65 "Design View");
66 tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
67
68 JEditorPane area;
69 try {
70 area = new JEditorPane("text/plain", XMLUtils.toString(doc));
71 } catch (IOException e) {
72 logger.error(e.getMessage());
73 area = new JEditorPane("text/plain","Error during the reading phase");
74 }
75 area.setName(this.xmlFilename);
76 area.setEditable(false);
77 tabbedPane.addTab("Source", LAResourceBundle.getIcon("XMLConfigView.icon.text"), new JScrollPane(area),
78 "Source View");
79 tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
80
81
82 add(tabbedPane, BorderLayout.CENTER);
83
84
85 }
86
87 public Icon getIcon() {
88 return LAResourceBundle.getIcon("XMLConfigView.icon");
89 }
90
91 public String getTitle() {
92 return LAResourceBundle.getLocalizedString("XMLConfigView.title");
93 }
94
95 protected void setCurrentMessage(int oldFirstIndex, int oldLastIndex, int newFirstIndex,
96 int newLastIndex) {
97
98 }
99
100 private Document getDocument(){
101 Document doc = null;
102 try {
103 File xmlFile = new File(xmlFilename);
104 if (!xmlFile.exists()) {
105 System.err.println("File not found : " + xmlFile.getAbsolutePath());
106 }
107 xmlFile = null;
108 InputSource inputSource = new InputSource(xmlFilename);
109 DOMParser xmlParser = new DOMParser();
110 xmlParser.setFeature("http://xml.org/sax/features/namespaces", true);
111 xmlParser.parse(inputSource);
112 doc = xmlParser.getDocument();
113 } catch (Exception e) {
114 logger.error(e.getMessage());
115 }
116
117 return doc;
118 }
119
120 }