summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Kennke <roman@kennke.org>2006-12-07 14:50:40 +0000
committerRoman Kennke <roman@kennke.org>2006-12-07 14:50:40 +0000
commit34e58a8ec519d3a288be9958ee28d2bf5cae41ab (patch)
treec3a176775468f5f3d2970c3b5891b280ad2fc575
parent11e0b4379965e75fce42833a155724a8f0990151 (diff)
downloadclasspath-34e58a8ec519d3a288be9958ee28d2bf5cae41ab.tar.gz
2006-12-06 Roman Kennke <kennke@aicas.com>
* examples/gnu/classpath/examples/swing/BrowserEditorKit.java: New class. * examples/gnu/classpath/examples/swing/HtmlDemo.java (LoadActionListener): Call setPage() helper method. (createContent): Register tweaked editor kit. For FormSubmitEvents call submitForm(), otherwise setPage(). (postData): Helper method for posting form data. (setPage): Helper method for navigating to a new URL. (submitForm): Helper method for submitting a form. * examples/gnu/classpath/examples/swing/forms.html: Added text/password fields and select boxes. * examples/gnu/classpath/examples/swing/welcome.html: Fixed typo.
-rw-r--r--ChangeLog15
-rw-r--r--examples/gnu/classpath/examples/swing/BrowserEditorKit.java57
-rw-r--r--examples/gnu/classpath/examples/swing/HtmlDemo.java119
-rw-r--r--examples/gnu/classpath/examples/swing/forms.html37
-rw-r--r--examples/gnu/classpath/examples/swing/welcome.html2
5 files changed, 212 insertions, 18 deletions
diff --git a/ChangeLog b/ChangeLog
index 1080fda21..1337fccd3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,20 @@
2006-12-06 Roman Kennke <kennke@aicas.com>
+ * examples/gnu/classpath/examples/swing/BrowserEditorKit.java:
+ New class.
+ * examples/gnu/classpath/examples/swing/HtmlDemo.java
+ (LoadActionListener): Call setPage() helper method.
+ (createContent): Register tweaked editor kit. For FormSubmitEvents
+ call submitForm(), otherwise setPage().
+ (postData): Helper method for posting form data.
+ (setPage): Helper method for navigating to a new URL.
+ (submitForm): Helper method for submitting a form.
+ * examples/gnu/classpath/examples/swing/forms.html:
+ Added text/password fields and select boxes.
+ * examples/gnu/classpath/examples/swing/welcome.html: Fixed typo.
+
+2006-12-06 Roman Kennke <kennke@aicas.com>
+
* javax/swing/text/html/FormView.java
(SubmitThread.postData): Implemented.
(SubmitThread.run): Pass data to postData().
diff --git a/examples/gnu/classpath/examples/swing/BrowserEditorKit.java b/examples/gnu/classpath/examples/swing/BrowserEditorKit.java
new file mode 100644
index 000000000..f61275e57
--- /dev/null
+++ b/examples/gnu/classpath/examples/swing/BrowserEditorKit.java
@@ -0,0 +1,57 @@
+/* BrowserEditorKit.java -- A tweaked editor kit for the browser
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.classpath.examples.swing;
+
+import javax.swing.text.html.HTMLEditorKit;
+
+/**
+ * A tweaked editor kit for out browser.
+ */
+public class BrowserEditorKit
+ extends HTMLEditorKit
+{
+ public BrowserEditorKit()
+ {
+ super();
+ // Turn off automatic form submission so that we can receive notification
+ // instead and can update out location field.
+ setAutoFormSubmission(false);
+ }
+}
+
diff --git a/examples/gnu/classpath/examples/swing/HtmlDemo.java b/examples/gnu/classpath/examples/swing/HtmlDemo.java
index 2bd2da959..35a9df290 100644
--- a/examples/gnu/classpath/examples/swing/HtmlDemo.java
+++ b/examples/gnu/classpath/examples/swing/HtmlDemo.java
@@ -43,13 +43,18 @@ import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.net.MalformedURLException;
import java.net.URL;
+import java.net.URLConnection;
import java.util.LinkedList;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
+import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
@@ -59,6 +64,7 @@ import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
+import javax.swing.text.html.FormSubmitEvent;
/**
* Parses and displays HTML content.
@@ -77,11 +83,11 @@ public class HtmlDemo extends JPanel
String urlStr = url.getText();
try
{
- html.setPage(url.getText());
+ setPage(new URL(url.getText()));
}
- catch (IOException ex)
+ catch (MalformedURLException ex)
{
- System.err.println("exception while loading: " + ex);
+ // Do something more useful here.
ex.printStackTrace();
}
}
@@ -125,24 +131,24 @@ public class HtmlDemo extends JPanel
{
setLayout(new BorderLayout());
+ JEditorPane.registerEditorKitForContentType("text/html",
+ BrowserEditorKit.class.getName());
html.setEditable(false);
html.addHyperlinkListener(new HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent event)
{
- URL u = event.getURL();
- if (u != null)
+ if (event instanceof FormSubmitEvent)
{
- try
- {
- url.setText(u.toString());
- html.setPage(u.toString());
- history.addLast(u);
- }
- catch (IOException ex)
+ submitForm((FormSubmitEvent) event);
+ }
+ else
+ {
+ URL u = event.getURL();
+ if (u != null)
{
- ex.printStackTrace();
+ setPage(u);
}
}
}
@@ -269,6 +275,93 @@ public class HtmlDemo extends JPanel
}
/**
+ * Helper method to navigate to a new URL.
+ *
+ * @param u the new URL to navigate to
+ */
+ void setPage(URL u)
+ {
+ try
+ {
+ url.setText(u.toString());
+ html.setPage(u.toString());
+ history.addLast(u);
+ }
+ catch (IOException ex)
+ {
+ // Do something more useful here.
+ ex.printStackTrace();
+ }
+ }
+
+ /**
+ * Submits a form when a FormSubmitEvent is received. The HTML API
+ * provides automatic form submit but when this is enabled we don't
+ * receive any notification and can't update our location field.
+ *
+ * @param ev the form submit event
+ */
+ void submitForm(FormSubmitEvent ev)
+ {
+ URL url = ev.getURL();
+ String data = ev.getData();
+ FormSubmitEvent.MethodType method = ev.getMethod();
+ if (method == FormSubmitEvent.MethodType.POST)
+ {
+ try
+ {
+ URLConnection conn = url.openConnection();
+ postData(conn, data);
+ }
+ catch (IOException ex)
+ {
+ // Deal with this.
+ ex.printStackTrace();
+ }
+ }
+ else
+ {
+ try
+ {
+ url = new URL(url.toString() + "?" + data);
+ }
+ catch (MalformedURLException ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+ setPage(url);
+ }
+
+ /**
+ * Posts the form data for forms with HTTP POST method.
+ *
+ * @param conn the connection
+ * @param data the form data
+ */
+ private void postData(URLConnection conn, String data)
+ {
+ conn.setDoOutput(true);
+ PrintWriter out = null;
+ try
+ {
+ out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));
+ out.print(data);
+ out.flush();
+ }
+ catch (IOException ex)
+ {
+ // Deal with this!
+ ex.printStackTrace();
+ }
+ finally
+ {
+ if (out != null)
+ out.close();
+ }
+ }
+
+ /**
* Returns a DemoFactory that creates a HtmlDemo.
*
* @return a DemoFactory that creates a HtmlDemo
diff --git a/examples/gnu/classpath/examples/swing/forms.html b/examples/gnu/classpath/examples/swing/forms.html
index 2cfdb4bcc..010a94c93 100644
--- a/examples/gnu/classpath/examples/swing/forms.html
+++ b/examples/gnu/classpath/examples/swing/forms.html
@@ -41,6 +41,7 @@ exception statement from your version. -->
<title>HTML text styles</title>
</head>
<body>
+ <form>
<a href="welcome.html">Back to start page</a>
<h1>Some form elements</h1>
<h2>Textarea</h2>
@@ -49,6 +50,12 @@ exception statement from your version. -->
that has a size of 30 columns and 5 rows
</textarea>
+ <h2>Input fields</h2>
+ <p>
+ <input type="text" value="This is a normal textfield">
+ <input type="password" value="secret password">
+ </p>
+
<h2>Buttons</h2>
<p>
<input type="submit"></input>
@@ -58,12 +65,34 @@ exception statement from your version. -->
<h2>Checkboxes and Radiobuttons</h2>
<p>
- <input type="checkbox">Check this!</input>
- <input type="checkbox">Or this</input>
+ <input type="checkbox" name="2">Check this!</input>
+ <input type="checkbox" name="2">Or this</input>
+ </p>
+ <p>
+ <input type="radio" name="1">A radio button</input>
+ <input type="radio" name="1">Another radio</input>
+ </p>
+ <h2>Select lists and combo boxes</h2>
+ <p>
+ <select>
+ <option>Value1</option>
+ <option>Value2</option>
+ <option>Value3</option>
+ <option label="Labeled value 4">Value4</option>
+ <option>Value5</option>
+ <option>Value6</option>
+ </select>
</p>
<p>
- <input type="radio">A radio button</input>
- <input type="radio">Another radio</input>
+ <select size="3">
+ <option>Value1</option>
+ <option>Value2</option>
+ <option>Value3</option>
+ <option label="Labeled value 4">Value4</option>
+ <option>Value5</option>
+ <option>Value6</option>
+ </select>
</p>
+ </form>
</body>
</html> \ No newline at end of file
diff --git a/examples/gnu/classpath/examples/swing/welcome.html b/examples/gnu/classpath/examples/swing/welcome.html
index dc41183d6..8bc987494 100644
--- a/examples/gnu/classpath/examples/swing/welcome.html
+++ b/examples/gnu/classpath/examples/swing/welcome.html
@@ -55,7 +55,7 @@ exception statement from your version. -->
capabilities of GNU Classpath's Swing.</p>
<ul>
<li><a href="textstyles.html">Text styles</li>
- <li><a href="forms.html"><p>Form elements</li>
+ <li><a href="forms.html">Form elements</li>
<li><a href="tables.html">Tables</li>
<li><a href="frames.html">Frames</li>
</ul>