File: OverlayLabels.java

package info (click to toggle)
imagej 1.52j-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,604 kB
  • sloc: java: 120,017; sh: 279; xml: 161; makefile: 6
file content (120 lines) | stat: -rw-r--r-- 3,874 bytes parent folder | download | duplicates (5)
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
package ij.plugin;
import ij.*;
import ij.process.*;
import ij.gui.*;
import ij.util.Tools;
import ij.plugin.filter.Analyzer;
import java.awt.*;
import java.util.Vector;

/** This plugin implements the Image/Overlay/Labels command. */
public class OverlayLabels implements PlugIn, DialogListener {
	private static final String[] fontSizes = {"7", "8", "9", "10", "12", "14", "18", "24", "28", "36", "48", "72"};
	private static Overlay defaultOverlay = new Overlay();
	private ImagePlus imp;
	private Overlay overlay;
	private GenericDialog gd;
	private boolean showLabels;
	private boolean showNames;
	private boolean drawBackgrounds;
	private String colorName;
	private int fontSize;
	private boolean bold;
	
	public void run(String arg) {
		imp = WindowManager.getCurrentImage();
		overlay = null;
		if (imp!=null) {
			ImageCanvas ic = imp.getCanvas();
			if (ic!=null)
				overlay = ic.getShowAllList();
			if (overlay==null)
				overlay = imp.getOverlay();
		}
		if (overlay==null)
			overlay = defaultOverlay;
		showDialog();
		if (!gd.wasCanceled()) {
			defaultOverlay.drawLabels(overlay.getDrawLabels());
			defaultOverlay.drawNames(overlay.getDrawNames());
			defaultOverlay.drawBackgrounds(overlay.getDrawBackgrounds());
			defaultOverlay.setLabelColor(overlay.getLabelColor());
			defaultOverlay.setLabelFont(overlay.getLabelFont());
		}
	}
	
	public void showDialog() {
		showLabels = overlay.getDrawLabels();
		showNames = overlay.getDrawNames();
		drawBackgrounds = overlay.getDrawBackgrounds();
		colorName = Colors.getColorName(overlay.getLabelColor(), "white");
		fontSize = 12;
		Font font = overlay.getLabelFont();
		if (font!=null) {
			fontSize = font.getSize();
			bold = font.getStyle()==Font.BOLD;
		}
		gd = new GenericDialog("Labels");
		gd.addChoice("Color:", Colors.colors, colorName);
		gd.addChoice("Font size:", fontSizes, ""+fontSize);
		gd.addCheckbox("Show labels", showLabels);
		gd.addCheckbox("Use names as labels", showNames);
		gd.addCheckbox("Draw backgrounds", drawBackgrounds);
		gd.addCheckbox("Bold", bold);
		gd.addDialogListener(this);
		gd.showDialog();
	}
	
	public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) {
		if (gd.wasCanceled()) return false;
		String colorName2 = colorName;
		boolean showLabels2 = showLabels;
		boolean showNames2 = showNames;
		boolean drawBackgrounds2 = drawBackgrounds;
		boolean bold2 = bold;
		int fontSize2 = fontSize;
		colorName = gd.getNextChoice();
		fontSize = (int)Tools.parseDouble(gd.getNextChoice(), 12);
		showLabels = gd.getNextBoolean();
		showNames = gd.getNextBoolean();
		drawBackgrounds = gd.getNextBoolean();
		bold = gd.getNextBoolean();
		boolean colorChanged = !colorName.equals(colorName2);
		boolean sizeChanged = fontSize!=fontSize2;
		boolean changes = showLabels!=showLabels2 || showNames!=showNames2
			|| drawBackgrounds!=drawBackgrounds2 || colorChanged || sizeChanged
			|| bold!=bold2;
		if (changes) {
			if ((showNames&&!showNames2) || colorChanged || sizeChanged) {
				showLabels = true;
				Vector checkboxes = gd.getCheckboxes();
				((Checkbox)checkboxes.elementAt(0)).setState(true);
			}
			overlay.drawLabels(showLabels);
			Analyzer.drawLabels(showLabels);
			overlay.drawNames(showNames);
			overlay.drawBackgrounds(drawBackgrounds);
			Color color = Colors.getColor(colorName, Color.white);
			overlay.setLabelColor(color);
			if (sizeChanged || bold || bold!=bold2)
				overlay.setLabelFont(new Font("SansSerif", bold?Font.BOLD:Font.PLAIN, fontSize));
			if (imp!=null) {
				Overlay o = imp.getOverlay();
				if (o==null) {
					ImageCanvas ic = imp.getCanvas();
					if (ic!=null)
						o = ic.getShowAllList();
				}
				if (o!=null)
					imp.draw();
			}
		}
		return true;
	}

	/** Creates an empty Overlay that has the current label settings. */
	public static Overlay createOverlay() {
		return defaultOverlay.duplicate();
	}

}