summaryrefslogtreecommitdiff
path: root/NET/worlds/console/WorldsMarkPart.java
blob: d9afcab02cdc3c24fc2e92029a72d3919c86898c (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
package NET.worlds.console;

import NET.worlds.core.IniFile;
import NET.worlds.network.NetUpdate;
import NET.worlds.network.RemoteFileConst;
import NET.worlds.network.URL;
import NET.worlds.scape.FrameEvent;
import NET.worlds.scape.Pilot;
import NET.worlds.scape.Restorer;
import NET.worlds.scape.Room;
import NET.worlds.scape.Saver;
import NET.worlds.scape.TeleportAction;
import NET.worlds.scape.TeleportStatus;
import NET.worlds.scape.World;
import java.awt.Container;
import java.awt.Event;
import java.awt.Font;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.io.File;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Vector;

public class WorldsMarkPart implements FramePart, DialogReceiver, TeleportStatus, RemoteFileConst {
   private static final String worldsMarksFileName = "Gamma.worldsmarks";
   private static final int MAX_HISTORY = 10;
   private static URL userMarksURL = URL.make("home:Gamma.worldsmarks");
   private String name;
   private Menu menu;
   private Menu letsMenu;
   private MenuItem addItem = new MenuItem(Console.message("Add-WorldsMark"));
   private MenuItem deleteItem = new MenuItem(Console.message("Delete-WorldsMark"));
   private MenuItem editItem = new MenuItem(Console.message("Edit-WorldsMark"));
   private MenuItem locationItem = new MenuItem(Console.message("Change-Location"));
   private Menu historyMenu = new Menu(Console.message("Back"));
   private static Font font;
   private boolean teleporting = false;
   private boolean startLocationDialog = false;
   static Vector<String> systemMarksNames;
   private static Vector<Vector<BookmarkMenuItem>> systemMarks;
   private static Vector<BookmarkMenuItem> userMarks;
   private static Vector<BookmarkMenuItem> historyItems;
   private static int firstUserItem;
   private static Hashtable<String, String> renamed;
   private static Vector<String> excludes;
   private static Vector<String> sequence;

   static {
      Locale currentLocale = Locale.getDefault();
      System.out.println("System Locale is " + currentLocale);
      String defLang = IniFile.gamma().getIniString("DEFAULTLANGUAGE", "");
      if (!defLang.equals("")) {
         String code1 = null;
         String code2 = null;
         if (defLang.length() >= 2) {
            code1 = defLang.substring(0, 2);
         }

         if (defLang.length() >= 5) {
            code2 = defLang.substring(3, 5);
         }

         if (code1 != null && code2 != null) {
            Locale.setDefault(new Locale(code1, code2));
            currentLocale = Locale.getDefault();
            System.out.println("New Locale is " + currentLocale);
         } else {
            System.out.println("ERROR: DEFAULTLANGUAGE mustbe in the form xx_XX");
         }
      }

      font = new Font(Console.message("MenuFont"), 0, 12);
      historyItems = new Vector<BookmarkMenuItem>();
      historyItems.addElement(new BookmarkMenuItem(Console.message("end-of-last"), "world:restart"));
      renamed = new Hashtable<String, String>();
      renamed.put("lets", "Hang");
      renamed.put("worldschat", "WorldsCenter");
      renamed.put("polygram", "WorldsStore.com");
      renamed.put("stadium", "NY Yankees");
      renamed.put("bowie", "Bowie I");
      renamed.put("texas", "Texas");
      renamed.put("hanson", "Hanson I");
      excludes = new Vector<String>();
      excludes.addElement("funhouse");
      excludes.addElement("chaos");
      sequence = new Vector<String>();
      sequence.addElement(getFirstWorld().toLowerCase());
      sequence.addElement("avatargallery");
      sequence.addElement("dressingroom");
   }

   public WorldsMarkPart() {
      this(null);
   }

   public WorldsMarkPart(String name) {
      this.name = name;
   }

   public boolean isTeleporting() {
      return this.teleporting;
   }

   public static String getExternalName(String name) {
      String extName = renamed.get(name.toLowerCase());
      return extName != null ? extName : name;
   }

   public Menu getMenu() {
      if (this.menu == null) {
         if (this.name != null) {
            this.menu = new Menu(this.name);
         } else {
            this.menu = new PopupMenu();
         }

         this.letsMenu = new PopupMenu();
         this.historyMenu.setFont(font);
         this.letsMenu.add(this.historyMenu);
         this.letsMenu.addSeparator();
         if (userMarks == null) {
            loadMarks();
         }

         if (systemMarks.size() != 0) {
            for (int i = 0; i < systemMarks.size(); i++) {
               String subName = systemMarksNames.elementAt(i);
               Menu subMenu = new Menu(getExternalName(subName));
               addItems(subMenu, systemMarks.elementAt(i));
               subMenu.setFont(font);
               this.letsMenu.add(subMenu);
            }
         }

         this.addMainMenuItem(this.addItem);
         this.addMainMenuItem(this.deleteItem);
         this.addMainMenuItem(this.editItem);
         this.addMainMenuItem(this.locationItem);
         this.addMainMenuSeparator();
         addItems(this.menu, userMarks);
         addItems(this.historyMenu, historyItems);
      }

      return this.menu;
   }

   public Menu getLetsMenu() {
      if (this.letsMenu == null) {
         this.getMenu();
      }

      return this.letsMenu;
   }

   public static PopupMenu getPackageMenu(String pname) {
      int p = findPackageNum(pname);
      if (p < 0) {
         return null;
      } else {
         PopupMenu m = new PopupMenu();
         Vector<BookmarkMenuItem> v = systemMarks.elementAt(p);

         for (int i = 0; i < v.size(); i++) {
            MenuItem mi = v.elementAt(i);
            if (mi instanceof BookmarkMenuItem) {
               BookmarkMenuItem bmi = v.elementAt(i);
               String name = bmi.getLabel();
               String targ = bmi.getTarget();
               URL u = URL.make(TeleportAction.toURLString(targ));
               String pkg = TeleportAction.getPackageNameOfWorld(u);
               if (pkg != null && findPackage(pkg) == null) {
                  String dirName = URL.make("home:" + pkg).unalias();
                  if (!new File(dirName).isDirectory()) {
                     name = "Download " + name;
                  }
               }

               m.add(new BookmarkMenuItem(name, targ));
            }
         }

         boolean isFirst = true;

         for (int ix = 0; ix < userMarks.size(); ix++) {
            URL u = URL.make(TeleportAction.toURLString(getBookmarkTarget(ix)));
            String s = TeleportAction.getPackageNameOfWorld(u);
            if (s != null && s.equalsIgnoreCase(pname)) {
               if (isFirst) {
                  isFirst = false;
                  m.addSeparator();
               }

               BookmarkMenuItem item = new BookmarkMenuItem(getBookmarkName(ix), getBookmarkTarget(ix));
               m.add(item);
            }
         }

         return m;
      }
   }

   private static void addItems(Menu m, Vector<BookmarkMenuItem> v) {
      for (int i = 0; i < v.size(); i++) {
         MenuItem mItem = v.elementAt(i);
         mItem.setFont(font);
         m.add(mItem);
      }
   }

   private void addMainMenuItem(MenuItem item) {
      item.setFont(font);
      this.menu.add(item);
      firstUserItem++;
   }

   private void addMainMenuSeparator() {
      this.menu.addSeparator();
      firstUserItem++;
   }

   public static String getFirstWorld() {
      IniFile ini = new IniFile("InstalledWorlds");
      return ini.getIniString("InstalledWorld0", "");
   }

   public static String getFirstSystemMarkURL() {
      if (userMarks == null) {
         loadMarks();
      }

      for (int i = 0; i < systemMarks.size(); i++) {
         Vector<BookmarkMenuItem> v = systemMarks.elementAt(i);
         if (v.size() > 0) {
            return v.elementAt(0).getTarget();
         }
      }

      int j = 0;
      return j < userMarks.size() ? userMarks.elementAt(j).getTarget() : null;
   }

   private static void shortenNames(String prefix, Vector<BookmarkMenuItem> v) {
      int count = v.size();
      int pfxlen = prefix.length();

      for (int i = 0; i < count; i++) {
         MenuItem item = v.elementAt(i);
         String label = item.getLabel();
         if (label.startsWith(prefix)) {
            item.setLabel(label.substring(pfxlen).trim());
         }
      }
   }

   private static int findPackageNum(String pname) {
      Vector<String> v = systemMarksNames;
      if (v != null) {
         int i = v.size();

         while (--i >= 0) {
            String s = v.elementAt(i);
            if (s.equalsIgnoreCase(pname)) {
               return i;
            }
         }
      }

      return -1;
   }

   public static String findPackage(String pname) {
      int i = findPackageNum(pname);
      return i < 0 ? null : systemMarksNames.elementAt(i);
   }

   private static void loadMarks() {
      systemMarks = new Vector<Vector<BookmarkMenuItem>>();
      systemMarksNames = new Vector<String>();
      IniFile ini = new IniFile("InstalledWorlds");
      String[] sequenced = new String[sequence.size()];

      for (int count = 0; count < NetUpdate.maxInstalledWorlds(); count++) {
         String curvalue = ini.getIniString("InstalledWorld" + count, "");
         if (!curvalue.equals("")) {
            NetUpdate.maxInstalledWorld(count);
            String pkgLower = curvalue.toLowerCase();
            if (!excludes.contains(pkgLower) && (!pkgLower.equals("polygram") || !World.isWorldsStoreProscribed())) {
               int index = sequence.indexOf(pkgLower);
               if (index != -1) {
                  sequenced[index] = curvalue;
               } else {
                  systemMarksNames.addElement(curvalue);
               }
            }
         }
      }

      for (int i = sequenced.length - 1; i >= 0; i--) {
         if (sequenced[i] != null) {
            systemMarksNames.insertElementAt(sequenced[i], 0);
         }
      }

      int ix = 0;

      while (ix < systemMarksNames.size()) {
         String worldName = systemMarksNames.elementAt(ix);
         Vector<BookmarkMenuItem> v = (Vector<BookmarkMenuItem>)loadVector(URL.make("home:" + worldName + "/" + "Gamma.worldsmarks"));
         if (v.size() != 0) {
            shortenNames(worldName, v);
            systemMarks.addElement(v);
            ix++;
         } else {
            systemMarksNames.removeElementAt(ix);
         }
      }

      userMarks = (Vector<BookmarkMenuItem>)loadVector(userMarksURL);
   }

   private static Vector<?> loadVector(URL url) {
      Vector<?> v = null;

      try {
         Restorer r = new Restorer(url);
         v = r.restoreVector();
         r.done();
      } catch (Exception var3) {
         if (v != null) {
            saveVector(url, (Vector<BookmarkMenuItem>)v);
         } else {
            v = new Vector();
         }
      }

      return v;
   }

   private static void saveMarks() {
      saveVector(userMarksURL, userMarks);
   }

   private static void saveVector(URL url, Vector<BookmarkMenuItem> v) {
      try {
         Saver s = new Saver(url);
         s.saveVector(v);
         s.done();
      } catch (Exception var3) {
      }
   }

   static void gotoBookmark(int index) {
      TeleportAction.teleport(getBookmarkTarget(index), null);
   }

   static int getBookmarkCount() {
      return userMarks.size();
   }

   private static BookmarkMenuItem getBookmark(int index) {
      return userMarks.elementAt(index);
   }

   static String getBookmarkName(int index) {
      return getBookmark(index).getLabel();
   }

   static String getBookmarkTarget(int index) {
      return getBookmark(index).getTarget();
   }

   void addBookmark(String name, String target) {
      BookmarkMenuItem item = new BookmarkMenuItem(name, target);
      this.menu.add(item);
      userMarks.addElement(item);
      saveMarks();
   }

   void removeBookmark(int index) {
      userMarks.removeElementAt(index);
      this.menu.remove(index + firstUserItem);
      saveMarks();
   }

   void changeBookmark(int index, String name, String target) {
      BookmarkMenuItem item = getBookmark(index);
      item.setLabel(name);
      item.setTarget(target);
      saveMarks();
   }

   @Override
   public void dialogDone(Object who, boolean confirmed) {
      if (confirmed) {
         if (who instanceof LocationDialog) {
            LocationDialog locationDialog = (LocationDialog)who;
            String location = locationDialog.getLocationURL();
            if (location.length() != 0) {
               TeleportAction.teleport(location, null);
            }
         } else {
            BookmarkAddDialog adder = (BookmarkAddDialog)who;
            BookmarkEditDialog editor = adder.getEditor();
            this.addBookmark(editor.getName(), editor.getTarget());
         }
      }
   }

   private static BookmarkMenuItem getHistory(int index) {
      return historyItems.elementAt(index);
   }

   private static BookmarkMenuItem getItemFromTarget(String target, Vector<BookmarkMenuItem> vec) {
      for (int i = 0; i < vec.size(); i++) {
         BookmarkMenuItem item = vec.elementAt(i);
         if (item.getTarget().equals(target)) {
            return item;
         }
      }

      return null;
   }

   private static BookmarkMenuItem getItemFromName(String name, Vector<BookmarkMenuItem> vec) {
      for (int i = 0; i < vec.size(); i++) {
         BookmarkMenuItem item = vec.elementAt(i);
         if (item.getLabel().equals(name)) {
            return item;
         }
      }

      return null;
   }

   private static String getWorldName(String url) {
      int rindex = url.toLowerCase().lastIndexOf(".world");
      int windex;
      return rindex == -1 || (windex = url.lastIndexOf(47, rindex)) == -1 && (windex = url.lastIndexOf(58, rindex)) == -1
         ? null
         : url.substring(windex + 1, rindex);
   }

   public static String getCurrentPackageName() {
      Pilot pilot = Pilot.getActive();
      if (pilot == null) {
         return null;
      } else {
         World world = pilot.getWorld();
         if (world == null) {
            return null;
         } else {
            URL url = world.getSourceURL();
            return url == null ? null : TeleportAction.getPackageNameOfWorld(url);
         }
      }
   }

   private static String getCurrentURLName() {
      String name = "";
      Pilot pilot = Pilot.getActive();
      if (pilot != null) {
         World world = pilot.getWorld();
         if (world != null) {
            URL url = world.getSourceURL();
            if (url != null) {
               String worldFileName = getWorldName(url.getAbsolute());
               if (worldFileName != null) {
                  name = worldFileName;
               }
            }

            String worldName = world.getName();
            if (worldName != null) {
               if (!name.equals("")) {
                  name = name + " ";
               }

               name = name + worldName;
            }

            Room room = pilot.getRoom();
            if (room != null) {
               String roomName = room.getName();
               if (roomName != null) {
                  if (!name.equals("")) {
                     name = name + " ";
                  }

                  name = name + roomName;
               }
            }
         }
      }

      return name;
   }

   static String getCurrentPositionName() {
      return getCurrentPositionName(userMarks);
   }

   static String getCurrentPositionName(Vector<BookmarkMenuItem> v) {
      String position = getCurrentPositionURL(false);
      int count = systemMarks.size();

      for (int i = 0; i < count; i++) {
         BookmarkMenuItem item;
         if ((item = getItemFromTarget(position, systemMarks.elementAt(i))) != null) {
            return systemMarksNames.elementAt(i) + " " + item.getLabel();
         }
      }

      BookmarkMenuItem item;
      if ((item = getItemFromTarget(position, userMarks)) == null && (item = getItemFromTarget(position, historyItems)) == null) {
         String defaultName = getCurrentURLName();
         String name = defaultName;
         int number = 1;

         while (getItemFromName(name, v) != null) {
            name = defaultName + "-" + ++number;
         }

         return name;
      } else {
         return item.getLabel();
      }
   }

   static String getCurrentPositionURL(boolean mustHaveChannel) {
      Pilot pilot = Pilot.getActive();
      if (pilot == null) {
         return "";
      } else {
         String newURL = pilot.getURL();
         if (newURL == null) {
            return "";
         } else {
            if (!mustHaveChannel) {
               int dynamicChannelIndex = newURL.indexOf("<dimension-");
               if (dynamicChannelIndex < 0) {
                  dynamicChannelIndex = newURL.indexOf("<>@");
               }

               int channelEndIndex = newURL.indexOf(">@");
               if (dynamicChannelIndex > 0 && dynamicChannelIndex < channelEndIndex) {
                  newURL = newURL.substring(0, dynamicChannelIndex) + newURL.substring(channelEndIndex + 1);
               }
            }

            return newURL;
         }
      }
   }

   @Override
   public void teleportStatus(String err, String url) {
      this.teleporting = err != null && err.length() == 0;
      if (err == null) {
         url = getCurrentPositionURL(true);
         if (url.length() == 0) {
            return;
         }

         Menu menu = this.getMenu();

         for (int i = 0; i < historyItems.size(); i++) {
            if (getHistory(i).getTarget().equals(url)) {
               historyItems.removeElementAt(i);
               this.historyMenu.remove(i);
               break;
            }
         }

         BookmarkMenuItem item = new BookmarkMenuItem(getCurrentPositionName(historyItems), url);
         historyItems.insertElementAt(item, 0);
         this.historyMenu.insert(item, 0);

         while (historyItems.size() > 10) {
            historyItems.removeElementAt(10);
            this.historyMenu.remove(10);
         }
      }
   }

   @Override
   public void activate(Console c, Container f, Console prev) {
   }

   @Override
   public void deactivate() {
      if (this.menu != null) {
         this.menu.removeAll();
      }

      this.menu = null;
      this.letsMenu = null;
      firstUserItem = 0;
   }

   @Override
   public boolean action(Event event, Object what) {
      Object target = event.target;
      if (this.menu != null) {
         if (target == this.addItem) {
            if (Pilot.getActive().getRoom().getAllowTeleport()) {
               new BookmarkAddDialog(Console.getFrame(), this);
            } else {
               Console.println(Console.message("no-teleport"));
            }
         } else if (target == this.deleteItem) {
            new BookmarkDeleteDialog(this);
         } else if (target == this.editItem) {
            new BookmarkListDialog(this);
         } else if (target == this.locationItem) {
            this.startLocationDialog = true;
         } else {
            if (!(target instanceof BookmarkMenuItem)) {
               return false;
            }

            TeleportAction.teleport(((BookmarkMenuItem)target).getTarget(), null);
         }

         return true;
      } else {
         return false;
      }
   }

   @Override
   public boolean handle(FrameEvent f) {
      if (this.startLocationDialog) {
         this.startLocationDialog = false;
         String url = "";
         Pilot pilot = Pilot.getActive();
         if (pilot != null) {
            url = pilot.getURL();
         }

         new LocationDialog(Console.getFrame(), this, Console.message("Change-Location"), url);
      }

      return true;
   }
}