001: /* *************************************************************************
002:
003: Millstone(TM)
004: Open Sourced User Interface Library for
005: Internet Development with Java
006:
007: Millstone is a registered trademark of IT Mill Ltd
008: Copyright (C) 2000-2005 IT Mill Ltd
009:
010: *************************************************************************
011:
012: This library is free software; you can redistribute it and/or
013: modify it under the terms of the GNU Lesser General Public
014: license version 2.1 as published by the Free Software Foundation.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: *************************************************************************
026:
027: For more information, contact:
028:
029: IT Mill Ltd phone: +358 2 4802 7180
030: Ruukinkatu 2-4 fax: +358 2 4802 7181
031: 20540, Turku email: info@itmill.com
032: Finland company www: www.itmill.com
033:
034: Primary source for MillStone information and releases: www.millstone.org
035:
036: ********************************************************************** */
037:
038: package org.millstone.examples.features;
039:
040: import java.net.URL;
041: import java.util.Iterator;
042: import java.util.Map;
043:
044: import org.millstone.base.terminal.DownloadStream;
045: import org.millstone.base.terminal.ExternalResource;
046: import org.millstone.base.terminal.ParameterHandler;
047: import org.millstone.base.terminal.URIHandler;
048: import org.millstone.base.ui.*;
049:
050: public class FeatureParameters extends Feature implements URIHandler,
051: ParameterHandler {
052:
053: private Label context = new Label();
054: private Label relative = new Label();
055: private Table params = new Table();
056:
057: public FeatureParameters() {
058: super ();
059: params.addContainerProperty("Values", String.class, "");
060: }
061:
062: protected Component getDemoComponent() {
063:
064: OrderedLayout l = new OrderedLayout();
065:
066: Label info = new Label(
067: "To test this feature, try to "
068: + "add some get parameters to URL. For example if you have "
069: + "the feature browser installed in your local host, try url: ");
070: info.setCaption("Usage info");
071: l.addComponent(info);
072: try {
073: URL u1 = new URL(getApplication().getURL(),
074: "test/uri?test=1&test=2");
075: URL u2 = new URL(getApplication().getURL(),
076: "foo/bar?mary=john&count=3");
077:
078: l.addComponent(new Link(u1.toString(),
079: new ExternalResource(u1)));
080: l.addComponent(new Label("Or this: "));
081: l.addComponent(new Link(u2.toString(),
082: new ExternalResource(u2)));
083: } catch (Exception e) {
084: System.out
085: .println("Couldn't get hostname for this machine: "
086: + e.toString());
087: e.printStackTrace();
088: }
089:
090: // URI
091: Panel p1 = new Panel("URI Handler");
092: context.setCaption("Last URI handler context");
093: p1.addComponent(context);
094: relative.setCaption("Last relative URI");
095: p1.addComponent(relative);
096: l.addComponent(p1);
097:
098: // Parameters
099: Panel p2 = new Panel("Parameter Handler");
100: params.setCaption("Last parameters");
101: params.setColumnHeaderMode(Table.COLUMN_HEADER_MODE_ID);
102: params.setRowHeaderMode(Table.ROW_HEADER_MODE_ID);
103: p2.addComponent(params);
104: l.addComponent(p2);
105:
106: return l;
107: }
108:
109: protected String getDescriptionXHTML() {
110: return "This is a demonstration of how URL parameters can be recieved and handled."
111: + "Parameters and URL:s can be received trough the windows by registering "
112: + "URIHandler and ParameterHandler classes window.";
113: }
114:
115: protected String getImage() {
116: return "parameters.jpg";
117: }
118:
119: protected String getTitle() {
120: return "Parameters";
121: }
122:
123: /** Add URI and parametes handlers to window.
124: * @see org.millstone.base.ui.Component#attach()
125: */
126: public void attach() {
127: super .attach();
128: getWindow().addURIHandler(this );
129: getWindow().addParameterHandler(this );
130: }
131:
132: /** Remove all handlers from window
133: * @see org.millstone.base.ui.Component#detach()
134: */
135: public void detach() {
136: super .detach();
137: getWindow().removeURIHandler(this );
138: getWindow().removeParameterHandler(this );
139: }
140:
141: /** Update URI
142: * @see org.millstone.base.terminal.URIHandler#handleURI(URL, String)
143: */
144: public DownloadStream handleURI(URL context, String relativeUri) {
145: this .context.setValue(context.toString());
146: this .relative.setValue(relativeUri);
147: return null;
148: }
149:
150: /** Update parameters table
151: * @see org.millstone.base.terminal.ParameterHandler#handleParameters(Map)
152: */
153: public void handleParameters(Map parameters) {
154: params.removeAllItems();
155: for (Iterator i = parameters.keySet().iterator(); i.hasNext();) {
156: String name = (String) i.next();
157: String[] values = (String[]) parameters.get(name);
158: String v = "";
159: for (int j = 0; j < values.length; j++) {
160: if (v.length() > 0)
161: v += ", ";
162: v += "'" + values[j] + "'";
163: }
164: params.addItem(new Object[] { v }, name);
165: }
166: }
167: }
168:
169: /* This Millstone sample code is public domain. *
170: * For more information see www.millstone.org. */
|