Source code: org/meowers/cide/edcide/map/MapEditor.java
1 /*
2 * MapEditor.java
3 *
4 * Created on February 6, 2002, 4:28 PM
5 */
6
7 package org.meowers.cide.edcide.map;
8
9 import org.meowers.cide.data.*;
10 import org.meowers.cide.edcide.*;
11 import org.meowers.cide.edcide.util.*;
12 import java.awt.*;
13 import javax.swing.*;
14 import javax.swing.table.*;
15
16 /**
17 * The editing panel for <code>Map</code> objects.
18 *
19 * @author Adam Miezianko
20 * @version %I%, %G%
21 */
22 public class MapEditor extends EdCideEditor {
23
24 private Map map;
25 private GameData gameData;
26 private EdCideTreeNode mapNode;
27 private Point startDrag;
28
29 /**
30 * Creates a new editing panel for <code>Map</code> objects and set it to
31 * edit the specified map.
32 *
33 * @param gameData the <code>GameData</code> this map belongs to.
34 * @param mapNode the node of the tree containing the <code>Map</code> to
35 * edit.
36 */
37 public MapEditor(GameData gameData, EdCideTreeNode mapNode) {
38 initComponents();
39
40 this.mapNode = mapNode;
41 this.gameData = gameData;
42
43 editSplit.setDividerLocation(40);
44
45 map = (Map) mapNode.getGameObject();
46
47 // populate top controls
48 mapNameTextField.setText(map.getName());
49
50 // populate the tile set combo box
51 int tileSetCount = gameData.getGameObjectCount(GameObject.TILE_SET);
52 for (int j = 0; j < tileSetCount; j++) {
53 tileSetCombo.addItem(gameData.getGameObject(GameObject.TILE_SET, j));
54 }
55
56 // create the cell editor
57 JComboBox mapCellEditor = new JComboBox();
58 for (int i = 0; i < gameData.getGameObjectCount(GameObject.TILE); i++)
59 mapCellEditor.addItem(gameData.getGameObject(GameObject.TILE, i));
60
61 //mapTable.setDefaultEditor(Map.class, new DefaultCellEditor(mapCellEditor));
62 mapTable.setDefaultRenderer(Map.class, new MapTableCellRenderer());
63
64 // populate the data
65 mapTable.setModel(new MapTableModel(map));
66
67 // set cell sizes
68 resizeTable();
69
70 // load the tile list
71 tileList.setCellRenderer(new TileListCellRenderer());
72 tileList.setModel(new TileListModel(gameData));
73
74 }
75
76 /** This method is called from within the constructor to
77 * initialize the form.
78 * WARNING: Do NOT modify this code. The content of this method is
79 * always regenerated by the Form Editor.
80 */
81 private void initComponents() {//GEN-BEGIN:initComponents
82 topPanel = new javax.swing.JPanel();
83 generalPanel = new javax.swing.JPanel();
84 mapNameLabel = new javax.swing.JLabel();
85 mapNameTextField = new javax.swing.JTextField();
86 mapSizePanel = new javax.swing.JPanel();
87 sizeLabel = new javax.swing.JLabel();
88 sizeTextField = new javax.swing.JTextField();
89 addWidthButton = new javax.swing.JButton();
90 addHeightButton = new javax.swing.JButton();
91 editSplit = new javax.swing.JSplitPane();
92 tableScrollPane = new javax.swing.JScrollPane();
93 mapTable = new javax.swing.JTable();
94 toolPanel = new javax.swing.JPanel();
95 tileSetCombo = new javax.swing.JComboBox();
96 listScrollPane = new javax.swing.JScrollPane();
97 tileList = new javax.swing.JList();
98
99 setLayout(new java.awt.BorderLayout());
100
101 addMouseListener(new java.awt.event.MouseAdapter() {
102 public void mousePressed(java.awt.event.MouseEvent evt) {
103 formMousePressed(evt);
104 }
105 });
106
107 topPanel.setLayout(new java.awt.GridLayout(1, 0));
108
109 generalPanel.setBorder(new javax.swing.border.TitledBorder("General"));
110 mapNameLabel.setText("Map Name ");
111 mapNameLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
112 generalPanel.add(mapNameLabel);
113
114 mapNameTextField.setPreferredSize(new java.awt.Dimension(70, 19));
115 generalPanel.add(mapNameTextField);
116
117 topPanel.add(generalPanel);
118
119 mapSizePanel.setLayout(new java.awt.GridLayout(2, 0));
120
121 mapSizePanel.setBorder(new javax.swing.border.TitledBorder("Map Size"));
122 sizeLabel.setText("Amount ");
123 sizeLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
124 mapSizePanel.add(sizeLabel);
125
126 sizeTextField.setPreferredSize(new java.awt.Dimension(45, 19));
127 mapSizePanel.add(sizeTextField);
128
129 addWidthButton.setText("+ Width");
130 addWidthButton.addActionListener(new java.awt.event.ActionListener() {
131 public void actionPerformed(java.awt.event.ActionEvent evt) {
132 addWidthButtonActionPerformed(evt);
133 }
134 });
135
136 mapSizePanel.add(addWidthButton);
137
138 addHeightButton.setText("+ Height");
139 addHeightButton.addActionListener(new java.awt.event.ActionListener() {
140 public void actionPerformed(java.awt.event.ActionEvent evt) {
141 addHeightButtonActionPerformed(evt);
142 }
143 });
144
145 mapSizePanel.add(addHeightButton);
146
147 topPanel.add(mapSizePanel);
148
149 add(topPanel, java.awt.BorderLayout.NORTH);
150
151 editSplit.setOneTouchExpandable(true);
152 mapTable.setCellSelectionEnabled(true);
153 mapTable.addMouseListener(new java.awt.event.MouseAdapter() {
154 public void mouseReleased(java.awt.event.MouseEvent evt) {
155 mapTableMouseReleased(evt);
156 }
157 public void mousePressed(java.awt.event.MouseEvent evt) {
158 mapTableMousePressed(evt);
159 }
160 public void mouseClicked(java.awt.event.MouseEvent evt) {
161 mapTableMouseClicked(evt);
162 }
163 });
164
165 tableScrollPane.setViewportView(mapTable);
166
167 editSplit.setRightComponent(tableScrollPane);
168
169 toolPanel.setLayout(new java.awt.BorderLayout());
170
171 tileSetCombo.addActionListener(new java.awt.event.ActionListener() {
172 public void actionPerformed(java.awt.event.ActionEvent evt) {
173 tileSetComboActionPerformed(evt);
174 }
175 });
176
177 toolPanel.add(tileSetCombo, java.awt.BorderLayout.NORTH);
178
179 listScrollPane.setPreferredSize(new java.awt.Dimension(55, 131));
180 tileList.setPreferredSize(new java.awt.Dimension(40, 0));
181 listScrollPane.setViewportView(tileList);
182
183 toolPanel.add(listScrollPane, java.awt.BorderLayout.CENTER);
184
185 editSplit.setLeftComponent(toolPanel);
186
187 add(editSplit, java.awt.BorderLayout.CENTER);
188
189 }//GEN-END:initComponents
190
191 private void tileSetComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tileSetComboActionPerformed
192 tileList.setModel(new TileListModel((TileSet) tileSetCombo.getSelectedItem()));
193 }//GEN-LAST:event_tileSetComboActionPerformed
194
195 private void mapTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mapTableMouseReleased
196 Point stopDrag = evt.getPoint();
197 int rowStart, colStart, rowStop, colStop;
198
199 if (startDrag.x > stopDrag.x) {
200 rowStart = mapTable.rowAtPoint(stopDrag);
201 rowStop = mapTable.rowAtPoint(startDrag);
202 } else {
203 rowStart = mapTable.rowAtPoint(startDrag);
204 rowStop = mapTable.rowAtPoint(stopDrag);
205 }
206 if (startDrag.y > stopDrag.y) {
207 colStart = mapTable.columnAtPoint(stopDrag);
208 colStop = mapTable.columnAtPoint(startDrag);
209 } else {
210 colStart = mapTable.columnAtPoint(startDrag);
211 colStop = mapTable.columnAtPoint(stopDrag);
212 }
213
214 TableModel tm = mapTable.getModel();
215 for (int row = rowStart; row <= rowStop; row++)
216 for (int col = colStart; col <= colStop; col++)
217 tm.setValueAt(tileList.getSelectedValue(), row, col);
218
219 }//GEN-LAST:event_mapTableMouseReleased
220
221 private void mapTableMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mapTableMousePressed
222 startDrag = evt.getPoint();
223 }//GEN-LAST:event_mapTableMousePressed
224
225 private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
226
227 }//GEN-LAST:event_formMousePressed
228
229 private void addHeightButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addHeightButtonActionPerformed
230 // grab the dimensions
231 ValidatedInteger sizeVI = new ValidatedInteger(sizeTextField, 0, 10, "Width Change");
232
233 // if they are valid
234 if (sizeVI.isValid()) {
235
236 map.addHeight(sizeVI.getValue());
237 mapTable.setModel(new MapTableModel(map));
238
239 resizeTable();
240 }
241 }//GEN-LAST:event_addHeightButtonActionPerformed
242
243 private void mapTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mapTableMouseClicked
244 if (evt.getComponent() == mapTable) {
245 int row = mapTable.getSelectedRow();
246 int column = mapTable.getSelectedColumn();
247 if (column != -1 && row != -1) {
248 TableModel tm = mapTable.getModel();
249 tm.setValueAt(tileList.getSelectedValue(), row, column);
250 }
251 }
252 }//GEN-LAST:event_mapTableMouseClicked
253
254 private void addWidthButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addWidthButtonActionPerformed
255
256 // grab the dimensions
257 ValidatedInteger sizeVI = new ValidatedInteger(sizeTextField, 0, 10, "Width Change");
258
259 // if they are valid
260 if (sizeVI.isValid()) {
261
262 map.addWidth(sizeVI.getValue());
263 mapTable.setModel(new MapTableModel(map));
264
265 resizeTable();
266 }
267 }//GEN-LAST:event_addWidthButtonActionPerformed
268
269 private void resizeTable() {
270 mapTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
271 int tileSize = gameData.getTileSize();
272 mapTable.setRowHeight(tileSize);
273 TableColumnModel cm = mapTable.getColumnModel();
274 for (int i = 0; i < mapTable.getColumnCount(); i++) {
275 TableColumn tc = cm.getColumn(i);
276 tc.setMaxWidth(tileSize);
277 tc.setPreferredWidth(tileSize);
278 }
279 }
280
281 public boolean apply() {
282 // grab the name
283 map.setName(mapNameTextField.getText());
284 mapNode.updateText();
285
286 return (true);
287 }
288
289 public void revert() {
290 }
291
292 // Variables declaration - do not modify//GEN-BEGIN:variables
293 private javax.swing.JPanel topPanel;
294 private javax.swing.JPanel generalPanel;
295 private javax.swing.JLabel mapNameLabel;
296 private javax.swing.JTextField mapNameTextField;
297 private javax.swing.JPanel mapSizePanel;
298 private javax.swing.JLabel sizeLabel;
299 private javax.swing.JTextField sizeTextField;
300 private javax.swing.JButton addWidthButton;
301 private javax.swing.JButton addHeightButton;
302 private javax.swing.JSplitPane editSplit;
303 private javax.swing.JScrollPane tableScrollPane;
304 private javax.swing.JTable mapTable;
305 private javax.swing.JPanel toolPanel;
306 private javax.swing.JComboBox tileSetCombo;
307 private javax.swing.JScrollPane listScrollPane;
308 private javax.swing.JList tileList;
309 // End of variables declaration//GEN-END:variables
310
311 }