22 package com.realtime.crossfire.jxclient.window;
29 import java.io.BufferedReader;
30 import java.io.BufferedWriter;
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;
54 private static final Pattern
PATTERN = Pattern.compile(
" ");
68 final String skinName = skin.getSkinName();
69 final File dialogsFile;
72 }
catch (
final IOException ex) {
73 System.err.println(skin.getSkinName()+
": "+ex.getMessage());
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)) {
82 final String line = br.readLine();
87 final String[] tmp = PATTERN.split(line, -1);
88 if (tmp.length != 6) {
89 throw new IOException(
"syntax error: "+line);
103 throw new IOException(
"syntax error: "+line);
108 dialog = skin.getDialog(tmp[1]);
110 throw new IOException(
"no such dialog: "+tmp[1], ex);
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);
128 dialog.setSize(w, h);
129 }
catch (
final IllegalArgumentException ex) {
130 throw new IOException(
"invalid dialog size for "+tmp[1]+
": "+w+
"x"+h, ex);
137 windowRenderer.openDialog(dialog,
false);
139 windowRenderer.closeDialog(dialog);
145 }
catch (
final FileNotFoundException ignored) {
147 }
catch (
final IOException ex) {
148 System.err.println(dialogsFile+
": "+ex.getMessage());
158 final File dialogsFile;
161 }
catch (
final IOException ex) {
162 System.err.println(skin.getSkinName()+
": "+ex.getMessage());
166 final File dir = dialogsFile.getParentFile();
167 if (dir != null && !dir.exists() && !dir.mkdirs()) {
168 System.err.println(skin.getSkinName()+
": cannot create directory");
171 final List<Gui> openDialogs =
new LinkedList<>();
172 for (
final Gui dialog : windowRenderer.getOpenDialogs()) {
173 openDialogs.add(0, dialog);
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) {
184 for (
final Gui dialog : skin) {
185 if (!windowRenderer.isDialogOpen(dialog)) {
192 }
catch (
final IOException ex) {
193 System.err.println(dialogsFile+
": "+ex.getMessage());
204 private static void saveDialog(@NotNull
final Gui dialog, @NotNull
final String type, @NotNull
final Writer bw)
throws IOException {
205 if (dialog.isAutoSize()) {
209 if (!dialog.isSaveDialog()) {
213 final int w = dialog.getWidth();
218 final int h = dialog.getHeight();
225 bw.write(dialog.getName());
227 bw.write(Integer.toString(dialog.getX()));
229 bw.write(Integer.toString(dialog.getY()));
231 bw.write(Integer.toString(dialog.getWidth()));
233 bw.write(Integer.toString(dialog.getHeight()));
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.
Utility class to return references to settings files.
boolean isSaveDialog()
Returns whether this dialog retains its position across restarts.
Defines a JXClient skin consisting of a main Gui and zero or more dialog Guis.
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.
Renders a Gui instance into a Frame.
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.
boolean isAutoSize()
Returns whether this dialog is an auto-size dialog.
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.