forked from imagej/ImageJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menus.java
1744 lines (1632 loc) · 60.1 KB
/
Menus.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
package ij;
import ij.process.*;
import ij.util.*;
import ij.gui.ImageWindow;
import ij.plugin.MacroInstaller;
import ij.gui.Toolbar;
import ij.macro.Interpreter;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.applet.Applet;
import java.awt.event.*;
import java.util.zip.*;
/**
This class installs and updates ImageJ's menus. Note that menu labels,
even in submenus, must be unique. This is because ImageJ uses a single
hash table for all menu labels. If you look closely, you will see that
File->Import->Text Image... and File->Save As->Text Image... do not use
the same label. One of the labels has an extra space.
@see ImageJ
*/
public class Menus {
public static final char PLUGINS_MENU = 'p';
public static final char IMPORT_MENU = 'i';
public static final char SAVE_AS_MENU = 's';
public static final char SHORTCUTS_MENU = 'h'; // 'h'=hotkey
public static final char ABOUT_MENU = 'a';
public static final char FILTERS_MENU = 'f';
public static final char TOOLS_MENU = 't';
public static final char UTILITIES_MENU = 'u';
public static final int WINDOW_MENU_ITEMS = 6; // fixed items at top of Window menu
public static final int NORMAL_RETURN = 0;
public static final int COMMAND_IN_USE = -1;
public static final int INVALID_SHORTCUT = -2;
public static final int SHORTCUT_IN_USE = -3;
public static final int NOT_INSTALLED = -4;
public static final int COMMAND_NOT_FOUND = -5;
public static final int MAX_OPEN_RECENT_ITEMS = 15;
private static Menus instance;
private static MenuBar mbar;
private static CheckboxMenuItem gray8Item,gray16Item,gray32Item,
color256Item,colorRGBItem,RGBStackItem,HSBStackItem,LabStackItem,HSB32Item;
private static PopupMenu popup;
private static ImageJ ij;
private static Applet applet;
private Hashtable demoImagesTable = new Hashtable();
private static String ImageJPath, pluginsPath, macrosPath;
private static Properties menus;
private static Properties menuSeparators;
private static Menu pluginsMenu, saveAsMenu, shortcutsMenu, utilitiesMenu, macrosMenu;
static Menu window, openRecentMenu;
private static Hashtable pluginsTable;
private static int nPlugins, nMacros;
private static Hashtable shortcuts;
private static Hashtable macroShortcuts;
private static Vector pluginsPrefs; // commands saved in IJ_Prefs
static int windowMenuItems2; // non-image windows listed in Window menu + separator
private String error;
private String jarError;
private String pluginError;
private boolean isJarErrorHeading;
private static boolean installingJars, duplicateCommand;
private static Vector jarFiles; // JAR files in plugins folder with "_" in their name
private Map menuEntry2jarFile = new HashMap();
private static Vector macroFiles; // Macros and scripts in the plugins folder
private static int userPluginsIndex; // First user plugin or submenu in Plugins menu
private static boolean addSorted;
private static int defaultFontSize = IJ.isWindows()?15:0;
private static int fontSize;
private static double scale = 1.0;
private static Font cachedFont;
static boolean jnlp; // true when using Java WebStart
public static int setMenuBarCount;
Menus(ImageJ ijInstance, Applet appletInstance) {
ij = ijInstance;
String title = ij!=null?ij.getTitle():null;
applet = appletInstance;
instance = this;
fontSize = Prefs.getInt(Prefs.MENU_SIZE, defaultFontSize);
}
String addMenuBar() {
scale = Prefs.getGuiScale();
//if ((scale>=1.5&&scale<2.0) || (scale>=2.5&&scale<3.0))
// scale = (int)Math.round(scale);
nPlugins = nMacros = userPluginsIndex = 0;
addSorted = installingJars = duplicateCommand = false;
error = null;
mbar = null;
menus = new Properties();
pluginsTable = new Hashtable();
shortcuts = new Hashtable();
pluginsPrefs = new Vector();
macroShortcuts = null;
setupPluginsAndMacrosPaths();
Menu file = getMenu("File");
Menu newMenu = getMenu("File>New", true);
addPlugInItem(file, "Open...", "ij.plugin.Commands(\"open\")", KeyEvent.VK_O, false);
addPlugInItem(file, "Open Next", "ij.plugin.NextImageOpener", KeyEvent.VK_O, true);
Menu openSamples = getMenu("File>Open Samples", true);
openSamples.addSeparator();
addPlugInItem(openSamples, "Cache Sample Images ", "ij.plugin.URLOpener(\"cache\")", 0, false);
addOpenRecentSubMenu(file);
Menu importMenu = getMenu("File>Import", true);
Menu showFolderMenu = new Menu("Show Folder");
fixFontSize(showFolderMenu);
file.add(showFolderMenu);
addPlugInItem(showFolderMenu, "Image", "ij.plugin.SimpleCommands(\"showdirImage\")", 0, false);
addPlugInItem(showFolderMenu, "Plugins", "ij.plugin.SimpleCommands(\"showdirPlugins\")", 0, false);
addPlugInItem(showFolderMenu, "Macros", "ij.plugin.SimpleCommands(\"showdirMacros\")", 0, false);
addPlugInItem(showFolderMenu, "LUTs", "ij.plugin.SimpleCommands(\"showdirLuts\")", 0, false);
addPlugInItem(showFolderMenu, "ImageJ", "ij.plugin.SimpleCommands(\"showdirImageJ\")", 0, false);
addPlugInItem(showFolderMenu, "temp", "ij.plugin.SimpleCommands(\"showdirTemp\")", 0, false);
addPlugInItem(showFolderMenu, "Home", "ij.plugin.SimpleCommands(\"showdirHome\")", 0, false);
file.addSeparator();
addPlugInItem(file, "Close", "ij.plugin.Commands(\"close\")", KeyEvent.VK_W, false);
addPlugInItem(file, "Close All", "ij.plugin.Commands(\"close-all\")", KeyEvent.VK_W, true);
addPlugInItem(file, "Save", "ij.plugin.Commands(\"save\")", KeyEvent.VK_S, false);
saveAsMenu = getMenu("File>Save As", true);
addPlugInItem(file, "Revert", "ij.plugin.Commands(\"revert\")", KeyEvent.VK_R, true);
file.addSeparator();
addPlugInItem(file, "Page Setup...", "ij.plugin.filter.Printer(\"setup\")", 0, false);
addPlugInItem(file, "Print...", "ij.plugin.filter.Printer(\"print\")", KeyEvent.VK_P, false);
Menu edit = getMenu("Edit");
addPlugInItem(edit, "Undo", "ij.plugin.Commands(\"undo\")", KeyEvent.VK_Z, false);
edit.addSeparator();
addPlugInItem(edit, "Cut", "ij.plugin.Clipboard(\"cut\")", KeyEvent.VK_X, false);
addPlugInItem(edit, "Copy", "ij.plugin.Clipboard(\"copy\")", KeyEvent.VK_C, false);
addPlugInItem(edit, "Copy to System", "ij.plugin.Clipboard(\"scopy\")", 0, false);
addPlugInItem(edit, "Paste", "ij.plugin.Clipboard(\"paste\")", KeyEvent.VK_V, false);
addPlugInItem(edit, "Paste Control...", "ij.plugin.frame.PasteController", 0, false);
edit.addSeparator();
addPlugInItem(edit, "Clear", "ij.plugin.filter.Filler(\"clear\")", 0, false);
addPlugInItem(edit, "Clear Outside", "ij.plugin.filter.Filler(\"outside\")", 0, false);
addPlugInItem(edit, "Fill", "ij.plugin.filter.Filler(\"fill\")", KeyEvent.VK_F, false);
addPlugInItem(edit, "Draw", "ij.plugin.filter.Filler(\"draw\")", KeyEvent.VK_D, false);
addPlugInItem(edit, "Invert", "ij.plugin.filter.Filters(\"invert\")", KeyEvent.VK_I, true);
edit.addSeparator();
getMenu("Edit>Selection", true);
Menu optionsMenu = getMenu("Edit>Options", true);
Menu image = getMenu("Image");
Menu imageType = getMenu("Image>Type");
gray8Item = addCheckboxItem(imageType, "8-bit", "ij.plugin.Converter(\"8-bit\")");
gray16Item = addCheckboxItem(imageType, "16-bit", "ij.plugin.Converter(\"16-bit\")");
gray32Item = addCheckboxItem(imageType, "32-bit", "ij.plugin.Converter(\"32-bit\")");
color256Item = addCheckboxItem(imageType, "8-bit Color", "ij.plugin.Converter(\"8-bit Color\")");
colorRGBItem = addCheckboxItem(imageType, "RGB Color", "ij.plugin.Converter(\"RGB Color\")");
imageType.add(new MenuItem("-"));
RGBStackItem = addCheckboxItem(imageType, "RGB Stack", "ij.plugin.Converter(\"RGB Stack\")");
HSBStackItem = addCheckboxItem(imageType, "HSB Stack", "ij.plugin.Converter(\"HSB Stack\")");
HSB32Item = addCheckboxItem(imageType, "HSB (32-bit)", "ij.plugin.Converter(\"HSB (32-bit)\")");
LabStackItem = addCheckboxItem(imageType, "Lab Stack", "ij.plugin.Converter(\"Lab Stack\")");
image.add(imageType);
image.addSeparator();
getMenu("Image>Adjust", true);
addPlugInItem(image, "Show Info...", "ij.plugin.ImageInfo", KeyEvent.VK_I, false);
addPlugInItem(image, "Properties...", "ij.plugin.filter.ImageProperties", KeyEvent.VK_P, true);
getMenu("Image>Color", true);
getMenu("Image>Stacks", true);
getMenu("Image>Stacks>Animation_", true);
getMenu("Image>Stacks>Tools_", true);
Menu hyperstacksMenu = getMenu("Image>Hyperstacks", true);
image.addSeparator();
addPlugInItem(image, "Crop", "ij.plugin.Resizer(\"crop\")", KeyEvent.VK_X, true);
addPlugInItem(image, "Duplicate...", "ij.plugin.Duplicator", KeyEvent.VK_D, true);
addPlugInItem(image, "Rename...", "ij.plugin.SimpleCommands(\"rename\")", 0, false);
addPlugInItem(image, "Scale...", "ij.plugin.Scaler", KeyEvent.VK_E, false);
getMenu("Image>Transform", true);
getMenu("Image>Zoom", true);
getMenu("Image>Overlay", true);
image.addSeparator();
getMenu("Image>Lookup Tables", true);
Menu process = getMenu("Process");
addPlugInItem(process, "Smooth", "ij.plugin.filter.Filters(\"smooth\")", KeyEvent.VK_S, true);
addPlugInItem(process, "Sharpen", "ij.plugin.filter.Filters(\"sharpen\")", 0, false);
addPlugInItem(process, "Find Edges", "ij.plugin.filter.Filters(\"edge\")", 0, false);
addPlugInItem(process, "Find Maxima...", "ij.plugin.filter.MaximumFinder", 0, false);
addPlugInItem(process, "Enhance Contrast...", "ij.plugin.ContrastEnhancer", 0, false);
getMenu("Process>Noise", true);
getMenu("Process>Shadows", true);
getMenu("Process>Binary", true);
getMenu("Process>Math", true);
getMenu("Process>FFT", true);
Menu filtersMenu = getMenu("Process>Filters", true);
process.addSeparator();
getMenu("Process>Batch", true);
addPlugInItem(process, "Image Calculator...", "ij.plugin.ImageCalculator", 0, false);
addPlugInItem(process, "Subtract Background...", "ij.plugin.filter.BackgroundSubtracter", 0, false);
addItem(process, "Repeat Command", KeyEvent.VK_R, false);
Menu analyzeMenu = getMenu("Analyze");
addPlugInItem(analyzeMenu, "Measure", "ij.plugin.filter.Analyzer", KeyEvent.VK_M, false);
addPlugInItem(analyzeMenu, "Analyze Particles...", "ij.plugin.filter.ParticleAnalyzer", 0, false);
addPlugInItem(analyzeMenu, "Summarize", "ij.plugin.filter.Analyzer(\"sum\")", 0, false);
addPlugInItem(analyzeMenu, "Distribution...", "ij.plugin.Distribution", 0, false);
addPlugInItem(analyzeMenu, "Label", "ij.plugin.filter.Filler(\"label\")", 0, false);
addPlugInItem(analyzeMenu, "Clear Results", "ij.plugin.filter.Analyzer(\"clear\")", 0, false);
addPlugInItem(analyzeMenu, "Set Measurements...", "ij.plugin.filter.Analyzer(\"set\")", 0, false);
analyzeMenu.addSeparator();
addPlugInItem(analyzeMenu, "Set Scale...", "ij.plugin.filter.ScaleDialog", 0, false);
addPlugInItem(analyzeMenu, "Calibrate...", "ij.plugin.filter.Calibrator", 0, false);
if (IJ.isMacOSX()) {
addPlugInItem(analyzeMenu, "Histogram", "ij.plugin.Histogram", 0, false);
shortcuts.put(Integer.valueOf(KeyEvent.VK_H),"Histogram");
} else
addPlugInItem(analyzeMenu, "Histogram", "ij.plugin.Histogram", KeyEvent.VK_H, false);
addPlugInItem(analyzeMenu, "Plot Profile", "ij.plugin.Profiler(\"plot\")", KeyEvent.VK_K, false);
addPlugInItem(analyzeMenu, "Surface Plot...", "ij.plugin.SurfacePlotter", 0, false);
getMenu("Analyze>Gels", true);
Menu toolsMenu = getMenu("Analyze>Tools", true);
// the plugins will be added later, after a separator
addPluginsMenu();
Menu window = getMenu("Window");
addPlugInItem(window, "Show All", "ij.plugin.WindowOrganizer(\"show\")", KeyEvent.VK_CLOSE_BRACKET, false);
String key = IJ.isWindows()?"enter":"return";
addPlugInItem(window, "Main Window ["+key+"]", "ij.plugin.WindowOrganizer(\"imagej\")", 0, false);
addPlugInItem(window, "Put Behind [tab]", "ij.plugin.Commands(\"tab\")", 0, false);
addPlugInItem(window, "Cascade", "ij.plugin.WindowOrganizer(\"cascade\")", 0, false);
addPlugInItem(window, "Tile", "ij.plugin.WindowOrganizer(\"tile\")", 0, false);
window.addSeparator();
Menu help = getMenu("Help");
addPlugInItem(help, "ImageJ Website...", "ij.plugin.BrowserLauncher", 0, false);
help.addSeparator();
addPlugInItem(help, "Dev. Resources...", "ij.plugin.BrowserLauncher(\""+IJ.URL2+"/developer/index.html\")", 0, false);
addPlugInItem(help, "Macro Functions...", "ij.plugin.BrowserLauncher(\"https://wsr.imagej.net/developer/macro/functions.html\")", 0, false);
Menu examplesMenu = getExamplesMenu(ij);
addPlugInItem(examplesMenu, "Open as Panel", "ij.plugin.SimpleCommands(\"opencp\")", 0, false);
help.add(examplesMenu);
help.addSeparator();
addPlugInItem(help, "Update ImageJ...", "ij.plugin.ImageJ_Updater", 0, false);
addPlugInItem(help, "Release Notes...", "ij.plugin.BrowserLauncher(\"https://wsr.imagej.net/notes.html\")", 0, false);
addPlugInItem(help, "Refresh Menus", "ij.plugin.ImageJ_Updater(\"menus\")", 0, false);
help.addSeparator();
Menu aboutMenu = getMenu("Help>About Plugins", true);
addPlugInItem(help, "About ImageJ...", "ij.plugin.AboutBox", 0, false);
if (applet==null) {
menuSeparators = new Properties();
installPlugins();
}
// make sure "Quit" is the last item in the File menu
file.addSeparator();
addPlugInItem(file, "Quit", "ij.plugin.Commands(\"quit\")", 0, false);
//System.out.println("MenuBar.setFont: "+fontSize+" "+scale+" "+getFont());
if (fontSize!=0 || scale>1.0)
mbar.setFont(getFont());
if (ij!=null) {
ij.setMenuBar(mbar);
Menus.setMenuBarCount++;
}
// Add deleted sample images to commands table
pluginsTable.put("Lena (68K)", "ij.plugin.URLOpener(\"lena-std.tif\")");
pluginsTable.put("Bridge (174K)", "ij.plugin.URLOpener(\"bridge.gif\")");
if (pluginError!=null)
error = error!=null?error+="\n"+pluginError:pluginError;
if (jarError!=null)
error = error!=null?error+="\n"+jarError:jarError;
return error;
}
public static Menu getExamplesMenu(ActionListener listener) {
Menu menu = new Menu("Examples");
Menu submenu = new Menu("Plots");
addExample(submenu, "Example Plot", "Example_Plot_.ijm");
addExample(submenu, "Semi-log Plot", "Semi-log_Plot_.ijm");
addExample(submenu, "Arrow Plot", "Arrow_Plot_.ijm");
addExample(submenu, "Damped Wave Plot", "Damped_Wave_Plot_.ijm");
addExample(submenu, "Dynamic Plot", "Dynamic_Plot_.ijm");
addExample(submenu, "Dynamic Plot 2D", "Dynamic_Plot_2D_.ijm");
addExample(submenu, "Custom Plot Symbols", "Custom_Plot_Symbols_.ijm");
addExample(submenu, "Histograms", "Histograms_.ijm");
addExample(submenu, "Bar Charts", "Bar_Charts_.ijm");
addExample(submenu, "Shapes", "Plot_Shapes_.ijm");
addExample(submenu, "Plot Styles", "Plot_Styles_.ijm");
addExample(submenu, "Random Data", "Random_Data_.ijm");
addExample(submenu, "Plot Results", "Plot_Results_.ijm");
addExample(submenu, "Plot With Spectrum", "Plot_With_Spectrum_.ijm");
submenu.addActionListener(listener);
menu.add(submenu);
submenu = new Menu("Tools");
addExample(submenu, "Annular Selection", "Annular_Selection_Tool.ijm");
addExample(submenu, "Big Cursor", "Big_Cursor_Tool.ijm");
addExample(submenu, "Circle Tool", "Circle_Tool.ijm");
addExample(submenu, "Point Picker", "Point_Picker_Tool.ijm");
addExample(submenu, "Star Tool", "Star_Tool.ijm");
addExample(submenu, "Animated Icon Tool", "Animated_Icon_Tool.ijm");
submenu.addActionListener(listener);
menu.add(submenu);
submenu = new Menu("Macro");
addExample(submenu, "Sphere", "Sphere.ijm");
addExample(submenu, "Dialog Box", "Dialog_Box.ijm");
addExample(submenu, "Process Folder", "Batch_Process_Folder.ijm");
addExample(submenu, "OpenDialog Demo", "OpenDialog_Demo.ijm");
addExample(submenu, "Save All Images", "Save_All_Images.ijm");
addExample(submenu, "Sine/Cosine Table", "Sine_Cosine_Table.ijm");
addExample(submenu, "Non-numeric Table", "Non-numeric_Table.ijm");
addExample(submenu, "Overlay", "Overlay.ijm");
addExample(submenu, "Stack Overlay", "Stack_Overlay.ijm");
addExample(submenu, "Array Functions", "Array_Functions.ijm");
addExample(submenu, "Dual Progress Bars", "Dual_Progress_Bars.ijm");
addExample(submenu, "Grab Viridis Colormap", "Grab_Viridis_Colormap.ijm");
addExample(submenu, "Custom Measurement", "Custom_Measurement.ijm");
addExample(submenu, "Synthetic Images", "Synthetic_Images.ijm");
addExample(submenu, "Spiral Rotation", "Spiral_Rotation.ijm");
addExample(submenu, "Curve Fitting", "Curve_Fitting.ijm");
addExample(submenu, "Colors of 2021", "Colors_of_2021.ijm");
addExample(submenu, "Turtle Graphics", "Turtle_Graphics.ijm");
addExample(submenu, "Easter Eggs", "Easter_Eggs.ijm");
submenu.addActionListener(listener);
menu.add(submenu);
submenu = new Menu("JavaScript");
addExample(submenu, "Sphere", "Sphere.js");
addExample(submenu, "Plasma Cloud", "Plasma_Cloud.js");
addExample(submenu, "Cloud Debugger", "Cloud_Debugger.js");
addExample(submenu, "Synthetic Images", "Synthetic_Images.js");
addExample(submenu, "Points", "Points.js");
addExample(submenu, "Spiral Rotation", "Spiral_Rotation.js");
addExample(submenu, "Example Plot", "Example_Plot.js");
addExample(submenu, "Semi-log Plot", "Semi-log_Plot.js");
addExample(submenu, "Arrow Plot", "Arrow_Plot.js");
addExample(submenu, "Dynamic Plot", "Dynamic_Plot.js");
addExample(submenu, "Plot Styles", "Plot_Styles.js");
addExample(submenu, "Plot Random Data", "Plot_Random_Data.js");
addExample(submenu, "Histogram Plots", "Histogram_Plots.js");
addExample(submenu, "JPEG Quality Plot", "JPEG_Quality_Plot.js");
addExample(submenu, "Process Folder", "Batch_Process_Folder.js");
addExample(submenu, "Sine/Cosine Table", "Sine_Cosine_Table.js");
addExample(submenu, "Non-numeric Table", "Non-numeric_Table.js");
addExample(submenu, "Overlay", "Overlay.js");
addExample(submenu, "Stack Overlay", "Stack_Overlay.js");
addExample(submenu, "Dual Progress Bars", "Dual_Progress_Bars.js");
addExample(submenu, "Gamma Adjuster", "Gamma_Adjuster.js");
addExample(submenu, "Custom Measurement", "Custom_Measurement.js");
addExample(submenu, "Terabyte VirtualStack", "Terabyte_VirtualStack.js");
addExample(submenu, "Event Listener", "Event_Listener.js");
addExample(submenu, "FFT Filter", "FFT_Filter.js");
addExample(submenu, "Curve Fitting", "Curve_Fitting.js");
addExample(submenu, "Overlay Text", "Overlay_Text.js");
addExample(submenu, "Crop Multiple Rois", "Crop_Multiple_Rois.js");
addExample(submenu, "Show all LUTs", "Show_all_LUTs.js");
addExample(submenu, "Dialog Demo", "Dialog_Demo.js");
submenu.addActionListener(listener);
menu.add(submenu);
submenu = new Menu("BeanShell");
addExample(submenu, "Sphere", "Sphere.bsh");
addExample(submenu, "Example Plot", "Example_Plot.bsh");
addExample(submenu, "Semi-log Plot", "Semi-log_Plot.bsh");
addExample(submenu, "Arrow Plot", "Arrow_Plot.bsh");
addExample(submenu, "Sine/Cosine Table", "Sine_Cosine_Table.bsh");
submenu.addActionListener(listener);
menu.add(submenu);
submenu = new Menu("Python");
addExample(submenu, "Sphere", "Sphere.py");
addExample(submenu, "Animated Gaussian Blur", "Animated_Gaussian_Blur.py");
addExample(submenu, "Spiral Rotation", "Spiral_Rotation.py");
addExample(submenu, "Overlay", "Overlay.py");
submenu.addActionListener(listener);
menu.add(submenu);
submenu = new Menu("Java");
addExample(submenu, "Sphere", "Sphere_.java");
addExample(submenu, "Plasma Cloud", "Plasma_Cloud.java");
addExample(submenu, "Gamma Adjuster", "Gamma_Adjuster.java");
addExample(submenu, "Plugin", "My_Plugin.java");
addExample(submenu, "Plugin Filter", "Filter_Plugin.java");
addExample(submenu, "Plugin Frame", "Plugin_Frame.java");
addExample(submenu, "Plugin Tool", "Prototype_Tool.java");
submenu.addActionListener(listener);
menu.add(submenu);
menu.addSeparator();
CheckboxMenuItem item = new CheckboxMenuItem("Autorun Examples");
menu.add(item);
item.addItemListener(ij);
item.setState(Prefs.autoRunExamples);
fixFontSize(menu);
return menu;
}
private static void addExample(Menu menu, String label, String command) {
MenuItem item = new MenuItem(label);
menu.add(item);
item.setActionCommand(command);
fixFontSize(item);
}
void addOpenRecentSubMenu(Menu menu) {
openRecentMenu = getMenu("File>Open Recent");
for (int i=0; i<MAX_OPEN_RECENT_ITEMS; i++) {
String path = Prefs.getString("recent" + (i/10)%10 + i%10);
if (path==null) break;
MenuItem item = new MenuItem(path);
openRecentMenu.add(item);
item.addActionListener(ij);
}
menu.add(openRecentMenu);
}
static void addItem(Menu menu, String label, int shortcut, boolean shift) {
if (menu==null)
return;
MenuItem item;
if (shortcut==0)
item = new MenuItem(label);
else {
if (shift) {
item = new MenuItem(label, new MenuShortcut(shortcut, true));
shortcuts.put(Integer.valueOf(shortcut+200),label);
} else {
item = new MenuItem(label, new MenuShortcut(shortcut));
shortcuts.put(Integer.valueOf(shortcut),label);
}
}
if (addSorted) {
if (menu==pluginsMenu)
addItemSorted(menu, item, userPluginsIndex);
else
addOrdered(menu, item);
} else
menu.add(item);
item.addActionListener(ij);
fixFontSize(item);
}
void addPlugInItem(Menu menu, String label, String className, int shortcut, boolean shift) {
pluginsTable.put(label, className);
nPlugins++;
addItem(menu, label, shortcut, shift);
}
CheckboxMenuItem addCheckboxItem(Menu menu, String label, String className) {
pluginsTable.put(label, className);
nPlugins++;
CheckboxMenuItem item = new CheckboxMenuItem(label);
menu.add(item);
item.addItemListener(ij);
item.setState(false);
return item;
}
static Menu addSubMenu(Menu menu, String name) {
String value;
String key = name.toLowerCase(Locale.US);
int index;
Menu submenu=new Menu(name.replace('_', ' '));
index = key.indexOf(' ');
if (index>0)
key = key.substring(0, index);
for (int count=1; count<100; count++) {
value = Prefs.getString(key + (count/10)%10 + count%10);
if (value==null)
break;
if (count==1)
menu.add(submenu);
if (value.equals("-"))
submenu.addSeparator();
else
addPluginItem(submenu, value);
}
if (name.equals("Lookup Tables") && applet==null)
addLuts(submenu);
fixFontSize(submenu);
return submenu;
}
static void addLuts(Menu submenu) {
String path = IJ.getDirectory("luts");
if (path==null) return;
File f = new File(path);
String[] list = null;
if (applet==null && f.exists() && f.isDirectory())
list = f.list();
if (list==null) return;
if (IJ.isLinux() || IJ.isMacOSX())
Arrays.sort(list);
submenu.addSeparator();
for (int i=0; i<list.length; i++) {
String name = list[i];
if (name.endsWith(".lut")) {
name = name.substring(0,name.length()-4);
if (name.contains("_") && !name.contains(" "))
name = name.replace("_", " ");
MenuItem item = new MenuItem(name);
submenu.add(item);
item.addActionListener(ij);
nPlugins++;
}
}
}
static void addPluginItem(Menu submenu, String s) {
if (s.startsWith("\"-\"")) {
// add menu separator if command="-"
addSeparator(submenu);
return;
}
int lastComma = s.lastIndexOf(',');
if (lastComma<=0)
return;
String command = s.substring(1,lastComma-1);
int keyCode = 0;
boolean shift = false;
if (command.endsWith("]")) {
int openBracket = command.lastIndexOf('[');
if (openBracket>0) {
String shortcut = command.substring(openBracket+1,command.length()-1);
keyCode = convertShortcutToCode(shortcut);
boolean functionKey = keyCode>=KeyEvent.VK_F1 && keyCode<=KeyEvent.VK_F12;
if (keyCode>0 && !functionKey)
command = command.substring(0,openBracket);
}
}
if (keyCode>=KeyEvent.VK_F1 && keyCode<=KeyEvent.VK_F12) {
shortcuts.put(Integer.valueOf(keyCode),command);
keyCode = 0;
} else if (keyCode>=265 && keyCode<=290) {
keyCode -= 200;
shift = true;
}
addItem(submenu,command,keyCode,shift);
while(s.charAt(lastComma+1)==' ' && lastComma+2<s.length())
lastComma++; // remove leading spaces
String className = s.substring(lastComma+1,s.length());
//IJ.log(command+" "+className);
if (installingJars)
duplicateCommand = pluginsTable.get(command)!=null;
pluginsTable.put(command, className);
nPlugins++;
}
void checkForDuplicate(String command) {
if (pluginsTable.get(command)!=null) {
}
}
void addPluginsMenu() {
String value,label,className;
int index;
//pluginsMenu = new Menu("Plugins");
pluginsMenu = getMenu("Plugins");
for (int count=1; count<100; count++) {
value = Prefs.getString("plug-in" + (count/10)%10 + count%10);
if (value==null)
break;
char firstChar = value.charAt(0);
if (firstChar=='-')
pluginsMenu.addSeparator();
else if (firstChar=='>') {
String submenu = value.substring(2,value.length()-1);
//Menu menu = getMenu("Plugins>" + submenu, true);
Menu menu = addSubMenu(pluginsMenu, submenu);
if (submenu.equals("Shortcuts"))
shortcutsMenu = menu;
else if (submenu.equals("Utilities"))
utilitiesMenu = menu;
else if (submenu.equals("Macros"))
macrosMenu = menu;
} else
addPluginItem(pluginsMenu, value);
}
userPluginsIndex = pluginsMenu.getItemCount();
if (userPluginsIndex<0) userPluginsIndex = 0;
}
/** Install plugins using "pluginxx=" keys in IJ_Prefs.txt.
Plugins not listed in IJ_Prefs are added to the end
of the Plugins menu. */
void installPlugins() {
int nPlugins0 = nPlugins;
String value, className;
char menuCode;
Menu menu;
String[] pluginList = getPlugins();
String[] pluginsList2 = null;
Hashtable skipList = new Hashtable();
for (int index=0; index<100; index++) {
value = Prefs.getString("plugin" + (index/10)%10 + index%10);
if (value==null)
break;
menuCode = value.charAt(0);
switch (menuCode) {
case PLUGINS_MENU: default: menu = pluginsMenu; break;
case IMPORT_MENU: menu = getMenu("File>Import"); break;
case SAVE_AS_MENU: menu = getMenu("File>Save As"); break;
case SHORTCUTS_MENU: menu = shortcutsMenu; break;
case ABOUT_MENU: menu = getMenu("Help>About Plugins"); break;
case FILTERS_MENU: menu = getMenu("Process>Filters"); break;
case TOOLS_MENU: menu = getMenu("Analyze>Tools"); break;
case UTILITIES_MENU: menu = utilitiesMenu; break;
}
String prefsValue = value;
value = value.substring(2,value.length()); //remove menu code and coma
className = value.substring(value.lastIndexOf(',')+1,value.length());
boolean found = className.startsWith("ij.");
if (!found && pluginList!=null) { // does this plugin exist?
if (pluginsList2==null)
pluginsList2 = getStrippedPlugins(pluginList);
for (int i=0; i<pluginsList2.length; i++) {
if (className.startsWith(pluginsList2[i])) {
found = true;
break;
}
}
}
if (found && menu!=pluginsMenu) {
addPluginItem(menu, value);
pluginsPrefs.addElement(prefsValue);
if (className.endsWith("\")")) { // remove any argument
int argStart = className.lastIndexOf("(\"");
if (argStart>0)
className = className.substring(0, argStart);
}
skipList.put(className, "");
}
}
if (pluginList!=null) {
for (int i=0; i<pluginList.length; i++) {
if (!skipList.containsKey(pluginList[i]))
installUserPlugin(pluginList[i]);
}
}
if ((nPlugins-nPlugins0)<=1 && IJ.getDir("imagej")!=null && IJ.getDir("imagej").startsWith("/private")) {
pluginsMenu.addSeparator();
addPlugInItem(pluginsMenu, "Why are Plugins Missing?", "ij.plugin.SimpleCommands(\"missing\")", 0, false);
}
installJarPlugins();
installMacros();
}
/** Installs macros and scripts located in the plugins folder. */
void installMacros() {
if (macroFiles==null)
return;
for (int i=0; i<macroFiles.size(); i++) {
String name = (String)macroFiles.elementAt(i);
installMacro(name);
}
}
/** Installs a macro or script in the Plugins menu, or submenu, with
with underscores in the file name replaced by spaces. */
void installMacro(String name) {
Menu menu = pluginsMenu;
String dir = null;
int slashIndex = name.indexOf('/');
if (slashIndex>0) {
dir = name.substring(0, slashIndex);
name = name.substring(slashIndex+1, name.length());
menu = getPluginsSubmenu(dir);
slashIndex = name.indexOf('/');
if (slashIndex>0) {
String dir2 = name.substring(0, slashIndex);
name = name.substring(slashIndex+1, name.length());
String menuName = "Plugins>"+dir+">"+dir2;
menu = getMenu(menuName);
dir += File.separator+dir2;
}
}
String command = name.replace('_',' ');
if (command.endsWith(".js")||command.endsWith(".py"))
command = command.substring(0, command.length()-3); //remove ".js" or ".py"
else
command = command.substring(0, command.length()-4); //remove ".txt", ".ijm" or ".bsh"
command = command.trim();
if (pluginsTable.get(command)!=null) // duplicate command?
command = command + " Macro";
MenuItem item = new MenuItem(command);
addOrdered(menu, item);
item.addActionListener(ij);
String path = (dir!=null?dir+File.separator:"") + name;
pluginsTable.put(command, "ij.plugin.Macro_Runner(\""+path+"\")");
nMacros++;
}
static int addPluginSeparatorIfNeeded(Menu menu) {
if (menuSeparators == null)
return 0;
Integer i = (Integer)menuSeparators.get(menu);
if (i == null) {
if (menu.getItemCount() > 0)
addSeparator(menu);
i = Integer.valueOf(menu.getItemCount());
menuSeparators.put(menu, i);
}
return i.intValue();
}
/** Inserts 'item' into 'menu' in alphanumeric order. */
static void addOrdered(Menu menu, MenuItem item) {
String label = item.getLabel();
int start = addPluginSeparatorIfNeeded(menu);
for (int i=start; i<menu.getItemCount(); i++) {
if (label.compareTo(menu.getItem(i).getLabel())<0) {
menu.insert(item, i);
return;
}
}
menu.add(item);
}
public static String getJarFileForMenuEntry(String menuEntry) {
if (instance == null)
return null;
return (String)instance.menuEntry2jarFile.get(menuEntry);
}
/** Install plugins located in JAR files. */
void installJarPlugins() {
if (jarFiles==null)
return;
installingJars = true;
for (int i=0; i<jarFiles.size(); i++) {
isJarErrorHeading = false;
String jar = (String)jarFiles.elementAt(i);
InputStream is = getConfigurationFile(jar);
if (is==null) continue;
ArrayList entries = new ArrayList(20);
LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is));
try {
while(true) {
String s = lnr.readLine();
if (s==null) break;
if (s.length()>=3 && !s.startsWith("#"))
entries.add(s);
}
}
catch (IOException e) {}
finally {
try {if (lnr!=null) lnr.close();}
catch (IOException e) {}
}
for (int j=0; j<entries.size(); j++)
installJarPlugin(jar, (String)entries.get(j));
}
}
/** Install a plugin located in a JAR file. */
void installJarPlugin(String jar, String s) {
addSorted = false;
Menu menu;
s = s.trim();
if (s.startsWith("Plugins>")) {
int firstComma = s.indexOf(',');
if (firstComma==-1 || firstComma<=8)
menu = null;
else {
String name = s.substring(8, firstComma);
menu = getPluginsSubmenu(name);
}
} else if (s.startsWith("\"") || s.startsWith("Plugins")) {
String name = getSubmenuName(jar);
if (name!=null)
menu = getPluginsSubmenu(name);
else
menu = pluginsMenu;
addSorted = true;
} else {
int firstQuote = s.indexOf('"');
String name = firstQuote<0 ? s : s.substring(0, firstQuote).trim();
int comma = name.indexOf(',');
if (comma >= 0)
name = name.substring(0, comma);
if (name.startsWith("Help>About")) // for backward compatibility
name = "Help>About Plugins";
menu = getMenu(name);
}
int firstQuote = s.indexOf('"');
if (firstQuote==-1)
return;
s = s.substring(firstQuote, s.length()); // remove menu
if (menu!=null) {
addPluginSeparatorIfNeeded(menu);
addPluginItem(menu, s);
addSorted = false;
}
String menuEntry = s;
if (s.startsWith("\"")) {
int quote = s.indexOf('"', 1);
menuEntry = quote<0?s.substring(1):s.substring(1, quote);
} else {
int comma = s.indexOf(',');
if (comma > 0)
menuEntry = s.substring(0, comma);
}
if (duplicateCommand) {
if (jarError==null) jarError = "";
addJarErrorHeading(jar);
String jar2 = (String)menuEntry2jarFile.get(menuEntry);
if (jar2 != null && jar2.startsWith(pluginsPath))
jar2 = jar2.substring(pluginsPath.length());
jarError += " Duplicate command: " + s
+ (jar2 != null ? " (already in " + jar2 + ")"
: "") + "\n";
} else
menuEntry2jarFile.put(menuEntry, jar);
duplicateCommand = false;
}
void addJarErrorHeading(String jar) {
if (!isJarErrorHeading) {
if (!jarError.equals(""))
jarError += " \n";
jarError += "Plugin configuration error: " + jar + "\n";
isJarErrorHeading = true;
}
}
/** Returns the specified ImageJ menu (e.g., "File>New") or null if it is not found. */
public static Menu getImageJMenu(String menuPath) {
if (menus==null && !GraphicsEnvironment.isHeadless())
IJ.init();
if (menus==null)
return null;
if (menus.get(menuPath)!=null)
return getMenu(menuPath, false);
else
return null;
}
private static Menu getMenu(String menuPath) {
return getMenu(menuPath, false);
}
private static Menu getMenu(String menuName, boolean readFromProps) {
if (menuName.endsWith(">"))
menuName = menuName.substring(0, menuName.length() - 1);
Menu result = (Menu)menus.get(menuName);
if (result==null) {
int offset = menuName.lastIndexOf('>');
if (offset < 0) {
result = new Menu(menuName);
if (mbar == null)
mbar = new MenuBar();
if (menuName.equals("Help"))
mbar.setHelpMenu(result);
else
mbar.add(result);
if (menuName.equals("Window"))
window = result;
else if (menuName.equals("Plugins"))
pluginsMenu = result;
} else {
String parentName = menuName.substring(0, offset);
String menuItemName = menuName.substring(offset + 1);
Menu parentMenu = getMenu(parentName);
result = new Menu(menuItemName);
addPluginSeparatorIfNeeded(parentMenu);
if (readFromProps)
result = addSubMenu(parentMenu, menuItemName);
else if (parentName.startsWith("Plugins") && menuSeparators != null)
addItemSorted(parentMenu, result, parentName.equals("Plugins")?userPluginsIndex:0);
else
parentMenu.add(result);
if (menuName.equals("File>Open Recent"))
openRecentMenu = result;
}
menus.put(menuName, result);
}
//System.out.println("menuName: "+menuName);
if (IJ.isWindows() && menuName!=null && menuName.contains(">"))
fixFontSize(result);
return result;
}
Menu getPluginsSubmenu(String submenuName) {
return getMenu("Plugins>" + submenuName);
}
String getSubmenuName(String jarPath) {
//IJ.log("getSubmenuName: \n"+jarPath+"\n"+pluginsPath);
if (pluginsPath == null)
return null;
if (jarPath.startsWith(pluginsPath))
jarPath = jarPath.substring(pluginsPath.length() - 1);
int index = jarPath.lastIndexOf(File.separatorChar);
if (index<0) return null;
String name = jarPath.substring(0, index);
index = name.lastIndexOf(File.separatorChar);
if (index<0) return null;
name = name.substring(index+1);
if (name.equals("plugins")) return null;
return name;
}
static void addItemSorted(Menu menu, MenuItem item, int startingIndex) {
String itemLabel = item.getLabel();
int count = menu.getItemCount();
boolean inserted = false;
for (int i=startingIndex; i<count; i++) {
MenuItem mi = menu.getItem(i);
String label = mi.getLabel();
//IJ.log(i+ " "+itemLabel+" "+label + " "+(itemLabel.compareTo(label)));
if (itemLabel.compareTo(label)<0) {
menu.insert(item, i);
inserted = true;
break;
}
}
if (!inserted) menu.add(item);
}
static void addSeparator(Menu menu) {
menu.addSeparator();
}
/** Opens the configuration file ("plugins.config") from a JAR file and returns it as an InputStream. */
InputStream getConfigurationFile(String jar) {
try {
ZipFile jarFile = new ZipFile(jar);
Enumeration entries = jarFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
if (entry.getName().endsWith("plugins.config"))
return jarFile.getInputStream(entry);
}
jarFile.close();
}
catch (Throwable e) {
IJ.log(jar+": "+e);
}
return autoGenerateConfigFile(jar);
}
/** Creates a configuration file for JAR/ZIP files that do not have one. */
InputStream autoGenerateConfigFile(String jar) {
StringBuffer sb = null;
try {
ZipFile jarFile = new ZipFile(jar);
Enumeration entries = jarFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String name = entry.getName();
if (name.endsWith(".class") && name.indexOf("_")>0 && name.indexOf("$")==-1
&& name.indexOf("/_")==-1 && !name.startsWith("_")) {
if (Character.isLowerCase(name.charAt(0))&&name.indexOf("/")!=-1)
continue;
if (sb==null) sb = new StringBuffer();
String className = name.substring(0, name.length()-6);
int slashIndex = className.lastIndexOf('/');
String plugins = "Plugins";
if (slashIndex >= 0) {
plugins += ">" + className.substring(0, slashIndex).replace('/', '>').replace('_', ' ');
name = className.substring(slashIndex + 1);
} else
name = className;
name = name.replace('_', ' ');
className = className.replace('/', '.');
sb.append(plugins + ", \""+name+"\", "+className+"\n");
}
}
jarFile.close();
}
catch (Throwable e) {
IJ.log(jar+": "+e);
}
if (sb==null)
return null;
else
return new ByteArrayInputStream(sb.toString().getBytes());
}
/** Returns a list of the plugins with directory names removed. */
String[] getStrippedPlugins(String[] plugins) {
String[] plugins2 = new String[plugins.length];
int slashPos;
for (int i=0; i<plugins2.length; i++) {
plugins2[i] = plugins[i];
slashPos = plugins2[i].lastIndexOf('/');
if (slashPos>=0)
plugins2[i] = plugins[i].substring(slashPos+1,plugins2[i].length());
}
return plugins2;
}