Major cleanup of Utils class.
[trakem2.git] / ini / trakem2 / display / LayerPanel.java
blobf8498c3d38121182fbb69905c9de174db544579b
1 /**
3 TrakEM2 plugin for ImageJ(C).
4 Copyright (C) 2005, 2006 Albert Cardona and Rodney Douglas.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation (http://www.gnu.org/licenses/gpl.txt )
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 You may contact Albert Cardona at acardona at ini.phys.ethz.ch
20 Institute of Neuroinformatics, University of Zurich / ETH, Switzerland.
21 **/
23 package ini.trakem2.display;
25 import ij.IJ;
26 import ini.trakem2.utils.Utils;
28 import javax.swing.BoxLayout;
29 import javax.swing.BorderFactory;
30 import javax.swing.JLabel;
31 import javax.swing.JSlider;
32 import javax.swing.JPanel;
33 import javax.swing.JPopupMenu;
34 import javax.swing.JMenuItem;
35 import javax.swing.event.ChangeEvent;
36 import javax.swing.event.ChangeListener;
37 import java.awt.Event;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.MouseAdapter;
41 import java.awt.event.MouseListener;
42 import java.awt.event.MouseEvent;
43 import java.awt.Color;
44 import java.awt.Graphics;
45 import java.awt.Dimension;
48 public final class LayerPanel extends JPanel implements MouseListener {
50 private final JLabel title;
51 private final JSlider slider = new JSlider(javax.swing.SwingConstants.HORIZONTAL, 0, 100, 0);
52 private Color color = Color.white;
53 private float alpha = 0.0f; // for overlays
55 private final Display display;
56 protected final Layer layer;
58 public LayerPanel(final Display display, final Layer layer) {
59 this.display = display;
60 this.layer = layer;
62 this.slider.addChangeListener(new ChangeListener() {
63 public void stateChanged(final ChangeEvent ce) {
64 final float a = slider.getValue() / 100.0f;
65 setAlpha(a);
66 display.storeLayerAlpha(LayerPanel.this, a);
67 display.repaint();
69 });
71 // Insert this mouse listener before the slider's
72 /* // BIZARRE autoscrolling of the slider when popup is up. And cannot be fixed with if (popup), neither within the ChangeEvent (which doesn't register)
73 final MouseListener[] mls = slider.getMouseListeners();
74 for (final MouseListener ml : mls) slider.removeMouseListener(ml); // works because getMouseListeners returns an immutable array.
75 slider.addMouseListener(new MouseAdapter() {
76 public void mousePressed(MouseEvent me) {
77 doMousePressed(me);
78 me.consume();
80 });
81 for (final MouseListener ml : mls) slider.addMouseListener(ml);
84 this.title = new JLabel(makeTitle());
85 this.title.addMouseListener(this);
87 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
88 add(title);
89 add(slider);
91 final Dimension dim = new Dimension(250 - Display.scrollbar_width, DisplayablePanel.HEIGHT);
92 setMinimumSize(dim);
93 setMaximumSize(dim);
94 //setPreferredSize(dim);
96 addMouseListener(this);
97 setBackground(this.color);
98 setBorder(BorderFactory.createLineBorder(Color.black));
101 private final String makeTitle() {
102 return new StringBuffer().append(layer.getParent().indexOf(layer) + 1).append(':').append(' ').append(layer.getTitle()).toString();
105 public final void setColor(final Color color) {
106 this.color = color;
107 setBackground(color);
108 slider.setBackground(color);
109 if (Color.white == color) {
110 title.setForeground(Color.black);
111 } else {
112 title.setForeground(Color.white);
114 repaint();
117 public final Color getColor() { return color; }
119 public final void setAlpha(final float alpha) {
120 if (alpha < 0 || alpha > 1) return;
121 this.alpha = alpha;
122 slider.setValue((int)(alpha * 100));
125 public final float getAlpha() { return alpha; }
127 public final void paint(final Graphics g) {
128 title.setText(makeTitle());
129 if (display.getLayer() == layer) {
130 setBackground(Color.green);
131 slider.setBackground(Color.green);
132 } else {
133 setBackground(color);
134 slider.setBackground(color);
136 super.paint(g);
139 private class ColorActionListener implements ActionListener {
140 final Color c;
141 ColorActionListener(final Color c) { this.c = c; }
142 public void actionPerformed(final ActionEvent ae) {
143 setColor(c);
144 display.setColorChannel(layer, c);
148 public void mousePressed(final MouseEvent me) {
149 doMousePressed(me);
152 private void doMousePressed(final MouseEvent me) {
153 if (Utils.isPopupTrigger(me)) {
154 JPopupMenu popup = new JPopupMenu();
155 JMenuItem item = new JMenuItem("Set as red channel"); popup.add(item);
156 if (Color.red == this.color) item.setEnabled(false);
157 else item.addActionListener(new ColorActionListener(Color.red));
158 item = new JMenuItem("Set as blue channel"); popup.add(item);
159 if (Color.blue == this.color) item.setEnabled(false);
160 else item.addActionListener(new ColorActionListener(Color.blue));
161 item = new JMenuItem("Reset"); popup.add(item);
162 if (Color.white == this.color) item.setEnabled(false);
163 else item.addActionListener(new ColorActionListener(Color.white));
164 popup.addSeparator();
165 item = new JMenuItem("Reset all layer coloring"); popup.add(item);
166 item.addActionListener(new ActionListener() {
167 public void actionPerformed(ActionEvent ae) {
168 display.resetLayerColors();
171 item = new JMenuItem("Reset all layer alphas"); popup.add(item);
172 item.addActionListener(new ActionListener() {
173 public void actionPerformed(ActionEvent ae) {
174 display.resetLayerAlphas();
177 popup.show(this, me.getX(), me.getY());
178 me.consume();
179 return;
182 final int mod = me.getModifiers();
183 Utils.log2("mouse pressed : " + mod);
184 if (0 == (mod & Event.ALT_MASK) && 0 == (mod & Event.SHIFT_MASK)) {
185 // Would mess up translation of red/blue colors when scrolling
186 // So just do nothing.
188 display.toLayer(this.layer);
189 setColor(Color.white);
190 display.setColorChannel(this.layer, this.color);
191 repaint();
193 } else if (display.getLayer() == this.layer) {
194 // do nothing
195 } else if (0 != (mod & Event.ALT_MASK)) {
196 if (this.color == Color.blue) {
197 // unset
198 setColor(Color.white);
199 } else {
200 // set as blue channel
201 setColor(Color.blue);
203 display.setColorChannel(this.layer, this.color);
204 me.consume();
205 } else if (0 != (mod & Event.SHIFT_MASK)) {
206 if (this.color == Color.red) {
207 // unset
208 setColor(Color.white);
209 } else {
210 // set as red channel
211 setColor(Color.red);
213 display.setColorChannel(this.layer, this.color);
214 me.consume();
218 public void mouseReleased(MouseEvent me) {}
219 public void mouseEntered(MouseEvent me) {}
220 public void mouseExited (MouseEvent me) {}
221 public void mouseClicked(MouseEvent me) {}
223 public final String toString() {
224 return "Layer panel for " + layer.getTitle();