Crossfire JXClient, Trunk
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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.window;
24 
29 import java.io.BufferedReader;
30 import java.io.BufferedWriter;
31 import java.io.IOException;
32 import java.nio.charset.StandardCharsets;
33 import java.nio.file.Files;
34 import java.nio.file.NoSuchFileException;
35 import java.nio.file.Path;
36 import org.jetbrains.annotations.NotNull;
37 
42 public class ShortcutsLoader {
43 
47  private ShortcutsLoader() {
48  }
49 
56  public static void loadShortcuts(@NotNull final Shortcuts shortcuts, @NotNull final CharSequence hostname, @NotNull final CharSequence character) {
57  final Path file;
58  try {
59  file = Filenames.getShortcutsFile(hostname, character);
60  } catch (final IOException ex) {
61  System.err.println("Cannot read shortcuts file: "+ex.getMessage());
62  return;
63  }
64 
65  try {
66  shortcuts.clearShortcuts();
67  shortcuts.setFile(file);
68  try (BufferedReader br = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
69  for (final ShortcutSlot shortcutSlot : ShortcutSlot.values()) {
70  final String line = br.readLine();
71  shortcuts.setShortcutString(shortcutSlot, line == null ? "" : line, false);
72  }
73  // ignore excess entries in file
74  }
75  } catch (final NoSuchFileException ignored) {
76  //noinspection UnnecessaryReturnStatement
77  return;
78  } catch (final IOException ex) {
79  System.err.println("Cannot read shortcuts file "+file+": "+ex.getMessage());
80  //noinspection UnnecessaryReturnStatement
81  return;
82  }
83  }
84 
90  public static void saveShortcuts(@NotNull final Path file, @NotNull final Iterable<Shortcut> shortcuts) {
91  try {
92  try (BufferedWriter bw = Files.newBufferedWriter(file)) {
93  for (Shortcut shortcut : shortcuts) {
94  if (shortcut == null) {
95  bw.write("\n");
96  } else {
97  bw.write(shortcut.getCommand());
98  bw.write("\n");
99  }
100  }
101  }
102  } catch (final IOException ex) {
103  System.err.println("Cannot write shortcuts file "+file+": "+ex.getMessage());
104  //noinspection UnnecessaryReturnStatement
105  return;
106  }
107  }
108 
109 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.shortcuts.Shortcuts
Definition: Shortcuts.java:43
com.realtime.crossfire.jxclient.window.ShortcutsLoader.saveShortcuts
static void saveShortcuts(@NotNull final Path file, @NotNull final Iterable< Shortcut > shortcuts)
Definition: ShortcutsLoader.java:90
com.realtime.crossfire.jxclient.settings
Definition: CommandHistory.java:23
com.realtime.crossfire.jxclient.shortcuts.Shortcuts.file
Path file
Definition: Shortcuts.java:56
com.realtime.crossfire.jxclient.window.ShortcutsLoader.loadShortcuts
static void loadShortcuts(@NotNull final Shortcuts shortcuts, @NotNull final CharSequence hostname, @NotNull final CharSequence character)
Definition: ShortcutsLoader.java:56
com.realtime.crossfire.jxclient.window.ShortcutsLoader
Definition: ShortcutsLoader.java:42
com.realtime.crossfire.jxclient.shortcuts
Definition: Shortcut.java:23
com.realtime.crossfire.jxclient.window.ShortcutsLoader.ShortcutsLoader
ShortcutsLoader()
Definition: ShortcutsLoader.java:47
com.realtime.crossfire
com.realtime
com.realtime.crossfire.jxclient.shortcuts.ShortcutSlot
Definition: ShortcutSlot.java:8
com
com.realtime.crossfire.jxclient.shortcuts.Shortcut
Definition: Shortcut.java:35
com.realtime.crossfire.jxclient.settings.Filenames.getShortcutsFile
static Path getShortcutsFile(@NotNull final CharSequence hostname, @NotNull final CharSequence character)
Definition: Filenames.java:110
com.realtime.crossfire.jxclient.settings.Filenames
Definition: Filenames.java:37