-
Notifications
You must be signed in to change notification settings - Fork 25
/
ResNode.java
446 lines (403 loc) · 12 KB
/
ResNode.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* Copyright (C) 2006, 2007, 2009 IsmAvatar <IsmAvatar@gmail.com>
* Copyright (C) 2007 Clam <clamisgood@gmail.com>
* Copyright (C) 2008, 2009 Quadduc <quadduc@gmail.com>
* Copyright (C) 2010 Medo <smaxein@googlemail.com>
* Copyright (C) 2013, 2014 Robert B. Colton
* Copyright (C) 2014 egofree
*
* This file is part of LateralGM.
* LateralGM is free software and comes with ABSOLUTELY NO WARRANTY.
* See LICENSE for details.
*/
package org.lateralgm.components.impl;
import java.awt.Font;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JInternalFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import org.lateralgm.components.GmTreeGraphics;
import org.lateralgm.main.LGM;
import org.lateralgm.main.Listener;
import org.lateralgm.main.Prefs;
import org.lateralgm.main.UpdateSource;
import org.lateralgm.main.UpdateSource.UpdateEvent;
import org.lateralgm.main.UpdateSource.UpdateListener;
import org.lateralgm.main.UpdateSource.UpdateTrigger;
import org.lateralgm.main.Util;
import org.lateralgm.messages.Messages;
import org.lateralgm.resources.Background;
import org.lateralgm.resources.GmObject;
import org.lateralgm.resources.InstantiableResource;
import org.lateralgm.resources.Resource;
import org.lateralgm.resources.ResourceReference;
import org.lateralgm.resources.Sprite;
import org.lateralgm.subframes.InstantiableResourceFrame;
import org.lateralgm.subframes.ResourceFrame;
import org.lateralgm.subframes.ResourceFrame.ResourceFrameFactory;
import org.lateralgm.subframes.RoomFrame;
import org.lateralgm.subframes.SubframeInformer;
public class ResNode extends DefaultNode implements Transferable,UpdateListener
{
/**
* NOTE: Default UID generated, change if necessary.
*/
private static final long serialVersionUID = -2072032756973999470L;
public static DataFlavor DATA_FLAVOR;
static
{
try
{
DATA_FLAVOR = Util.createJVMLocalDataFlavor(ResNode.class);
}
catch (ClassNotFoundException e)
{
LGM.showDefaultExceptionHandler(e);
}
}
private static final DataFlavor[] flavors = { DATA_FLAVOR };
public static final Map<Class<?>,ImageIcon> ICON;
static
{
ICON = new HashMap<Class<?>,ImageIcon>();
for (Entry<String,Class<? extends Resource<?,?>>> k : Resource.kindsByName3.entrySet())
ICON.put(k.getValue(),LGM.getIconForKey("Resource." + k.getKey()));
}
public static final byte STATUS_PRIMARY = 1;
public static final byte STATUS_GROUP = 2;
public static final byte STATUS_SECONDARY = 3;
/** One of PRIMARY, GROUP, or SECONDARY*/
public byte status;
/** What kind of Resource this is */
public Class<?> kind;
/**
* The <code>Resource</code> this node represents.
*/
private final ResourceReference<? extends Resource<?,?>> res;
public ResourceFrame<?,?> frame = null;
private final NameUpdater nameUpdater = new NameUpdater(this);
private final UpdateTrigger trigger = new UpdateTrigger();
public final UpdateSource updateSource = new UpdateSource(this,trigger);
public boolean newRes = false;
@Override
public Icon getIcon()
{
if (status == STATUS_SECONDARY)
{
if (kind == Sprite.class || kind == Background.class || kind == GmObject.class)
{
if (icon == null) updateIcon();
return icon;
}
return ICON.get(kind);
}
if (Prefs.iconizeGroup && getChildCount() > 0)
{
ResNode n = (ResNode) getChildAt(0);
if (n.status == STATUS_SECONDARY) return n.getIcon();
}
return null;
}
@Override
public Icon getIconisedGroup() {
if (status != ResNode.STATUS_PRIMARY && kind == Sprite.class || kind == Background.class || kind == GmObject.class)
return getIcon();
return null;
}
@Override
public Icon getLeafIcon() {
if (status == ResNode.STATUS_SECONDARY)
return getIcon();
return null;
}
// NOTE: DO NOT use this for tree graphics, format with HTML instead because there is a Java LNF bug that causes this not to work.
public Font getFont(Font com) {
if (Prefs.boldPrimaryNodes && status == ResNode.STATUS_PRIMARY) {
com = com.deriveFont(Font.BOLD);
} else {
com = com.deriveFont(Font.PLAIN);
}
return com;
}
private void updateIcon()
{
icon = GmTreeGraphics.getResourceIcon(res);
}
public ResNode(String name, byte status, Class<?> kind,
ResourceReference<? extends Resource<?,?>> res)
{
super(name);
this.status = status;
this.kind = kind;
this.res = res;
Resource<?,?> r = deRef();
if (r != null)
{
r.setNode(this);
res.updateSource.addListener(this);
}
}
public ResNode(String name, byte status, Class<?> kind)
{
this(name,status,kind,null);
}
public ResNode addChild(String name, byte stat, Class<?> k)
{
ResNode b = new ResNode(name,stat,k,null);
add(b);
return b;
}
@Override
public boolean getAllowsChildren()
{
if (status == STATUS_SECONDARY) return false;
if (isRoot()) return false;
return true;
}
public DataFlavor[] getTransferDataFlavors()
{
return flavors;
}
public boolean isDataFlavorSupported(DataFlavor flavor)
{
return flavor.equals(DATA_FLAVOR);
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException
{
if (flavor != DATA_FLAVOR) throw new UnsupportedFlavorException(flavor);
return this;
}
public void openFrame()
{
openFrame(false);
}
public void openFrame(boolean newRes)
{
this.newRes = newRes;
Resource<?,?> r = deRef();
if (r != null) {
r.changed = newRes;
}
if (SubframeInformer.fireSubframeRequest(r,this)) return;
ResourceFrame<?,?> rf = frame;
boolean wasVisible = false;
if (frame == null)
{
ResourceFrameFactory factory = ResourceFrame.factories.get(kind);
rf = factory == null ? null : factory.makeFrame(r,this);
if (rf != null)
{
frame = rf;
if (rf instanceof InstantiableResourceFrame<?,?>) LGM.mdi.add(rf);
}
} else {
wasVisible = frame.isVisible();
}
if (rf != null)
{
SubframeInformer.fireSubframeAppear(rf,wasVisible);
rf.toTop();
}
}
private static JMenuItem makeMenuItem(String command, ActionListener al, boolean setIcon)
{
JMenuItem menuItem = new JMenuItem(Messages.getString(command));
menuItem.setActionCommand(command);
menuItem.addActionListener(al);
ImageIcon icon = LGM.getIconForKey(command);
if (icon != null && setIcon)
{
menuItem.setIcon(icon);
}
//menuItem.addKeyListener(kl);
menuItem.addKeyListener(null);
return menuItem;
}
public void showMenu(MouseEvent e)
{
JPopupMenu popup = new JPopupMenu();
ActionListener al = new Listener.NodeMenuListener(this);
//KeyListener kl = new Listener.NodeKeyListener(this);
if (!isInstantiable())
{
JMenuItem editItem = makeMenuItem("Listener.TREE_PROPERTIES",al,true);
popup.add(editItem);
editItem.setFocusable(true);
editItem.setAccelerator(KeyStroke.getKeyStroke(Messages.getKeyboardString("Listener.TREE_PROPERTIES")));
popup.show(e.getComponent(),e.getX(),e.getY());
return;
}
if (status == ResNode.STATUS_SECONDARY)
{
JMenuItem insertItem = makeMenuItem("Listener.TREE_INSERT_RESOURCE",al,true);
insertItem.setFocusable(true);
popup.add(insertItem);
insertItem.setAccelerator(KeyStroke.getKeyStroke(Messages.getKeyboardString("Listener.TREE_INSERT_RESOURCE")));
JMenuItem duplicateItem = makeMenuItem("Listener.TREE_DUPLICATE_RESOURCE",al,true);
duplicateItem.setFocusable(true);
popup.add(duplicateItem);
duplicateItem.setAccelerator(KeyStroke.getKeyStroke(Messages.getKeyboardString("Listener.TREE_DUPLICATE_RESOURCE")));
popup.add(makeMenuItem("Listener.TREE_INSERT_GROUP",al,true)); //$NON-NLS-1$
}
else
{
popup.add(makeMenuItem("Listener.TREE_CREATE_RESOURCE",al,true)); //$NON-NLS-1$
popup.add(makeMenuItem("Listener.TREE_CREATE_GROUP",al,true)); //$NON-NLS-1$
}
if (status != ResNode.STATUS_SECONDARY) popup.add(makeMenuItem("Listener.TREE_SORT",al,false)); //$NON-NLS-1$
if (status != ResNode.STATUS_PRIMARY)
{
popup.addSeparator();
JMenuItem deleteItem = makeMenuItem("Listener.TREE_DELETE",al,true);
deleteItem.setFocusable(true);
deleteItem.requestFocus();
popup.add(deleteItem);
// KeyStroke.getKeyStroke("BACK_SPACE"); for delete key on mac
deleteItem.setAccelerator(KeyStroke.getKeyStroke(Messages.getKeyboardString("Listener.TREE_DELETE")));
JMenuItem renameItem = makeMenuItem("Listener.TREE_RENAME",al,true);
renameItem.setFocusable(true);
popup.add(renameItem);
renameItem.setAccelerator(KeyStroke.getKeyStroke(Messages.getKeyboardString("Listener.TREE_RENAME")));
}
if (status == ResNode.STATUS_SECONDARY)
{
JMenuItem editItem = makeMenuItem("Listener.TREE_PROPERTIES",al,true);
editItem.setFocusable(true);
popup.add(editItem);
editItem.setAccelerator(KeyStroke.getKeyStroke(Messages.getKeyboardString("Listener.TREE_PROPERTIES")));
}
popup.show(e.getComponent(),e.getX(),e.getY());
}
public void add(MutableTreeNode arg0)
{
super.add(arg0);
fireUpdate();
}
public void insert(MutableTreeNode newChild, int childIndex)
{
super.insert(newChild,childIndex);
fireUpdate();
}
public void remove(int childIndex)
{
super.remove(childIndex);
fireUpdate();
}
private void fireUpdate()
{
fireUpdate(trigger.getEvent());
}
private Resource<?,?> deRef()
{
return Util.deRef((ResourceReference<?>) res);
}
private void fireUpdate(UpdateEvent e)
{
trigger.fire(e);
if (e != null && parent != null && parent instanceof ResNode) ((ResNode) parent).fireUpdate(e);
}
/**
* Recursively checks (from this node down) for a node with a res field
* referring to the same instance as res.
* @param res The resource to look for
* @return Whether the resource was found
*/
public boolean contains(ResourceReference<? extends Resource<?,?>> res)
{
if (this.res == res) return true; //Just in case
if (children != null) for (Object obj : children)
if (obj instanceof ResNode)
{
ResNode node = (ResNode) obj;
if (node.isLeaf())
{
if (node.res == res) return true;
}
else
{
if (node.contains(res)) return true;
}
}
return false;
}
//TODO: This should be generic if ResNode is ever changed to have
//generic tree node children.
public Vector<ResNode> getChildren()
{
return (Vector)children;
}
public ResourceReference<? extends Resource<?,?>> getRes()
{
return res;
}
public void updated(UpdateEvent e)
{
// If a sprite, a background, or an object has been udpated, reset the undo
if (kind == Sprite.class || kind == Background.class || kind == GmObject.class)
{
for (JInternalFrame room : LGM.mdi.getAllFrames())
{
if (room instanceof RoomFrame) ((RoomFrame) room).resetUndoManager();
}
}
if (status == STATUS_SECONDARY)
{
icon = null;
Resource<?,?> r = deRef();
if (r != null)
{
setUserObject(r.getName());
Util.invokeOnceLater(nameUpdater);
}
else
removeFromParent();
}
fireUpdate(e);
}
private class NameUpdater implements Runnable
{
ResNode node;
public NameUpdater(ResNode resNode)
{
node = resNode;
}
public void run()
{
if (frame != null && frame instanceof InstantiableResourceFrame<?,?>)
{
InstantiableResourceFrame<?,?> resFrame = (InstantiableResourceFrame<?,?>) frame;
Resource<?,?> r = deRef();
if (r != null)
{
String n = r.getName();
resFrame.setTitle(n);
if (!resFrame.name.getText().equals(n)) resFrame.name.setText(n);
}
}
if (LGM.tree == null) return;
DefaultTreeModel model = ((DefaultTreeModel) LGM.tree.getModel());
model.nodeChanged(node);
}
}
public boolean isInstantiable()
{
return InstantiableResource.class.isAssignableFrom(kind);
}
public boolean isEditable()
{
return isInstantiable();
}
}