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