1 /*
2 * SSHTools - Java SSH2 API
3 *
4 * Copyright (C) 2002-2003 Lee David Painter and Contributors.
5 *
6 * Contributions made by:
7 *
8 * Brett Smith
9 * Richard Pernavas
10 * Erwin Bolwidt
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26 package com.sshtools.common.ui;
27
28 import com.sshtools.j2ssh.configuration.ConfigurationLoader;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import java.awt.BorderLayout;
34 import java.awt.GridLayout;
35 import java.awt.event.ComponentAdapter;
36 import java.awt.event.ComponentEvent;
37
38 import java.beans.PropertyChangeEvent;
39 import java.beans.PropertyVetoException;
40 import java.beans.VetoableChangeListener;
41
42 import javax.swing.JFrame;
43 import javax.swing.JInternalFrame;
44 import javax.swing.JOptionPane;
45 import javax.swing.JPanel;
46 import javax.swing.JSeparator;
47
48
49 /**
50 *
51 *
52 * @author $author$
53 * @version $Revision: 1.4 $
54 */
55 public class SshToolsApplicationInternalFrame extends JInternalFrame
56 implements SshToolsApplicationContainer {
57 // Preference names
58
59 /** */
60 public final static String PREF_LAST_FRAME_GEOMETRY = "application.lastFrameGeometry";
61
62 /** */
63 protected Log log = LogFactory.getLog(SshToolsApplicationInternalFrame.class);
64
65 /** */
66 protected StandardAction exitAction;
67
68 /** */
69 protected StandardAction aboutAction;
70
71 /** */
72 protected StandardAction newWindowAction;
73
74 /** */
75 protected JSeparator toolSeparator;
76
77 //
78 private SshToolsApplicationPanel panel;
79 private SshToolsApplication application;
80 private boolean showAboutBox = true;
81 private boolean showExitAction = true;
82 private boolean showNewWindowAction = true;
83 private boolean showMenu = true;
84
85 public void showAboutBox(boolean showAboutBox) {
86 this.showAboutBox = showAboutBox;
87 }
88
89 public void showExitAction(boolean showExitAction) {
90 this.showExitAction = showExitAction;
91 }
92
93 public void showNewWindowAction(boolean showNewWindowAction) {
94 this.showNewWindowAction = showNewWindowAction;
95 }
96
97 /**
98 *
99 *
100 * @param application
101 * @param panel
102 *
103 * @throws SshToolsApplicationException
104 */
105 public void init(final SshToolsApplication application,
106 SshToolsApplicationPanel panel) throws SshToolsApplicationException {
107 this.panel = panel;
108 this.application = application;
109
110 if (application != null) {
111 setTitle(ConfigurationLoader.getVersionString(
112 application.getApplicationName(),
113 application.getApplicationVersion())); // + " " + application.getApplicationVersion());
114 }
115
116 // Register the File menu
117 panel.registerActionMenu(new SshToolsApplicationPanel.ActionMenu(
118 "File", "File", 'f', 0));
119
120 // Register the Exit action
121 if (showExitAction && (application != null)) {
122 panel.registerAction(exitAction = new ExitAction(application, this));
123
124 // Register the New Window Action
125 }
126
127 if (showNewWindowAction && (application != null)) {
128 panel.registerAction(newWindowAction = new NewWindowAction(
129 application));
130
131 // Register the Help menu
132 }
133
134 panel.registerActionMenu(new SshToolsApplicationPanel.ActionMenu(
135 "Help", "Help", 'h', 99));
136
137 // Register the About box action
138 if (showAboutBox && (application != null)) {
139 panel.registerAction(aboutAction = new AboutAction(this, application));
140 }
141
142 getApplicationPanel().rebuildActionComponents();
143
144 JPanel p = new JPanel(new BorderLayout());
145
146 if (panel.getJMenuBar() != null) {
147 setJMenuBar(panel.getJMenuBar());
148 }
149
150 if (panel.getToolBar() != null) {
151 JPanel t = new JPanel(new BorderLayout());
152 t.add(panel.getToolBar(), BorderLayout.NORTH);
153 t.add(toolSeparator = new JSeparator(JSeparator.HORIZONTAL),
154 BorderLayout.SOUTH);
155 toolSeparator.setVisible(panel.getToolBar().isVisible());
156
157 final SshToolsApplicationPanel pnl = panel;
158 panel.getToolBar().addComponentListener(new ComponentAdapter() {
159 public void componentHidden(ComponentEvent evt) {
160 log.debug("Tool separator is now " +
161 pnl.getToolBar().isVisible());
162 toolSeparator.setVisible(pnl.getToolBar().isVisible());
163 }
164 });
165 p.add(t, BorderLayout.NORTH);
166 }
167
168 p.add(panel, BorderLayout.CENTER);
169
170 if (panel.getStatusBar() != null) {
171 p.add(panel.getStatusBar(), BorderLayout.SOUTH);
172 }
173
174 getContentPane().setLayout(new GridLayout(1, 1));
175 getContentPane().add(p);
176
177 // Watch for the frame closing
178 //setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
179 addVetoableChangeListener(new VetoableChangeListener() {
180 public void vetoableChange(PropertyChangeEvent evt)
181 throws PropertyVetoException {
182 if (application != null) {
183 application.closeContainer(SshToolsApplicationInternalFrame.this);
184 } else {
185 if (evt.getPropertyName().equals(IS_CLOSED_PROPERTY)) {
186 boolean changed = ((Boolean) evt.getNewValue()).booleanValue();
187
188 if (changed) {
189 int confirm = JOptionPane.showOptionDialog(SshToolsApplicationInternalFrame.this,
190 "Close " + getTitle() + "?",
191 "Close Operation",
192 JOptionPane.YES_NO_OPTION,
193 JOptionPane.QUESTION_MESSAGE, null,
194 null, null);
195
196 if (confirm == 0) {
197 SshToolsApplicationInternalFrame.this.getDesktopPane()
198 .remove(SshToolsApplicationInternalFrame.this);
199 }
200 }
201 }
202 }
203 }
204 });
205
206 /*this.addWindowListener(new WindowAdapter() {
207 public void windowClosing(WindowEvent evt) {
208 if(application!=null)
209 application.closeContainer(SshToolsApplicationFrame.this);
210 else
211 hide();
212 }
213 });
214 // If this is the first frame, center the window on the screen
215 /*Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
216 boolean found = false;
217 if (application!=null && application.getContainerCount() != 0) {
218 for (int i = 0; (i < application.getContainerCount()) && !found;
219 i++) {
220 SshToolsApplicationContainer c = application.getContainerAt(i);
221 if (c instanceof SshToolsApplicationFrame) {
222 SshToolsApplicationFrame f = (SshToolsApplicationFrame) c;
223 setSize(f.getSize());
224 Point newLocation = new Point(f.getX(), f.getY());
225 newLocation.x += 48;
226 newLocation.y += 48;
227 if (newLocation.x > (screenSize.getWidth() - 64)) {
228 newLocation.x = 0;
229 }
230 if (newLocation.y > (screenSize.getHeight() - 64)) {
231 newLocation.y = 0;
232 }
233 setLocation(newLocation);
234 found = true;
235 }
236 }
237 }
238 if (!found) {
239 // Is there a previous stored geometry we can use?
240 if (PreferencesStore.preferenceExists(PREF_LAST_FRAME_GEOMETRY)) {
241 setBounds(PreferencesStore.getRectangle(
242 PREF_LAST_FRAME_GEOMETRY, getBounds()));
243 }
244 else {
245 pack();
246 UIUtil.positionComponent(SwingConstants.CENTER, this);
247 }
248 }*/
249 pack();
250 }
251
252 /**
253 *
254 *
255 * @param title
256 */
257 public void setContainerTitle(String title) {
258 setTitle(title);
259 }
260
261 /**
262 *
263 *
264 * @return
265 */
266 public SshToolsApplication getApplication() {
267 return application;
268 }
269
270 /**
271 *
272 *
273 * @param visible
274 */
275 public void setContainerVisible(boolean visible) {
276 setVisible(visible);
277 }
278
279 /**
280 *
281 *
282 * @return
283 */
284 public boolean isContainerVisible() {
285 return isVisible();
286 }
287
288 /**
289 *
290 *
291 * @return
292 */
293 public SshToolsApplicationPanel getApplicationPanel() {
294 return panel;
295 }
296
297 /**
298 *
299 */
300 public void closeContainer() {
301 /* If this is the last frame to close, then store its geometry for use
302 when the next frame opens */
303 if ((application != null) && (application.getContainerCount() == 1)) {
304 PreferencesStore.putRectangle(PREF_LAST_FRAME_GEOMETRY, getBounds());
305 }
306
307 dispose();
308 getApplicationPanel().deregisterAction(newWindowAction);
309 getApplicationPanel().deregisterAction(exitAction);
310 getApplicationPanel().deregisterAction(aboutAction);
311 getApplicationPanel().rebuildActionComponents();
312 }
313 }