package ch.rakudave.jnetmap.controller;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URI;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import ch.rakudave.jnetmap.controller.command.CommandHistory;
import ch.rakudave.jnetmap.model.Map;
import ch.rakudave.jnetmap.util.IO;
import ch.rakudave.jnetmap.util.Icons;
import ch.rakudave.jnetmap.util.Lang;
import ch.rakudave.jnetmap.util.Settings;
import ch.rakudave.jnetmap.util.SwingHelper;
import ch.rakudave.jnetmap.util.logging.FileAppender;
import ch.rakudave.jnetmap.util.logging.Logger;
/**
* @author rakudave
*/
@SuppressWarnings("serial")
public class Actions {
// New file
public static Action newMap(String label) {
return new AbstractAction(label, Icons.get("new")) {
@Override
public void actionPerformed(ActionEvent e) {
Controller.getView().openMap(new Map());
}
};
}
// Open file
public static Action open(String label) {
return new AbstractAction(label, Icons.get("open")) {
@Override
public void actionPerformed(ActionEvent e) {
Object[] o = SwingHelper.openDialog((Component) Controller.getView(), new FileNameExtensionFilter("jNetMap file *.jnm", "jnm"), true);
if (o == null) return;
File f = (File) o[0];
if (f == null) return;
Map map = Controller.getMapFromFile(f.getAbsolutePath(), (String) o[1]);
if (map != null) {
Controller.open(map);
} else {
JOptionPane.showMessageDialog((Component) Controller.getView(), Lang.getNoHTML("message.fail.open"),
Lang.getNoHTML("message.error"), JOptionPane.ERROR_MESSAGE);
}
}
};
}
// Save file
public static Action save() {
return new AbstractAction(Lang.getNoHTML("menu.file.save"), Icons.get("save")) {
@Override
public void actionPerformed(ActionEvent e) {
Map map = Controller.getCurrentMap();
if (map == null) {
return;
} else if (Lang.getNoHTML("map.unsaved").equals(map.getFilePath())) {
saveAs().actionPerformed(null);
} else {
if (!map.save()) {
JOptionPane.showMessageDialog((Component) Controller.getView(), Lang.getNoHTML("message.fail.save"),
Lang.getNoHTML("message.error"), JOptionPane.ERROR_MESSAGE);
}
}
}
};
}
// Save As...
public static Action saveAs() {
return new AbstractAction(Lang.getNoHTML("menu.file.saveas"), Icons.get("save-as")) {
@Override
public void actionPerformed(ActionEvent e) {
Map map = Controller.getCurrentMap();
if (map == null) return;
Object[] o = SwingHelper.saveDialog((Component) Controller.getView(), new FileNameExtensionFilter("jNetMap file *.jnm", "jnm"), true);
if (o == null) return;
File f = (File) o[0];
if (f == null) return;
map.setFile(f);
map.setPassword((String) o[1]);
if (!map.save()) {
JOptionPane.showMessageDialog((Component) Controller.getView(), Lang.getNoHTML("message.fail.save"),
Lang.getNoHTML("message.error"), JOptionPane.ERROR_MESSAGE);
}
}
};
}
// Show preferences dialog
public static Action preferences(String label) {
return new AbstractAction(label, Icons.get("preferences")) {
@Override
public void actionPerformed(ActionEvent e) {
Controller.preferences.setVisible(true);
}
};
}
// Quit
public static Action quit(String label) {
return new AbstractAction(label, Icons.get("quit")) {
@Override
public void actionPerformed(ActionEvent e) {
if (!Controller.closeAllMaps()) return;
// save preferences
try {
Controller.getView().saveViewProperties();
} catch (Exception ex) {
Logger.error("Unable to save preferences", ex);
}
Settings.save();
Controller.getView().dispose();
try {
// pass close to InstanceDetector, he will System.exit()
// after closing the ports, threadpool etc...
String[] close = { "close" };
new ObjectOutputStream(new Socket(InetAddress.getLocalHost(), 61337).getOutputStream()).writeObject(close);
} catch (Exception ex) {
}
}
};
}
// Show documentation
public static Action viewDoc(String label) {
return new AbstractAction(label, Icons.get("help")) {
@Override
public void actionPerformed(ActionEvent e) {
Scheduler.execute(new Runnable() {
@Override
public void run() {
File pdf = new File(IO.userDir, "guide.pdf");
try {
Desktop.getDesktop().open(pdf);
} catch (Exception ex) {
Logger.error("Failed to open user guide: " + pdf.getAbsolutePath(), ex);
}
}
});
}
};
}
// Show website
public static Action viewWebsite() {
return new AbstractAction(Lang.getNoHTML("menu.help.website"), Icons.get("jnetmap_small")) {
@Override
public void actionPerformed(ActionEvent e) {
Scheduler.execute(new Runnable() {
@Override
public void run() {
openWebsite("http://jnetmap.sourceforge.net");
}
});
}
};
}
// report a bug
public static Action reportBug() {
return new AbstractAction(Lang.getNoHTML("menu.help.bug"), Icons.get("bug")) {
@Override
public void actionPerformed(ActionEvent e) {
Scheduler.execute(new Runnable() {
@Override
public void run() {
openWebsite("http://sourceforge.net/tracker/?func=add&group_id=162154&atid=822669");
}
});
}
};
}
// request a feature
public static Action requestFeature() {
return new AbstractAction(Lang.getNoHTML("menu.help.feature"), Icons.get("add")) {
@Override
public void actionPerformed(ActionEvent e) {
Scheduler.execute(new Runnable() {
@Override
public void run() {
openWebsite("http://sourceforge.net/tracker/?func=add&group_id=162154&atid=822672");
}
});
}
};
}
// contact developer
public static Action contactDeveloper() {
return new AbstractAction(Lang.getNoHTML("menu.help.contact"), Icons.get("mail")) {
@Override
public void actionPerformed(ActionEvent e) {
Scheduler.execute(new Runnable() {
@Override
public void run() {
String mail = "mailto:public@rakudave.ch?subject=jNetMap-"+
Controller.version.replaceAll(" ", "-")+
"&attachment="+FileAppender.logfile.getAbsolutePath();
try {
Desktop.getDesktop().mail(new URI(mail));
} catch (Exception ex) {
Logger.error("Failed to open mail-client: " + mail, ex);
openWebsite("http://jnetmap.sourceforge.net?file=kop6.php#mail");
}
}
});
}
};
}
public static void openWebsite(String url) {
try {
Desktop.getDesktop().browse(new URI(url));
} catch (Exception ex) {
Logger.error("Failed to open website: " + url, ex);
}
}
public static Action undo() {
return new AbstractAction(Lang.getNoHTML("menu.undo"), Icons.get("undo")) {
@Override
public void actionPerformed(ActionEvent e) {
CommandHistory history = Controller.getCurrentMap().getHistory();
try {
history.undo();
} catch (Exception ex) {
Logger.error("An error occured during the last 'undo'", ex);
}
}
};
}
public static Action redo() {
return new AbstractAction(Lang.getNoHTML("menu.redo"), Icons.get("redo")) {
@Override
public void actionPerformed(ActionEvent e) {
CommandHistory history = Controller.getCurrentMap().getHistory();
try {
history.redo();
} catch (Exception ex) {
Logger.error("An error occured during the last 'redo'", ex);
}
}
};
}
public static Action refresh() {
return new AbstractAction(Lang.getNoHTML("menu.view.refresh"), Icons.get("refresh")) {
@Override
public void actionPerformed(ActionEvent e) {
if (Controller.getCurrentMap() == null) return;
Scheduler.execute(new Runnable() {
@Override
public void run() {
StatusUpdater.refresh(Controller.getCurrentMap());
}
});
}
};
}
private Actions() {}
}