00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.window;
00023
00024 import com.realtime.crossfire.jxclient.settings.Filenames;
00025 import com.realtime.crossfire.jxclient.shortcuts.Shortcut;
00026 import com.realtime.crossfire.jxclient.shortcuts.ShortcutCommand;
00027 import com.realtime.crossfire.jxclient.shortcuts.ShortcutSpell;
00028 import com.realtime.crossfire.jxclient.shortcuts.Shortcuts;
00029 import java.io.BufferedReader;
00030 import java.io.BufferedWriter;
00031 import java.io.File;
00032 import java.io.FileInputStream;
00033 import java.io.FileNotFoundException;
00034 import java.io.FileOutputStream;
00035 import java.io.IOException;
00036 import java.io.InputStreamReader;
00037 import java.io.OutputStreamWriter;
00038 import org.jetbrains.annotations.NotNull;
00039
00044 public class ShortcutsLoader {
00045
00049 private ShortcutsLoader() {
00050 }
00051
00058 public static void loadShortcuts(@NotNull final Shortcuts shortcuts, @NotNull final CharSequence hostname, @NotNull final CharSequence character) {
00059 final File file;
00060 try {
00061 file = Filenames.getShortcutsFile(hostname, character);
00062 } catch (final IOException ex) {
00063 System.err.println("Cannot read shortcuts file: "+ex.getMessage());
00064 return;
00065 }
00066
00067 try {
00068 shortcuts.clearShortcuts();
00069 try {
00070 final FileInputStream fis = new FileInputStream(file);
00071 try {
00072 final InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
00073 try {
00074 final BufferedReader br = new BufferedReader(isr);
00075 try {
00076 int index = 0;
00077 while (true) {
00078 final String line = br.readLine();
00079 if (line == null) {
00080 break;
00081 }
00082
00083 if (line.equals("empty")) {
00084 shortcuts.setShortcut(index, null);
00085 index++;
00086 } else if (line.startsWith("spell cast ")) {
00087 shortcuts.setSpellShortcut(index, line.substring(11).trim(), true);
00088 index++;
00089 } else if (line.startsWith("spell invoke ")) {
00090 shortcuts.setSpellShortcut(index, line.substring(13).trim(), false);
00091 index++;
00092 } else if (line.startsWith("command ")) {
00093 shortcuts.setCommandShortcut(index, line.substring(8).trim());
00094 index++;
00095 } else {
00096 System.err.println("shortcut: ignoring undefined entry '"+line+"'");
00097 }
00098 }
00099 } finally {
00100 br.close();
00101 }
00102 } finally {
00103 isr.close();
00104 }
00105 } finally {
00106 fis.close();
00107 }
00108 } catch (final IOException ex) {
00109 shortcuts.clearShortcuts();
00110 shortcuts.resetModified();
00111 shortcuts.setFile(file);
00112 throw ex;
00113 }
00114 shortcuts.resetModified();
00115 shortcuts.setFile(file);
00116 } catch (final FileNotFoundException ignored) {
00117
00118 return;
00119 } catch (final IOException ex) {
00120 System.err.println("Cannot read shortcuts file "+file+": "+ex.getMessage());
00121
00122 return;
00123 }
00124 }
00125
00130 public static void saveShortcuts(@NotNull final Shortcuts shortcuts) {
00131 try {
00132 if (!shortcuts.isModified()) {
00133 return;
00134 }
00135
00136 final File file = shortcuts.getFile();
00137 if (file == null) {
00138 return;
00139 }
00140
00141 final FileOutputStream fos = new FileOutputStream(file);
00142 try {
00143 final OutputStreamWriter osw = new OutputStreamWriter(fos);
00144 try {
00145 final BufferedWriter bw = new BufferedWriter(osw);
00146 try {
00147 for (final Shortcut shortcut : shortcuts) {
00148 if (shortcut == null) {
00149 bw.write("empty\n");
00150 } else if (shortcut instanceof ShortcutSpell) {
00151 final ShortcutSpell shortcutSpell = (ShortcutSpell)shortcut;
00152 bw.write("spell ");
00153 bw.write(shortcutSpell.isCast() ? "cast " : "invoke ");
00154 bw.write(shortcutSpell.getSpell().getName());
00155 bw.write("\n");
00156 } else if (shortcut instanceof ShortcutCommand) {
00157 final ShortcutCommand shortcutCommand = (ShortcutCommand)shortcut;
00158 bw.write("command ");
00159 bw.write(shortcutCommand.getCommand());
00160 bw.write("\n");
00161 } else {
00162 throw new AssertionError();
00163 }
00164 }
00165 } finally {
00166 bw.close();
00167 }
00168 } finally {
00169 osw.close();
00170 }
00171 } finally {
00172 fos.close();
00173 }
00174 } catch (final IOException ex) {
00175 System.err.println("Cannot write shortcuts file "+shortcuts.getFile()+": "+ex.getMessage());
00176 return;
00177 }
00178
00179 shortcuts.resetModified();
00180 }
00181
00182 }