001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Pavel Dolgov
019: * @version $Revision: 1.3 $
020: */package org.apache.harmony.applet;
021:
022: import java.awt.*;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.awt.event.WindowAdapter;
026: import java.awt.event.WindowEvent;
027: import java.net.MalformedURLException;
028: import java.net.URL;
029: import java.util.HashMap;
030:
031: /**
032: * Standalone applet launcher
033: */
034: public final class Launcher implements Callback {
035:
036: private final Factory factory;
037:
038: private final Frame frame;
039: private final Panel placeholder;
040: private final Label status;
041:
042: private final int appletId = 1;
043: private final int documentId = 2;
044:
045: private URL codeBase;
046: private String className;
047:
048: private Launcher() {
049: frame = createFrame();
050: placeholder = (Panel) frame.getComponent(1);
051: status = (Label) ((Container) frame.getComponent(2))
052: .getComponent(0);
053:
054: factory = new Factory(this );
055: }
056:
057: public static void main(String[] args) {
058:
059: if (args.length < 2) {
060: System.err
061: .println("Two parameters required: <code_base_url> <class_name>");
062: return;
063: }
064:
065: Launcher launcher = new Launcher();
066: try {
067: launcher.show(new URL(args[0]), args[1]);
068: } catch (MalformedURLException e) {
069: e.printStackTrace();
070: }
071: }
072:
073: public void showDocument(int documentId, URL url, String target) {
074: System.err.println("showDocument " + url + " " + target);
075: }
076:
077: public void showStatus(int documentId, String status) {
078: this .status.setText(status);
079: }
080:
081: public void appletResize(int appletId, int width, int height) {
082: if (appletId != this .appletId) {
083: return;
084: }
085:
086: int dw = width - placeholder.getWidth();
087: int dh = height - placeholder.getHeight();
088:
089: frame.setSize(frame.getWidth() + dw, frame.getHeight() + dh);
090: frame.invalidate();
091: frame.validate();
092: }
093:
094: private Frame createFrame() {
095: Frame f = new Frame("Applet viewer");
096: f.setSize(500, 400);
097: f.setLayout(new ThreeTierLayout());
098: f.setBackground(SystemColor.control);
099:
100: Panel panel = new Panel();
101: f.add(panel, "north");
102:
103: Button button = new Button(" Start ");
104: button.addActionListener(new ActionListener() {
105: public void actionPerformed(ActionEvent e) {
106: factory.start(appletId);
107: }
108: });
109: panel.add(button);
110:
111: button = new Button(" Stop ");
112: button.addActionListener(new ActionListener() {
113: public void actionPerformed(ActionEvent e) {
114: factory.stop(appletId);
115: }
116: });
117: panel.add(button);
118:
119: button = new Button(" Restart ");
120: button.addActionListener(new ActionListener() {
121: public void actionPerformed(ActionEvent e) {
122: factory.stop(appletId);
123: factory.destroy(appletId);
124: factory.init(appletId);
125: factory.start(appletId);
126: }
127: });
128: panel.add(button);
129:
130: button = new Button(" Reload ");
131: button.addActionListener(new ActionListener() {
132: public void actionPerformed(ActionEvent e) {
133: Launcher.this .hide();
134: Launcher.this .show();
135: }
136: });
137: panel.add(button);
138:
139: button = new Button(" Exit ");
140: button.addActionListener(new ActionListener() {
141: public void actionPerformed(ActionEvent e) {
142: Launcher.this .hide();
143: frame.dispose();
144: }
145: });
146: panel.add(button);
147:
148: Panel place = new Panel();
149: place.setName("placeholder");
150: place.setLayout(new BorderLayout());
151: place.setVisible(false);
152: f.add(place, "center");
153:
154: Label status = new Label("status");
155: status.setName("status");
156:
157: Container statusBar = new SunkenBar(3, 3);
158: statusBar.add(status);
159: f.add(statusBar, "south");
160:
161: f.addWindowListener(new WindowAdapter() {
162: @Override
163: public void windowClosing(WindowEvent e) {
164: Launcher.this .hide();
165: e.getWindow().dispose();
166: }
167: });
168:
169: return f;
170: }
171:
172: void show(URL codeBase, String className) {
173: this .codeBase = codeBase;
174: this .className = className;
175: show();
176: }
177:
178: private void show() {
179:
180: URL documentBase = null;
181:
182: try {
183: documentBase = new URL(codeBase.toString() + className
184: + ".html");
185: } catch (MalformedURLException e) {
186: throw new RuntimeException(e);
187: }
188:
189: Parameters params = new Parameters(appletId, 0, documentBase,
190: documentId, codeBase, className,
191: new HashMap<String, String>(), className, placeholder);
192:
193: frame.setVisible(true);
194: factory.createAndRun(params);
195: }
196:
197: void hide() {
198: factory.stop(appletId);
199: factory.destroy(appletId);
200: factory.dispose(appletId);
201: }
202:
203: private static class SunkenBar extends Container {
204:
205: private static final long serialVersionUID = -8850392912011177434L;
206:
207: private final int offset;
208: private final int margin;
209:
210: SunkenBar(int offset, int margin) {
211: this .offset = offset;
212: this .margin = margin;
213: }
214:
215: @Override
216: public void paint(Graphics g) {
217:
218: super .paint(g);
219:
220: int w = getWidth(), h = getHeight();
221:
222: g.setColor(SystemColor.controlShadow);
223: g.drawLine(offset, offset, w - offset, offset);
224: g.drawLine(offset, offset, offset, h - offset);
225:
226: g.setColor(SystemColor.controlHighlight);
227: g.drawLine(offset, h - offset, w - offset, h - offset);
228: g.drawLine(w - offset, offset, w - offset, h - offset);
229: }
230:
231: @SuppressWarnings("deprecation")
232: @Deprecated
233: @Override
234: public void layout() {
235: Component child = getComponent(0);
236:
237: child.setBounds(offset + margin, offset + 1, getWidth() - 2
238: * (offset + margin), getHeight() - 2 * offset - 1);
239: }
240:
241: @Override
242: public Dimension getPreferredSize() {
243: Component child = getComponent(0);
244: Dimension size = child.getPreferredSize();
245: size.width += 2 * (offset + margin);
246: return size;
247: }
248: }
249:
250: private static class ThreeTierLayout implements LayoutManager {
251: private final Component items[] = new Component[3];
252:
253: public void addLayoutComponent(String name, Component comp) {
254: if (name.equals("north")) {
255: items[0] = comp;
256: } else if (name.equals("center")) {
257: items[1] = comp;
258: } else if (name.equals("south")) {
259: items[2] = comp;
260: }
261: }
262:
263: public void layoutContainer(Container parent) {
264: Insets insets = parent.getInsets();
265: int width = parent.getWidth() - insets.left - insets.right;
266: int height = parent.getHeight() - insets.top
267: - insets.bottom;
268:
269: int h0 = items[0].getPreferredSize().height;
270: items[0].setBounds(insets.left, insets.top, width, h0);
271:
272: int h2 = items[2].getPreferredSize().height;
273: items[2].setBounds(insets.left, insets.top + height - h2,
274: width, h2);
275:
276: int h1 = height - h0 - h2;
277: items[1].setBounds(insets.left, insets.top + h0, width, h1);
278: }
279:
280: public Dimension minimumLayoutSize(Container parent) {
281: Dimension result = new Dimension();
282: for (int i = 0; i < 3; i++) {
283: if (items[i] == null) {
284: continue;
285: }
286: Dimension size = items[i].getMinimumSize();
287: if (result.width < size.width) {
288: result.width = size.width;
289: }
290: result.height += size.height;
291: }
292: return result;
293: }
294:
295: public Dimension preferredLayoutSize(Container parent) {
296: Dimension result = new Dimension();
297: for (int i = 0; i < 3; i++) {
298: if (items[i] == null) {
299: continue;
300: }
301: Dimension size = items[i].getPreferredSize();
302: if (result.width < size.width) {
303: result.width = size.width;
304: }
305: result.height += size.height;
306: }
307: return result;
308: }
309:
310: public void removeLayoutComponent(Component comp) {
311: for (int i = 0; i < 3; i++) {
312: if (items[i] == comp) {
313: items[i] = null;
314: }
315: }
316: }
317:
318: }
319: }
|