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.configuration.SshToolsConnectionProfile;
029:
030: import com.sshtools.j2ssh.configuration.ConfigurationLoader;
031: import com.sshtools.j2ssh.connection.ChannelEventListener;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: import java.awt.LayoutManager;
037:
038: import java.io.File;
039: import java.io.IOException;
040:
041: import java.util.Comparator;
042:
043: import javax.swing.SwingConstants;
044:
045: /**
046: *
047: *
048: * @author $author$
049: * @version $Revision: 1.12 $
050: */
051: public abstract class SshToolsApplicationSessionPanel extends
052: SshToolsApplicationPanel {
053: /** */
054: public final static String PREF_CONNECTION_FILE_DIRECTORY = "sshapps.connectionFile.directory";
055:
056: /** */
057: protected Log log = LogFactory
058: .getLog(SshToolsApplicationSessionPanel.class);
059:
060: /** */
061: protected SshToolsConnectionProfile currentConnectionProfile;
062:
063: /** */
064: protected SessionManager manager;
065:
066: /**
067: * Creates a new SshToolsApplicationClientPanel object.
068: */
069: public SshToolsApplicationSessionPanel() {
070: super ();
071: }
072:
073: /**
074: * Creates a new SshToolsApplicationClientPanel object.
075: *
076: * @param mgr
077: */
078: public SshToolsApplicationSessionPanel(LayoutManager mgr) {
079: super (mgr);
080: }
081:
082: /**
083: *
084: *
085: * @return
086: */
087: public abstract SshToolsConnectionTab[] getAdditionalConnectionTabs();
088:
089: public abstract void addEventListener(
090: ChannelEventListener eventListener);
091:
092: public abstract boolean requiresConfiguration();
093:
094: public abstract String getId();
095:
096: /**
097: *
098: *
099: * @param manager
100: * @param profile
101: *
102: * @throws IOException
103: */
104: public final boolean openSession(SessionManager manager,
105: SshToolsConnectionProfile profile) throws IOException {
106: this .manager = manager;
107:
108: // Set the current connection properties
109: setCurrentConnectionProfile(profile);
110:
111: if (requiresConfiguration()
112: && !profile.getApplicationPropertyBoolean(getId()
113: + ".configured", false)) {
114: if (!editSettings(profile)) {
115: return false;
116: }
117: }
118:
119: return onOpenSession();
120: }
121:
122: /**
123: *
124: *
125: * @throws IOException
126: */
127: public abstract boolean onOpenSession() throws IOException;
128:
129: /**
130: *
131: *
132: * @return
133: */
134: public boolean isConnected() {
135: return (manager != null) && manager.isConnected();
136: }
137:
138: /**
139: *
140: *
141: * @param file
142: */
143: public void setContainerTitle(File file) {
144: String verString = "";
145:
146: if (application != null) {
147: verString = ConfigurationLoader.getVersionString(
148: application.getApplicationName(), application
149: .getApplicationVersion());
150: }
151:
152: if (container != null) {
153: container.setContainerTitle((file == null) ? verString
154: : (verString + " [" + file.getName() + "]"));
155: }
156: }
157:
158: /**
159: *
160: *
161: * @param profile
162: */
163: public void setCurrentConnectionProfile(
164: SshToolsConnectionProfile profile) {
165: currentConnectionProfile = profile;
166: }
167:
168: /**
169: *
170: *
171: * @return
172: */
173: public SshToolsConnectionProfile getCurrentConnectionProfile() {
174: return currentConnectionProfile;
175: }
176:
177: /**
178: *
179: *
180: * @param profile
181: *
182: * @return
183: */
184: public boolean editSettings(SshToolsConnectionProfile profile) {
185: final SshToolsConnectionPanel panel = new SshToolsConnectionPanel(
186: false);
187: SshToolsConnectionTab[] tabs = getAdditionalConnectionTabs();
188:
189: for (int i = 0; (tabs != null) && (i < tabs.length); i++) {
190: tabs[i].setConnectionProfile(profile);
191: panel.addTab(tabs[i]);
192: }
193:
194: panel.setConnectionProfile(profile);
195:
196: final Option ok = new Option("Ok",
197: "Apply the settings and close this dialog", 'o');
198: final Option cancel = new Option("Cancel",
199: "Close this dialog without applying the settings", 'c');
200: OptionCallback callback = new OptionCallback() {
201: public boolean canClose(OptionsDialog dialog, Option option) {
202: if (option == ok) {
203: return panel.validateTabs();
204: }
205:
206: return true;
207: }
208: };
209:
210: OptionsDialog od = OptionsDialog.createOptionDialog(
211: SshToolsApplicationSessionPanel.this , new Option[] {
212: ok, cancel }, panel, "Connection Settings", ok,
213: callback, null);
214: od.pack();
215: UIUtil.positionComponent(SwingConstants.CENTER, od);
216: od.setVisible(true);
217:
218: if (od.getSelectedOption() == ok) {
219: // Apply the changes to the profile
220: panel.applyTabs();
221:
222: // Ask the session manager to apply them to persistence
223: manager.applyProfileChanges(profile);
224:
225: return true;
226: }
227:
228: return false;
229: }
230:
231: /*public static class ActionMenu
232: implements Comparable {
233: int weight;
234: int mnemonic;
235: String name;
236: String displayName;
237: public ActionMenu(String name, String displayName, int mnemonic,
238: int weight) {
239: this.name = name;
240: this.displayName = displayName;
241: this.mnemonic = mnemonic;
242: this.weight = weight;
243: }
244: public int compareTo(Object o) {
245: int i = new Integer(weight).compareTo(new Integer(
246: ( (ActionMenu) o).weight));
247: return (i == 0)
248: ? displayName.compareTo( ( (ActionMenu) o).displayName) : i;
249: }
250: }*/
251: class ToolBarActionComparator implements Comparator {
252: public int compare(Object o1, Object o2) {
253: int i = ((Integer) ((StandardAction) o1)
254: .getValue(StandardAction.TOOLBAR_GROUP))
255: .compareTo((Integer) ((StandardAction) o2)
256: .getValue(StandardAction.TOOLBAR_GROUP));
257:
258: return (i == 0) ? ((Integer) ((StandardAction) o1)
259: .getValue(StandardAction.TOOLBAR_WEIGHT))
260: .compareTo((Integer) ((StandardAction) o2)
261: .getValue(StandardAction.TOOLBAR_WEIGHT))
262: : i;
263: }
264: }
265:
266: class MenuItemActionComparator implements Comparator {
267: public int compare(Object o1, Object o2) {
268: int i = ((Integer) ((StandardAction) o1)
269: .getValue(StandardAction.MENU_ITEM_GROUP))
270: .compareTo((Integer) ((StandardAction) o2)
271: .getValue(StandardAction.MENU_ITEM_GROUP));
272:
273: return (i == 0) ? ((Integer) ((StandardAction) o1)
274: .getValue(StandardAction.MENU_ITEM_WEIGHT))
275: .compareTo((Integer) ((StandardAction) o2)
276: .getValue(StandardAction.MENU_ITEM_WEIGHT))
277: : i;
278: }
279: }
280: }
|