KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > browser > StyleBrowser


1 /*
2  * StyleBrowser.java
3  *
4  * Steady State CSS2 Parser
5  *
6  * Copyright (C) 1999, 2002 Steady State Software Ltd. All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  * To contact the authors of the library, write to Steady State Software Ltd.,
23  * 49 Littleworth, Wing, Buckinghamshire, LU7 0JX, England
24  *
25  * http://www.steadystate.com/css/
26  * mailto:css@steadystate.co.uk
27  *
28  * $Id: StyleBrowser.java,v 1.1.1.1 2003/12/28 21:22:36 davidsch Exp $
29  */

30
31 package browser;
32
33 import java.awt.*;
34 import java.awt.event.*;
35 import java.io.*;
36 import javax.swing.*;
37 import javax.swing.tree.*;
38 import com.steadystate.css.parser.CSSOMParser;
39 import org.w3c.dom.css.*;
40 import org.w3c.css.sac.CSSException;
41 import org.w3c.css.sac.InputSource;
42
43 /**
44  * @author David Schweinsberg
45  * @version $Release$
46  */

47 public class StyleBrowser {
48
49     private JFrame _frame;
50     private JTree _tree;
51     private DefaultTreeModel _treeModel;
52
53     public StyleBrowser() {
54         _frame = new JFrame("Style Browser");
55
56         JPanel panel = new JPanel(true);
57         _frame.getContentPane().add("Center", panel);
58
59         _frame.setJMenuBar(createMenuBar());
60         //_frame.setBackground(Color.lightGray);
61

62         _tree = new JTree((TreeModel)null);
63
64         // Enable tool tips for the tree, without this tool tips will not
65
// be picked up
66
ToolTipManager.sharedInstance().registerComponent(_tree);
67
68         // Make the tree use an instance of StyleBrowserCellRenderer for
69
// drawing
70
_tree.setCellRenderer(new StyleBrowserCellRenderer());
71
72         // Put the Tree in a scroller
73
JScrollPane sp = new JScrollPane();
74         sp.setPreferredSize(new Dimension(500, 500));
75         sp.getViewport().add(_tree);
76
77         // Show it
78
panel.setLayout(new BorderLayout());
79         panel.add("Center", sp);
80
81         _frame.addWindowListener(
82                 new WindowAdapter() {
83                     public void windowClosing(WindowEvent e) {
84                         System.exit(0);
85                     }
86                 }
87         );
88
89         _frame.pack();
90         _frame.show();
91     }
92
93     private JMenuBar createMenuBar() {
94         JMenu menu;
95         JMenuBar menuBar = new JMenuBar();
96         JMenuItem menuItem;
97
98         menu = new JMenu("File");
99         menuBar.add(menu);
100
101         menuItem = menu.add(new JMenuItem("Open..."));
102         menuItem.addActionListener(
103                 new ActionListener() {
104                     public void actionPerformed(ActionEvent e) {
105                         openStyleSheet();
106                     }
107                 }
108         );
109
110         menuItem = menu.add(new JMenuItem("Exit"));
111         menuItem.addActionListener(
112                 new ActionListener() {
113                     public void actionPerformed(ActionEvent e) {
114                         System.exit(0);
115                     }
116                 }
117         );
118
119         menu = new JMenu("Help");
120         menuBar.add(menu);
121
122         menuItem = menu.add(new JMenuItem("About"));
123         menuItem.addActionListener(
124                 new ActionListener() {
125                     public void actionPerformed(ActionEvent e) {
126                         showAbout();
127                     }
128                 }
129         );
130
131         return menuBar;
132     }
133
134     private void loadStyleSheet(String JavaDoc pathName) {
135         try {
136             _frame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
137
138             // Parse the style sheet
139
// (the old way)
140
// InputStream is = new FileInputStream(pathName);
141
// CSS2Parser parser = new CSS2Parser(is);
142
// CSSStyleSheet stylesheet = parser.styleSheet();
143

144             // (the new way)
145
Reader r = new FileReader(pathName);
146             CSSOMParser parser = new CSSOMParser();
147             InputSource is = new InputSource(r);
148             CSSStyleSheet stylesheet = parser.parseStyleSheet(is);
149
150             // Create the tree to put the information in
151
_treeModel = (DefaultTreeModel)
152             StyleBrowserTreeBuilder.createStyleSheetTree(stylesheet);
153
154             _tree.setModel(_treeModel);
155
156             _frame.setTitle("Style Browser - " + pathName);
157         }
158         catch(CSSException e) {
159             JOptionPane.showMessageDialog(
160             null,
161             e.getMessage(),
162             "CSS Exception",
163             JOptionPane.ERROR_MESSAGE);
164         }
165         catch(FileNotFoundException e) {
166             JOptionPane.showMessageDialog(
167             null,
168             e.getMessage(),
169             "File Not Found",
170             JOptionPane.ERROR_MESSAGE);
171         }
172         catch(IOException e) {
173             JOptionPane.showMessageDialog(
174             null,
175             e.getMessage(),
176             "I/O Exception",
177             JOptionPane.ERROR_MESSAGE);
178         }
179         finally {
180             _frame.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
181         }
182     }
183
184     private void openStyleSheet() {
185         JFileChooser chooser = new JFileChooser();
186
187         ExampleFileFilter filter = new ExampleFileFilter();
188         filter.addExtension("css");
189         filter.setDescription("Cascading Style Sheets");
190
191         chooser.setFileFilter(filter);
192
193         if (chooser.showOpenDialog(_frame) == JFileChooser.APPROVE_OPTION)
194         loadStyleSheet(chooser.getSelectedFile().getPath());
195     }
196
197     private void showAbout() {
198         JOptionPane.showMessageDialog(
199         null,
200         "DOM CSS Style Browser\n" +
201         "Copyright (C) 1999, 2002 Steady State Software Ltd.\n" +
202         "http://www.steadystate.com/css/",
203         "About Style Browser",
204         JOptionPane.INFORMATION_MESSAGE);
205     }
206
207     static public void main(String JavaDoc[] args) {
208         new StyleBrowser();
209     }
210 }
211
Popular Tags