Crossfire JXClient, Trunk
DialogStateParser.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 
30 import java.io.BufferedReader;
31 import java.io.BufferedWriter;
32 import java.io.IOException;
33 import java.io.Writer;
34 import java.nio.charset.StandardCharsets;
35 import java.nio.file.Files;
36 import java.nio.file.NoSuchFileException;
37 import java.nio.file.Path;
38 import java.util.LinkedList;
39 import java.util.List;
40 import java.util.regex.Pattern;
41 import org.jetbrains.annotations.NotNull;
42 
47 public class DialogStateParser {
48 
52  @NotNull
53  private static final Pattern PATTERN = Pattern.compile(" ");
54 
58  private DialogStateParser() {
59  }
60 
66  public static void load(@NotNull final JXCSkin skin, @NotNull final JXCWindowRenderer windowRenderer) {
67  final String skinName = skin.getSkinName();
68  final Path dialogsFile;
69  try {
70  dialogsFile = Filenames.getDialogsFile(skinName);
71  } catch (final IOException ex) {
72  System.err.println(skin.getSkinName()+": "+ex.getMessage());
73  return;
74  }
75 
76  try {
77  try (BufferedReader br = Files.newBufferedReader(dialogsFile, StandardCharsets.UTF_8)) {
78  while (true) {
79  final String line = br.readLine();
80  if (line == null) {
81  break;
82  }
83 
84  final String[] tmp = PATTERN.split(line, -1);
85  if (tmp.length != 6) {
86  throw new IOException("syntax error: "+line);
87  }
88 
89  final boolean open = switch (tmp[0]) {
90  case "open" -> true;
91  case "close" -> false;
92  default -> throw new IOException("syntax error: "+line);
93  };
94 
95  final Gui dialog;
96  try {
97  dialog = skin.getDialog(tmp[1]);
98  } catch (final JXCSkinException ex) {
99  throw new IOException("no such dialog: "+tmp[1], ex);
100  }
101 
102  if (!dialog.isAutoSize() && dialog.isSaveDialog()) {
103  final int x;
104  final int y;
105  final int w;
106  final int h;
107  try {
108  x = Integer.parseInt(tmp[2]);
109  y = Integer.parseInt(tmp[3]);
110  w = Integer.parseInt(tmp[4]);
111  h = Integer.parseInt(tmp[5]);
112  } catch (final NumberFormatException ex) {
113  throw new IOException("syntax error: "+line, ex);
114  }
115 
116  dialog.setBounds(x, y, w, h, windowRenderer.getWindowWidth(), windowRenderer.getWindowHeight());
117  }
118 
119  if (open) {
120  windowRenderer.openDialog(dialog, false);
121  } else {
122  windowRenderer.closeDialog(dialog);
123  }
124  }
125  }
126  } catch (final NoSuchFileException ignored) {
127  // ignore
128  } catch (final IOException ex) {
129  System.err.println(dialogsFile+": "+ex.getMessage());
130  }
131  }
132 
138  public static void save(@NotNull final JXCSkin skin, @NotNull final JXCWindowRenderer windowRenderer) {
139  final Path dialogsFile;
140  try {
141  dialogsFile = Filenames.getDialogsFile(skin.getSkinName());
142  } catch (final IOException ex) {
143  System.err.println(skin.getSkinName()+": "+ex.getMessage());
144  return;
145  }
146 
147  final Path dir = dialogsFile.getParent();
148  if (dir != null) {
149  try {
150  Files.createDirectories(dir);
151  } catch (final IOException ex) {
152  System.err.println(skin.getSkinName()+": cannot create directory: "+ex);
153  }
154  }
155 
156  final List<Gui> openDialogs = new LinkedList<>();
157  for (Gui dialog : windowRenderer.getOpenDialogs()) {
158  openDialogs.add(0, dialog);
159  }
160 
161  try {
162  try (BufferedWriter bw = Files.newBufferedWriter(dialogsFile, StandardCharsets.UTF_8)) {
163  for (Gui dialog : openDialogs) {
164  saveDialog(dialog, "open", bw);
165  }
166 
167  for (Gui dialog : skin) {
168  if (!windowRenderer.isDialogOpen(dialog)) {
169  saveDialog(dialog, "close", bw);
170  }
171  }
172  }
173  } catch (final IOException ex) {
174  System.err.println(dialogsFile+": "+ex.getMessage());
175  }
176  }
177 
185  private static void saveDialog(@NotNull final Gui dialog, @NotNull final String type, @NotNull final Writer bw) throws IOException {
186  if (dialog.isAutoSize()) {
187  return;
188  }
189 
190  if (!dialog.isSaveDialog()) {
191  return;
192  }
193 
194  final int w = dialog.getComponent().getWidth();
195  if (w <= 0) {
196  return;
197  }
198 
199  final int h = dialog.getComponent().getHeight();
200  if (h <= 0) {
201  return;
202  }
203 
204  bw.write(type);
205  bw.write(" ");
206  bw.write(dialog.getComponent().getName());
207  bw.write(" ");
208  bw.write(Integer.toString(dialog.getComponent().getX()));
209  bw.write(" ");
210  bw.write(Integer.toString(dialog.getComponent().getY()));
211  bw.write(" ");
212  bw.write(Integer.toString(dialog.getComponent().getWidth()));
213  bw.write(" ");
214  bw.write(Integer.toString(dialog.getComponent().getHeight()));
215  bw.write("\n");
216  }
217 
218 }
com.realtime.crossfire.jxclient.window.DialogStateParser.load
static void load(@NotNull final JXCSkin skin, @NotNull final JXCWindowRenderer windowRenderer)
Loads the dialogs state from a file.
Definition: DialogStateParser.java:66
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.gui.gui.Gui.isAutoSize
boolean isAutoSize()
Returns whether this dialog is an auto-size dialog.
Definition: Gui.java:179
com.realtime.crossfire.jxclient.skin.skin
Definition: DefaultJXCSkin.java:23
com.realtime.crossfire.jxclient.gui.gui.Gui
Combines a list of GUIElements to for a gui.
Definition: Gui.java:49
com.realtime.crossfire.jxclient.window.DialogStateParser.saveDialog
static void saveDialog(@NotNull final Gui dialog, @NotNull final String type, @NotNull final Writer bw)
Saves the state of one dialog.
Definition: DialogStateParser.java:185
com.realtime.crossfire.jxclient.settings.Filenames.getDialogsFile
static Path getDialogsFile(@NotNull final String skinName)
Returns the file for storing dialog related information for a skin.
Definition: Filenames.java:164
com.realtime.crossfire.jxclient.skin
com.realtime.crossfire.jxclient.skin.skin.JXCSkinException
Exception thrown if a skin related problem occurs.
Definition: JXCSkinException.java:31
com.realtime.crossfire.jxclient.settings
Definition: CommandHistory.java:23
com.realtime.crossfire.jxclient.window.DialogStateParser.save
static void save(@NotNull final JXCSkin skin, @NotNull final JXCWindowRenderer windowRenderer)
Saves the dialogs state to a file.
Definition: DialogStateParser.java:138
com.realtime.crossfire.jxclient.window.DialogStateParser.PATTERN
static final Pattern PATTERN
The pattern to split fields in the save file.
Definition: DialogStateParser.java:53
com.realtime.crossfire.jxclient.settings.Filenames
Utility class to return references to settings files.
Definition: Filenames.java:37
com.realtime.crossfire.jxclient.window.DialogStateParser
Utility class to store or restore the dialog states to/from a file.
Definition: DialogStateParser.java:47
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.gui.misc.JXCWindowRenderer
Renders a Gui instance into a Frame.
Definition: JXCWindowRenderer.java:87
com.realtime.crossfire.jxclient.gui.gui.Gui.setBounds
void setBounds(final int x, final int y, final int width, final int height, final int windowWidth, final int windowHeight)
Sets the position and size of this dialog.
Definition: Gui.java:641
com.realtime.crossfire.jxclient.gui.gui
Definition: AbstractGUIElement.java:23
com.realtime.crossfire
com.realtime
com.realtime.crossfire.jxclient.skin.skin.JXCSkin
Defines a JXClient skin consisting of a main Gui and zero or more dialog Guis.
Definition: JXCSkin.java:42
com
com.realtime.crossfire.jxclient.gui.misc
Definition: GUICheckBox.java:23
com.realtime.crossfire.jxclient.window.DialogStateParser.DialogStateParser
DialogStateParser()
Private constructor to prevent instantiation.
Definition: DialogStateParser.java:58
com.realtime.crossfire.jxclient.gui.gui.Gui.isSaveDialog
boolean isSaveDialog()
Returns whether this dialog retains its position across restarts.
Definition: Gui.java:583