KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > HTMLTest


1 package org.joshy.html;
2
3 import org.joshy.html.swing.DOMInspector;
4 import javax.swing.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import org.joshy.x;
8 import org.joshy.u;
9 import org.w3c.dom.*;
10 import java.io.File JavaDoc;
11 import org.joshy.html.box.Box;
12
13 public class HTMLTest extends JFrame {
14     public static final int text_width = 600;
15     private final HTMLPanel panel = new HTMLPanel();
16
17     public HTMLTest(String JavaDoc[] args) throws Exception JavaDoc {
18         super("xhtml rendering test");
19         panel.setPreferredSize(new Dimension(text_width,text_width));
20         JScrollPane scroll = new JScrollPane(panel);
21         scroll.setVerticalScrollBarPolicy(scroll.VERTICAL_SCROLLBAR_ALWAYS);
22         scroll.setHorizontalScrollBarPolicy(scroll.HORIZONTAL_SCROLLBAR_ALWAYS);
23         scroll.setPreferredSize(new Dimension(text_width,text_width));
24         panel.setViewportComponent(scroll);
25         panel.setJScrollPane(scroll);
26         panel.addMouseListener(new ClickMouseListener(panel));
27
28         if(args.length > 0) {
29             File JavaDoc file = new File JavaDoc(args[0]);
30             panel.setDocument(x.loadDocument(args[0]),file.toURL());
31         }
32
33         getContentPane().add("Center",scroll);
34
35
36         JMenuBar mb = new JMenuBar();
37         JMenu file = new JMenu("File");
38         mb.add(file);
39         file.add(new AbstractAction("Quit") {
40             public void actionPerformed(ActionEvent evt) {
41                 System.exit(0);
42             }
43         });
44
45         JMenu test = new JMenu("Test");
46         mb.add(test);
47
48         addFileLoadAction(test, "background colors and images", "demos/background.xhtml");
49         addFileLoadAction(test, "borders", "demos/border.xhtml");
50         addFileLoadAction(test, "box sizing", "demos/box-sizing.xhtml");
51         addFileLoadAction(test, "mixed test 1", "demos/content.xhtml");
52         addFileLoadAction(test, "line breaking", "demos/breaking.xhtml");
53         addFileLoadAction(test, "headers", "demos/header.xhtml");
54         addFileLoadAction(test, "inline image", "demos/image.xhtml");
55         addFileLoadAction(test, "list ", "demos/list.xhtml");
56         addFileLoadAction(test, "nesting", "demos/nested.xhtml");
57         addFileLoadAction(test, "general styled text", "demos/paragraph.xhtml");
58         addFileLoadAction(test, "CSS selectors", "demos/selectors.xhtml");
59         addFileLoadAction(test, "table", "demos/table.xhtml");
60         addFileLoadAction(test, "text alignment", "demos/text-alignment.xhtml");
61         addFileLoadAction(test, "whitespace handling", "demos/whitespace.xhtml");
62         addFileLoadAction(test, "itunes email", "demos/itunes/itunes1.xhtml");
63         addFileLoadAction(test, "follow links", "demos/link.xhtml");
64         addFileLoadAction(test, "Hamlet (slow!)", "demos/hamlet.xhtml");
65
66
67         JMenu debug = new JMenu("Debug");
68         mb.add(debug);
69         debug.add(new AbstractAction("draw boxes") {
70             public void actionPerformed(ActionEvent evt) {
71                 panel.c.debug_draw_boxes = !panel.c.debug_draw_boxes;
72                 panel.repaint();
73             }
74         });
75         debug.add(new AbstractAction("draw line boxes") {
76             public void actionPerformed(ActionEvent evt) {
77                 panel.c.debug_draw_line_boxes = !panel.c.debug_draw_line_boxes;
78                 panel.repaint();
79             }
80         });
81         debug.add(new AbstractAction("draw inline boxes") {
82             public void actionPerformed(ActionEvent evt) {
83                 panel.c.debug_draw_inline_boxes = !panel.c.debug_draw_inline_boxes;
84                 panel.repaint();
85             }
86         });
87         debug.add(new AbstractAction("DOM tree inspector") {
88             public void actionPerformed(ActionEvent evt) {
89                 JFrame frame = new JFrame();
90                 frame.getContentPane().add(new DOMInspector(panel.doc));
91                 frame.pack();
92                 frame.setSize(text_width,600);
93                 frame.show();
94             }
95         });
96         debug.add(new AbstractAction("Print Box Tree") {
97             public void actionPerformed(ActionEvent evt) {
98                 panel.printTree();
99             }
100         });
101
102
103         setJMenuBar(mb);
104
105     }
106
107     public void addFileLoadAction(JMenu menu, String JavaDoc display, final String JavaDoc file) {
108         menu.add(new AbstractAction(display) {
109             public void actionPerformed(ActionEvent evt) {
110                 try {
111                     panel.setDocument(file);
112                 } catch (Exception JavaDoc ex) {
113                     u.p(ex);
114                 }
115                 panel.repaint();
116             }
117         });
118     }
119
120
121     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
122
123         final JFrame frame = new HTMLTest(args);
124         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
125         frame.pack();
126         frame.setSize(text_width,300);
127         frame.show();
128         /*
129         new Thread(new Runnable() {
130             public void run() {
131                 for(int i=0; i<100; i=i+1) {
132                     u.sleep(100);
133                     frame.resize(131-i,200);
134                     System.out.println("blah = " + (131-i));
135                     frame.validate();
136                     frame.invalidate();
137                     frame.repaint();
138                 }
139             }
140         }).start();
141         */

142     }
143 }
144
145 class ClickMouseListener extends MouseAdapter {
146     HTMLPanel panel;
147
148     public ClickMouseListener(HTMLPanel panel) {
149         this.panel = panel;
150     }
151
152     public void mousePressed(MouseEvent evt) {
153         Box box = panel.findBox(evt.getX(),evt.getY());
154         if(box == null) return;
155         u.p("pressed " + box);
156         if(box.node != null) {
157             Node node = box.node;
158             if(node.getNodeType() == node.TEXT_NODE) {
159                 node = node.getParentNode();
160             }
161
162             if(node.getNodeName().equals("a")) {
163                 u.p("clicked on a link");
164                 box.clicked = true;
165                 box.color = new Color(255,255,0);
166                 panel.repaint();
167             }
168
169         }
170     }
171     public void mouseReleased(MouseEvent evt) {
172         Box box = panel.findBox(evt.getX(),evt.getY());
173         if(box == null) return;
174         u.p("pressed " + box);
175         if(box.node != null) {
176             Node node = box.node;
177             if(node.getNodeType() == node.TEXT_NODE) {
178                 node = node.getParentNode();
179             }
180
181             if(node.getNodeName().equals("a")) {
182                 u.p("clicked on a link");
183                 box.clicked = true;
184                 box.color = new Color(255,0,0);
185                 panel.repaint();
186                 followLink((Element)node);
187             }
188
189         }
190     }
191
192     private void followLink(final Element elem) {
193         try {
194             if(elem.hasAttribute("href")) {
195                 panel.setDocumentRelative(elem.getAttribute("href"));
196             }
197         } catch (Exception JavaDoc ex) {
198             u.p(ex);
199         }
200     }
201
202 }
203
204
205
206
207
208
209
210
Popular Tags