package com.traxel.lumbermill.desk;
import com.traxel.lumbermill.ClientMill;
import com.traxel.lumbermill.Mill;
import com.traxel.lumbermill.ServerMill;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
public abstract class MillAction extends AbstractAction {
private final MillDesktopControl desktopControl;
public MillAction(
final String name,
final MillDesktopControl desktopControl) {
super(name);
this.desktopControl = desktopControl;
}
@Override
final Mill mill;
mill = getMill();
if (mill != null) {
desktopControl.add(mill.getMillFrame());
} else {
System.err.println("Null Mill? Probably unparseable settings.");
}
}
protected String
getSettings(
final String message,
final String defaultValue) {
return JOptionPane.showInputDialog(desktopControl.getDesktop(),
message,
defaultValue);
}
private static final String NAME = "New Server Mill";
private static final String MESSAGE = "Please enter the port to listen on.\n"
+ "Range is 1025 - 65535.";
private static final String DEFAULT = "4445";
super(NAME, desktopControl);
}
@Override
final String portString;
portString = getSettings(MESSAGE, DEFAULT);
try {
final int port = Integer.parseInt(portString);
return new ServerMill(port);
} catch (NumberFormatException e) {
System.err.println("NumberFormatException");
}
return null;
}
}
private static final String NAME = "New Client Mill";
private static final String MESSAGE = "Please enter the host and port to listen to.\n"
+ "<host>:<port>, 'apps.server.com:4545',"
+ " '127.0.0.1:6500'";
private static final String DEFAULT = "localhost:4545";
super(NAME, desktopControl);
}
@Override
final String hostPort;
final String host;
final String portString;
final String[] parts;
hostPort = getSettings(MESSAGE, DEFAULT);
parts = hostPort.split(":");
if ((parts != null) && (parts.length == 2)) {
host = parts[0];
portString = parts[1];
try {
final int port = Integer.parseInt(portString);
return new ClientMill(host, port);
} catch (NumberFormatException e) {
System.err.println("NumberFormatException: "
+ portString);
}
} else {
System.err.println("Didn't split on ':' correctly: "
+ hostPort);
}
return null;
}
}
}