Crossfire JXClient, Trunk  R20561
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-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 java.io.Writer;
39 import java.util.LinkedList;
40 import java.util.List;
41 import java.util.regex.Pattern;
42 import org.jetbrains.annotations.NotNull;
43 
48 public class DialogStateParser {
49 
53  @NotNull
54  private static final Pattern PATTERN = Pattern.compile(" ");
55 
59  private DialogStateParser() {
60  }
61 
67  public static void load(@NotNull final JXCSkin skin, @NotNull final JXCWindowRenderer windowRenderer) {
68  final String skinName = skin.getSkinName();
69  final File dialogsFile;
70  try {
71  dialogsFile = Filenames.getDialogsFile(skinName);
72  } catch (final IOException ex) {
73  System.err.println(skin.getSkinName()+": "+ex.getMessage());
74  return;
75  }
76 
77  try {
78  try (final FileInputStream fis = new FileInputStream(dialogsFile)) {
79  try (final InputStreamReader isr = new InputStreamReader(fis, "UTF-8")) {
80  try (final BufferedReader br = new BufferedReader(isr)) {
81  while (true) {
82  final String line = br.readLine();
83  if (line == null) {
84  break;
85  }
86 
87  final String[] tmp = PATTERN.split(line, -1);
88  if (tmp.length != 6) {
89  throw new IOException("syntax error: "+line);
90  }
91 
92  final boolean open;
93  switch (tmp[0]) {
94  case "open":
95  open = true;
96  break;
97 
98  case "close":
99  open = false;
100  break;
101 
102  default:
103  throw new IOException("syntax error: "+line);
104  }
105 
106  final Gui dialog;
107  try {
108  dialog = skin.getDialog(tmp[1]);
109  } catch (final JXCSkinException ex) {
110  throw new IOException("no such dialog: "+tmp[1], ex);
111  }
112 
113  if (!dialog.isAutoSize() && dialog.isSaveDialog()) {
114  final int x;
115  final int y;
116  final int w;
117  final int h;
118  try {
119  x = Integer.parseInt(tmp[2]);
120  y = Integer.parseInt(tmp[3]);
121  w = Integer.parseInt(tmp[4]);
122  h = Integer.parseInt(tmp[5]);
123  } catch (final NumberFormatException ex) {
124  throw new IOException("syntax error: "+line, ex);
125  }
126 
127  try {
128  dialog.setSize(w, h);
129  } catch (final IllegalArgumentException ex) {
130  throw new IOException("invalid dialog size for "+tmp[1]+": "+w+"x"+h, ex);
131  }
132 
133  dialog.setPosition(x, y);
134  }
135 
136  if (open) {
137  windowRenderer.openDialog(dialog, false);
138  } else {
139  windowRenderer.closeDialog(dialog);
140  }
141  }
142  }
143  }
144  }
145  } catch (final FileNotFoundException ignored) {
146  // ignore
147  } catch (final IOException ex) {
148  System.err.println(dialogsFile+": "+ex.getMessage());
149  }
150  }
151 
157  public static void save(@NotNull final JXCSkin skin, @NotNull final JXCWindowRenderer windowRenderer) {
158  final File dialogsFile;
159  try {
160  dialogsFile = Filenames.getDialogsFile(skin.getSkinName());
161  } catch (final IOException ex) {
162  System.err.println(skin.getSkinName()+": "+ex.getMessage());
163  return;
164  }
165 
166  final File dir = dialogsFile.getParentFile();
167  if (dir != null && !dir.exists() && !dir.mkdirs()) {
168  System.err.println(skin.getSkinName()+": cannot create directory");
169  }
170 
171  final List<Gui> openDialogs = new LinkedList<>();
172  for (final Gui dialog : windowRenderer.getOpenDialogs()) {
173  openDialogs.add(0, dialog);
174  }
175 
176  try {
177  try (final FileOutputStream fos = new FileOutputStream(dialogsFile)) {
178  try (final OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8")) {
179  try (final BufferedWriter bw = new BufferedWriter(osw)) {
180  for (final Gui dialog : openDialogs) {
181  saveDialog(dialog, "open", bw);
182  }
183 
184  for (final Gui dialog : skin) {
185  if (!windowRenderer.isDialogOpen(dialog)) {
186  saveDialog(dialog, "close", bw);
187  }
188  }
189  }
190  }
191  }
192  } catch (final IOException ex) {
193  System.err.println(dialogsFile+": "+ex.getMessage());
194  }
195  }
196 
204  private static void saveDialog(@NotNull final Gui dialog, @NotNull final String type, @NotNull final Writer bw) throws IOException {
205  if (dialog.isAutoSize()) {
206  return;
207  }
208 
209  if (!dialog.isSaveDialog()) {
210  return;
211  }
212 
213  final int w = dialog.getWidth();
214  if (w <= 0) {
215  return;
216  }
217 
218  final int h = dialog.getHeight();
219  if (h <= 0) {
220  return;
221  }
222 
223  bw.write(type);
224  bw.write(" ");
225  bw.write(dialog.getName());
226  bw.write(" ");
227  bw.write(Integer.toString(dialog.getX()));
228  bw.write(" ");
229  bw.write(Integer.toString(dialog.getY()));
230  bw.write(" ");
231  bw.write(Integer.toString(dialog.getWidth()));
232  bw.write(" ");
233  bw.write(Integer.toString(dialog.getHeight()));
234  bw.write("\n");
235  }
236 
237 }
static void save(@NotNull final JXCSkin skin, @NotNull final JXCWindowRenderer windowRenderer)
Saves the dialogs state to a file.
static final Pattern PATTERN
The pattern to split fields in the save file.
Combines a list of GUIElements to for a gui.
Definition: Gui.java:43
Utility class to return references to settings files.
Definition: Filenames.java:34
boolean isSaveDialog()
Returns whether this dialog retains its position across restarts.
Definition: Gui.java:550
Defines a JXClient skin consisting of a main Gui and zero or more dialog Guis.
Definition: JXCSkin.java:40
Utility class to store or restore the dialog states to/from a file.
void setPosition(final int x, final int y)
Sets the position of this dialog.
Definition: Gui.java:132
static void saveDialog(@NotNull final Gui dialog, @NotNull final String type, @NotNull final Writer bw)
Saves the state of one dialog.
static File getDialogsFile(@NotNull final String skinName)
Returns the file for storing dialog related information for a skin.
Definition: Filenames.java:148
boolean isAutoSize()
Returns whether this dialog is an auto-size dialog.
Definition: Gui.java:159
static void load(@NotNull final JXCSkin skin, @NotNull final JXCWindowRenderer windowRenderer)
Loads the dialogs state from a file.
DialogStateParser()
Private constructor to prevent instantiation.
Exception thrown if a skin related problem occurs.