This initially inserts some HTML content in the HtmlDemo that can
already be rendered. It also adds the HTML demo to the Swing activity
board and removes some cruft in the Swing demo.
2006-08-24 Roman Kennke <[EMAIL PROTECTED]>
* examples/gnu/classpath/examples/swing/Demo.java
(LaterMain.run): Removed unused local variable.
(Demo): Don't put desktop in scrollpane.
(addChildren): Removed unused method.
(mkButtonBar): Added HTML demo.
(mkMenuBar): Added HTML demo.
(mkPanel): Removed unused method.
(mkScrollPane): Removed unused method.
(mkTree): Removed unused method.
(valign2str): Removed unused method.
* examples/gnu/classpath/examples/swing/HtmlDemo.java:
Initialize text field with some HTML that already works.
(DEBUG): New field. Set to true for debugging output.
(createContent): Dump element tree after parsing.
/Roman
Index: examples/gnu/classpath/examples/swing/Demo.java
===================================================================
RCS file: /cvsroot/classpath/classpath/examples/gnu/classpath/examples/swing/Demo.java,v
retrieving revision 1.49
diff -u -1 -2 -r1.49 Demo.java
--- examples/gnu/classpath/examples/swing/Demo.java 25 Jul 2006 10:49:08 -0000 1.49
+++ examples/gnu/classpath/examples/swing/Demo.java 25 Aug 2006 11:59:22 -0000
@@ -19,25 +19,24 @@
02110-1301 USA.
*/
package gnu.classpath.examples.swing;
import gnu.classpath.examples.java2d.JNIOverhead;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
-import javax.swing.tree.*;
import javax.swing.plaf.basic.BasicLookAndFeel;
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.MetalTheme;
import javax.swing.plaf.metal.OceanTheme;
import java.lang.reflect.Method;
import java.net.URL;
public class Demo
{
@@ -152,24 +151,26 @@
examples.add(new JMenuItem(new PopupAction("Tree",
TreeDemo.createDemoFactory())));
examples.add(new JMenuItem(new PopupAction("Theme Editor",
MetalThemeEditor.createDemoFactory())));
examples.add(new JMenuItem(new PopupAction("DocumentFilter",
DocumentFilterDemo.createDemoFactory())));
examples.add(new JMenuItem(new PopupAction("NavigationFilter",
NavigationFilterDemo.createDemoFactory())));
examples.add(new JMenuItem(new PopupAction("JNI Overhead",
JNIOverhead.createDemoFactory())));
+ examples.add(new JMenuItem(new PopupAction("HTML Demo",
+ HtmlDemo.createDemoFactory())));
final JMenuItem vmMenu;
help.add(new JMenuItem("just play with the widgets"));
help.add(new JMenuItem("and enjoy the sensation of"));
help.add(new JMenuItem("your neural connections growing"));
help.add(new JSeparator());
help.add(vmMenu = new JMenuItem("Really, which VM is this running on?"));
vmMenu.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
@@ -285,39 +286,24 @@
bar.add(b);
b = mkButton(stockIcon("go-down"));
triggerDialog(b, "down");
bar.add(b);
b = mkButton(stockIcon("go-forward"));
triggerDialog(b, "forward");
bar.add(b);
return bar;
}
- private static String valign2str(int a)
- {
- switch (a)
- {
- case SwingConstants.CENTER:
- return "Center";
- case SwingConstants.TOP:
- return "Top";
- case SwingConstants.BOTTOM:
- return "Bottom";
- default:
- return "Unknown";
- }
- }
-
static String halign2str(int a)
{
switch (a)
{
case SwingConstants.CENTER:
return "Center";
case SwingConstants.RIGHT:
return "Right";
case SwingConstants.LEFT:
return "Left";
default:
return "Unknown";
@@ -345,87 +331,63 @@
}
static JButton mkButton(String title)
{
return mkButton(title, null, -1, -1, -1, -1);
}
static JButton mkButton(Icon i)
{
return mkButton(null, i, -1, -1, -1, -1);
}
-
- private static JScrollPane mkScrollPane(JComponent inner)
- {
- JScrollPane jsp;
- jsp = new JScrollPane(inner,
- JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
- JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
-
- return jsp;
- }
-
public Demo()
{
frame = new JFrame("Swing Activity Board");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(mkMenuBar());
JComponent component = (JComponent) frame.getContentPane();
component.setLayout(new BorderLayout());
component.add(mkToolBar(), BorderLayout.NORTH);
JPanel main = new JPanel();
main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
desktop = createDesktop();
-
- // Put the desktop in a scrollpane. The scrollbars may show then
- // up when the them or LaF is changed.
- main.add(new JScrollPane(desktop));
+ main.add(desktop);
main.add(mkButtonBar());
component.add(main, BorderLayout.CENTER);
frame.pack();
frame.show();
}
public static class LaterMain
implements Runnable
{
public void run()
{
- Demo demo = new Demo();
+ new Demo();
}
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new LaterMain());
}
private static JButton mkBigButton(String title)
{
JButton b = new JButton(title);
b.setMargin(new Insets(5,5,5,5));
return b;
}
- private static JPanel mkPanel(JComponent[] inners)
- {
- JPanel p = new JPanel();
- for (int i = 0; i < inners.length; ++i)
- {
- p.add(inners[i]);
- }
- return p;
- }
-
static JButton mkDisposerButton(final JFrame c)
{
JButton close = mkBigButton("Close");
close.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
c.dispose();
}
});
return close;
}
@@ -470,70 +432,24 @@
JInternalFrame frame = new JInternalFrame((String) getValue(NAME));
frame.setClosable(true);
frame.setIconifiable(true);
frame.setMaximizable(true);
frame.setResizable(true);
frame.setContentPane(demoFactory.createDemo());
frame.pack();
desktop.add(frame);
frame.setVisible(true);
}
}
- /**
- * Create the tree.
- *
- * @return thr scroll pane, containing the tree.
- */
- private static JComponent mkTree()
- {
- DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root node");
-
- addChildren("Node", root, 12);
-
- JTree tree = new JTree(root);
- tree.setLargeModel(true);
- DefaultTreeSelectionModel dtsm = new DefaultTreeSelectionModel();
- dtsm.setSelectionMode(DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
- tree.setSelectionModel(dtsm);
-
- // Make it editable.
- tree.setEditable(true);
-
- JComponent t = mkScrollPane(tree);
- t.setPreferredSize(new Dimension(200,200));
- return t;
- }
-
- /**
- * Add the specified number of children to this parent node. For each
- * child, the method is called recursively adding the nChildren-3 number of
- * grandchildren.
- *
- * @param parent the parent node
- * @param nChildren the number of children
- */
- private static void addChildren(String name, DefaultMutableTreeNode parent,
- int nChildren)
- {
- for (int i = 0; i < nChildren; i++)
- {
- String child_name = parent+"."+i;
- DefaultMutableTreeNode child = new DefaultMutableTreeNode
- (child_name);
- parent.add(child);
- addChildren(child_name, child, nChildren-3);
- }
- }
-
private JPanel mkButtonBar()
{
JPanel panel = new JPanel(new GridLayout(3, 1, 5, 5));
panel.add(new JButton(new PopupAction("Buttons",
ButtonDemo.createDemoFactory())));
panel.add(new JButton(new PopupAction("Slider",
SliderDemo.createDemoFactory())));
panel.add(new JButton(new PopupAction("ProgressBar",
ProgressBarDemo.createDemoFactory())));
panel.add(new JButton(new PopupAction("ScrollBar",
ScrollBarDemo.createDemoFactory())));
panel.add(new JButton(new PopupAction("Spinner",
@@ -549,24 +465,26 @@
panel.add(new JButton(new PopupAction("Table",
TableDemo.createDemoFactory())));
panel.add(new JButton(new PopupAction("List",
ListDemo.createDemoFactory())));
panel.add(new JButton(new PopupAction("TabbedPane",
TabbedPaneDemo.createDemoFactory())));
panel.add(new JButton(new PopupAction("Tree",
TreeDemo.createDemoFactory())));
panel.add(new JButton(new PopupAction("Theme Editor",
MetalThemeEditor.createDemoFactory())));
panel.add(new JButton(new PopupAction("JNI Overhead",
JNIOverhead.createDemoFactory())));
+ panel.add(new JButton(new PopupAction("HTML",
+ HtmlDemo.createDemoFactory())));
JButton exitDisposer = mkDisposerButton(frame);
panel.add(exitDisposer);
panel.setMaximumSize(new Dimension(Integer.MAX_VALUE,
panel.getPreferredSize().height));
exitDisposer.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(1);
}
Index: examples/gnu/classpath/examples/swing/HtmlDemo.java
===================================================================
RCS file: /cvsroot/classpath/classpath/examples/gnu/classpath/examples/swing/HtmlDemo.java,v
retrieving revision 1.4
diff -u -1 -2 -r1.4 HtmlDemo.java
--- examples/gnu/classpath/examples/swing/HtmlDemo.java 16 Jul 2006 15:03:10 -0000 1.4
+++ examples/gnu/classpath/examples/swing/HtmlDemo.java 25 Aug 2006 11:59:22 -0000
@@ -43,44 +43,77 @@
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
+import javax.swing.text.AbstractDocument;
import javax.swing.text.Element;
import javax.swing.text.html.HTMLDocument;
/**
* Parses and displays HTML content.
*
* @author Audrius Meskauskas ([EMAIL PROTECTED])
*/
public class HtmlDemo extends JPanel
{
-
+
+ /**
+ * Setting this to true causes the parsed element structure to be dumped.
+ */
+ private static final boolean DEBUG = true;
+
JTextPane html = new JTextPane();
- JTextArea text = new JTextArea("<html><body>" +
- "123456789HR!<hr>987654321"+
- "123456789BR!<br>987654321"+
- "<p id='insertHere'>Insertion target</p><p>"+
- "<font color=red>ma</font>"+
- "<sup>sup</sup>normal<sub>sub</sub>normal</p><p>Table:"+
- "<table><tr>a<td>b<td>c<tr>x<td>y<td>z</table></body></html>");
+ JTextArea text = new JTextArea("<html><body>\n"
+
+ + "<h1>H1 Headline</h1>\n"
+ + "<h2>H2 Headline</h2>\n"
+ + "<h3>H3 Headline</h3>\n"
+ + "<h4>H4 Headline</h3>\n"
+ + "<h5>H5 Headline</h5>\n"
+ + "<h6>H6 Headline</h6>\n"
+ + "<h1>CSS colors via font tag</h1>\n"
+ + "<p>"
+ + "<font color=\"maroon\">maroon</font>\n"
+ + "<font color=\"red\">red</font>\n"
+ + "<font color=\"orange\">orange</font>\n"
+ + "<font color=\"yellow\">yellow</font>\n"
+ + "<font color=\"olive\">olive</font>\n"
+ + "<font color=\"purple\">purlpe</font>\n"
+ + "<font color=\"fuchsia\">fuchsia</font>\n"
+ + "<font color=\"white\">white</font>\n"
+ + "<font color=\"lime\">lime</font>\n"
+ + "<font color=\"green\">green</font>\n"
+ + "<font color=\"navy\">navy</font>\n"
+ + "<font color=\"blue\">blue</font>\n"
+ + "<font color=\"aqua\">aqua</font>\n"
+ + "<font color=\"teal\">teal</font>\n"
+ + "<font color=\"black\">black</font>\n"
+ + "<font color=\"silver\">silver</font>\n"
+ + "<font color=\"gray\">gray</font>\n"
+ + "</p>"
+ + "<h1>Some HTML formatting tags</h1>\n"
+ + "<p>Normal <b>Bold</b> <i>Italic</i> <b><i>Bold + Italic</i></b></p>\n"
+ + "<p><big>Big</big> <em>Emphasized</em> <small>Small</small>\n"
+ + "<strike>Strike</strike> <strong>Strong</strong> <u>Underline</u></p>\n"
+ + "<p>Normal vs <sup>Superscript</sup> vs <sub>Subscript</sub> text</p>\n"
+ + "</body></html>\n");
JPanel buttons;
int n;
public HtmlDemo()
{
super();
html.setContentType("text/html"); // not now.
createContent();
}
@@ -102,25 +135,27 @@
center.add(new JScrollPane(html));
buttons = new JPanel();
JButton parse = new JButton("parse");
parse.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String t = text.getText();
System.out.println("HtmlDemo.java.createContent:Parsing started");
html.setText(t);
- System.out.println("HtmlDemo.java.createContent:Parsing completed");
+ System.out.println("HtmlDemo.java.createContent:Parsing completed");
+ if (DEBUG)
+ ((AbstractDocument) html.getDocument()).dump(System.out);
}
});
buttons.add(parse);
JButton insertBeforeEnd = new JButton("before end");
insertBeforeEnd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
HTMLDocument doc = (HTMLDocument) html.getDocument();
Element el = doc.getElement("insertHere");