forked from imagej/ImageJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowManager.java
647 lines (594 loc) · 19.9 KB
/
WindowManager.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
package ij;
import ij.plugin.Converter;
import ij.plugin.frame.Recorder;
import ij.plugin.frame.Editor;
import ij.text.TextWindow;
import ij.plugin.frame.PlugInFrame;
import ij.plugin.frame.Commands;
import ij.util.Tools;
import ij.macro.Interpreter;
import java.awt.*;
import java.util.*;
import ij.gui.*;
/** This class consists of static methods used to manage ImageJ's windows. */
public class WindowManager {
public static boolean checkForDuplicateName;
private static Vector imageList = new Vector(); // list of image windows
private static Vector activations = new Vector(); // list of image windows, ordered by activation time
private static Vector nonImageList = new Vector(); // list of non-image windows (Frames and Dialogs)
private static ImageWindow currentWindow; // active image window
private static Window frontWindow;
private static Window frontTable;
private static Frame frontFrame;
private static Hashtable tempImageTable = new Hashtable();
private WindowManager() {
}
/** Makes the image contained in the specified window the active image. */
public static void setCurrentWindow(ImageWindow win) {
if (win==null || win.isClosed() || win.getImagePlus()==null) // deadlock-"wait to lock"
return;
//IJ.log("setCurrentWindow: "+win.getImagePlus().getTitle()+" ("+(currentWindow!=null?currentWindow.getImagePlus().getTitle():"null") + ")");
setWindow(win);
tempImageTable.remove(Thread.currentThread());
if (win==currentWindow || imageList.size()==0)
return;
if (currentWindow!=null) {
// free up pixel buffers used by current window
ImagePlus imp = currentWindow.getImagePlus();
if (imp!=null ) {
if (!Prefs.keepUndoBuffers)
imp.trimProcessor();
imp.saveRoi();
}
}
Undo.reset();
currentWindow = win;
activations.remove(win);
activations.add(win);
Menus.updateMenus();
if (Recorder.record && !IJ.isMacro())
Recorder.record("selectImage", win.getImagePlus().getTitle());
}
/** Returns the active ImageWindow. */
public static ImageWindow getCurrentWindow() {
return currentWindow;
}
static int getCurrentIndex() {
return imageList.indexOf(currentWindow);
}
/** Returns a reference to the active image or null if there isn't one.
* @see ij.IJ#getImage
*/
public static ImagePlus getCurrentImage() {
ImagePlus img = (ImagePlus)tempImageTable.get(Thread.currentThread());
//String str = (img==null)?" null":"";
if (img==null)
img = getActiveImage();
//if (img!=null) IJ.log("getCurrentImage: "+img.getTitle()+" "+Thread.currentThread().hashCode()+str);
return img;
}
/** Makes the specified image temporarily the active
image for this thread. Call again with a null
argument to revert to the previous active image. */
public static void setTempCurrentImage(ImagePlus img) {
//IJ.log("setTempImage: "+(img!=null?""+img:"null")+" "+Thread.currentThread().hashCode());
if (img==null)
tempImageTable.remove(Thread.currentThread());
else
tempImageTable.put(Thread.currentThread(), img);
}
/** Sets a temporary image for the specified thread. */
public static void setTempCurrentImage(Thread thread, ImagePlus img) {
if (thread==null)
throw new RuntimeException("thread==null");
if (img==null)
tempImageTable.remove(thread);
else
tempImageTable.put(thread, img);
}
/** Returns the active ImagePlus. */
private static ImagePlus getActiveImage() {
if (currentWindow!=null)
return currentWindow.getImagePlus();
else if (frontWindow!=null && (frontWindow instanceof ImageWindow))
return frontWindow!=null?((ImageWindow)frontWindow).getImagePlus():null;
else if (imageList.size()>0) {
ImagePlus imp = getFocusManagerActiveImage();
if (imp!=null)
return imp;
ImageWindow win = (ImageWindow)imageList.get(imageList.size()-1);
return win.getImagePlus();
} else
return Interpreter.getLastBatchModeImage();
}
private static ImagePlus getFocusManagerActiveImage() {
if (IJ.isMacro())
return null;
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
Window win = kfm.getActiveWindow();
ImagePlus imp = null;
if (win!=null && (win instanceof ImageWindow))
imp = ((ImageWindow)win).getImagePlus();
return imp;
}
/** Returns the number of open image windows. */
public static int getWindowCount() {
int count = imageList.size();
return count;
}
/** Returns the number of open images. */
public static int getImageCount() {
int count = imageList.size();
count += Interpreter.getBatchModeImageCount();
if (count==0 && getCurrentImage()!=null)
count = 1;
return count;
}
/** Returns the front most window or null. */
public static Window getActiveWindow() {
return frontWindow;
}
/** Returns the Window containing the active table, or null.
* @see ij.measure.ResultsTable#getActiveTable
*/
public static Window getActiveTable() {
return frontTable;
}
/** Obsolete; replaced by getActiveWindow. */
public static Frame getFrontWindow() {
return frontFrame;
}
/** Returns a list of the IDs of open images. Returns
null if no image windows are open. */
public synchronized static int[] getIDList() {
int nWindows = imageList.size();
int[] batchModeImages = Interpreter.getBatchModeImageIDs();
int nBatchImages = batchModeImages.length;
if ((nWindows+nBatchImages)==0)
return null;
int[] list = new int[nWindows+nBatchImages];
for (int i=0; i<nWindows; i++) {
ImageWindow win = (ImageWindow)imageList.get(i);
list[i] = win.getImagePlus().getID();
}
int index = 0;
for (int i=nWindows; i<nWindows+nBatchImages; i++)
list[i] = batchModeImages[index++];
//IJ.log("getIDList");
//for (int i=0; i<list.length; i++) {
//IJ.log(" "+i+" "+nWindows+" "+nBatchImages+" "+list[i]+" "+getImage((list[i])));
//}
return list;
}
/** Returns a list of the titles of all open images. */
public synchronized static String[] getImageTitles() {
int[] list = getIDList();
if (list==null)
return new String[0];
String[] titles = new String[list.length];
for (int i=0; i<list.length; i++) {
ImagePlus img = getImage(list[i]);
titles[i] = img.getTitle();
}
return titles;
}
/** Returns an array containing a list of the non-image Frames. */
public synchronized static Frame[] getNonImageWindows() {
ArrayList list = new ArrayList();
for (int i=0; i<nonImageList.size(); i++) {
Object win = nonImageList.get(i);
if (win instanceof Frame)
list.add(win);
}
Frame[] frames = new Frame[list.size()];
list.toArray(frames);
return frames;
}
/** Returns an array containing a list of the non-image Frames and Dialogs. */
public synchronized static Window[] getAllNonImageWindows() {
ArrayList list = new ArrayList();
for (int i=0; i<nonImageList.size(); i++) {
Object win = nonImageList.get(i);
if (win instanceof Window)
list.add(win);
}
Window[] windows = new Window[list.size()];
list.toArray(windows);
return windows;
}
/** Returns an array containing the titles of non-image Frames and Dialogs. */
public synchronized static String[] getNonImageTitles() {
ArrayList list = new ArrayList();
for (int i=0; i<nonImageList.size(); i++) {
Object win = nonImageList.get(i);
String title = win instanceof Frame?((Frame)win).getTitle():((Dialog)win).getTitle();
list.add(title);
}
String[] titles = new String[list.size()];
list.toArray(titles);
return titles;
}
/** For IDs less than zero, returns the ImagePlus with the specified ID.
Returns null if no open window has a matching ID or no images are open.
For IDs greater than zero, returns the Nth ImagePlus. Returns null if
the ID is zero. */
public synchronized static ImagePlus getImage(int imageID) {
if (imageID>0)
imageID = getNthImageID(imageID);
if (imageID==0 || getImageCount()==0)
return null;
ImagePlus imp2 = Interpreter.getBatchModeImage(imageID);
if (imp2!=null)
return imp2;
ImagePlus imp = null;
for (int i=0; i<imageList.size(); i++) {
ImageWindow win = (ImageWindow)imageList.get(i);
imp2 = win.getImagePlus();
if (imageID==imp2.getID()) {
imp = imp2;
break;
}
}
imp2 = getCurrentImage();
if (imp==null &&imp2!=null && imp2.getID()==imageID)
return imp2;
return imp;
}
/** Returns the ID of the Nth open image. Returns zero if n<=0
or n greater than the number of open image windows. */
public synchronized static int getNthImageID(int n) {
if (n<=0) return 0;
if (Interpreter.isBatchMode()) {
int[] list = getIDList();
if (n>list.length)
return 0;
else
return list[n-1];
} else {
if (n>imageList.size()) return 0;
ImageWindow win = (ImageWindow)imageList.get(n-1);
if (win!=null)
return win.getImagePlus().getID();
else
return 0;
}
}
/** Returns the first image that has the specified title or null if it is not found. */
public synchronized static ImagePlus getImage(String title) {
int[] wList = getIDList();
if (wList==null) return null;
for (int i=0; i<wList.length; i++) {
ImagePlus imp = getImage(wList[i]);
if (imp!=null && imp.getTitle().equals(title))
return imp;
}
return null;
}
/** Adds the specified window to the Window menu. */
public synchronized static void addWindow(Window win) {
if (win==null)
return;
else if (win instanceof ImageWindow)
addImageWindow((ImageWindow)win);
else {
Menus.insertWindowMenuItem(win);
nonImageList.add(win);
}
}
/** Adds the specified Frame to the Window menu. */
public static void addWindow(Frame win) {
addWindow((Window)win);
}
private static void addImageWindow(ImageWindow win) {
ImagePlus imp = win.getImagePlus();
if (imp==null) return;
checkForDuplicateName(imp);
imageList.add(win);
Menus.addWindowMenuItem(imp);
setCurrentWindow(win);
}
static void checkForDuplicateName(ImagePlus imp) {
if (checkForDuplicateName) {
String name = imp.getTitle();
if (isDuplicateName(name))
imp.setTitle(getUniqueName(name));
}
checkForDuplicateName = false;
}
static boolean isDuplicateName(String name) {
if (name==null)
return false;
int n = imageList.size();
for (int i=0; i<n; i++) {
ImageWindow win = (ImageWindow)imageList.get(i);
String name2 = win.getImagePlus().getTitle();
if (name.equals(name2))
return true;
}
return false;
}
/*
static boolean isDuplicateBatchModeName(String name) {
int[] list = getIDList();
for (int i=0; i<list.length; i++) {
ImagePlus imp = getImage(list[i]);
if (imp==null) return false;
String name2 = imp.getTitle();
if (name.equals(name2))
return true;
}
return false;
}
*/
/** Returns a unique name by adding, before the extension, -1, -2, etc. as needed. */
public static String getUniqueName(String name) {
return getUniqueName(null, name);
}
public static String getUniqueName(ImagePlus imp, String name) {
String name2 = name;
String extension = "";
int len = name2.length();
int lastDot = name2.lastIndexOf(".");
if (lastDot!=-1 && len-lastDot<6 && lastDot!=len-1) {
extension = name2.substring(lastDot, len);
name2 = name2.substring(0, lastDot);
}
int lastDash = name2.lastIndexOf("-");
len = name2.length();
if (imp!=null && imp.getProp("UniqueName")==null)
lastDash = -1;
if (lastDash!=-1&&len-lastDash<4&&lastDash<len-1&&Character.isDigit(name2.charAt(lastDash+1))&&name2.charAt(lastDash+1)!='0')
name2 = name2.substring(0, lastDash);
for (int i=1; i<=99; i++) {
String name3 = name2+"-"+ i + extension;
if (!isDuplicateName(name3))
return name3;
}
return name;
}
/** If 'name' is not unique, adds -1, -2, etc. as needed to make it unique. */
public static String makeUniqueName(String name) {
return isDuplicateName(name)?getUniqueName(name):name;
}
/** Removes the specified window from the Window menu. */
public static void removeWindow(Window win) {
if (win instanceof ImageWindow)
removeImageWindow((ImageWindow)win);
else {
int index = nonImageList.indexOf(win);
ImageJ ij = IJ.getInstance();
if (index>=0) {
Menus.removeWindowMenuItem(index);
nonImageList.removeElement(win);
}
if (win!=null && win==frontTable)
frontTable = null;
}
setWindow(null);
}
/** Removes the specified Frame from the Window menu. */
public static void removeWindow(Frame win) {
removeWindow((Window)win);
}
private static void removeImageWindow(ImageWindow win) {
int index = imageList.indexOf(win);
if (index==-1)
return; // not on the window list
try {
synchronized(WindowManager.class) {
imageList.remove(win);
}
activations.remove(win);
if (imageList.size()>1 && !Prefs.closingAll) {
ImageWindow win2 = activations.size()>0?(ImageWindow)activations.get(activations.size()-1):null;
setCurrentWindow(win2);
} else
currentWindow = null;
setTempCurrentImage(null); //???
int nonImageCount = nonImageList.size();
if (nonImageCount>0)
nonImageCount++;
Menus.removeWindowMenuItem(nonImageCount+index);
Menus.updateMenus();
Undo.reset();
} catch (Exception e) { }
}
/** The specified Window becomes the front window. */
public static void setWindow(Window win) {
//System.out.println("setWindow(W): "+win);
frontWindow = win;
if (win instanceof Frame)
frontFrame = (Frame)win;
}
/** The specified frame becomes the front window, the one returnd by getFrontWindow(). */
public static void setWindow(Frame win) {
frontWindow = win;
frontFrame = win;
if (win!=null && win instanceof TextWindow && !(win instanceof Editor) && !"Log".equals(((TextWindow)win).getTitle()))
frontTable = win;
//System.out.println("Set window(F): "+(win!=null?win.getTitle():"null"));
}
/** Closes all windows. Stops and returns false if an image or Editor "save changes" dialog is canceled. */
public synchronized static boolean closeAllWindows() {
Prefs.closingAll = true;
while (imageList.size()>0) {
if (!((ImageWindow)imageList.get(0)).close()) {
Prefs.closingAll = false;
return false;
}
if (!quittingViaMacro())
IJ.wait(100);
}
Prefs.closingAll = false;
Frame[] nonImages = getNonImageWindows();
for (int i=0; i<nonImages.length; i++) {
Frame frame = nonImages[i];
if (frame!=null && frame instanceof Commands)
((Commands)frame).close();
else if (frame!=null && (frame instanceof Editor)) {
((Editor)frame).close();
if (((Editor)frame).fileChanged())
return false;
if (!quittingViaMacro())
IJ.wait(100);
}
}
ImageJ ij = IJ.getInstance();
if (ij!=null && ij.quitting() && IJ.getApplet()==null)
return true;
for (int i=0; i<nonImages.length; i++) {
Frame frame = nonImages[i];
if ((frame instanceof PlugInFrame) && !(frame instanceof Editor))
((PlugInFrame)frame).close();
else if (frame instanceof TextWindow)
((TextWindow)frame).close();
else {
//frame.setVisible(false);
frame.dispose();
}
}
return true;
}
private static boolean quittingViaMacro() {
ImageJ ij = IJ.getInstance();
return ij!=null && ij.quittingViaMacro();
}
/** Activates the next image window on the window list. */
public static void putBehind() {
if (IJ.debugMode) IJ.log("putBehind");
if (imageList.size()<1 || currentWindow==null)
return;
int index = imageList.indexOf(currentWindow);
ImageWindow win = null;
int count = 0;
do {
index--;
if (index<0) index = imageList.size()-1;
win = (ImageWindow)imageList.get(index);
if (++count==imageList.size()) return;
} while (win instanceof HistogramWindow || win instanceof PlotWindow);
if (win==null) return;
ImagePlus imp = win.getImagePlus();
if (imp!=null)
IJ.selectWindow(imp.getID());
}
/** Returns the temporary current image for this thread, or null. */
public static ImagePlus getTempCurrentImage() {
return (ImagePlus)tempImageTable.get(Thread.currentThread());
}
/** Returns the window (a Frame or a Dialog) with the specified
title, or null if a window with that title is not found. */
public static Window getWindow(String title) {
for (int i=0; i<nonImageList.size(); i++) {
Object win = nonImageList.get(i);
String winTitle = win instanceof Frame?((Frame)win).getTitle():((Dialog)win).getTitle();
if (title.equals(winTitle))
return (Window)win;
}
return getImageWindow(title);
}
/** Obsolete; replaced by getWindow(). */
public static Frame getFrame(String title) {
for (int i=0; i<nonImageList.size(); i++) {
Object win = nonImageList.get(i);
String winTitle = win instanceof Frame?((Frame)win).getTitle():null;
if (title.equals(winTitle))
return (Frame)win;
}
Frame frame = getImageWindow(title);
if (frame==null) {
Window win = getWindow(title);
if (win!=null)
frame = new Frame("Proxy");
}
return frame;
}
private static Frame getImageWindow(String title) {
int[] wList = getIDList();
int len = wList!=null?wList.length:0;
for (int i=0; i<len; i++) {
ImagePlus imp = getImage(wList[i]);
if (imp!=null) {
if (imp.getTitle().equals(title))
return imp.getWindow();
}
}
return null;
}
/** Activates a window selected from the Window menu. */
synchronized static void activateWindow(String menuItemLabel, MenuItem item) {
for (int i=0; i<nonImageList.size(); i++) {
Object win = nonImageList.get(i);
String title = win instanceof Frame?((Frame)win).getTitle():((Dialog)win).getTitle();
if (menuItemLabel.equals(title)) {
if (win instanceof Frame)
toFront((Frame)win);
else
((Dialog)win).toFront();
((CheckboxMenuItem)item).setState(false);
if (Recorder.record && !IJ.isMacro())
Recorder.record("selectWindow", title);
return;
}
}
int lastSpace = menuItemLabel.lastIndexOf(' ');
if (lastSpace>0) // remove image size (e.g., " 90K")
menuItemLabel = menuItemLabel.substring(0, lastSpace);
String idString = item.getActionCommand();
int id = (int)Tools.parseDouble(idString, 0);
ImagePlus imp = WindowManager.getImage(id);
if (imp==null) return;
ImageWindow win1 = imp.getWindow();
if (win1==null) return;
setCurrentWindow(win1);
toFront(win1);
int index = imageList.indexOf(win1);
int n = Menus.window.getItemCount();
int start = Menus.WINDOW_MENU_ITEMS+Menus.windowMenuItems2;
for (int j=start; j<n; j++) {
MenuItem mi = Menus.window.getItem(j);
if (mi instanceof CheckboxMenuItem)
((CheckboxMenuItem)mi).setState((j-start)==index);
}
}
/** Repaints all open image windows. */
public synchronized static void repaintImageWindows() {
int[] list = getIDList();
if (list==null) return;
for (int i=0; i<list.length; i++) {
ImagePlus imp2 = getImage(list[i]);
if (imp2!=null) {
imp2.setTitle(imp2.getTitle()); // update "(G)" flag (global calibration)
ImageWindow win = imp2.getWindow();
if (win!=null) win.repaint();
}
}
}
public static void showList() {
for (int i=0; i<imageList.size(); i++) {
ImageWindow win = (ImageWindow)imageList.get(i);
ImagePlus imp = win.getImagePlus();
IJ.log(i + " " + imp.getTitle() + (win==currentWindow?"*":"")+" "+imp.getID());
}
for (int i=0; i<activations.size(); i++) {
ImageWindow win = (ImageWindow)activations.get(i);
ImagePlus imp = win.getImagePlus();
IJ.log(i + " " + imp.getTitle() + " " + imp.getID());
}
if (imageList.size()==0) IJ.log("imageList is empty");
if (activations.size()==0) IJ.log("activations list is empty");
IJ.log(" ");
}
public static void toFront(Frame frame) {
if (frame==null) return;
if (frame.getState()==Frame.ICONIFIED)
frame.setState(Frame.NORMAL);
frame.toFront();
}
public static void toFront(Window window) {
if (window==null) return;
if (window instanceof Frame && ((Frame)window).getState()==Frame.ICONIFIED)
((Frame)window).setState(Frame.NORMAL);
window.toFront();
}
}