186 lines
6.0 KiB
Java

package mod_quicklaunch;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import basics.BasicGuiApp;
import basics.BasicMod;
import guis.MainGui;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import main.Start;
import manager.SettingManager;
import scenes.SettingScene;
import update.Updater;
public class QuickLaunch extends BasicMod {
private File textfile;
private BufferedReader br;
private BufferedWriter bw;
private ArrayList<Shortcut> shortcuts;
private String tmpSh = ".";
private String tmpPa = ".";
public void init() {
try {
this.setModName("QL");
System.out.println("QL: Starting QuickLaunch");
this.textfile = new File(SettingManager.getJarDirectory()+File.separator+"File.txt");
System.out.println("QL: File name: File.txt");
if (!this.textfile.exists()) {
System.out.println("QL: There ist no File");
this.textfile.createNewFile();
System.out.println("QL: File.txt created");
}
shortcuts = new ArrayList<Shortcut>();
System.out.println("QL: Finished Initialisation");
if(SettingManager.isLoadFileOnBoot()){
loadFile();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
public void checkInput(String input) throws IOException {
if (input.equalsIgnoreCase("/cos")){
this.centerOnScreen();
} else if (input.equalsIgnoreCase("/cu")) {
this.checkForUpdates();
} else if (input.equalsIgnoreCase("exit")) {
System.exit(-1);
} else if ((input.equalsIgnoreCase("help")) || (input.equalsIgnoreCase("/?"))) {
this.writeHelp();
} else if (input.equalsIgnoreCase("loadFile") || input.equalsIgnoreCase("/lf")) {
this.loadFile();
} else if (input.equalsIgnoreCase("saveFile") || input.equalsIgnoreCase("/sf")) {
this.saveFile(false);
}else if (input.equalsIgnoreCase("/s")) {
this.showShortcuts();
} else if (input.equalsIgnoreCase("getPath")|| input.equalsIgnoreCase("/gp")) {
this.getFilePath();
} else if (input.equalsIgnoreCase("/v")) {
this.QLversion();
} else if (input.equalsIgnoreCase("/c")) {
this.showSettings();
} else {
int i = 0;
for (Shortcut sh : shortcuts) {
if (input.equalsIgnoreCase(sh.getShortcut())){
i = shortcuts.indexOf(sh);
//Runtime.getRuntime().exec("cmd /c \"" + shortcuts.get(i).getPath() + "\"");
ProcessBuilder pb = new ProcessBuilder(TokenConverter.convert(shortcuts.get(i).getPath()));
pb.start();
System.out.println("'" + shortcuts.get(i).getPath() + "' started");
//MainGui.addNotification("'" + shortcuts.get(i).getPath() + "' started", 2);
break;
}
}
}
}
private void centerOnScreen() {
BasicGuiApp.mainStage.centerOnScreen();
}
private void checkForUpdates() {
new Thread(new Runnable() {
public void run() {
Updater up = new Updater();
up.checkForUpdate();
}
}).start();
}
private void loadFile() throws IOException {
System.out.println("loading shortcuts");
this.br = new BufferedReader(new FileReader(this.textfile));
shortcuts.clear();
tmpSh = "";
tmpPa = "";
while (this.tmpSh != null) {
this.tmpSh = this.br.readLine();
if (this.tmpSh == null) break;
System.out.println(this.tmpSh);
tmpPa = this.br.readLine();
System.out.println(this.tmpPa);
shortcuts.add(new Shortcut(tmpSh, tmpPa));
System.out.println("shortcut added");
}
br.close();
try{
MainGui.addNotification("All shortcuts loaded", 2);
}catch(NullPointerException npe){
//will always fail when file gets read on boot
}
}
private void saveFile(boolean silent) throws IOException {
this.bw = new BufferedWriter(new FileWriter(this.textfile));
int i = 0;
while (i <= shortcuts.size() - 1) {
this.bw.write(shortcuts.get(i).getShortcut());
this.bw.newLine();
this.bw.write(shortcuts.get(i).getPath());
this.bw.newLine();
++i;
}
this.bw.close();
if(!silent) {
MainGui.addNotification("Data has been written", 2);
}
}
private void showShortcuts() {
Stage stage = new Stage();
ShortcutWindow sw = new ShortcutWindow(shortcuts);
Scene s = new Scene(sw);
stage.setScene(s);
stage.setResizable(false);
stage.initStyle(StageStyle.UNDECORATED);
stage.showAndWait();
try {
saveFile(true);
} catch (IOException e) {
e.printStackTrace();
}
}
private void getFilePath(){
MainGui.addNotification("Path: " + textfile.getAbsolutePath(), 6);
}
private void QLversion() {
MainGui.addNotification("QuickLaunch version: " + Start.VERSION , 5);
}
private void writeHelp() {
Stage stage = new Stage();
HelpWindow hw = new HelpWindow();
Scene s = new Scene(hw);
stage.setTitle("Help dialog");
stage.setResizable(false);
stage.setScene(s);
stage.showAndWait();
}
private void showSettings() {
SettingScene sc = new SettingScene();
BasicGuiApp.mainStage.setScene(sc);
}
}