1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package cs.pancava.caltha.menu;
21
22 import cs.pancava.caltha.Editor;
23 import cs.pancava.caltha.MainMenu;
24 import cs.pancava.caltha.graphics.EditorGraphicsWorld2D;
25 import cs.pancava.caltha.worlds.GameEntity;
26 import cs.pancava.caltha.worlds.Room;
27 import cs.pancava.caltha.worlds.Route;
28 import java.awt.Dimension;
29 import java.awt.Rectangle;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 import java.awt.event.MouseAdapter;
33 import java.awt.event.MouseEvent;
34 import javax.swing.JMenuItem;
35 import javax.swing.JPopupMenu;
36
37
38
39
40
41
42
43
44 public class World2DPopupMenuListener extends MouseAdapter
45 {
46
47
48
49 private static JPopupMenu popupMenu;
50
51
52
53 private static JMenuItem addRoomIM;
54
55
56
57 private static JMenuItem connectRoomIM;
58
59
60
61 private static JMenuItem removeRoomIM;
62
63
64
65 private static JMenuItem disconnectRoomIM;
66
67
68
69
70 private int mouseX;
71
72
73
74
75 private int mouseY;
76
77
78
79
80
81
82 private final int ROOM_SIZE = 50;
83
84
85
86
87 private EditorGraphicsWorld2D editorGraphicsWorld2D;
88
89
90
91
92
93
94 public World2DPopupMenuListener(final EditorGraphicsWorld2D graphicsWorld2D)
95 {
96 this.editorGraphicsWorld2D = graphicsWorld2D;
97
98 World2DPopupMenuListener.popupMenu = new JPopupMenu();
99
100 this.createItemMenuAddRoom();
101 this.createItemMenuConnectRoom();
102 this.createItemMenuRemoveRoom();
103 this.createItemMenuDisconnectRoom();
104 }
105
106
107
108
109 public static void updateText()
110 {
111 if (World2DPopupMenuListener.addRoomIM != null)
112 {
113 World2DPopupMenuListener.addRoomIM.setText(Editor.getResourcesBundleString("cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuAddRoom_addRoomIM"));
114 MainMenu.updateTextTipMenuItem(World2DPopupMenuListener.addRoomIM, "cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuAddRoom_addRoomIM_tip");
115 }
116
117 if (World2DPopupMenuListener.connectRoomIM != null)
118 {
119 World2DPopupMenuListener.connectRoomIM.setText(Editor.getResourcesBundleString("cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuConnectRoom_connectRoomIM"));
120 MainMenu.updateTextTipMenuItem(World2DPopupMenuListener.connectRoomIM, "cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuConnectRoom_connectRoomIM_tip");
121 }
122
123 if (World2DPopupMenuListener.removeRoomIM != null)
124 {
125 World2DPopupMenuListener.removeRoomIM.setText(Editor.getResourcesBundleString("cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuRemoveRoom_removeRoomIM"));
126 MainMenu.updateTextTipMenuItem(World2DPopupMenuListener.removeRoomIM, "cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuRemoveRoom_removeRoomIM_tip");
127 }
128
129 if (World2DPopupMenuListener.disconnectRoomIM != null)
130 {
131 World2DPopupMenuListener.disconnectRoomIM.setText(Editor.getResourcesBundleString("cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuDisconnectRoom_disconnectRoomIM"));
132 MainMenu.updateTextTipMenuItem(World2DPopupMenuListener.disconnectRoomIM, "cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuDisconnectRoom_disconnectRoomIM_tip");
133 }
134 }
135
136
137
138
139
140
141 private void addRoom(final int x, final int y)
142 {
143 final GameEntity tmpRoom2 = new Room(x, y, this.ROOM_SIZE);
144 GameEntity tmpRoom1 = null;
145
146
147
148
149 for (GameEntity ge : Editor.getEditorDesktop().getWorld().getWorldRoomsAL())
150 {
151 if (ge.isSeleted())
152 {
153
154
155 tmpRoom1 = ge;
156
157
158
159 }
160 }
161
162 Editor.getEditorDesktop().getWorld().addGameEntity(tmpRoom2);
163
164 if (tmpRoom1 != null)
165 {
166
167 Editor.getEditorDesktop().getWorld().addGameEntity(new Route(tmpRoom1, tmpRoom2));
168 }
169 }
170
171
172
173
174 private void connectRoom()
175 {
176 this.editorGraphicsWorld2D.setConnectRoom(true);
177 }
178
179
180
181
182 private void createItemMenuAddRoom()
183 {
184 World2DPopupMenuListener.addRoomIM = MainMenu.addMenuItem("cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuAddRoom_addRoomIM",
185 "cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuAddRoom_addRoomIM_tip",
186 true);
187
188 World2DPopupMenuListener.addRoomIM.addActionListener(new ActionListener()
189 {
190 @Override
191 public void actionPerformed(final ActionEvent e)
192 {
193
194
195
196
197 addRoom(mouseX, mouseY);
198
199
200
201 editorGraphicsWorld2D.repaint();
202
203
204 expandGraphicsWorld2DArea();
205
206
207
208 moveVisibleArea();
209 }
210 });
211 World2DPopupMenuListener.popupMenu.add(World2DPopupMenuListener.addRoomIM);
212 }
213
214
215
216
217 private void createItemMenuConnectRoom()
218 {
219 World2DPopupMenuListener.connectRoomIM = MainMenu.addMenuItem("cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuConnectRoom_connectRoomIM",
220 "cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuConnectRoom_connectRoomIM_tip",
221 false);
222
223 World2DPopupMenuListener.connectRoomIM.addActionListener(new ActionListener()
224 {
225 @Override
226 public void actionPerformed(final ActionEvent e)
227 {
228
229
230
231
232 connectRoom();
233
234
235
236 editorGraphicsWorld2D.repaint();
237 }
238 });
239 World2DPopupMenuListener.popupMenu.add(World2DPopupMenuListener.connectRoomIM);
240 }
241
242
243
244
245 private void createItemMenuDisconnectRoom()
246 {
247 World2DPopupMenuListener.disconnectRoomIM = MainMenu.addMenuItem("cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuDisconnectRoom_disconnectRoomIM",
248 "cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuDisconnectRoom_disconnectRoomIM_tip",
249 false);
250
251 World2DPopupMenuListener.disconnectRoomIM.addActionListener(new ActionListener()
252 {
253 @Override
254 public void actionPerformed(final ActionEvent e)
255 {
256
257
258
259
260 disconnectRoom();
261
262
263
264 editorGraphicsWorld2D.repaint();
265 }
266 });
267 World2DPopupMenuListener.popupMenu.add(World2DPopupMenuListener.disconnectRoomIM);
268 }
269
270
271
272
273 private void createItemMenuRemoveRoom()
274 {
275 World2DPopupMenuListener.removeRoomIM = MainMenu.addMenuItem("cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuRemoveRoom_removeRoomIM",
276 "cs.pancava.caltha.menu.World2DPopupMenuListener-createItemMenuRemoveRoom_removeRoomIM_tip",
277 true);
278
279 World2DPopupMenuListener.removeRoomIM.addActionListener(new ActionListener()
280 {
281 @Override
282 public void actionPerformed(final ActionEvent e)
283 {
284
285
286
287
288 removeRoom();
289
290
291
292 editorGraphicsWorld2D.repaint();
293 }
294 });
295 World2DPopupMenuListener.popupMenu.add(World2DPopupMenuListener.removeRoomIM);
296 }
297
298
299
300
301 private void disconnectRoom()
302 {
303 this.editorGraphicsWorld2D.setDisconnectRoom(true);
304 }
305
306
307
308
309
310
311 private void expandGraphicsWorld2DArea()
312 {
313
314 Dimension tmpDimension = editorGraphicsWorld2D.getPreferredSize();
315
316
317 boolean scrollbarGrowingArea = false;
318
319 if (tmpDimension.width - editorGraphicsWorld2D.getScrollbarGrowingAreaX() < mouseX)
320 {
321 tmpDimension.setSize(tmpDimension.width + editorGraphicsWorld2D.getNewAreaX(), tmpDimension.height);
322 scrollbarGrowingArea = true;
323 }
324
325 if (tmpDimension.height - editorGraphicsWorld2D.getScrollbarGrowingAreaY() < mouseY)
326 {
327 tmpDimension.setSize(tmpDimension.width, tmpDimension.height + editorGraphicsWorld2D.getNewAreaY());
328 scrollbarGrowingArea = true;
329 }
330
331 if (scrollbarGrowingArea)
332 {
333
334 editorGraphicsWorld2D.setPreferredSize(tmpDimension);
335 editorGraphicsWorld2D.revalidate();
336 }
337 }
338
339
340
341
342
343 @Override
344 public final void mousePressed(final MouseEvent e)
345 {
346
347
348
349 World2DPopupMenuListener.connectRoomIM.setEnabled(this.editorGraphicsWorld2D.getSelectedObject());
350 World2DPopupMenuListener.disconnectRoomIM.setEnabled(this.editorGraphicsWorld2D.getSelectedObject());
351
352 this.maybeShowPopup(e);
353 }
354
355
356
357
358
359 @Override
360 public final void mouseReleased(final MouseEvent e)
361 {
362 this.maybeShowPopup(e);
363 }
364
365
366
367
368
369
370 private void moveVisibleArea()
371 {
372 Rectangle tmpVisibleArea = new Rectangle(editorGraphicsWorld2D.getVisibleRect());
373
374 if (tmpVisibleArea.x + ROOM_SIZE > mouseX)
375 {
376 editorGraphicsWorld2D.scrollRectToVisible(new Rectangle(tmpVisibleArea.x - ROOM_SIZE, tmpVisibleArea.y, tmpVisibleArea.width, tmpVisibleArea.height));
377
378 tmpVisibleArea = new Rectangle(editorGraphicsWorld2D.getVisibleRect());
379 }
380
381 if (tmpVisibleArea.x + tmpVisibleArea.width - ROOM_SIZE < mouseX)
382 {
383 editorGraphicsWorld2D.scrollRectToVisible(new Rectangle(tmpVisibleArea.x + ROOM_SIZE, tmpVisibleArea.y, tmpVisibleArea.width, tmpVisibleArea.height));
384 tmpVisibleArea = new Rectangle(editorGraphicsWorld2D.getVisibleRect());
385 }
386
387 if (tmpVisibleArea.y + ROOM_SIZE > mouseY)
388 {
389 editorGraphicsWorld2D.scrollRectToVisible(new Rectangle(tmpVisibleArea.x, tmpVisibleArea.y - ROOM_SIZE, tmpVisibleArea.width, tmpVisibleArea.height));
390 tmpVisibleArea = new Rectangle(editorGraphicsWorld2D.getVisibleRect());
391 }
392
393 if (tmpVisibleArea.y + tmpVisibleArea.height - ROOM_SIZE < mouseY)
394 {
395 editorGraphicsWorld2D.scrollRectToVisible(new Rectangle(tmpVisibleArea.x, tmpVisibleArea.y + ROOM_SIZE, tmpVisibleArea.width, tmpVisibleArea.height));
396
397 }
398 }
399
400
401
402
403
404 private void maybeShowPopup(final MouseEvent e)
405 {
406 if (e.isPopupTrigger())
407 {
408 this.mouseX = e.getX();
409 this.mouseY = e.getY();
410
411 World2DPopupMenuListener.popupMenu.show(e.getComponent(), e.getX(), e.getY());
412 }
413 }
414
415
416
417
418 private void removeRoom()
419 {
420 this.editorGraphicsWorld2D.setRemoveRoom(true);
421 }
422 }