/*
* Copyright 2006 Magnus Weinberg
*
* This file is part of Eden.
*
* Eden is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Eden is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Eden; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package frontend;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import main.Prefs;
import net.FileSocket;
import net.NetFuncs;
/**
* This class handles the events generated by menus, popups, etc of the gui
*
* @author Magnus Weinberg
*/
public class ActionHandler implements ActionListener {
/*
* The gui generating the events
*/
private AwtGui gui;
/**
* Create an class for handling of our gui events.
*
* @param gui
*/
public ActionHandler(AwtGui gui) {
this.gui = gui;
}
/*
* Runs haltNow() on all active transfers in the list
*/
public void stopAll() {
Enumeration enumer = AwtShared.connections.elements();
while (enumer.hasMoreElements()) {
Object listElem = enumer.nextElement();
if (listElem instanceof FileSocket) { //Ensures sanity
FileSocket fs = ((FileSocket) listElem);
fs.reportError = false;
fs.haltNow();
}
}
}
/*
* Actionhandling
*/
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() instanceof JMenuItem) {
String menuText = ((JMenuItem) arg0.getSource()).getText();
if (menuText.equals("Stop all transfers")) {
if (JOptionPane.showConfirmDialog(gui, "Are you sure?",
"Please confirm.", JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION) {
stopAll();
}
} else if (menuText.equals("Exit")) {
gui.dispose();
} else if (menuText.equals("Send as server")) {
transferInit(NetFuncs.SEND_AS_SERVER, menuText);
} else if (menuText.equals("Receive as server")) {
transferInit(NetFuncs.RECEIVE_AS_SERVER, menuText);
} else if (menuText.equals("Send as client")) {
transferInit(NetFuncs.SEND_AS_CLIENT, menuText);
} else if (menuText.equals("Receive as client")) {
transferInit(NetFuncs.RECEIVE_AS_CLIENT, menuText);
} else if (menuText.equals("Clear list")) {
AwtShared.clearLogs();
} else if (menuText.equals("Abort transfer(s)")) {
try {
if (JOptionPane.showConfirmDialog(gui, "Are you sure?",
"Please confirm.", JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION) {
int[] selectedIx = AwtShared.conList
.getSelectedIndices();
for (int i = 0; i < selectedIx.length; i++) {
FileSocket sel = (FileSocket) AwtShared.conList
.getModel().getElementAt(selectedIx[i]);
sel.reportError = false;
sel.haltNow();
}
}
} catch (HeadlessException he) {
he.printStackTrace();
}
} else if (menuText.equals("About")) {
JOptionPane.showMessageDialog(gui,
"Eden - File transfer suite\n" + "Version: 0.2.0\n"
+ "---------------------------------------\n"
+ "Developer: Magnus Weinberg\n"
+ "Email: magnus.weinberg@home.se\n"
+ "---------------------------------------\n"
+ "Program analyst: Patrik Larsson\n"
+ "Email: patrik.mrx@gmail.com\n"
+ "---------------------------------------\n"
+ "Occasional debugger: Tobias Olsson\n"
+ "Email: tobias@olsson.be", "About Eden",
JOptionPane.PLAIN_MESSAGE);
} else if (menuText.equals("Users manual")) {
JEditorPane manualPane = new JEditorPane();
manualPane.setContentType("text/html");
manualPane.setText(AwtShared.htmlManual);
manualPane.setEditable(false);
manualPane.setCaretPosition(0);
JFrame manualFrame = new JFrame("Users manual");
manualFrame.getContentPane().add(new JScrollPane(manualPane));
manualFrame.setSize(700, 400);
manualFrame.setLocationRelativeTo(gui);
manualFrame.setLocation(50, 50);
manualFrame
.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
manualFrame.setVisible(true);
}
}
}
/*
* Shows a dialog with settings for transfers, then an opendialog for
* file/directory selection and if everything goes as it should, a
* FileSocket is created.
*
* @param type The type of transfer to get settings for. Can be: <br>
* NetFuncs.SEND_AS_CLIENT, <br> NetFuncs.SEND_AS_SERVER, <br>
* NetFuncs.RECEIVE_AS_CLIENT, <br> NetFuncs.RECEIVE_AS_SERVER @param text
* The text caption of the transfer dialog
*/
private void transferInit(int type, String text) {
JTextField port = new JTextField(Prefs.defaultPort);
JTextField host = new JTextField(Prefs.defaultHost);
JPasswordField pass = new JPasswordField();
CheckBoxWithTextField cbwtf = new CheckBoxWithTextField(
"Crypt with password", false, pass);
pass.setEchoChar('*');
JPanel dialog = new JPanel();
if (type == NetFuncs.RECEIVE_AS_SERVER
|| type == NetFuncs.SEND_AS_SERVER) {
dialog.setLayout(new GridLayout(2, 2));
dialog.add(new JLabel("Port:"));
dialog.add(port);
dialog.add(cbwtf);
dialog.add(pass);
} else { //receive or send as client
dialog.setLayout(new GridLayout(3, 2));
dialog.add(new JLabel("Host:"));
dialog.add(host);
dialog.add(new JLabel("Port:"));
dialog.add(port);
dialog.add(cbwtf);
dialog.add(pass);
}
if (JOptionPane.showConfirmDialog(gui, dialog, text + "?",
JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION) {
JFileChooser fc;
if (type == NetFuncs.RECEIVE_AS_SERVER
|| type == NetFuncs.RECEIVE_AS_CLIENT) {
fc = new JFileChooser(Prefs.defaultDlDir);
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setDialogTitle("Select download location");
} else {
fc = new JFileChooser(Prefs.defaultUlDir);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setDialogTitle("Select file to send");
}
if (fc.showOpenDialog(gui) == JFileChooser.APPROVE_OPTION) {
String fileOrDir = fc.getSelectedFile().getPath();
switch (type) {
case NetFuncs.RECEIVE_AS_SERVER:
Prefs.defaultDlDir = fileOrDir;
break;
case NetFuncs.RECEIVE_AS_CLIENT:
Prefs.defaultDlDir = fileOrDir;
Prefs.defaultHost = host.getText();
break;
case NetFuncs.SEND_AS_SERVER:
Prefs.defaultUlDir = NetFuncs.dirnameFromString(fileOrDir);
break;
case NetFuncs.SEND_AS_CLIENT:
Prefs.defaultUlDir = NetFuncs.dirnameFromString(fileOrDir);
Prefs.defaultHost = host.getText();
break;
}
Prefs.defaultPort = port.getText();
new FileSocket(host.getText(), port.getText(), fileOrDir, type,
cbwtf.isSelected(), new String(pass.getPassword()));
}
}
}
}