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: package org.apache.harmony.tools.appletviewer;
019:
020: import java.applet.Applet;
021:
022: import java.awt.BorderLayout;
023: import java.awt.Dimension;
024: import java.awt.FlowLayout;
025: import java.awt.Font;
026: import java.awt.GridLayout;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.awt.event.ItemEvent;
030: import java.awt.event.ItemListener;
031: import java.awt.event.WindowEvent;
032: import java.awt.event.WindowListener;
033:
034: import java.io.IOException;
035: import java.io.File;
036: import java.io.FileOutputStream;
037:
038: import java.util.Enumeration;
039: import java.util.HashSet;
040:
041: import javax.swing.AbstractAction;
042: import javax.swing.BorderFactory;
043: import javax.swing.JButton;
044: import javax.swing.JCheckBox;
045: import javax.swing.JDialog;
046: import javax.swing.JFrame;
047: import javax.swing.JLabel;
048: import javax.swing.JMenu;
049: import javax.swing.JMenuBar;
050: import javax.swing.JMenuItem;
051: import javax.swing.JOptionPane;
052: import javax.swing.JPanel;
053: import javax.swing.JSeparator;
054: import javax.swing.JTextField;
055: import javax.swing.SwingConstants;
056: import javax.swing.border.Border;
057:
058: class AppletFrame extends JFrame {
059: private final Applet applet;
060: private final JLabel statusLabel;
061:
062: private static ShutdownHandler shutdownHandler = new ShutdownHandler();
063:
064: public AppletFrame(AppletInfo appletInfo) throws Exception {
065: // Load applet class
066: applet = ViewerAppletContext.loadApplet(appletInfo);
067:
068: applet.setPreferredSize(new Dimension(appletInfo.getWidth(),
069: appletInfo.getHeight()));
070:
071: shutdownHandler.addFrame(this );
072:
073: // Create menu bar
074: setJMenuBar(createMenu());
075:
076: // Create applet pane
077: setLayout(new BorderLayout());
078: JPanel appletPanel = new JPanel();
079: appletPanel.add(applet);
080: add(appletPanel, BorderLayout.WEST);
081:
082: // Create status pane
083: JPanel panel = new JPanel();
084: panel.setLayout(new BorderLayout());
085: panel.setMinimumSize(new Dimension(100, 15));
086: panel.setPreferredSize(new Dimension(100, 15));
087: statusLabel = new JLabel();
088: statusLabel.setMinimumSize(new Dimension(100, 15));
089: statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
090: panel.add(statusLabel, BorderLayout.WEST);
091: add(panel, BorderLayout.SOUTH);
092: appletInfo.setStatusLabel(statusLabel);
093:
094: // Start applet and make frame visible
095: // Init should be called after pack to make components displayable
096: pack();
097: applet.init();
098: setVisible(true);
099: applet.start();
100: }
101:
102: private JMenuBar createMenu() {
103: JMenuBar menuBar = new JMenuBar();
104:
105: // Create Control menu
106: JMenu controlMenu = new JMenu("Control");
107: controlMenu.add(new JMenuItem(new StartAction()));
108: controlMenu.add(new JMenuItem(new StopAction()));
109: controlMenu.add(new JSeparator());
110: controlMenu.add(new JMenuItem(new PropertiesAction()));
111: controlMenu.add(new JSeparator());
112: controlMenu.add(new JMenuItem(new CloseAction()));
113: controlMenu.add(new JMenuItem(new ExitAction()));
114:
115: menuBar.add(controlMenu);
116:
117: return menuBar;
118: }
119:
120: Applet getApplet() {
121: return applet;
122: }
123:
124: private class StartAction extends AbstractAction {
125: public StartAction() {
126: super ("Start");
127: }
128:
129: public void actionPerformed(final ActionEvent e) {
130: applet.start();
131: applet.setEnabled(true);
132: }
133: }
134:
135: private class StopAction extends AbstractAction {
136: public StopAction() {
137: super ("Stop");
138: }
139:
140: public void actionPerformed(ActionEvent e) {
141: applet.stop();
142: applet.setEnabled(false);
143: }
144: }
145:
146: private class PropertiesAction extends AbstractAction {
147: public PropertiesAction() {
148: super ("Properties");
149: }
150:
151: public void actionPerformed(final ActionEvent e) {
152: showSetPropDialog(AppletFrame.this );
153: }
154:
155: private void showSetPropDialog(final JFrame frame) {
156: final JDialog dialog = new JDialog(frame,
157: "Harmony AppletViewer Properties");
158:
159: // Sheet part of Dialog
160: JLabel httpHost = new JLabel(Main.httpProxyHost);
161: httpHost.setFont(httpHost.getFont().deriveFont(Font.PLAIN));
162:
163: JLabel httpPort = new JLabel(Main.httpProxyPort);
164: httpPort.setFont(httpPort.getFont().deriveFont(Font.PLAIN));
165:
166: JLabel httpsHost = new JLabel(Main.httpsProxyHost);
167: httpsHost.setFont(httpsHost.getFont()
168: .deriveFont(Font.PLAIN));
169:
170: JLabel httpsPort = new JLabel(Main.httpsProxyPort);
171: httpsPort.setFont(httpsPort.getFont()
172: .deriveFont(Font.PLAIN));
173:
174: JLabel ftpHost = new JLabel(Main.ftpProxyHost);
175: ftpHost.setFont(ftpHost.getFont().deriveFont(Font.PLAIN));
176:
177: JLabel ftpPort = new JLabel(Main.ftpProxyPort);
178: ftpPort.setFont(ftpPort.getFont().deriveFont(Font.PLAIN));
179:
180: final JTextField tfHttpHost = new JTextField(
181: Main.properties.getProperty(Main.httpProxyHost));
182: Dimension d = tfHttpHost.getPreferredSize();
183: tfHttpHost.setPreferredSize(new Dimension(50, d.height));
184:
185: final JTextField tfHttpPort = new JTextField(
186: Main.properties.getProperty(Main.httpProxyPort));
187: tfHttpPort.setPreferredSize(new Dimension(50, d.height));
188:
189: final JTextField tfHttpsHost = new JTextField(
190: Main.properties.getProperty(Main.httpsProxyHost));
191: tfHttpsHost.setPreferredSize(new Dimension(50, d.height));
192:
193: final JTextField tfHttpsPort = new JTextField(
194: Main.properties.getProperty(Main.httpsProxyPort));
195: tfHttpsPort.setPreferredSize(new Dimension(50, d.height));
196:
197: final JTextField tfFtpHost = new JTextField(Main.properties
198: .getProperty(Main.ftpProxyHost));
199: tfFtpHost.setPreferredSize(new Dimension(50, d.height));
200:
201: final JTextField tfFtpPort = new JTextField(Main.properties
202: .getProperty(Main.ftpProxyPort));
203: tfFtpPort.setPreferredSize(new Dimension(50, d.height));
204:
205: JPanel sheetPanel = new JPanel();
206:
207: sheetPanel.setLayout(new GridLayout(6, 2));
208:
209: sheetPanel.add(httpHost);
210: sheetPanel.add(tfHttpHost);
211:
212: sheetPanel.add(httpPort);
213: sheetPanel.add(tfHttpPort);
214:
215: sheetPanel.add(httpsHost);
216: sheetPanel.add(tfHttpsHost);
217:
218: sheetPanel.add(httpsPort);
219: sheetPanel.add(tfHttpsPort);
220:
221: sheetPanel.add(ftpHost);
222: sheetPanel.add(tfFtpHost);
223:
224: sheetPanel.add(ftpPort);
225: sheetPanel.add(tfFtpPort);
226:
227: sheetPanel.setBorder(BorderFactory.createEmptyBorder(5, 5,
228: 5, 5));
229:
230: final boolean useSameServer;
231:
232: final JCheckBox sameServer = new JCheckBox(
233: "Use same proxy server for all protocols");
234: if (Main.properties.getProperty(Main.httpProxyHost).equals(
235: Main.properties.getProperty(Main.httpsProxyHost))
236: && Main.properties
237: .getProperty(Main.httpProxyHost)
238: .equals(
239: Main.properties
240: .getProperty(Main.ftpProxyHost))
241: && Main.properties
242: .getProperty(Main.httpProxyPort)
243: .equals(
244: Main.properties
245: .getProperty(Main.httpsProxyPort))
246: && Main.properties
247: .getProperty(Main.httpProxyPort)
248: .equals(
249: Main.properties
250: .getProperty(Main.ftpProxyPort))) {
251:
252: sameServer.setSelected(true);
253:
254: tfHttpsHost.setText("");
255: tfHttpsHost.setEditable(false);
256:
257: tfHttpsPort.setText("");
258: tfHttpsPort.setEditable(false);
259:
260: tfFtpHost.setText("");
261: tfFtpHost.setEditable(false);
262:
263: tfFtpPort.setText("");
264: tfFtpPort.setEditable(false);
265: useSameServer = true;
266: } else {
267: sameServer.setSelected(false);
268: useSameServer = false;
269: }
270:
271: sameServer.addItemListener(new ItemListener() {
272: public void itemStateChanged(ItemEvent e) {
273: if (e.getStateChange() == ItemEvent.SELECTED) {
274:
275: tfHttpsHost.setText("");
276: tfHttpsHost.setEditable(false);
277:
278: tfHttpsPort.setText("");
279: tfHttpsPort.setEditable(false);
280:
281: tfFtpHost.setText("");
282: tfFtpHost.setEditable(false);
283:
284: tfFtpPort.setText("");
285: tfFtpPort.setEditable(false);
286:
287: } else {
288:
289: tfHttpsHost.setEditable(true);
290:
291: tfHttpsPort.setEditable(true);
292:
293: tfFtpHost.setEditable(true);
294:
295: tfFtpPort.setEditable(true);
296:
297: }
298: tfHttpHost.setCaretPosition(0);
299: }
300: });
301:
302: JPanel checkBoxPanel = new JPanel();
303: checkBoxPanel.add(sameServer);
304: checkBoxPanel.setBorder(BorderFactory.createEmptyBorder(5,
305: 5, 5, 5));
306:
307: // Button part of Dialog
308: JButton apply = new JButton("Apply");
309: apply.addActionListener(new ActionListener() {
310: public void actionPerformed(ActionEvent e) {
311: if (!checkPort(Main.httpProxyPort, tfHttpPort
312: .getText().trim())) {
313: return;
314: }
315: if (sameServer.isSelected()) {
316: Main.properties.setProperty(Main.httpProxyHost,
317: tfHttpHost.getText().trim());
318: Main.properties.setProperty(Main.httpProxyPort,
319: tfHttpPort.getText().trim());
320: Main.properties.setProperty(
321: Main.httpsProxyHost, tfHttpHost
322: .getText().trim());
323: Main.properties.setProperty(
324: Main.httpsProxyPort, tfHttpPort
325: .getText().trim());
326: Main.properties.setProperty(Main.ftpProxyHost,
327: tfHttpHost.getText().trim());
328: Main.properties.setProperty(Main.ftpProxyPort,
329: tfHttpPort.getText().trim());
330: } else {
331: if (!checkPort(Main.httpsProxyPort, tfHttpsPort
332: .getText().trim())) {
333: return;
334: }
335: if (!checkPort(Main.ftpProxyPort, tfFtpPort
336: .getText().trim())) {
337: return;
338: }
339: Main.properties.setProperty(Main.httpProxyHost,
340: tfHttpHost.getText().trim());
341: Main.properties.setProperty(Main.httpProxyPort,
342: tfHttpPort.getText().trim());
343: Main.properties.setProperty(
344: Main.httpsProxyHost, tfHttpsHost
345: .getText().trim());
346: Main.properties.setProperty(
347: Main.httpsProxyPort, tfHttpsPort
348: .getText().trim());
349: Main.properties.setProperty(Main.ftpProxyHost,
350: tfFtpHost.getText().trim());
351: Main.properties.setProperty(Main.ftpProxyPort,
352: tfFtpPort.getText().trim());
353: }
354:
355: Enumeration<?> en = Main.properties.propertyNames();
356:
357: while (en.hasMoreElements()) {
358: String key = (String) en.nextElement();
359: String val = Main.properties.getProperty(key);
360: if (val != null && val != "") {
361: System.setProperty(key, val);
362: }
363: }
364:
365: Main.storeProxyProperties();
366:
367: dialog.setVisible(false);
368: dialog.dispose();
369: }
370:
371: private boolean checkPort(String portName, String value) {
372: boolean passed = true;
373: try {
374: if (Integer.parseInt(value) < 0) {
375: passed = false;
376: showErrorMessage(portName);
377: }
378: } catch (NumberFormatException e) {
379: passed = false;
380: showErrorMessage(portName);
381: }
382: return passed;
383: }
384:
385: private void showErrorMessage(String portName) {
386: JOptionPane.showMessageDialog(frame, portName
387: + " must be a positive integer value",
388: "Invalid entry", JOptionPane.ERROR_MESSAGE);
389: }
390: });
391:
392: JButton reset = new JButton("Reset");
393: reset.addActionListener(new ActionListener() {
394: public void actionPerformed(ActionEvent e) {
395:
396: tfHttpHost.setText(Main.properties
397: .getProperty(Main.httpProxyHost));
398: tfHttpPort.setText(Main.properties
399: .getProperty(Main.httpProxyPort));
400:
401: if (useSameServer) {
402: sameServer.setSelected(true);
403:
404: tfHttpsHost.setText("");
405: tfHttpsHost.setEditable(false);
406:
407: tfHttpsPort.setText("");
408: tfHttpsPort.setEditable(false);
409:
410: tfFtpHost.setText("");
411: tfFtpHost.setEditable(false);
412:
413: tfFtpPort.setText("");
414: tfFtpPort.setEditable(false);
415: } else {
416: tfHttpsHost.setText(Main.properties
417: .getProperty(Main.httpsProxyHost));
418: tfHttpsPort.setText(Main.properties
419: .getProperty(Main.httpsProxyPort));
420: tfFtpHost.setText(Main.properties
421: .getProperty(Main.ftpProxyHost));
422: tfFtpPort.setText(Main.properties
423: .getProperty(Main.ftpProxyPort));
424: sameServer.setSelected(false);
425: }
426:
427: }
428: });
429:
430: JButton cancel = new JButton("Cancel");
431: cancel.addActionListener(new ActionListener() {
432: public void actionPerformed(ActionEvent e) {
433: dialog.setVisible(false);
434: dialog.dispose();
435: }
436: });
437:
438: JPanel buttonPanel = new JPanel();
439: buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
440:
441: buttonPanel.add(apply);
442: buttonPanel.add(reset);
443: buttonPanel.add(cancel);
444:
445: JPanel contentPane = new JPanel();
446: contentPane.setLayout(new BorderLayout());
447: contentPane.add(sheetPanel, BorderLayout.NORTH);
448: contentPane.add(checkBoxPanel, BorderLayout.CENTER);
449: contentPane.add(buttonPanel, BorderLayout.SOUTH);
450:
451: dialog
452: .setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
453: dialog.setContentPane(contentPane);
454: dialog.setLocationRelativeTo(frame);
455: dialog.pack();
456:
457: dialog.setVisible(true);
458:
459: tfHttpHost.setCaretPosition(0);
460: }
461: }
462:
463: private class CloseAction extends AbstractAction {
464: public CloseAction() {
465: super ("Close");
466: }
467:
468: public void actionPerformed(ActionEvent e) {
469: AppletFrame.this .processWindowEvent(new WindowEvent(
470: AppletFrame.this , WindowEvent.WINDOW_CLOSING));
471: }
472: }
473:
474: private class ExitAction extends AbstractAction {
475: public ExitAction() {
476: super ("Exit");
477: }
478:
479: public void actionPerformed(ActionEvent e) {
480: System.exit(0);
481: }
482: }
483:
484: private static class ShutdownHandler implements WindowListener {
485: HashSet<JFrame> frameList = new HashSet<JFrame>();
486:
487: public void windowActivated(WindowEvent e) {
488: }
489:
490: public void windowClosed(WindowEvent e) {
491: }
492:
493: public void windowClosing(WindowEvent e) {
494: JFrame frame = (JFrame) e.getWindow();
495: frameList.remove(frame);
496:
497: Applet applet = ((AppletFrame) frame).getApplet();
498: if (applet != null) {
499: ViewerAppletContext ac = (ViewerAppletContext) applet
500: .getAppletContext();
501: ac.remove(applet);
502: }
503:
504: if (frameList.isEmpty())
505: System.exit(0);
506: }
507:
508: public void windowDeactivated(WindowEvent e) {
509: }
510:
511: public void windowDeiconified(WindowEvent e) {
512: }
513:
514: public void windowIconified(WindowEvent e) {
515: }
516:
517: public void windowOpened(WindowEvent e) {
518: }
519:
520: public void addFrame(JFrame frame) {
521: frameList.add(frame);
522: frame.addWindowListener(this);
523: }
524:
525: }
526:
527: }
|