Gridarta Editor
ConfirmErrorsDialog.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.dialog.gameobjectattributes;
21 
22 import java.awt.Component;
23 import java.awt.Container;
24 import java.awt.FlowLayout;
25 import java.awt.Frame;
26 import java.awt.GridLayout;
27 import java.awt.event.ActionListener;
28 import java.awt.event.WindowEvent;
29 import java.awt.event.WindowListener;
30 import javax.swing.AbstractButton;
31 import javax.swing.BorderFactory;
32 import javax.swing.Box;
33 import javax.swing.BoxLayout;
34 import javax.swing.JButton;
35 import javax.swing.JDialog;
36 import javax.swing.JLabel;
37 import javax.swing.JPanel;
38 import javax.swing.JScrollPane;
39 import javax.swing.JTextArea;
40 import javax.swing.JViewport;
41 import javax.swing.WindowConstants;
42 import org.jetbrains.annotations.NotNull;
43 
50 public class ConfirmErrorsDialog {
51 
55  private ConfirmErrorsDialog() {
56  }
57 
71  public static boolean askConfirmErrors(@NotNull final String errors, @NotNull final Component parent) {
72  final JDialog dialog = new JDialog((Frame) null, "Syntax Errors", true);
73  dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
74 
75  final JPanel mainPanel = new JPanel();
76  mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
77  final Container headerPanel = new JPanel(new GridLayout(2, 1));
78  final Container buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
79 
80  // create header labels
81  final Component header1 = new JLabel("The following lines from the archetype text appear to be wrong.");
82  final Component header2 = new JLabel("They do not match the type definitions:");
83  headerPanel.add(header1);
84  headerPanel.add(header2);
85 
86  // create text area for showing errors
87  final JTextArea textArea = new JTextArea(errors, 7, 25);
88  textArea.setBorder(BorderFactory.createEmptyBorder(1, 4, 0, 0));
89  final JScrollPane scrollPane = new JScrollPane(textArea);
90  scrollPane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
91 
92  // create buttons
93  final AbstractButton dumpButton = new JButton("Dump All Errors");
94  final AbstractButton keepButton = new JButton("Keep Above Text");
95  buttonPanel.add(dumpButton);
96  buttonPanel.add(keepButton);
97 
98  final boolean[] result = { true };
99 
100  // attach action listener to the buttons (and the dialog)
101  final ActionListener actionListener = e -> {
102  result[0] = e.getSource() == keepButton;
103  dialog.dispose();
104  };
105  keepButton.addActionListener(actionListener);
106  dumpButton.addActionListener(actionListener);
107  final WindowListener windowListener = new WindowListener() {
108 
109  @Override
110  public void windowOpened(@NotNull final WindowEvent e) {
111  }
112 
113  @Override
114  public void windowClosing(@NotNull final WindowEvent e) {
115  result[0] = true;
116  dialog.dispose();
117  }
118 
119  @Override
120  public void windowClosed(@NotNull final WindowEvent e) {
121  }
122 
123  @Override
124  public void windowIconified(@NotNull final WindowEvent e) {
125  }
126 
127  @Override
128  public void windowDeiconified(@NotNull final WindowEvent e) {
129  }
130 
131  @Override
132  public void windowActivated(@NotNull final WindowEvent e) {
133  }
134 
135  @Override
136  public void windowDeactivated(@NotNull final WindowEvent e) {
137  }
138 
139  };
140  dialog.addWindowListener(windowListener);
141 
142  // stick panels together
143  mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
144  mainPanel.add(headerPanel);
145  mainPanel.add(Box.createVerticalStrut(10));
146  mainPanel.add(scrollPane);
147  mainPanel.add(Box.createVerticalStrut(10));
148  mainPanel.add(buttonPanel);
149 
150  dialog.getContentPane().add(mainPanel);
151 
152  // pack, position and show the popup
153  dialog.pack();
154  dialog.setLocationRelativeTo(parent);
155  dialog.setVisible(true);
156  return result[0];
157  }
158 
159 }
net.sf.gridarta.gui.dialog.gameobjectattributes.ConfirmErrorsDialog.askConfirmErrors
static boolean askConfirmErrors(@NotNull final String errors, @NotNull final Component parent)
Open a popup dialog and ask the user to confirm (or modify) the encountered syntax errors.
Definition: ConfirmErrorsDialog.java:71
net.sf.gridarta.gui.dialog.gameobjectattributes.ConfirmErrorsDialog
Utility class for displaying a dialog to keep or dump invalid attributes of a game object.
Definition: ConfirmErrorsDialog.java:50
errors
errors
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:1
net.sf.gridarta.gui.dialog.gameobjectattributes.ConfirmErrorsDialog.ConfirmErrorsDialog
ConfirmErrorsDialog()
Private constructor to prevent instantiation.
Definition: ConfirmErrorsDialog.java:55