summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/EditTile.java
blob: d0ab6005fa782e54f8e4f90336f636b34123ccac (plain) (blame)
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
package NET.worlds.scape;

import NET.worlds.console.Console;
import NET.worlds.console.DialogDisabled;
import NET.worlds.console.DialogReceiver;
import NET.worlds.console.ExposedPanel;
import NET.worlds.console.Main;
import NET.worlds.console.MainCallback;
import NET.worlds.console.PolledDialog;
import NET.worlds.console.RenderCanvas;
import NET.worlds.console.SnapTool;
import NET.worlds.console.Tree;
import NET.worlds.console.TreeCallback;
import NET.worlds.network.URL;
import java.awt.Button;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.Point;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.Vector;

public class EditTile extends ExposedPanel implements DialogReceiver, TreeCallback, MainCallback, DialogDisabled {
   private Button editButton = new Button(Console.message("Edit"));
   private Button addButton = new Button(Console.message("Add"));
   private Button delButton = new Button(Console.message("Delete"));
   private Button helpButton = new Button(Console.message("Help"));
   private ToolBar toolbar = new ToolBar();
   private Label title = new Label();
   private String titleText = "";
   private boolean usingTitleAsPrompt;
   private PropList props = new PropList();
   private int queuedFunction = 0;
   private Object queuedObject;
   private Tree tree;
   private PolledDialog activePopup;
   private boolean startup = true;
   private World rootWorld = null;
   private boolean isDialogDisabled;
   private static final int NONE = 0;
   private static final int DELETE = 1;
   private static final int ADD = 2;
   private static final int CHANGE = 3;
   private static final int UPDATE = 4;
   private static final int CUT = 5;
   private static final int COPY = 6;
   private static final int PASTE = 7;
   private static final int UNDO = 8;
   private static final int EDIT = 9;
   private static final int DROP = 10;
   private static final int HELP = 11;
   private static ClipboardEntry clipboard;
   private static UndoStack undoStack = new UndoStack();
   private Undoable preAddStackTop;
   private boolean isAdd;
   private SendURLAction helpAction = new SendURLAction();
   private Persister helpBrowser;

   public void viewProperties(Object obj) {
      Console.getFrame().setShaperVisible(true);
      this.queue(3, obj);
   }

   public void libraryDrop(URL url, String propertyName, Component comp, Point location) {
      this.queue(10, new DropInfo(url, propertyName, comp, location));
   }

   public EditTile(Tree tree) {
      this.tree = tree;
      tree.setOwner(this);
      GridBagLayout gbag = new GridBagLayout();
      this.setLayout(gbag);
      GridBagConstraints c = new GridBagConstraints();
      c.fill = 2;
      c.gridwidth = 0;
      c.weightx = 1.0;
      c.weighty = 0.0;
      c.anchor = 18;
      this.add(gbag, this.toolbar, c);
      this.add(gbag, this.title, c);
      c.weighty = 1.0;
      c.fill = 1;
      c.gridwidth = 2;
      c.gridheight = 0;
      this.add(gbag, this.props, c);
      c.weightx = 0.0;
      c.weighty = 0.0;
      c.gridwidth = 0;
      c.gridheight = 1;
      c.fill = 2;
      this.add(gbag, this.editButton, c);
      this.add(gbag, this.addButton, c);
      this.add(gbag, this.delButton, c);
      this.add(gbag, this.helpButton, c);
      Main.register(this);
   }

   public void update() {
      this.queue(4);
   }

   private void add(GridBagLayout gbag, Component comp, GridBagConstraints c) {
      gbag.setConstraints(comp, c);
      this.add(comp);
   }

   private boolean queue(int function) {
      return this.queue(function, null);
   }

   private synchronized boolean queue(int function, Object obj) {
      if (this.queuedFunction == 0 && this.activePopup == null) {
         this.queuedFunction = function;
         this.queuedObject = obj;
         this.disable();
      }

      return true;
   }

   @Override
   public synchronized void mainCallback() {
      if (this.startup) {
         World w = Pilot.getActiveWorld();
         if (w != null) {
            this.change(w);
            this.startup = false;
         }
      }

      if (this.rootWorld != null) {
         this.rootWorld.incRef();
      }

      boolean enable = true;
      switch (this.queuedFunction) {
         case 0:
            return;
         case 1:
            this.delete(false);
            break;
         case 2:
            enable = this.doAdd();
            break;
         case 3:
            this.change(this.queuedObject);
            break;
         case 4:
            this.tree.update();
            break;
         case 5:
            this.delete(true);
            break;
         case 6:
            this.doCopy();
            break;
         case 7:
            this.doPaste();
            break;
         case 8:
            this.doUndo();
            break;
         case 9:
            enable = this.doEdit();
            break;
         case 10:
            if (!this.drop((DropInfo)this.queuedObject)) {
               Object[] arguments = new Object[]{new String("" + ((DropInfo)this.queuedObject).url)};
               Console.println(MessageFormat.format(Console.message("Target-doesnt"), arguments));
            }
            break;
         case 11:
            this.doHelp();
      }

      this.enable(enable);
      this.queuedFunction = 0;
      this.queuedObject = null;
   }

   private void delete(boolean isCut) {
      if (this.delButton.isEnabled()) {
         PropTreeNode e = (PropTreeNode)this.tree.getSelectedNode();
         if (e != null && this.tree.hasFocus()) {
            this.addUndoable(e.delete(isCut));
         } else {
            Property p = this.props.getSelectedProperty();

            assert p.canSetNull() && p.get() != null;

            this.addUndoableSet(p, null);
         }

         this.tree.update();
      }
   }

   public static void adjustDroppedSource(SuperRoot sr) {
      if (sr != null && (!(sr instanceof WObject) || !((WObject)sr).isDynamic())) {
         sr.setSourceURL(null);
      }
   }

   private boolean drop(DropInfo info) {
      Object dropped = info.url;
      if (info.url.endsWith(".class") || info.url.endsWith("." + WObject.getSaveExtension())) {
         SuperRoot sr = WobLoader.immediateLoad(info.url);
         adjustDroppedSource(sr);
         dropped = sr;
      }

      return this.drop(dropped, info.propertyName, false, info.comp, info.location);
   }

   private boolean drop(Object obj, String propertyName, boolean isPaste) {
      return this.drop(obj, propertyName, isPaste, null, null);
   }

   private boolean drop(Object obj, String propertyName, boolean isPaste, Component comp, Point location) {
      if (obj == null) {
         return false;
      } else {
         PropTreeNode e = null;
         Point3Temp dropPoint = null;
         if (comp == null && this.tree.hasFocus()) {
            e = (PropTreeNode)this.tree.getSelectedNode();
         } else if (comp == this.tree) {
            e = (PropTreeNode)this.tree.elementAt(location);
         } else if (comp instanceof RenderCanvas) {
            Camera c = ((RenderCanvas)comp).getCamera();
            dropPoint = Point3Temp.make();
            WObject picked = c.getObjectAt(location.x, location.y, false, dropPoint);
            if (picked != null) {
               if (obj instanceof WObject) {
                  picked = picked.getRoom();
               }
            } else {
               Pilot pilot = Pilot.getActive();
               picked = pilot.getRoom();
               dropPoint.set(0.0F, 180.0F, 0.0F);
               dropPoint.times(pilot);
            }

            if (picked != null) {
               this.change(picked);
               e = (PropTreeNode)this.tree.getSelectedNode();
            }
         }

         if (e == null) {
            return false;
         } else {
            boolean success = false;
            VectorProperty vp;
            PropAdder adder;
            if ((vp = e.getContainingVectorProperty()) != null && (adder = vp.getAdder()) != null) {
               success = adder.libraryDrop(this, obj, isPaste, true);
            }

            if (!success) {
               Object top;
               for (top = null; e != null; e = (PropTreeNode)e.getParent()) {
                  top = e.getObject();
                  if (top instanceof Properties) {
                     break;
                  }

                  if (top instanceof Property) {
                     Property prop = (Property)top;
                     top = ((Property)top).get();
                     if (top instanceof Properties) {
                        break;
                     }
                  }
               }

               if (e != null) {
                  Vector targets = new Vector();
                  int topLevelTargets = this.recurseFindDropTargets(targets, obj, propertyName, isPaste, new EnumProperties(top), 0, 10);
                  if (topLevelTargets == 1 || topLevelTargets == 0 && targets.size() == 1) {
                     success = ((LibraryDrop)targets.elementAt(0)).libraryDrop(this, obj, isPaste, true);
                  }
               }
            }

            if (success) {
               if (obj instanceof Properties) {
                  this.change(obj);
               } else {
                  this.tree.update();
               }
            }

            WObject wobj;
            if (dropPoint != null && obj instanceof WObject && !(obj instanceof Room) && (wobj = (WObject)obj).isActive()) {
               dropPoint.z = wobj.getZ();
               wobj.moveTo(SnapTool.snapTool().snapTo(dropPoint));
            }

            return success;
         }
      }
   }

   private int recurseFindDropTargets(Vector targets, Object obj, String propertyName, boolean isPaste, Enumeration props, int level, int maxLevel) {
      int firstLevelMatches = 0;
      if (level > maxLevel) {
         return 0;
      } else {
         while (props.hasMoreElements()) {
            Property p = (Property)props.nextElement();
            boolean propMatch = propertyName == null || propertyName.equals(p.getName());
            if (propMatch) {
               LibraryDrop target = null;
               if (p instanceof VectorProperty) {
                  target = ((VectorProperty)p).getAdder();
               } else {
                  target = p.getEditor();
               }

               if (target != null && target.libraryDrop(this, obj, isPaste, false)) {
                  if (level == 0) {
                     targets.insertElementAt(target, firstLevelMatches++);
                  } else {
                     targets.addElement(target);
                  }
                  continue;
               }
            }

            if (!(p instanceof VectorProperty)) {
               int err = this.recurseFindDropTargets(targets, obj, propertyName, isPaste, new EnumProperties(p.get()), level + 1, maxLevel);
               if (err != 0) {
                  return err;
               }
            }
         }

         return firstLevelMatches;
      }
   }

   private boolean change(Object obj) {
      Object parent = obj;
      Vector lineage = new Vector();

      Object p;
      while (parent instanceof Properties && (p = ((Properties)parent).propertyParent()) != null) {
         if (lineage != null) {
            Enumeration e = new EnumProperties(p);
            boolean found = false;

            while (e.hasMoreElements()) {
               Property prop = (Property)e.nextElement();
               Object o = prop.get();
               boolean isVector = prop instanceof VectorProperty;
               if (o == parent || isVector && o != null && ((Vector)o).indexOf(parent) != -1) {
                  if (isVector) {
                     lineage.insertElementAt(parent, 0);
                  }

                  lineage.insertElementAt(prop, 0);
                  found = true;
                  break;
               }
            }

            if (!found) {
               lineage = null;
            }
         }

         parent = p;
      }

      this.rootWorld = null;
      if (parent instanceof World) {
         this.rootWorld = (World)parent;
      }

      PropTreeNode root = new PropTreeNode(parent);
      if (lineage != null) {
         this.tree.change(root, lineage);
      } else {
         this.tree.change(root, obj);
      }

      return true;
   }

   @Override
   public void treeFocusChanged(boolean hasFocus) {
      if (hasFocus) {
         this.props.deselect(this.props.getSelectedIndex());
         this.adjustButtons();
      }
   }

   @Override
   public void treeChange(Object obj) {
      if (obj instanceof Property && !(obj instanceof VectorProperty)) {
         obj = ((Property)obj).get();
      }

      this.props.setObject(obj);
      this.toolbar.setCurrentObject(obj);
      String name = obj.getClass().getName();
      int index;
      if ((index = name.lastIndexOf(46)) != -1) {
         name = name.substring(index + 1);
      }

      this.setTitle(name + " " + Console.message("Properties"));
      this.adjustButtons();
   }

   private void setTitle(String titleText) {
      this.titleText = titleText;
      if (!this.usingTitleAsPrompt) {
         this.title.setText(titleText);
      }
   }

   public void setPrompt(String promptText) {
      this.title.setText((this.usingTitleAsPrompt = promptText != null) ? promptText : this.titleText);
   }

   private void adjustButtons() {
      PropTreeNode e = (PropTreeNode)this.tree.getSelectedNode();
      if (e != null && this.tree.hasFocus()) {
         this.editButton.enable(e.canEdit());
         this.delButton.enable(e.canDelete());
         this.addButton.enable(e.canAdd());
         this.helpButton.enable(true);
      } else {
         Property p = this.props.getSelectedProperty();
         if (p != null) {
            Object obj = p.get();
            this.editButton.enable(p.getEditor() != null);
            this.delButton.enable(p.canSetNull() && obj != null);
            this.addButton.enable(p.canSetNull() && p.getEditor() != null && obj == null);
            this.helpButton.enable(p.helpExists);
         } else {
            this.editButton.disable();
            this.delButton.disable();
            this.addButton.disable();
            this.helpButton.disable();
         }
      }
   }

   @Override
   public void dialogDisable(boolean disable) {
      this.isDialogDisabled = disable;
      this.toolbar.dialogDisable(disable);
   }

   @Override
   public boolean handleEvent(java.awt.Event event) {
      if (event.id == 701) {
         if (event.target == this.props) {
            this.tree.setFocus(false);
         }

         this.adjustButtons();
      }

      return this.isDialogDisabled ? false : super.handleEvent(event);
   }

   @Override
   public boolean action(java.awt.Event event, Object what) {
      Object target = event.target;
      if (target == this.delButton) {
         return this.queue(1);
      } else if (target == this.addButton) {
         return this.queue(2);
      } else if (target != this.editButton && (target != this.props || !this.editButton.isEnabled())) {
         return target == this.helpButton ? this.queue(11) : false;
      } else {
         return this.queue(9);
      }
   }

   @Override
   public void dialogDone(Object who, boolean confirmed) {
      if (who == this.activePopup) {
         this.activePopup = null;
         this.tree.enable();
         if (confirmed) {
            Undoable u;
            Object obj;
            if (this.isAdd
               && (u = undoStack.peek()) != this.preAddStackTop
               && u instanceof UndoablAdd
               && (obj = ((UndoablAdd)u).getObject()) instanceof Properties) {
               this.change(obj);
            } else {
               this.tree.update();
            }
         }

         this.enable();
      }
   }

   private boolean doEdit() {
      Property p = this.props.getSelectedProperty();
      PropEditor editor;
      if (p != null && (editor = p.getEditor()) != null) {
         this.isAdd = false;
         this.activePopup = editor.edit(this, "Edit " + p.getName());
         this.tree.disable();
         return false;
      } else {
         return true;
      }
   }

   private boolean doAdd() {
      PropTreeNode e = (PropTreeNode)this.tree.getSelectedNode();
      PropAdder adder;
      if (e != null && this.tree.hasFocus() && (adder = e.getAdder()) != null) {
         this.preAddStackTop = undoStack.peek();
         this.isAdd = true;
         this.activePopup = adder.add(this, "Add to " + e.getContainerName());
         this.tree.disable();
         return false;
      } else {
         return true;
      }
   }

   public void cut() {
      this.queue(5);
   }

   public void paste() {
      this.queue(7);
   }

   private boolean doPaste() {
      if (clipboard != null) {
         SuperRoot obj = clipboard.paste();
         if (this.drop(obj, null, true)) {
            return true;
         }

         clipboard.unPaste(obj);
         Console.println(Console.message("Clip-contents"));
      } else {
         Console.println(Console.message("Clip-empty"));
      }

      return false;
   }

   private boolean doHelp() {
      this.helpAction.showDialog = false;
      if (this.tree.hasFocus() && this.props.getSelectedProperty() == null) {
         try {
            SuperRoot s = (SuperRoot)((PropTreeNode)this.tree.getSelectedNode()).getObject();
            if (s != null) {
               this.helpAction.setDestination(s.getHelpURL());
            }
         } catch (ClassCastException var5) {
            Console.println(Console.message("No-help"));
         }
      } else {
         Property p = this.props.getSelectedProperty();
         if (p != null && p.helpExists) {
            try {
               SuperRoot s = (SuperRoot)((PropTreeNode)this.tree.getSelectedNode()).getObject();
               if (s != null) {
                  this.helpAction.setDestination(s.getHelpURL(p));
               }
            } catch (ClassCastException var4) {
               Console.println(Console.message("No-help"));
            }
         }
      }

      this.helpBrowser = this.helpAction.trigger(null, this.helpBrowser);
      return true;
   }

   public void copy() {
      this.queue(6);
   }

   private void doCopy() {
      SuperRoot item = this.getCurSuperRoot(true);
      if (item != null) {
         ClipboardEntry newClip = new ClipboardEntry();
         if (newClip.copy(item)) {
            this.addUndoable(new UndoabCopy(newClip));
         }
      }
   }

   public boolean save(String fileName) {
      SuperRoot item = this.getCurSuperRoot(true);

      try {
         item.saveFile(new URL(URL.getCurDir(), fileName));
         return true;
      } catch (IOException var4) {
         return false;
      }
   }

   private SuperRoot getCurSuperRoot(boolean allowContainers) {
      Object obj = this.props.getObject();
      return obj instanceof SuperRoot ? (SuperRoot)obj : null;
   }

   public void addUndoable(Undoable u) {
      undoStack.push(u);
   }

   public void addUndoableSet(Property prop, Object obj) {
      this.addUndoable(new UndoablSet(prop, obj));
   }

   public void addUndoableAdd(VectorProperty prop, Object obj, boolean changeTree) {
      this.addUndoable(new UndoablAdd(prop, obj));
      if (!changeTree) {
         this.preAddStackTop = undoStack.peek();
      }
   }

   public void addUndoablePaste(VectorProperty prop, Object obj) {
      try {
         this.addUndoable(new UndoablPaste(prop, clipboard, obj));
      } catch (Error var4) {
         Console.println(var4.getMessage());
      }
   }

   public void undo() {
      if (Main.isMainThread()) {
         this.doUndo();
      } else {
         this.queue(8);
      }
   }

   private void doUndo() {
      if (undoStack.undo()) {
         this.tree.update();
      }
   }

   public static void setClipboard(ClipboardEntry clip) {
      clipboard = clip;
   }

   public static ClipboardEntry getClipboard() {
      return clipboard;
   }
}