Crossfire JXClient, Trunk  R20561
ShortcutsLoader.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.window;
23 
29 import java.io.BufferedReader;
30 import java.io.BufferedWriter;
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.FileNotFoundException;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.InputStreamReader;
37 import java.io.OutputStreamWriter;
38 import org.jetbrains.annotations.NotNull;
39 
44 public class ShortcutsLoader {
45 
49  private ShortcutsLoader() {
50  }
51 
58  public static void loadShortcuts(@NotNull final Shortcuts shortcuts, @NotNull final CharSequence hostname, @NotNull final CharSequence character) {
59  final File file;
60  try {
61  file = Filenames.getShortcutsFile(hostname, character);
62  } catch (final IOException ex) {
63  System.err.println("Cannot read shortcuts file: "+ex.getMessage());
64  return;
65  }
66 
67  try {
68  shortcuts.clearShortcuts();
69  try {
70  try (final FileInputStream fis = new FileInputStream(file)) {
71  try (final InputStreamReader isr = new InputStreamReader(fis, "UTF-8")) {
72  try (final BufferedReader br = new BufferedReader(isr)) {
73  loadShortcuts(shortcuts, br);
74  }
75  }
76  }
77  } catch (final IOException ex) {
78  shortcuts.clearShortcuts();
79  shortcuts.resetModified();
80  shortcuts.setFile(file);
81  throw ex;
82  }
83  shortcuts.resetModified();
84  shortcuts.setFile(file);
85  } catch (final FileNotFoundException ignored) {
86  //noinspection UnnecessaryReturnStatement
87  return;
88  } catch (final IOException ex) {
89  System.err.println("Cannot read shortcuts file "+file+": "+ex.getMessage());
90  //noinspection UnnecessaryReturnStatement
91  return;
92  }
93  }
94 
101  private static void loadShortcuts(@NotNull final Shortcuts shortcuts, @NotNull final BufferedReader br) throws IOException {
102  int index = 0;
103  while (true) {
104  final String line = br.readLine();
105  if (line == null) {
106  break;
107  }
108 
109  if (line.equals("empty")) {
110  shortcuts.setShortcut(index, null);
111  index++;
112  } else if (line.startsWith("spell cast ")) {
113  shortcuts.setSpellShortcut(index, line.substring(11).trim(), true);
114  index++;
115  } else if (line.startsWith("spell invoke ")) {
116  shortcuts.setSpellShortcut(index, line.substring(13).trim(), false);
117  index++;
118  } else if (line.startsWith("command ")) {
119  shortcuts.setCommandShortcut(index, line.substring(8).trim());
120  index++;
121  } else {
122  System.err.println("shortcut: ignoring undefined entry '"+line+"'");
123  }
124  }
125  }
126 
131  public static void saveShortcuts(@NotNull final Shortcuts shortcuts) {
132  try {
133  if (!shortcuts.isModified()) {
134  return;
135  }
136 
137  final File file = shortcuts.getFile();
138  if (file == null) {
139  return;
140  }
141 
142  try (final FileOutputStream fos = new FileOutputStream(file)) {
143  try (final OutputStreamWriter osw = new OutputStreamWriter(fos)) {
144  try (final BufferedWriter bw = new BufferedWriter(osw)) {
145  for (final Shortcut shortcut : shortcuts) {
146  if (shortcut == null) {
147  bw.write("empty\n");
148  } else if (shortcut instanceof ShortcutSpell) {
149  final ShortcutSpell shortcutSpell = (ShortcutSpell)shortcut;
150  bw.write("spell ");
151  bw.write(shortcutSpell.isCast() ? "cast " : "invoke ");
152  bw.write(shortcutSpell.getSpell().getName());
153  bw.write("\n");
154  } else if (shortcut instanceof ShortcutCommand) {
155  final ShortcutCommand shortcutCommand = (ShortcutCommand)shortcut;
156  bw.write("command ");
157  bw.write(shortcutCommand.getCommand());
158  bw.write("\n");
159  } else {
160  throw new AssertionError();
161  }
162  }
163  }
164  }
165  }
166  } catch (final IOException ex) {
167  System.err.println("Cannot write shortcuts file "+shortcuts.getFile()+": "+ex.getMessage());
168  return;
169  }
170 
171  shortcuts.resetModified();
172  }
173 
174 }
String getName()
Returns the spell name.
Definition: Spell.java:197
Utility class to return references to settings files.
Definition: Filenames.java:34
static File getShortcutsFile(@NotNull final CharSequence hostname, @NotNull final CharSequence character)
Returns the shortcuts file.
Definition: Filenames.java:107
static void loadShortcuts(@NotNull final Shortcuts shortcuts, @NotNull final CharSequence hostname, @NotNull final CharSequence character)
Load shortcut info from the backing file.
static void saveShortcuts(@NotNull final Shortcuts shortcuts)
Save all shortcut info to the backing file.
ShortcutsLoader()
Private constructor to prevent instantiation.
String getCommand()
Returns the command to execute.
boolean isCast()
Returns whether the spell should be "cast" or "invoked".
static void loadShortcuts(@NotNull final Shortcuts shortcuts, @NotNull final BufferedReader br)
Load shortcut info.
Abstract base class for shortcut commands.
Definition: Shortcut.java:32
A Shortcut that executes a Crossfire command.