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.j2ssh.configuration.ConfigurationLoader;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: import java.awt.BorderLayout;
034: import java.awt.Dimension;
035: import java.awt.GridLayout;
036: import java.awt.Point;
037: import java.awt.Toolkit;
038: import java.awt.event.ComponentAdapter;
039: import java.awt.event.ComponentEvent;
040: import java.awt.event.WindowAdapter;
041: import java.awt.event.WindowEvent;
042:
043: import javax.swing.JFrame;
044: import javax.swing.JOptionPane;
045: import javax.swing.JPanel;
046: import javax.swing.JSeparator;
047: import javax.swing.SwingConstants;
048:
049: /**
050: *
051: *
052: * @author $author$
053: * @version $Revision: 1.19 $
054: */
055: public class SshToolsApplicationFrame extends JFrame implements
056: SshToolsApplicationContainer {
057: // Preference names
058:
059: /** */
060: public final static String PREF_LAST_FRAME_GEOMETRY = "application.lastFrameGeometry";
061:
062: /** */
063: protected Log log = LogFactory
064: .getLog(SshToolsApplicationFrame.class);
065:
066: /** */
067: protected StandardAction exitAction;
068:
069: /** */
070: protected StandardAction aboutAction;
071:
072: /** */
073: protected StandardAction newWindowAction;
074:
075: /** */
076: protected JSeparator toolSeparator;
077:
078: //
079: private SshToolsApplicationPanel panel;
080: private SshToolsApplication application;
081: private boolean showAboutBox = true;
082: private boolean showExitAction = true;
083: private boolean showNewWindowAction = true;
084: private boolean showMenu = true;
085:
086: public void showAboutBox(boolean showAboutBox) {
087: this .showAboutBox = showAboutBox;
088: }
089:
090: public void showExitAction(boolean showExitAction) {
091: this .showExitAction = showExitAction;
092: }
093:
094: public void showNewWindowAction(boolean showNewWindowAction) {
095: this .showNewWindowAction = showNewWindowAction;
096: }
097:
098: /**
099: *
100: *
101: * @param application
102: * @param panel
103: *
104: * @throws SshToolsApplicationException
105: */
106: public void init(final SshToolsApplication application,
107: SshToolsApplicationPanel panel)
108: throws SshToolsApplicationException {
109: this .panel = panel;
110: this .application = application;
111:
112: if (application != null) {
113: setTitle(ConfigurationLoader.getVersionString(application
114: .getApplicationName(), application
115: .getApplicationVersion())); // + " " + application.getApplicationVersion());
116: }
117:
118: setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
119:
120: // Register the File menu
121: panel
122: .registerActionMenu(new SshToolsApplicationPanel.ActionMenu(
123: "File", "File", 'f', 0));
124:
125: // Register the Exit action
126: if (showExitAction && (application != null)) {
127: panel.registerAction(exitAction = new ExitAction(
128: application, this ));
129:
130: // Register the New Window Action
131: }
132:
133: if (showNewWindowAction && (application != null)) {
134: panel.registerAction(newWindowAction = new NewWindowAction(
135: application));
136:
137: // Register the Help menu
138: }
139:
140: panel
141: .registerActionMenu(new SshToolsApplicationPanel.ActionMenu(
142: "Help", "Help", 'h', 99));
143:
144: // Register the About box action
145: if (showAboutBox && (application != null)) {
146: panel.registerAction(aboutAction = new AboutAction(this ,
147: application));
148: }
149:
150: getApplicationPanel().rebuildActionComponents();
151:
152: JPanel p = new JPanel(new BorderLayout());
153:
154: if (panel.getJMenuBar() != null) {
155: setJMenuBar(panel.getJMenuBar());
156: }
157:
158: if (panel.getToolBar() != null) {
159: JPanel t = new JPanel(new BorderLayout());
160: t.add(panel.getToolBar(), BorderLayout.NORTH);
161: t.add(
162: toolSeparator = new JSeparator(
163: JSeparator.HORIZONTAL), BorderLayout.SOUTH);
164: toolSeparator.setVisible(panel.getToolBar().isVisible());
165:
166: final SshToolsApplicationPanel pnl = panel;
167: panel.getToolBar().addComponentListener(
168: new ComponentAdapter() {
169: public void componentHidden(ComponentEvent evt) {
170: log.debug("Tool separator is now "
171: + pnl.getToolBar().isVisible());
172: toolSeparator.setVisible(pnl.getToolBar()
173: .isVisible());
174: }
175: });
176: p.add(t, BorderLayout.NORTH);
177: }
178:
179: p.add(panel, BorderLayout.CENTER);
180:
181: if (panel.getStatusBar() != null) {
182: p.add(panel.getStatusBar(), BorderLayout.SOUTH);
183: }
184:
185: getContentPane().setLayout(new GridLayout(1, 1));
186: getContentPane().add(p);
187:
188: // Watch for the frame closing
189: setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
190: addWindowListener(new WindowAdapter() {
191: public void windowClosing(WindowEvent evt) {
192: if (application != null) {
193: application
194: .closeContainer(SshToolsApplicationFrame.this );
195: } else {
196: int confirm = JOptionPane.showOptionDialog(
197: SshToolsApplicationFrame.this , "Close "
198: + getTitle() + "?",
199: "Close Operation",
200: JOptionPane.YES_NO_OPTION,
201: JOptionPane.QUESTION_MESSAGE, null, null,
202: null);
203:
204: if (confirm == 0) {
205: hide();
206: }
207: }
208: }
209: });
210:
211: // If this is the first frame, center the window on the screen
212: Dimension screenSize = Toolkit.getDefaultToolkit()
213: .getScreenSize();
214: boolean found = false;
215:
216: if ((application != null)
217: && (application.getContainerCount() != 0)) {
218: for (int i = 0; (i < application.getContainerCount())
219: && !found; i++) {
220: SshToolsApplicationContainer c = application
221: .getContainerAt(i);
222:
223: if (c instanceof SshToolsApplicationFrame) {
224: SshToolsApplicationFrame f = (SshToolsApplicationFrame) c;
225: setSize(f.getSize());
226:
227: Point newLocation = new Point(f.getX(), f.getY());
228: newLocation.x += 48;
229: newLocation.y += 48;
230:
231: if (newLocation.x > (screenSize.getWidth() - 64)) {
232: newLocation.x = 0;
233: }
234:
235: if (newLocation.y > (screenSize.getHeight() - 64)) {
236: newLocation.y = 0;
237: }
238:
239: setLocation(newLocation);
240: found = true;
241: }
242: }
243: }
244:
245: if (!found) {
246: // Is there a previous stored geometry we can use?
247: if (PreferencesStore
248: .preferenceExists(PREF_LAST_FRAME_GEOMETRY)) {
249: setBounds(PreferencesStore.getRectangle(
250: PREF_LAST_FRAME_GEOMETRY, getBounds()));
251: } else {
252: pack();
253: UIUtil.positionComponent(SwingConstants.CENTER, this );
254: }
255: }
256: }
257:
258: /**
259: *
260: *
261: * @param title
262: */
263: public void setContainerTitle(String title) {
264: setTitle(title);
265: }
266:
267: /**
268: *
269: *
270: * @return
271: */
272: public SshToolsApplication getApplication() {
273: return application;
274: }
275:
276: /**
277: *
278: *
279: * @param visible
280: */
281: public void setContainerVisible(boolean visible) {
282: setVisible(visible);
283: }
284:
285: /**
286: *
287: *
288: * @return
289: */
290: public boolean isContainerVisible() {
291: return isVisible();
292: }
293:
294: /**
295: *
296: *
297: * @return
298: */
299: public SshToolsApplicationPanel getApplicationPanel() {
300: return panel;
301: }
302:
303: /**
304: *
305: */
306: public void closeContainer() {
307: /* If this is the last frame to close, then store its geometry for use
308: when the next frame opens */
309: if ((application != null)
310: && (application.getContainerCount() == 1)) {
311: PreferencesStore.putRectangle(PREF_LAST_FRAME_GEOMETRY,
312: getBounds());
313: }
314:
315: dispose();
316: getApplicationPanel().deregisterAction(newWindowAction);
317: getApplicationPanel().deregisterAction(exitAction);
318: getApplicationPanel().deregisterAction(aboutAction);
319: getApplicationPanel().rebuildActionComponents();
320: }
321: }
|