001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.ui;
027:
028: import com.sshtools.common.util.PropertyUtil;
029:
030: import com.sshtools.j2ssh.configuration.ConfigurationLoader;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: import java.awt.BorderLayout;
036: import java.awt.Color;
037: import java.awt.GridBagConstraints;
038: import java.awt.GridBagLayout;
039: import java.awt.Insets;
040: import java.awt.event.ActionEvent;
041: import java.awt.event.ComponentAdapter;
042: import java.awt.event.ComponentEvent;
043:
044: import java.io.IOException;
045: import java.io.PrintWriter;
046: import java.io.StringWriter;
047:
048: import java.util.StringTokenizer;
049:
050: import javax.swing.BorderFactory;
051: import javax.swing.JApplet;
052: import javax.swing.JComponent;
053: import javax.swing.JLabel;
054: import javax.swing.JPanel;
055: import javax.swing.JProgressBar;
056: import javax.swing.JSeparator;
057:
058: /**
059: *
060: *
061: * @author $author$
062: * @version $Revision: 1.14 $
063: */
064: public abstract class SshToolsApplicationApplet extends JApplet {
065: // eurgghh!
066:
067: /** */
068: public final static String[][] PARAMETER_INFO = {
069: { "sshapps.log.file", "string",
070: "Logging output destination. Defaults to @console@" },
071: { "sshapps.log.level", "string",
072: "Logging level. DEBUG,FATAL,ERROR,WARN,INFO,DEBUG or OFF. Defaults to OFF" },
073: { "sshapps.ui.informationPanel.background", "hex color",
074: "Set the background color of the 'information panel'" },
075: { "sshapps.ui.informationPanel.foreground", "boolean",
076: "Set the foreground color of the 'information panel'" },
077: { "sshapps.ui.informationPanel.borderColor", "boolean",
078: "Set the border color of the 'information panel'" },
079: { "sshapps.ui.informationPanel.borderThickness", "integer",
080: "Set the border thickness of the 'information panel'" },
081: { "sshapps.ui.toolBar", "boolean",
082: "Enable / Disable the tool bar" },
083: { "sshapps.ui.menuBar", "boolean",
084: "Enable / Disable the menu bar" },
085: { "sshapps.ui.disabledActions", "string",
086: "Comma (,) separated list of disable actions" },
087: { "sshapps.ui.statusBar", "boolean",
088: "Enable / Disable the menu bar" } };
089:
090: /** */
091: protected Log log = LogFactory
092: .getLog(SshToolsApplicationApplet.class);
093:
094: // Private instance variables
095:
096: /** */
097: protected LoadingPanel loadingPanel;
098:
099: /** */
100: protected JSeparator toolSeparator;
101:
102: /** */
103: protected SshToolsApplicationPanel applicationPanel;
104:
105: /** */
106: protected Color infoForeground;
107:
108: /** */
109: protected int infoBorderThickness;
110:
111: /** */
112: protected boolean toolBar;
113:
114: /** */
115: protected boolean menuBar;
116:
117: /** */
118: protected boolean statusBar;
119:
120: /** */
121: protected Color infoBackground;
122:
123: /** */
124: protected Color infoBorderColor;
125:
126: /** */
127: protected String disabledActions;
128:
129: /**
130: *
131: *
132: * @param key
133: * @param def
134: *
135: * @return
136: */
137: public String getParameter(String key, String def) {
138: String v = getParameter(key);
139:
140: return (v != null) ? v : def;
141: }
142:
143: /**
144: *
145: */
146: public void init() {
147: try {
148: Runnable r = new Runnable() {
149: public void run() {
150: try {
151: getContentPane().setLayout(new BorderLayout());
152: setAppletComponent(loadingPanel = new LoadingPanel());
153: initApplet();
154:
155: JComponent p = buildAppletComponent();
156: startApplet();
157: setAppletComponent(p);
158: } catch (Throwable t) {
159: seriousAppletError(t);
160: }
161: }
162: };
163:
164: Thread t = new Thread(r);
165: t.start();
166: } catch (Throwable t) {
167: seriousAppletError(t);
168: }
169: }
170:
171: /**
172: *
173: *
174: * @throws IOException
175: */
176: public void initApplet() throws IOException {
177: /*ConfigurationLoader.setLogfile(getParameter("sshapps.log.file",
178: "@console@"));
179: log.getRootLogger().setLevel(org.apache.log4j.Level.toLevel(
180: getParameter("sshapps.log.level", "DEBUG")));*/
181: ConfigurationLoader.initialize(false);
182: infoBackground = PropertyUtil.stringToColor(getParameter(
183: "sshapps.ui.informationPanel.background", PropertyUtil
184: .colorToString(new Color(255, 255, 204))));
185: infoForeground = PropertyUtil.stringToColor(getParameter(
186: "sshapps.ui.informationPanel.foreground", PropertyUtil
187: .colorToString(Color.black)));
188: infoBorderColor = PropertyUtil.stringToColor(getParameter(
189: "sshapps.ui.informationPanel.borderColor", PropertyUtil
190: .colorToString(Color.black)));
191: infoBorderThickness = PropertyUtil.stringToInt(getParameter(
192: "sshapps.ui.informationPanel.borderThickness", "1"), 1);
193: toolBar = getParameter("sshapps.ui.toolBar", "true")
194: .equalsIgnoreCase("true");
195: menuBar = getParameter("sshapps.ui.menuBar", "true")
196: .equalsIgnoreCase("true");
197: statusBar = getParameter("sshapps.ui.statusBar", "true")
198: .equalsIgnoreCase("true");
199: disabledActions = getParameter("sshapps.ui.disabledActions", "");
200: }
201:
202: /**
203: *
204: */
205: public void startApplet() {
206: }
207:
208: /**
209: *
210: *
211: * @return
212: *
213: * @throws IOException
214: * @throws SshToolsApplicationException
215: */
216: public JComponent buildAppletComponent() throws IOException,
217: SshToolsApplicationException {
218: loadingPanel.setStatus("Creating application");
219: applicationPanel = createApplicationPanel();
220: loadingPanel.setStatus("Building action components");
221: applicationPanel.rebuildActionComponents();
222: log.debug("Disabled actions list = " + disabledActions);
223:
224: StringTokenizer tk = new StringTokenizer(
225: (disabledActions == null) ? "" : disabledActions, ",");
226:
227: while (tk.hasMoreTokens()) {
228: String n = tk.nextToken();
229: log.debug("Disable " + n);
230: applicationPanel.setActionVisible(n, false);
231: }
232:
233: JPanel p = new JPanel(new BorderLayout());
234: JPanel n = new JPanel(new BorderLayout());
235:
236: if (applicationPanel.getJMenuBar() != null) {
237: n.add(applicationPanel.getJMenuBar(), BorderLayout.NORTH);
238: log.debug("Setting menu bar visibility to " + menuBar);
239: applicationPanel.setMenuBarVisible(menuBar);
240: }
241:
242: if (applicationPanel.getToolBar() != null) {
243: JPanel t = new JPanel(new BorderLayout());
244: t.add(applicationPanel.getToolBar(), BorderLayout.NORTH);
245: applicationPanel.setToolBarVisible(toolBar);
246: t.add(
247: toolSeparator = new JSeparator(
248: JSeparator.HORIZONTAL), BorderLayout.SOUTH);
249: toolSeparator.setVisible(applicationPanel.getToolBar()
250: .isVisible());
251:
252: final SshToolsApplicationPanel pnl = applicationPanel;
253: applicationPanel.getToolBar().addComponentListener(
254: new ComponentAdapter() {
255: public void componentHidden(ComponentEvent evt) {
256: toolSeparator.setVisible(pnl.getToolBar()
257: .isVisible());
258: }
259: });
260: n.add(t, BorderLayout.SOUTH);
261: }
262:
263: p.add(n, BorderLayout.NORTH);
264: p.add(applicationPanel, BorderLayout.CENTER);
265:
266: if (applicationPanel.getStatusBar() != null) {
267: p.add(applicationPanel.getStatusBar(), BorderLayout.SOUTH);
268: applicationPanel.setStatusBarVisible(statusBar);
269: }
270:
271: return p;
272: }
273:
274: /**
275: *
276: *
277: * @param name
278: */
279: public void doAction(String name) {
280: StandardAction a = applicationPanel.getAction(name);
281:
282: if (a != null) {
283: if (a.isEnabled()) {
284: log.debug("Performing action " + a.getName());
285: a.actionPerformed(new ActionEvent(this ,
286: ActionEvent.ACTION_PERFORMED, a
287: .getActionCommand()));
288: } else {
289: log.warn("No performing action '" + a.getName()
290: + "' because it is disabled.");
291: }
292: } else {
293: log.error("No action named " + name);
294: }
295: }
296:
297: /**
298: *
299: *
300: * @return
301: *
302: * @throws SshToolsApplicationException
303: */
304: public abstract SshToolsApplicationPanel createApplicationPanel()
305: throws SshToolsApplicationException;
306:
307: /**
308: *
309: *
310: * @param component
311: */
312: protected void setAppletComponent(JComponent component) {
313: if (getContentPane().getComponentCount() > 0) {
314: getContentPane().invalidate();
315: getContentPane().removeAll();
316: }
317:
318: getContentPane().add(component, BorderLayout.CENTER);
319: getContentPane().validate();
320: getContentPane().repaint();
321: }
322:
323: /**
324: *
325: *
326: * @param t
327: */
328: protected void seriousAppletError(Throwable t) {
329: StringBuffer buf = new StringBuffer();
330: buf.append("<html><p>A serious error has occured ...</p><br>");
331: buf.append("<p><font size=\"-1\" color=\"#ff0000\"><b>");
332:
333: StringWriter writer = new StringWriter();
334: t.printStackTrace(new PrintWriter(writer, true));
335:
336: StringTokenizer tk = new StringTokenizer(writer.toString(),
337: "\n");
338:
339: while (tk.hasMoreTokens()) {
340: String msg = tk.nextToken();
341: buf.append(msg);
342:
343: if (tk.hasMoreTokens()) {
344: buf.append("<br>");
345: }
346: }
347:
348: buf.append("</b></font></p><html>");
349:
350: SshToolsApplicationAppletPanel p = new SshToolsApplicationAppletPanel();
351: p.setLayout(new GridBagLayout());
352:
353: GridBagConstraints gbc = new GridBagConstraints();
354: gbc.anchor = GridBagConstraints.CENTER;
355: gbc.insets = new Insets(0, 0, 8, 0);
356: gbc.fill = GridBagConstraints.NONE;
357: UIUtil.jGridBagAdd(p, new JLabel(buf.toString()), gbc,
358: GridBagConstraints.REMAINDER);
359: setAppletComponent(p);
360: }
361:
362: /**
363: *
364: */
365: public void start() {
366: }
367:
368: /**
369: *
370: */
371: public void stop() {
372: }
373:
374: /**
375: *
376: */
377: public void destroy() {
378: }
379:
380: /**
381: *
382: *
383: * @return
384: */
385: public String[][] getParameterInfo() {
386: return PARAMETER_INFO;
387: }
388:
389: public class SshToolsApplicationAppletContainer extends JPanel
390: implements SshToolsApplicationContainer {
391: // Private instance variables
392: private SshToolsApplicationPanel panel;
393: private SshToolsApplication application;
394:
395: //Construct the applet
396: public SshToolsApplicationAppletContainer() {
397: }
398:
399: public void init(SshToolsApplication application,
400: SshToolsApplicationPanel panel)
401: throws SshToolsApplicationException {
402: this .application = application;
403: this .panel = panel;
404: panel
405: .registerActionMenu(new SshToolsApplicationPanel.ActionMenu(
406: "Help", "Help", 'h', 99));
407: panel.registerAction(new AboutAction(this , application));
408: getApplicationPanel().rebuildActionComponents();
409: }
410:
411: public void setContainerTitle(String title) {
412: getAppletContext().showStatus(title);
413: }
414:
415: public SshToolsApplicationPanel getApplicationPanel() {
416: return panel;
417: }
418:
419: public void closeContainer() {
420: // We dont do anything here
421: }
422:
423: public void setContainerVisible(boolean visible) {
424: setVisible(visible);
425: }
426:
427: public boolean isContainerVisible() {
428: return isVisible();
429: }
430: }
431:
432: class SshToolsApplicationAppletPanel extends JPanel {
433: SshToolsApplicationAppletPanel() {
434: super ();
435: setOpaque(true);
436: setBackground(infoBackground);
437: setForeground(infoForeground);
438: setBorder(BorderFactory.createLineBorder(infoBorderColor,
439: infoBorderThickness));
440: }
441: }
442:
443: class LoadingPanel extends SshToolsApplicationAppletPanel {
444: private JProgressBar bar;
445:
446: LoadingPanel() {
447: super ();
448: setLayout(new GridBagLayout());
449:
450: GridBagConstraints gbc = new GridBagConstraints();
451: gbc.anchor = GridBagConstraints.CENTER;
452: gbc.insets = new Insets(0, 0, 8, 0);
453: gbc.fill = GridBagConstraints.NONE;
454: UIUtil.jGridBagAdd(this , new JLabel("Loading "
455: + getAppletInfo()), gbc,
456: GridBagConstraints.REMAINDER);
457: bar = new JProgressBar(0, 100);
458:
459: //bar.setIndeterminate(true);
460: bar.setStringPainted(true);
461: UIUtil.jGridBagAdd(this , bar, gbc,
462: GridBagConstraints.REMAINDER);
463: }
464:
465: public void setStatus(String status) {
466: bar.setString(status);
467: }
468: }
469: }
|