-
Notifications
You must be signed in to change notification settings - Fork 4
/
ItemPanel.java
203 lines (166 loc) · 5.25 KB
/
ItemPanel.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
Written 1999 by Douglas Greiman.
This software may be used and distributed according to the terms
of the GNU Public License, incorporated herein by reference.
*/
package duggelz.jape;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;
import java.util.Enumeration;
public class ItemPanel extends InsetPanel implements DataChangeListener
{
// Gui Elements
private Frame parent;
private GridBagLayout layout = new GridBagLayout();
private GridBagConstraints constraint = new GridBagConstraints();
private ItemDetailPanel itemDetailPanel;
// State machine
private boolean modified = false;
// Data sources
private Actor actor;
private Mercenary merc;
// Data Views
private Vector views = new Vector();
private ItemView currentView;
// Instance methods
public ItemPanel(Frame parent)
{
super(new Insets(10,10,10,10));
this.parent = parent;
this.setLayout(this.layout);
// Constraints
this.constraint.anchor = GridBagConstraints.SOUTHWEST;
this.constraint.fill = GridBagConstraints.BOTH;
this.constraint.gridx = 0;
this.constraint.gridheight = 1;
this.constraint.gridwidth = 1;
this.constraint.weightx = 1;
this.constraint.insets = new Insets(0,0,0,0);
// Add buttons for item slots
this.newRow();
this.addItem(Mercenary.HEADGEAR_1_INDEX);
this.addSpace();
this.addItem(Mercenary.HELMET_INDEX);
this.newRow();
this.addItem(Mercenary.HEADGEAR_2_INDEX);
this.addSpace();
this.addItem(Mercenary.BODY_ARMOR_INDEX);
this.newRow();
this.addItem(Mercenary.RIGHT_HAND_INDEX);
this.addSpace();
this.addItem(Mercenary.LEG_ARMOR_INDEX);
this.newRow();
this.addItem(Mercenary.LEFT_HAND_INDEX);
this.newRow();
this.addItem(Mercenary.BACKPACK_1_1_INDEX);
this.addItem(Mercenary.BACKPACK_1_2_INDEX);
this.addItem(Mercenary.BACKPACK_1_3_INDEX);
this.newRow();
this.addItem(Mercenary.BACKPACK_2_1_INDEX);
this.addItem(Mercenary.BACKPACK_2_2_INDEX);
this.addItem(Mercenary.BACKPACK_2_3_INDEX);
this.newRow();
this.addItem(Mercenary.BACKPACK_3_1_INDEX);
this.addItem(Mercenary.BACKPACK_3_2_INDEX);
this.addItem(Mercenary.BACKPACK_3_3_INDEX);
this.newRow();
this.addItem(Mercenary.BACKPACK_4_1_INDEX);
this.addItem(Mercenary.BACKPACK_4_2_INDEX);
this.addItem(Mercenary.BACKPACK_4_3_INDEX);
// Create item detail panel
this.newRow();
this.itemDetailPanel = new ItemDetailPanel(this.parent);
this.constraint.gridwidth = 3;
this.constraint.weightx = 1;
this.constraint.weighty = 1;
this.add(this.itemDetailPanel, this.constraint);
this.itemDetailPanel.addDataChangeListener(this);
}
private void newRow()
{
// Start a new row
this.constraint.gridy++;
this.constraint.gridx = 0;
}
private void addComponent(int colwidth, Component component)
{
// Add component to panel
this.constraint.gridwidth = colwidth;
this.add(component, this.constraint);
this.constraint.gridx += colwidth;
}
private void addSpace()
{
this.constraint.gridx++;
}
private void addItem(final int index)
{
// Create new button
final ItemView view = new ItemView(index);
this.addComponent(1, view);
this.views.addElement(view);
view.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doItemSelected(view);
}});
}
public void setActor(Actor actor, Mercenary merc)
{
// Set fields
this.actor = actor;
this.merc = merc;
// Update views from data sources
for( int idx = 0; idx < this.views.size(); ++idx ) {
ItemView view = (ItemView) this.views.elementAt(idx);
view.setActor(merc);
}
// Actor now clean
this.modified = false;
// Set detail fields
this.doItemSelected((ItemView) this.views.elementAt(0));
}
public boolean isModified() { return this.itemDetailPanel.isModified(); }
public void setModified(boolean modified) {
this.itemDetailPanel.setModified(modified);
this.modified = modified;
}
public void doItemSelected(ItemView view)
{
// Get item index
int index = view.getIndex();
this.currentView = view;
// Get new item
Item item = null;
if( merc != null ) {
item = this.merc.items[index];
}
// Change to new item
this.itemDetailPanel.setItem(item);
}
public void dataChanged(DataChangeEvent event)
{
// Extract the control was changed
FieldView view = (FieldView) event.getSource();
String oldValue = event.getOldValue();
String newValue = event.getNewValue();
// Update views from data sources
this.currentView.refresh();
// Actor now dirty
// System.err.println("ItemPanel.doValueChanged(" + view + ",'" + oldValue + "','" + newValue + "')");
this.modified = true;
this.fireDataChangeEvent(event);
}
// Data Change event processing
private DataChangeMixin mixin = new DataChangeMixin();
public void addDataChangeListener(DataChangeListener l) {
this.mixin.addDataChangeListener(l);
}
public void removeDataChangeListener(DataChangeListener l) {
this.mixin.removeDataChangeListener(l);
}
public void fireDataChangeEvent(DataChangeEvent e) {
this.mixin.fireDataChangeEvent(e);
}
}