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.transport.AbstractKnownHostsKeyVerification;
029: import com.sshtools.j2ssh.transport.InvalidHostFileException;
030: import com.sshtools.j2ssh.transport.publickey.SshPublicKey;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: import java.awt.BorderLayout;
036: import java.awt.Component;
037: import java.awt.GridBagConstraints;
038: import java.awt.GridBagLayout;
039: import java.awt.Insets;
040: import java.awt.event.ActionEvent;
041: import java.awt.event.ActionListener;
042:
043: import java.util.Iterator;
044: import java.util.Map;
045:
046: import javax.swing.AbstractListModel;
047: import javax.swing.BorderFactory;
048: import javax.swing.DefaultListCellRenderer;
049: import javax.swing.Icon;
050: import javax.swing.JButton;
051: import javax.swing.JList;
052: import javax.swing.JPanel;
053: import javax.swing.JScrollPane;
054: import javax.swing.event.ListSelectionEvent;
055: import javax.swing.event.ListSelectionListener;
056:
057: /**
058: *
059: *
060: * @author $author$
061: * @version $Revision: 1.15 $
062: */
063: public class HostsTab extends JPanel implements OptionsTab,
064: ActionListener {
065: /** */
066: public final static String GLOBAL_ICON = "/com/sshtools/common/ui/largeserveridentity.png";
067:
068: /** */
069: public final static String ALLOW_ICON = "/com/sshtools/common/ui/ok.png";
070:
071: /** */
072: public final static String DENY_ICON = "/com/sshtools/common/ui/cancel.png";
073:
074: /** */
075: public final static String REMOVE_ICON = "/com/sshtools/common/ui/remove.png";
076: private static Log log = LogFactory.getLog(HostsTab.class);
077:
078: //
079: private JList hosts;
080: private AbstractKnownHostsKeyVerification hostKeyVerifier;
081: private JButton remove;
082:
083: //private JButton deny;
084: private HostsListModel model;
085:
086: /**
087: * Creates a new HostsTab object.
088: *
089: * @param hostKeyVerifier
090: */
091: public HostsTab(AbstractKnownHostsKeyVerification hostKeyVerifier) {
092: super ();
093: this .hostKeyVerifier = hostKeyVerifier;
094: hosts = new JList(model = new HostsListModel());
095: hosts.setVisibleRowCount(10);
096: hosts.setCellRenderer(new HostRenderer());
097: hosts.addListSelectionListener(new ListSelectionListener() {
098: public void valueChanged(ListSelectionEvent evt) {
099: setAvailableActions();
100: }
101: });
102: remove = new JButton("Remove", new ResourceIcon(REMOVE_ICON));
103: remove.addActionListener(this );
104:
105: //deny = new JButton("Deny", new ResourceIcon(DENY_ICON));
106: //deny.addActionListener(this);
107: JPanel b = new JPanel(new GridBagLayout());
108: b.setBorder(BorderFactory.createEmptyBorder(0, 8, 0, 0));
109:
110: GridBagConstraints gbc = new GridBagConstraints();
111: gbc.fill = GridBagConstraints.HORIZONTAL;
112: gbc.insets = new Insets(0, 0, 4, 0);
113: gbc.anchor = GridBagConstraints.NORTH;
114: gbc.weightx = 1.0;
115: UIUtil
116: .jGridBagAdd(b, remove, gbc,
117: GridBagConstraints.REMAINDER);
118: gbc.weighty = 1.0;
119:
120: //UIUtil.jGridBagAdd(b, deny, gbc, GridBagConstraints.REMAINDER);
121: JPanel s = new JPanel(new BorderLayout());
122: s.add(new JScrollPane(hosts), BorderLayout.CENTER);
123: s.add(b, BorderLayout.EAST);
124:
125: IconWrapperPanel w = new IconWrapperPanel(new ResourceIcon(
126: GLOBAL_ICON), s);
127:
128: // This tab
129: setLayout(new BorderLayout());
130: add(w, BorderLayout.CENTER);
131: setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
132: reset();
133: }
134:
135: /**
136: *
137: *
138: * @param evt
139: */
140: public void actionPerformed(ActionEvent evt) {
141: if (evt.getSource() == remove) {
142: model.removeHostAt(hosts.getSelectedIndex());
143: }
144:
145: /*else if (evt.getSource() == deny) {
146: int i = hosts.getSelectedIndex();
147: HostWrapper w = model.getHostAt(i);
148: w.allow = false;
149: model.updateHostAt(i);
150: }*/
151: setAvailableActions();
152: }
153:
154: private void setAvailableActions() {
155: HostWrapper w = ((model.getSize() > 0) && (hosts
156: .getSelectedValues().length == 1)) ? model
157: .getHostAt(hosts.getSelectedIndex()) : null;
158: remove.setEnabled(w != null);
159:
160: //deny.setEnabled( (w != null) && w.allow);
161: }
162:
163: /**
164: *
165: */
166: public void reset() {
167: ((HostsListModel) hosts.getModel()).refresh();
168: setAvailableActions();
169: }
170:
171: /**
172: *
173: *
174: * @return
175: */
176: public String getTabContext() {
177: return "Options";
178: }
179:
180: /**
181: *
182: *
183: * @return
184: */
185: public Icon getTabIcon() {
186: return null;
187: }
188:
189: /**
190: *
191: *
192: * @return
193: */
194: public String getTabTitle() {
195: return "Hosts";
196: }
197:
198: /**
199: *
200: *
201: * @return
202: */
203: public String getTabToolTipText() {
204: return "Allowed and denied hosts.";
205: }
206:
207: /**
208: *
209: *
210: * @return
211: */
212: public int getTabMnemonic() {
213: return 'h';
214: }
215:
216: /**
217: *
218: *
219: * @return
220: */
221: public Component getTabComponent() {
222: return this ;
223: }
224:
225: /**
226: *
227: *
228: * @return
229: */
230: public boolean validateTab() {
231: return true;
232: }
233:
234: /**
235: *
236: */
237: public void applyTab() {
238: try {
239: Map map = hostKeyVerifier.allowedHosts();
240: String[] hosts = new String[map.keySet().size()];
241: map.keySet().toArray(hosts);
242: log
243: .debug("Checking if any allowed hosts need to be removed");
244:
245: for (int i = hosts.length - 1; i >= 0; i--) {
246: if (log.isDebugEnabled()) {
247: log.debug("Looking for host " + hosts[i]);
248: }
249:
250: HostWrapper w = model.getHost(hosts[i]);
251:
252: if (w != null) {
253: if (log.isDebugEnabled()) {
254: log.debug("Found host " + hosts[i]);
255: }
256:
257: if (!w.allow) {
258: if (log.isDebugEnabled()) {
259: log.debug("Denying host " + hosts[i]);
260: }
261:
262: hostKeyVerifier.removeAllowedHost(hosts[i]);
263:
264: //hostKeyVerifier.denyHost(hosts[i], true);
265: }
266: } else {
267: if (log.isDebugEnabled()) {
268: log.debug("Host removed " + hosts[i]);
269: }
270:
271: hostKeyVerifier.removeAllowedHost(hosts[i]);
272: }
273: }
274:
275: /*java.util.List list = hostKeyVerifier.deniedHosts();
276: log.debug("Checking if any denied hosts need to be removed");
277: for (int i = list.size() - 1; i >= 0; i--) {
278: String h = (String) list.get(i);
279: if (log.isDebugEnabled()) {
280: log.debug("Looking for host " + h);
281: }
282: HostWrapper w = model.getHost(h);
283: if (w == null) {
284: if (log.isDebugEnabled()) {
285: log.debug("Removing host " + h);
286: }
287: hostKeyVerifier.removeDeniedHost(h);
288: }
289: }*/
290: hostKeyVerifier.saveHostFile();
291: } catch (InvalidHostFileException ihfe) {
292: log.error("Failed to store hosts file.", ihfe);
293: }
294: }
295:
296: /**
297: *
298: */
299: public void tabSelected() {
300: }
301:
302: class HostRenderer extends DefaultListCellRenderer {
303: Icon allowIcon;
304: Icon denyIcon;
305:
306: public HostRenderer() {
307: allowIcon = new ResourceIcon(ALLOW_ICON);
308: denyIcon = new ResourceIcon(DENY_ICON);
309: }
310:
311: public Component getListCellRendererComponent(JList list,
312: Object value, int index, boolean isSelected,
313: boolean cellHasFocus) {
314: super .getListCellRendererComponent(list, value, index,
315: isSelected, cellHasFocus);
316:
317: HostWrapper w = (HostWrapper) value;
318: setIcon(w.allow ? allowIcon : denyIcon);
319: setText(w.host);
320:
321: return this ;
322: }
323: }
324:
325: class HostWrapper {
326: boolean allow;
327: String host;
328: SshPublicKey key;
329: Map keys;
330:
331: HostWrapper(boolean allow, String host, Map keys) {
332: this .allow = allow;
333: this .host = host;
334: this .keys = keys;
335: }
336: }
337:
338: class HostsListModel extends AbstractListModel {
339: java.util.List hosts;
340:
341: public HostsListModel() {
342: hosts = new java.util.ArrayList();
343: refresh();
344: }
345:
346: public void refresh() {
347: hosts.clear();
348:
349: Map map = hostKeyVerifier.allowedHosts();
350:
351: for (Iterator i = map.keySet().iterator(); i.hasNext();) {
352: String k = (String) i.next();
353: Map keys = (Map) map.get(k);
354: hosts.add(new HostWrapper(true, k, keys));
355: }
356:
357: /* java.util.List list = hostKeyVerifier.deniedHosts();
358: for (Iterator i = list.iterator(); i.hasNext(); ) {
359: String h = (String) i.next();
360: hosts.add(new HostWrapper(false, h, null));
361: }*/
362: fireContentsChanged(this , 0, getSize() - 1);
363: }
364:
365: public HostWrapper getHost(String name) {
366: for (Iterator i = hosts.iterator(); i.hasNext();) {
367: HostWrapper w = (HostWrapper) i.next();
368:
369: if (w.host.equals(name)) {
370: return w;
371: }
372: }
373:
374: return null;
375: }
376:
377: public int getSize() {
378: return hosts.size();
379: }
380:
381: public Object getElementAt(int index) {
382: return hosts.get(index);
383: }
384:
385: public HostWrapper getHostAt(int index) {
386: return (HostWrapper) hosts.get(index);
387: }
388:
389: public void removeHostAt(int index) {
390: hosts.remove(index);
391: fireIntervalRemoved(this , index, index);
392: }
393:
394: public void updateHostAt(int index) {
395: fireContentsChanged(this, index, index);
396: }
397: }
398: }
|