-
Notifications
You must be signed in to change notification settings - Fork 25
/
LGM.java
1593 lines (1430 loc) · 49.3 KB
/
LGM.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
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2006-2011 IsmAvatar <IsmAvatar@gmail.com>
* Copyright (C) 2006, 2007 TGMG <thegamemakerguru@gmail.com>
* Copyright (C) 2007, 2008 Quadduc <quadduc@gmail.com>
* Copyright (C) 2006, 2007, 2008 Clam <clamisgood@gmail.com>
* Copyright (C) 2013, 2014, 2015 Robert B. Colton
*
* This file is part of LateralGM.
*
* LateralGM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LateralGM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License (COPYING) for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.lateralgm.main;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.SplashScreen;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Scanner;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import javax.imageio.spi.IIORegistry;
import javax.swing.AbstractButton;
import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DropMode;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.LookAndFeel;
import javax.swing.RootPaneContainer;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.OceanTheme;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.lateralgm.components.ErrorDialog;
import org.lateralgm.components.GmMenuBar;
import org.lateralgm.components.GmTreeGraphics;
import org.lateralgm.components.impl.CustomFileFilter;
import org.lateralgm.components.impl.FramePrefsHandler;
import org.lateralgm.components.impl.GmTreeEditor;
import org.lateralgm.components.impl.ResNode;
import org.lateralgm.components.mdi.MDIPane;
import org.lateralgm.file.ProjectFile;
import org.lateralgm.file.ProjectFile.ResourceHolder;
import org.lateralgm.file.ProjectFile.SingletonResourceHolder;
import org.lateralgm.file.ResourceList;
import org.lateralgm.file.iconio.ICOFile;
import org.lateralgm.file.iconio.ICOImageReaderSPI;
import org.lateralgm.file.iconio.WBMPImageReaderSpiFix;
import org.lateralgm.main.Search.InvisibleTreeModel;
import org.lateralgm.messages.Messages;
import org.lateralgm.resources.Constants;
import org.lateralgm.resources.GameSettings;
import org.lateralgm.resources.InstantiableResource;
import org.lateralgm.resources.Resource;
import org.lateralgm.resources.library.LibManager;
import org.lateralgm.subframes.ConfigurationManager;
import org.lateralgm.subframes.ConstantsFrame;
import org.lateralgm.subframes.EventPanel;
import org.lateralgm.subframes.ExtensionPackagesFrame;
import org.lateralgm.subframes.GameInformationFrame;
import org.lateralgm.subframes.GameSettingFrame;
import org.lateralgm.subframes.PreferencesFrame;
import org.lateralgm.subframes.ResourceFrame;
import org.lateralgm.subframes.ResourceFrame.ResourceFrameFactory;
import com.sun.imageio.plugins.wbmp.WBMPImageReaderSpi;
public final class LGM
{
public static final String version = "1.8.183"; //$NON-NLS-1$
// TODO: This list holds the class loader for any loaded plugins which should be
// cleaned up and closed when the application closes.
public final static ArrayList<URLClassLoader> classLoaders = new ArrayList<URLClassLoader>();
public static boolean LOADING_PROJECT = false;
private static JDialog progressDialog = null;
private static JProgressBar progressDialogBar = null;
private static String progressTitle;
public static String iconspath = "org/lateralgm/icons/"; //$NON-NLS-1$
public static String iconspack = "Calico"; //$NON-NLS-1$
public static String themename = "Swing"; //$NON-NLS-1$
public static boolean themechanged = false;
public static int javaVersion;
public static File tempDir, workDir;
static
{
//Get Java Version
String jv = System.getProperty("java.version"); //$NON-NLS-1$
Scanner s = new Scanner(jv);
s.useDelimiter("[\\._-]"); //$NON-NLS-1$
int major = s.hasNextInt() ? s.nextInt() * 10000 : 0;
int minor = s.hasNextInt() ? s.nextInt() * 100 : 0;
int patch = s.hasNextInt() ? s.nextInt() : 0;
javaVersion = major + minor + patch;
s.close();
//Tweak service providers
IIORegistry reg = IIORegistry.getDefaultInstance();
reg.registerServiceProvider(new ICOImageReaderSPI());
reg.deregisterServiceProvider(reg.getServiceProviderByClass(WBMPImageReaderSpi.class));
reg.registerServiceProvider(new WBMPImageReaderSpiFix());
//Setup workdir and tempdir
try
{
workDir = new File(LGM.class.getProtectionDomain().getCodeSource().getLocation().toURI());
}
catch (Exception e)
{
System.err.println(Messages.format("LGM.NO_WORKDIR",e.getClass(),e.getLocalizedMessage())); //$NON-NLS-1$
}
tempDir = new File(System.getProperty("java.io.tmpdir"),"lgm"); //$NON-NLS-1$ //$NON-NLS-2$
if (!tempDir.exists())
{
tempDir.mkdir();
if (javaVersion >= 10600)
{
tempDir.setReadable(true,false);
tempDir.setWritable(true,false);
}
}
}
public static JFrame frame;
public static JPanel contents;
public static JToolBar tool;
public static JToolBar filterPanel;
public static JTree tree, searchTree;
public static ResNode root;
public static ProjectFile currentFile = new ProjectFile();
public static MDIPane mdi;
private static ConstantsFrame constantsFrame;
private static GameInformationFrame gameInfo;
private static GameSettingFrame gameSet;
private static ExtensionPackagesFrame extSet;
private static EventPanel eventSelect;
private static JDialog eventDialog;
private static AbstractButton eventButton;
public static PreferencesFrame prefFrame;
public static Cursor zoomCursor;
public static Cursor zoomInCursor;
public static Cursor zoomOutCursor;
public static GmMenuBar menuBar;
public static JComboBox<GameSettings> configsCombo;
private LGM()
{
}
public static JDialog getProgressDialog()
{
// lazy create the progress dialog
if (progressDialog == null)
{
progressDialog = new JDialog(LGM.frame,true);
progressDialogBar = new JProgressBar(0,100);
progressDialogBar.setStringPainted(true);
progressDialog.add(BorderLayout.CENTER,progressDialogBar);
progressDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
progressDialog.pack();
progressDialog.setLocationRelativeTo(LGM.frame);
progressDialog.setResizable(false);
}
return progressDialog;
}
public static void showProgressDialog()
{
getProgressDialog(); // lazy create
// reset the progress just in case look
// and feel doesn't support indeterminate
progressDialogBar.setValue(0);
progressDialogBar.setMinimum(0);
progressDialogBar.setMaximum(100);
// just starting to block, worker/thread
// possibly hasn't told us actual size
progressDialogBar.setIndeterminate(true);
// empty title indeterminate
progressDialog.setTitle("");
// begin modal blocking
progressDialog.setVisible(true);
}
public static void setProgressDialogVisible(final boolean visible)
{
if (visible) showProgressDialog();
else progressDialog.setVisible(false);
}
public static void setProgressTitle(String title)
{
progressTitle = title; // main progress title
}
public static void initProgressDialog(int min, int max, final String title)
{
progressDialogBar.setMinimum(min);
progressDialogBar.setMaximum(max);
// work size known so no longer indeterminate
progressDialogBar.setIndeterminate(false);
progressTitle = title;
}
public static void setProgress(final int value, final String message)
{
progressDialog.setTitle(progressTitle + " - " + message); //$NON-NLS-1$
progressDialogBar.setValue(value);
}
private static void createMouseCursors()
{
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image cimg = LGM.getIconForKey("CursorDisplay.ZOOM").getImage(); //$NON-NLS-1$
BufferedImage img = new BufferedImage(32,32,BufferedImage.TYPE_INT_ARGB);
Graphics g = img.createGraphics();
g.drawImage(cimg,0,0,null);
g.dispose();
zoomCursor = toolkit.createCustomCursor(img,new Point(0,0),"Zoom");
cimg = LGM.getIconForKey("CursorDisplay.ZOOM_IN").getImage(); //$NON-NLS-1$
img = new BufferedImage(32,32,BufferedImage.TYPE_INT_ARGB);
g = img.createGraphics();
g.drawImage(cimg,0,0,null);
g.dispose();
zoomInCursor = toolkit.createCustomCursor(img,new Point(0,0),"ZoomIn");
cimg = LGM.getIconForKey("CursorDisplay.ZOOM_OUT").getImage(); //$NON-NLS-1$
img = new BufferedImage(32,32,BufferedImage.TYPE_INT_ARGB);
g = img.createGraphics();
g.drawImage(cimg,0,0,null);
g.dispose();
zoomOutCursor = toolkit.createCustomCursor(img,new Point(0,0),"ZoomOut");
}
public static void setLookAndFeel(String LOOKANDFEEL)
{
if (LOOKANDFEEL.equals(themename) && !LOOKANDFEEL.equals("Custom"))
{
themechanged = false;
return;
}
themechanged = true;
themename = LOOKANDFEEL;
ArrayList<URL> urls = new ArrayList<>();
if (workDir != null)
{
File dir = new File(workDir,"lookandfeels"); //$NON-NLS-1$
if (!dir.exists()) {
dir = new File(workDir.getParent(),"lookandfeels"); //$NON-NLS-1$
}
File[] ps = dir.listFiles(new CustomFileFilter(null,".jar")); //$NON-NLS-1$
if (ps != null)
{
for (File f : ps)
{
if (!f.exists()) continue;
try
{
urls.add(f.toURI().toURL());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
URLClassLoader lafClassLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]));
UIManager.put("ClassLoader", lafClassLoader);
String lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
if (LOOKANDFEEL != null)
{
if (LOOKANDFEEL.equals("Swing"))
{
lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
// This theme is also known as Metal - Ocean
lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel"; //$NON-NLS-1$
MetalLookAndFeel.setCurrentTheme(new OceanTheme());
}
else if (LOOKANDFEEL.equals("Native"))
{
lookAndFeel = UIManager.getSystemLookAndFeelClassName();
}
else if (LOOKANDFEEL.equals("Nimbus"))
{
lookAndFeel = "javax.swing.plaf.nimbus.NimbusLookAndFeel"; //$NON-NLS-1$
}
else if (LOOKANDFEEL.equals("Windows"))
{
lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; //$NON-NLS-1$
}
else if (LOOKANDFEEL.equals("Windows Classic"))
{
lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"; //$NON-NLS-1$
// Fixes UI bug in the JDK where the buttons look way too big and get cut off.
// https://bugs.openjdk.java.net/browse/JDK-8140527
UIManager.put("InternalFrame.titleButtonWidth", 22); //$NON-NLS-1$
UIManager.put("InternalFrame.titleButtonHeight", 22); //$NON-NLS-1$
}
else if (LOOKANDFEEL.equals("CDE/Motif"))
{
lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; //$NON-NLS-1$
}
else if (LOOKANDFEEL.equals("Metal"))
{
lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel"; //$NON-NLS-1$
MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
}
else if (LOOKANDFEEL.equals("Ocean"))
{
lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel"; //$NON-NLS-1$
MetalLookAndFeel.setCurrentTheme(new OceanTheme());
}
else if (LOOKANDFEEL.equals("GTK+"))
{
lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; //$NON-NLS-1$
}
else if (LOOKANDFEEL.equals("Custom"))
{
lookAndFeel = Prefs.swingThemePath;
}
else
{
// Perhaps we did not get the name right, see if the theme is installed
// and attempt to use it.
boolean foundMatch = false;
LookAndFeelInfo lnfs[] = UIManager.getInstalledLookAndFeels();
for (int i = 0; i < lnfs.length; i++)
{
if (LOOKANDFEEL.equals(lnfs[i].getName()))
{
lookAndFeel = lnfs[i].getClassName();
foundMatch = true;
break;
}
}
if (!foundMatch)
{
System.err.println("Unexpected value of LOOKANDFEEL specified: " + LOOKANDFEEL);
lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
}
}
final ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
try
{
Thread.currentThread().setContextClassLoader(lafClassLoader);
UIManager.setLookAndFeel(lookAndFeel);
}
catch (Exception e)
{
LGM.showDefaultExceptionHandler(e);
}
finally
{
Thread.currentThread().setContextClassLoader(originalClassLoader);
}
}
}
// this function is for updating the application UI of
// all windows after controls have already been created
public static void updateLookAndFeel()
{
if (!themechanged) return;
Window windows[] = Window.getWindows();
for (Window window : windows)
SwingUtilities.updateComponentTreeUI(window);
}
public static ConstantsFrame getConstantsFrame()
{
return constantsFrame;
}
public static GameInformationFrame getGameInfo()
{
return gameInfo;
}
public static GameSettingFrame getGameSettings()
{
return gameSet;
}
public static ExtensionPackagesFrame getExtensionPackages()
{
return extSet;
}
public static void showConstantsFrame(final Constants cnsts)
{
Runnable run = new Runnable() {
@Override
public void run()
{
if (constantsFrame.res != cnsts && constantsFrame.resOriginal != cnsts) {
constantsFrame.res = cnsts;
constantsFrame.resOriginal = cnsts.clone();
constantsFrame.revertResource();
}
constantsFrame.updateTitle();
getConstantsFrame().setVisible(true);
getConstantsFrame().toTop();
}
};
constantsFrame.doDefaultCloseAction(run);
}
public static void showGameInformation()
{
getGameInfo().setVisible(true);
getGameInfo().toTop();
}
public static void showGameSettings(final GameSettings set)
{
Runnable run = new Runnable() {
@Override
public void run()
{
if (gameSet.res != set && gameSet.resOriginal != set) {
gameSet.res = set;
gameSet.resOriginal = set.clone();
gameSet.revertResource();
}
gameSet.updateTitle();
getGameSettings().setVisible(true);
getGameSettings().toTop();
}
};
gameSet.doDefaultCloseAction(run);
}
public static void showExtensionPackages()
{
getExtensionPackages().setVisible(true);
getExtensionPackages().toTop();
}
// low-level icon reading helper, no cache/keys just
// straight up read the file and return the icon
private static ImageIcon loadIcon(String filename)
{
String location = null;
URL url = null;
if (Prefs.iconPack.equals("Custom")) //$NON-NLS-1$
{
String custompath = Prefs.iconPath + filename;
if ((new File(custompath)).exists())
{
// we found the users custom icon where they said it would be
location = custompath;
}
else
{
// fallback to Calico built into the jar
String fallback = iconspath + "Calico/" + filename; //$NON-NLS-1$
url = LGM.class.getClassLoader().getResource(fallback);
}
}
else
{
// standard icon pack built into the jar is requested
String jarpath = iconspath + iconspack + '/' + filename;
url = LGM.class.getClassLoader().getResource(jarpath);
}
if (location != null) return new ImageIcon(location);
else if (url != null) return new ImageIcon(url);
return null;
}
// cached icon reading from filename
private static final HashMap<String,ImageIcon> iconCache = new HashMap<>();
public static ImageIcon findIcon(String filename)
{
ImageIcon ico = iconCache.get(filename);
if (ico != null) return ico;
ico = loadIcon(filename);
// cache relative filename so we don't cache
// multiple icon packs at the same time
if (ico != null) iconCache.put(filename,ico);
return ico;
}
// helper method for purging the icon cache such
// as when changing icon packs in preferences
public static void reloadIcons()
{
for (Entry<String,ImageIcon> ico : iconCache.entrySet())
{
// flush the image first in case we are
// reloading the same file
ico.getValue().getImage().flush();
// load in the icon from the right pack
ImageIcon fresh = loadIcon(ico.getKey());
// put the fresh image into the old ico
// so everybody using it gets updated
ico.getValue().setImage(fresh.getImage());
}
// repaint all the windows (dialogs included)
// and have them revalidate layouts in case
// the icons have different sizes than before
Window windows[] = Window.getWindows();
for (Window window : windows)
window.repaint();
}
// high-level, key-indexed icon reading used for initial
// icon setting on components & buttons
private static Properties iconProps = null;
public static ImageIcon getIconForKey(String key)
{
if (iconProps == null)
{
iconProps = new Properties();
InputStream is = LGM.class.getClassLoader().getResourceAsStream(
"org/lateralgm/main/icons.properties"); //$NON-NLS-1$
try
{
if (is == null) throw new IOException();
iconProps.load(is);
}
catch (IOException e)
{
System.err.println("Unable to read icons.properties"); //$NON-NLS-1$
}
}
String filename = iconProps.getProperty(key,""); //$NON-NLS-1$
if (!filename.isEmpty()) return findIcon(filename);
return null;
}
public static JButton makeButton(String key)
{
JButton but = new JButton();
makeButton(but,key);
return but;
}
public static AbstractButton makeButton(AbstractButton but, String key)
{
Icon ico = LGM.getIconForKey(key);
if (ico != null)
but.setIcon(ico);
else
but.setIcon(GmTreeGraphics.getBlankIcon());
but.setActionCommand(key);
but.setToolTipText(Messages.getString(key));
but.addActionListener(Listener.getInstance());
return but;
}
private static JToolBar createToolBar()
{
tool = new JToolBar();
tool.setFloatable(true);
tool.add(makeButton("Toolbar.NEW")); //$NON-NLS-1$
tool.add(makeButton("Toolbar.OPEN")); //$NON-NLS-1$
tool.add(makeButton("Toolbar.SAVE")); //$NON-NLS-1$
tool.add(makeButton("Toolbar.SAVEAS")); //$NON-NLS-1$
tool.addSeparator();
for (Class<? extends Resource<?,?>> k : Resource.kinds)
if (InstantiableResource.class.isAssignableFrom(k))
{
Icon ico = ResNode.ICON.get(k);
if (ico == null) ico = GmTreeGraphics.getBlankIcon();
JButton but = new JButton(ico);
but.setToolTipText(Messages.format("Toolbar.ADD",Resource.kindNames.get(k))); //$NON-NLS-1$
but.addActionListener(new Listener.ResourceAdder(false,k));
tool.add(but);
}
tool.addSeparator();
tool.add(makeButton("Toolbar.CST")); //$NON-NLS-1$
tool.add(makeButton("Toolbar.GMI")); //$NON-NLS-1$
tool.add(makeButton("Toolbar.PKG")); //$NON-NLS-1$
tool.addSeparator();
tool.add(new JLabel(Messages.getString("Toolbar.CONFIGURATION"))); //$NON-NLS-1$
configsCombo = new JComboBox<GameSettings>();
configsCombo.setModel(new DefaultComboBoxModel<GameSettings>(LGM.currentFile.gameSettings));
configsCombo.setMaximumSize(configsCombo.getPreferredSize());
tool.add(configsCombo);
tool.add(makeButton("Toolbar.CONFIG_MANAGE")); //$NON-NLS-1$
tool.addSeparator();
tool.add(makeButton("Toolbar.GMS")); //$NON-NLS-1$
tool.addSeparator();
tool.add(makeButton("Toolbar.PREFERENCES")); //$NON-NLS-1$
tool.add(makeButton("Toolbar.DOCUMENTATION")); //$NON-NLS-1$
tool.add(Box.createHorizontalGlue()); //right align after this
tool.add(eventButton = makeButton(new JToggleButton(),"Toolbar.EVENT_BUTTON")); //$NON-NLS-1$
return tool;
}
private static JTree createTree()
{
return createTree(newRoot());
}
private static JTree createTree(ResNode newroot)
{
InvisibleTreeModel ml = new InvisibleTreeModel(newroot);
ml.activateFilter(false);
tree = new JTree(ml);
GmTreeGraphics renderer = new GmTreeGraphics();
GmTreeEditor editor = new GmTreeEditor(tree,renderer);
editor.addCellEditorListener(Listener.getInstance());
tree.setEditable(true);
tree.addMouseListener(Listener.getInstance().mListener);
if (javaVersion >= 10600)
{
tree.setTransferHandler(Listener.getInstance());
tree.setDragEnabled(true);
tree.setDropMode(DropMode.ON_OR_INSERT);
}
tree.setCellRenderer(renderer);
tree.setRootVisible(false);
tree.setShowsRootHandles(true);
tree.setCellEditor(editor);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
//remove the cut, copy, and paste bindings
InputMap im = tree.getInputMap();
for (KeyStroke s : im.allKeys())
{
Object o = im.get(s);
if (o.equals("cut") || o.equals("copy") || o.equals("paste")) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
im.put(s,"none"); //null doesn't remove them //$NON-NLS-1$
}
return tree;
}
public static ResNode newRoot()
{
return root = new ResNode("Root",(byte) 0,null,null); //$NON-NLS-1$
}
private static JComponent createMDI()
{
mdi = new MDIPane();
JScrollPane scroll = new JScrollPane(mdi);
mdi.setScrollPane(scroll);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
mdi.setBackground(Color.GRAY);
return scroll;
}
public static void loadPlugins()
{
if (workDir == null) return;
File dir = new File(workDir.getParent(),"plugins"); //$NON-NLS-1$
if (!dir.exists()) dir = new File(workDir.getParent(),"Plugins"); //$NON-NLS-1$
File[] ps = dir.listFiles(new CustomFileFilter(null,".jar")); //$NON-NLS-1$
if (ps == null) return;
for (File f : ps)
{
if (!f.exists()) continue;
try
{
String pluginEntry = "LGM-Plugin"; //$NON-NLS-1$
JarFile jar = new JarFile(f);
Manifest mf = jar.getManifest();
jar.close();
String clastr = mf.getMainAttributes().getValue(pluginEntry);
if (clastr == null)
throw new Exception(Messages.format("LGM.PLUGIN_MISSING_ENTRY",pluginEntry)); //$NON-NLS-1$
URLClassLoader ucl = new URLClassLoader(new URL[] { f.toURI().toURL() });
ucl.loadClass(clastr).getDeclaredConstructor().newInstance();
classLoaders.add(ucl);
}
catch (Exception e)
{
String msgInd = "LGM.PLUGIN_LOAD_ERROR"; //$NON-NLS-1$
LGM.showDefaultExceptionHandler(new Exception(Messages.format(msgInd,f.getName()), e));
continue;
}
}
}
public static void populateTree()
{
/* TODO: This method here does not give the top level nodes for Game Info, Extensions, and
* Settings a proper resource reference, they get null. My commented code here will give them
* their proper references, but when a reload happens the references are lost again.
* I seriously do not believe there should be nodes with null references in the tree.
for (Class<? extends Resource<?,?>> k : Resource.kinds)
{
String name = Resource.kindNamesPlural.get(k);
byte status = InstantiableResource.class.isAssignableFrom(k) ? ResNode.STATUS_PRIMARY
: ResNode.STATUS_SECONDARY;
if (status == ResNode.STATUS_SECONDARY) {
SingletonResourceHolder<?> rh = (SingletonResourceHolder<?>) LGM.currentFile.resMap.get(k);
if (rh != null) {
Resource<?,?> res = rh.getResource();
root.add(new ResNode(name,status,k,res.reference));
} else {
root.addChild(name,status,k);
}
} else {
root.addChild(name,status,k);
}
}
tree.setSelectionPath(new TreePath(root).pathByAddingChild(root.getChildAt(0)));
}
*/
for (Class<? extends Resource<?,?>> k : Resource.kinds)
{
boolean hasNode = true;
// Check to see if the appropriate node already exists.
Enumeration<?> children = root.depthFirstEnumeration();
while (children.hasMoreElements()) {
ResNode it = (ResNode) children.nextElement();
if (it.kind == k) {
hasNode = false;
break;
}
}
if (!hasNode) continue;
try
{
//NOTE: Use reflection on the class to see if it has a variable telling us whether to create
//a node in the tree for the resource type.
hasNode = k.getField("hasNode").getBoolean(hasNode); //$NON-NLS-1$
}
catch (IllegalArgumentException e)
{
LGM.showDefaultExceptionHandler(e);
}
catch (NoSuchFieldException e)
{
LGM.showDefaultExceptionHandler(e);
}
catch (SecurityException e)
{
LGM.showDefaultExceptionHandler(e);
}
catch (IllegalAccessException e)
{
LGM.showDefaultExceptionHandler(e);
}
if (!hasNode) continue;
String name = Resource.kindNamesPlural.get(k);
byte status = InstantiableResource.class.isAssignableFrom(k) ? ResNode.STATUS_PRIMARY
: ResNode.STATUS_SECONDARY;
root.addChild(name,status,k);
}
tree.setSelectionPath(new TreePath(root).pathByAddingChild(root.getChildAt(0)));
}
/**
* Commits all front-end changes to the back-end.
* Usually do this in preparation for writing the back-end to some output stream,
* such as a file (e.g. saving) or a plugin (e.g. compiling).
* Notice that LGM actually traverses the tree committing *all* ResNodes with frames,
* rather than just the open MDI frames. Since GameSettings and GameInfo do not have
* ResourceFrames associated with them, we commit them separately.
* @see LGM#commitAll()
*/
public static void commitAll()
{
Enumeration<?> nodes = LGM.root.preorderEnumeration();
while (nodes.hasMoreElements())
{
ResNode node = (ResNode) nodes.nextElement();
if (node.frame != null) node.frame.commitChanges(); // update open frames
}
LGM.getExtensionPackages().commitChanges();
LGM.getConstantsFrame().commitChanges();
LGM.getGameInfo().commitChanges();
LGM.getGameSettings().commitChanges();
}
public static void reload(boolean newRoot)
{
LGM.mdi.closeAll();
InvisibleTreeModel ml = new InvisibleTreeModel(LGM.root);
LGM.tree.setModel(ml);
ml.activateFilter(Search.pruneResultsCB.isSelected());
if (ml.isActivatedFilter())
Search.applyFilter(root.getChildren(),ml.isActivatedFilter(),Search.filterText.getText(),false,Search.wholeWordCB.isSelected(),true);
LGM.tree.setSelectionRow(0);
// Reload the search tree so that orphaned references can be dumped.
DefaultMutableTreeNode searchRoot = (DefaultMutableTreeNode) searchTree.getModel().getRoot();
searchRoot.removeAllChildren();
// Reload because root is invisible.
((DefaultTreeModel)searchTree.getModel()).reload();
LGM.eventSelect.reload();
ConfigurationManager.getInstance().setConfigList(LGM.currentFile.gameSettings);
configsCombo.setModel(new DefaultComboBoxModel<GameSettings>(LGM.currentFile.gameSettings));
// NOTE: We do this to update the reference to the one now loaded
// since we never close these frames, then we simply revert their controls.
constantsFrame.res = LGM.currentFile.defaultConstants;
constantsFrame.resOriginal = LGM.currentFile.defaultConstants.clone();
constantsFrame.revertResource();
constantsFrame.setVisible(false);
gameInfo.res = LGM.currentFile.gameInfo;
gameInfo.resOriginal = LGM.currentFile.gameInfo.clone();
gameInfo.revertResource();
gameInfo.setVisible(false);
gameSet.res = LGM.currentFile.gameSettings.firstElement();
gameSet.resOriginal = LGM.currentFile.gameSettings.firstElement().clone();
gameSet.revertResource();
gameSet.setVisible(false);
LGM.fireReloadPerformed(newRoot);
}
public static interface ReloadListener
{
/**
* Called after LGM performs a reload, e.g. when a new file is created or loaded.
* A reload causes the MDI to be flushed, the tree to refresh
* (especially if a new root is provided), the EventPanel is recreated
* (a hack to ensure that the events' link selectors know of the new root),
* and Game Settings and Game Info are re-calibrated with their new settings
* (but not recreated). Note that the Menu bar is left untouched and remains in tact.
* @param newRoot indicates if a new root was provided to the tree
* (e.g. the tree had to be re-populated)
* @see LGM#reload(boolean newRoot)
*/
void reloadPerformed(boolean newRoot);
}
protected static ArrayList<ReloadListener> reloadListeners = new ArrayList<ReloadListener>();
static JTabbedPane treeTabs;
public static void addReloadListener(ReloadListener l)
{
reloadListeners.add(l);
}
public static void removeReloadListener(ReloadListener l)
{
reloadListeners.remove(l);
}
protected static void fireReloadPerformed(boolean newRoot)
{
for (ReloadListener rl : reloadListeners)
rl.reloadPerformed(newRoot);
LGM.LOADING_PROJECT = false;
}
public static void addPluginResource(PluginResource pr)
{
ImageIcon i = pr.getIcon();
if (i != null) ResNode.ICON.put(pr.getKind(),i);
String p = pr.getPrefix();
if (p != null) Prefs.prefixes.put(pr.getKind(),p);
Resource.addKind(pr.getKind(),pr.getName3(),pr.getName(),pr.getPlural());
LGM.currentFile.resMap.put(pr.getKind(),pr.getResourceHolder());
ResourceFrame.factories.put(pr.getKind(),pr.getResourceFrameFactory());
}
public static interface PluginResource
{
Class<? extends Resource<?,?>> getKind();
/** Can be null, in which case the default icon is used. */
ImageIcon getIcon();
String getName3();
String getName();
String getPlural();
String getPrefix();
ResourceHolder<?> getResourceHolder();
ResourceFrameFactory getResourceFrameFactory();
}
public static abstract class SingletonPluginResource<T extends Resource<T,?>> implements
PluginResource
{
public String getPlural()
{
return getName();
}
public String getPrefix()
{
return null;
}
public ResourceHolder<?> getResourceHolder()
{
return new SingletonResourceHolder<T>(getInstance());
}
public abstract T getInstance();
}
public static void applyPreferences() {
if (javaVersion >= 10700 && Prefs.locale != null) {
Locale.setDefault(Prefs.locale);
}
//TODO: Should probably make these preferences as well, but I don't have a Mac to test - Robert
//Put the Mac menu bar where it belongs (ignored by other systems)
System.setProperty("apple.laf.useScreenMenuBar","true"); //$NON-NLS-1$ //$NON-NLS-2$
//Set the Mac menu bar title to the correct name (also adds a useless About entry, so disabled)