Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/sshtools/apps/SshToolsApplication.java


1   /**
2    *   Sshtools - Applications
3    *
4    *   Copyright (C) 2002 Lee David Painter
5    *
6    *   Written by: 2002 Brett Smith <t_magicthize@users.sourceforge.net>
7    *
8    *   This program is free software; you can redistribute it and/or modify
9    *   it under the terms of the GNU General Public License as published by
10   *   the Free Software Foundation; either version 2 of the License, or
11   *   (at your option) any later version.
12   *
13   *   This program is distributed in the hope that it will be useful,
14   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   *   GNU General Public License for more details.
17   *
18   *   You should have received a copy of the GNU General Public License
19   *   along with this program; if not, write to the Free Software
20   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   */
22  package com.sshtools.apps;
23  
24  import javax.swing.*;
25  import java.awt.*;
26  import java.io.*;
27  import java.awt.event.*;
28  import java.util.*;
29  import java.net.URL;
30  import com.sshtools.j2ssh.ui.ResourceIcon;
31  import com.sshtools.j2ssh.ui.UIUtil;
32  import java.lang.reflect.*;
33  
34  import org.apache.log4j.*;
35  
36  /**
37   *  <p>Provides some common features for SshTools applications such as SshTerm and
38   *  ShiFT</p>. When constructing the instance of this class, the <code>Class</code>
39   *  of an implementation of <code>SshToolsApplicationFrame</code> must be
40   *  provided.
41   *
42   *@author     Brett Smith (<A HREF="mailto:t_magicthize@users.sourceforge.net">
43   *              t_magicthize@users.sourceforge.net</A> )
44   *@created    12 January 2002
45   *@see com.sshtools.apps.SshToolsApplicationFrame
46   *@version    $Id: SshToolsApplication.java,v 1.2 2003/01/20 14:28:12 t_magicthize Exp $
47   */
48  
49  public abstract class SshToolsApplication {
50  
51      protected static Vector containers = new Vector();
52      protected org.apache.log4j.Logger log =
53              org.apache.log4j.Logger.getLogger(SshToolsApplication.class);
54      protected Class panelClass, defaultContainerClass;
55  
56      /**
57       * Construct a new application
58       *
59       * @param panelClass the class used to create the application panel
60       * @param defaultContainerClass the class to use when creating a new container
61       */
62      public SshToolsApplication(Class panelClass, Class defaultContainerClass) {
63          this.panelClass = panelClass;
64          this.defaultContainerClass = defaultContainerClass;
65      }
66  
67      /**
68       * Return the application name
69       *
70       * @return application ame
71       */
72      public abstract String getApplicationName();
73  
74      /**
75       * Return the application version
76       *
77       * @return application version
78       */
79      public abstract String getApplicationVersion();
80  
81      /**
82       * Return the application large icon
83       *
84       * @return application large icon
85       */
86      public abstract Icon getApplicationLargeIcon();
87  
88      /**
89       * Return the license details for the about box
90       *
91       * @return about license details
92       */
93      public abstract String getAboutLicenseDetails();
94  
95      /**
96       * Return the URL for the about box
97       *
98       * @return about url
99       */
100     public abstract String getAboutURL();
101 
102     /**
103      * Return the authros  for the about box
104      *
105      * @return authors
106      */
107     public abstract String getAboutAuthors();
108 
109     /**
110      *  Exit
111      */
112     public void exit() {
113         log.debug("Exiting application");
114         PreferencesStore.savePreferences();
115         System.exit(0);
116     }
117 
118     /**
119      * Return the number of containers this application currently has
120      *
121      * @param container count
122      */
123     public int getContainerCount() {
124         return containers.size();
125     }
126 
127     /**
128      * Return the container at the given index in the stack
129      *
130      * @param idx index
131      * @return container
132      */
133     public SshToolsApplicationContainer getContainerAt(int idx) {
134         return (SshToolsApplicationContainer)containers.elementAt(idx);
135     }
136 
137     /**
138      * Return the container for a given panel
139      *
140      * @param panel panel
141      * @return container
142      */
143     public SshToolsApplicationContainer getContainerForPanel(SshToolsApplicationPanel panel) {
144         for(Iterator i = containers.iterator(); i.hasNext() ; ) {
145             SshToolsApplicationContainer c = (SshToolsApplicationContainer)i.next();
146             if(c.getApplicationPanel() == panel)
147                 return c;
148         }
149         return null;
150     }
151 
152     /**
153      * Check that a frame can close and exit when there are no more frames
154      * to close
155      *
156      * @param frame frame to close
157      */
158     public void closeContainer(SshToolsApplicationContainer container) {
159         log.debug("Asking " + container + " if it can close");
160         if(container.getApplicationPanel().canClose()) {
161             log.debug("Closing");
162             for(Iterator i = containers.iterator(); i.hasNext() ; )
163                 log.debug(i.next() + " is currently open");
164             container.getApplicationPanel().close();
165             container.closeContainer();
166             containers.removeElement(container);
167             if(containers.size() == 0)
168                 exit();
169             else {
170                 log.debug("Not closing completely because there are containers still open");
171                 for(Iterator i = containers.iterator(); i.hasNext() ; )
172                     log.debug(i.next() + " is still open");
173             }
174         }
175     }
176 
177     /**
178      *  Create a new container of the default class
179      *
180      * @throws SshToolsApplicationException if the container can't be created for any reason
181      */
182     public void newContainer()
183         throws SshToolsApplicationException {
184 
185         SshToolsApplicationContainer container = null;
186         try {
187             SshToolsApplicationPanel panel =
188                     (SshToolsApplicationPanel)panelClass.newInstance();
189             panel.init(this);
190             container = (SshToolsApplicationContainer)defaultContainerClass.newInstance();
191             container.init(this, panel);
192             panel.setContainer(container);
193             if(!container.isContainerVisible())
194                 container.setContainerVisible(true);
195             containers.addElement(container);
196         }
197         catch(Throwable t) {
198             throw new SshToolsApplicationException(t);
199         }
200     }
201 
202     /**
203      *  Convert a container to a different container
204      *
205      * @param container container to convert
206      * @param newContainerClass new container class
207      * @return the new container
208      * @throws SshToolsApplicationException if the container can't be converted for any reason
209      */
210     public SshToolsApplicationContainer convertContainer(SshToolsApplicationContainer container,
211             Class newContainerClass)
212         throws SshToolsApplicationException {
213 
214         int idx = containers.indexOf(container);
215         if(idx == -1) {
216             System.out.println("NOT FOUND CONTAINER = " + container);
217             throw new SshToolsApplicationException(
218                 "Container is not being manager by the application.");
219         }
220         SshToolsApplicationContainer newContainer = null;
221         try {
222             container.closeContainer();
223             SshToolsApplicationPanel panel = container.getApplicationPanel();
224             newContainer = (SshToolsApplicationContainer)newContainerClass.newInstance();
225             newContainer.init(this, panel);
226             panel.setContainer(newContainer);
227             if(!newContainer.isContainerVisible())
228                 newContainer.setContainerVisible(true);
229             containers.setElementAt(newContainer, idx);
230             return newContainer;
231         }
232         catch(Throwable t) {
233             throw new SshToolsApplicationException(t);
234         }
235     }
236 
237 
238     /**
239      *  The main entry method for an SshTools application. The extending class
240      *  should call this from its <code>main</code> method
241      *
242      * @param prefs the file to store preferencese in
243      * @param args command line arguments
244      * @param exception
245      */
246     public static void init(File prefs, String[] args)
247                 throws Exception {
248 
249         // Set up basic logging
250 //        BasicConfigurator.configure();
251 
252         // Set up the nice Gnome icons
253         UIManager.put("OptionPane.errorIcon", new ResourceIcon("/com/sshtools/apps/dialog-error4.png"));
254       UIManager.put("OptionPane.informationIcon", new ResourceIcon("/com/sshtools/apps/dialog-information.png"));
255       UIManager.put("OptionPane.warningIcon", new ResourceIcon("/com/sshtools/apps/dialog-warning2.png"));
256       UIManager.put("OptionPane.questionIcon", new ResourceIcon("/com/sshtools/apps/dialog-question3.png"));
257 
258         //
259         PreferencesStore.init(prefs);
260     }
261 }