-
Notifications
You must be signed in to change notification settings - Fork 94
/
PropertiesWindow.java
145 lines (129 loc) · 4.28 KB
/
PropertiesWindow.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
*
* Copyright (c) 2000-2003 by Rodney Kinney
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License (LGPL) as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, copies are available
* at http://www.opensource.org.
*/
package VASSAL.configure;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import VASSAL.build.Buildable;
import VASSAL.build.Builder;
import VASSAL.build.Configurable;
import VASSAL.build.GameModule;
import VASSAL.build.module.documentation.HelpWindow;
import VASSAL.tools.ErrorDialog;
/**
* Window for editing properties of a {@link Configurable} object
*/
public class PropertiesWindow extends JDialog {
private static final long serialVersionUID = 1L;
private Configurer configurer;
private Configurable target;
private Element originalState;
public PropertiesWindow(Frame owner, boolean modal, final Configurable target, HelpWindow helpWindow) {
super(owner, modal);
initialize(target, helpWindow);
}
protected void initialize(final Configurable target, HelpWindow helpWindow) {
this.target = target;
originalState = target.getBuildElement(Builder.createNewDocument());
Node child = originalState.getFirstChild();
while (child != null) {
Node nextChild = child.getNextSibling();
if (Node.ELEMENT_NODE == child.getNodeType()) {
// Cull Buildables from the state.
try {
final Class<?> c = GameModule.getGameModule().getDataArchive().loadClass(((Element)child).getTagName());
if (Buildable.class.isAssignableFrom(c)) {
originalState.removeChild(child);
}
}
catch (ClassNotFoundException e) {
// This element doesn't correspond to a class. Skip it.
}
catch (LinkageError e) {
ErrorDialog.bug(e);
}
}
child = nextChild;
}
setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
configurer = target.getConfigurer();
target.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (Configurable.NAME_PROPERTY.equals(evt.getPropertyName())) {
setTitle((String) evt.getNewValue());
}
}
});
add(configurer.getControls());
setTitle(ConfigureTree.getConfigureName(target));
final Box buttonBox = Box.createHorizontalBox();
final JButton okButton = new JButton("Ok");
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
save();
}
});
buttonBox.add(okButton);
final JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cancel();
}
});
buttonBox.add(cancelButton);
if (target.getHelpFile() != null) {
final Action helpAction =
new ShowHelpAction(target.getHelpFile().getContents(), null);
final JButton helpButton = new JButton(helpAction);
buttonBox.add(helpButton);
pack();
}
add(buttonBox);
pack();
setLocationRelativeTo(getParent());
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
cancel();
}
});
}
public void cancel() {
target.build(originalState);
dispose();
}
public void save() {
configurer.getValue();
dispose();
}
}