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;
90  switch (tmp[0]) {
91  case "open":
92  open = true;
93  break;
94 
95  case "close":
96  open = false;
97  break;
98 
99  default:
100  throw new IOException("syntax error: "+line);
101  }
102 
103  final Gui dialog;
104  try {
105  dialog = skin.getDialog(tmp[1]);
106  } catch (final JXCSkinException ex) {
107  throw new IOException("no such dialog: "+tmp[1], ex);
108  }
109 
110  if (!dialog.isAutoSize() && dialog.isSaveDialog()) {
111  final int x;
112  final int y;
113  final int w;
114  final int h;
115  try {
116  x = Integer.parseInt(tmp[2]);
117  y = Integer.parseInt(tmp[3]);
118  w = Integer.parseInt(tmp[4]);
119  h = Integer.parseInt(tmp[5]);
120  } catch (final NumberFormatException ex) {
121  throw new IOException("syntax error: "+line, ex);
122  }
123 
124  dialog.setBounds(x, y, w, h, windowRenderer.getWindowWidth(), windowRenderer.getWindowHeight());
125  }
126 
127  if (open) {
128  windowRenderer.openDialog(dialog, false);
129  } else {
130  windowRenderer.closeDialog(dialog);
131  }
132  }
133  }
134  } catch (final NoSuchFileException ignored) {
135  // ignore
136  } catch (final IOException ex) {
137  System.err.println(dialogsFile+": "+ex.getMessage());
138  }
139  }
140 
146  public static void save(@NotNull final JXCSkin skin, @NotNull final JXCWindowRenderer windowRenderer) {
147  final Path dialogsFile;
148  try {
149  dialogsFile = Filenames.getDialogsFile(skin.getSkinName());
150  } catch (final IOException ex) {
151  System.err.println(skin.getSkinName()+": "+ex.getMessage());
152  return;
153  }
154 
155  final Path dir = dialogsFile.getParent();
156  if (dir != null) {
157  try {
158  Files.createDirectories(dir);
159  } catch (final IOException ex) {
160  System.err.println(skin.getSkinName()+": cannot create directory: "+ex);
161  }
162  }
163 
164  final List<Gui> openDialogs = new LinkedList<>();
165  for (Gui dialog : windowRenderer.getOpenDialogs()) {
166  openDialogs.add(0, dialog);
167  }
168 
169  try {
170  try (BufferedWriter bw = Files.newBufferedWriter(dialogsFile, StandardCharsets.UTF_8)) {
171  for (Gui dialog : openDialogs) {
172  saveDialog(dialog, "open", bw);
173  }
174 
175  for (Gui dialog : skin) {
176  if (!windowRenderer.isDialogOpen(dialog)) {
177  saveDialog(dialog, "close", bw);
178  }
179  }
180  }
181  } catch (final IOException ex) {
182  System.err.println(dialogsFile+": "+ex.getMessage());
183  }
184  }
185 
193  private static void saveDialog(@NotNull final Gui dialog, @NotNull final String type, @NotNull final Writer bw) throws IOException {
194  if (dialog.isAutoSize()) {
195  return;
196  }
197 
198  if (!dialog.isSaveDialog()) {
199  return;
200  }
201 
202  final int w = dialog.getComponent().getWidth();
203  if (w <= 0) {
204  return;
205  }
206 
207  final int h = dialog.getComponent().getHeight();
208  if (h <= 0) {
209  return;
210  }
211 
212  bw.write(type);
213  bw.write(" ");
214  bw.write(dialog.getComponent().getName());
215  bw.write(" ");
216  bw.write(Integer.toString(dialog.getComponent().getX()));
217  bw.write(" ");
218  bw.write(Integer.toString(dialog.getComponent().getY()));
219  bw.write(" ");
220  bw.write(Integer.toString(dialog.getComponent().getWidth()));
221  bw.write(" ");
222  bw.write(Integer.toString(dialog.getComponent().getHeight()));
223  bw.write("\n");
224  }
225 
226 }
com.realtime.crossfire.jxclient.gui.gui.Gui.isSaveDialog
boolean isSaveDialog()
Definition: Gui.java:603
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.skin.skin
Definition: DefaultJXCSkin.java:23
com.realtime.crossfire.jxclient.gui.gui.Gui.isAutoSize
boolean isAutoSize()
Definition: Gui.java:179
com.realtime.crossfire.jxclient.gui.gui.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)
Definition: DialogStateParser.java:193
com.realtime.crossfire.jxclient.skin
com.realtime.crossfire.jxclient.settings.Filenames.getDialogsFile
static Path getDialogsFile(@NotNull final String skinName)
Definition: Filenames.java:164
com.realtime.crossfire.jxclient.window.DialogStateParser.PATTERN
static final Pattern PATTERN
Definition: DialogStateParser.java:53
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)
Definition: Gui.java:662
com.realtime.crossfire.jxclient.window.DialogStateParser.DialogStateParser
DialogStateParser()
Definition: DialogStateParser.java:58
com.realtime.crossfire.jxclient.skin.skin.JXCSkinException
Definition: JXCSkinException.java:31
com.realtime.crossfire.jxclient.window.DialogStateParser.save
static void save(@NotNull final JXCSkin skin, @NotNull final JXCWindowRenderer windowRenderer)
Definition: DialogStateParser.java:146
com.realtime.crossfire.jxclient.settings
Definition: CommandHistory.java:23
com.realtime.crossfire.jxclient.gui.misc.JXCWindowRenderer
Definition: JXCWindowRenderer.java:87
com.realtime.crossfire.jxclient.skin.skin.JXCSkin
Definition: JXCSkin.java:42
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.window.DialogStateParser
Definition: DialogStateParser.java:47
com.realtime.crossfire.jxclient.gui.gui
Definition: AbstractGUIElement.java:23
com.realtime.crossfire
com.realtime
com
com.realtime.crossfire.jxclient.window.DialogStateParser.load
static void load(@NotNull final JXCSkin skin, @NotNull final JXCWindowRenderer windowRenderer)
Definition: DialogStateParser.java:66
com.realtime.crossfire.jxclient.gui.misc
Definition: GUICheckBox.java:23
com.realtime.crossfire.jxclient.settings.Filenames
Definition: Filenames.java:37