Added popup menu to layer's tab panels to reset all alphas and colors in one shot.
[trakem2.git] / ini / trakem2 / display / LayerPanel.java
bloba3a4346006f6feb29e981eae87a871270c2e3a84
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.MouseListener;
41 import java.awt.event.MouseEvent;
42 import java.awt.Color;
43 import java.awt.Graphics;
44 import java.awt.Dimension;
47 public final class LayerPanel extends JPanel implements MouseListener {
49 private final JLabel title;
50 private final JSlider slider = new JSlider(javax.swing.SwingConstants.HORIZONTAL, 0, 100, 0);
51 private Color color = Color.white;
52 private float alpha = 0.0f; // for overlays
54 private final Display display;
55 protected final Layer layer;
57 public LayerPanel(final Display display, final Layer layer) {
58 this.display = display;
59 this.layer = layer;
61 this.slider.addChangeListener(new ChangeListener() {
62 public void stateChanged(final ChangeEvent ce) {
63 final float a = slider.getValue() / 100.0f;
64 setAlpha(a);
65 display.storeLayerAlpha(LayerPanel.this, a);
66 display.repaint();
68 });
70 this.title = new JLabel(makeTitle());
71 this.title.addMouseListener(this);
73 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
74 add(title);
75 add(slider);
77 final Dimension dim = new Dimension(250 - Display.scrollbar_width, DisplayablePanel.HEIGHT);
78 setMinimumSize(dim);
79 setMaximumSize(dim);
80 //setPreferredSize(dim);
82 addMouseListener(this);
83 setBackground(this.color);
84 setBorder(BorderFactory.createLineBorder(Color.black));
87 private final String makeTitle() {
88 return new StringBuffer().append(layer.getParent().indexOf(layer) + 1).append(':').append(' ').append(layer.getTitle()).toString();
91 public final void setColor(final Color color) {
92 this.color = color;
93 setBackground(color);
94 slider.setBackground(color);
95 if (Color.white == color) {
96 title.setForeground(Color.black);
97 } else {
98 title.setForeground(Color.white);
100 repaint();
103 public final Color getColor() { return color; }
105 public final void setAlpha(final float alpha) {
106 if (alpha < 0 || alpha > 1) return;
107 this.alpha = alpha;
108 slider.setValue((int)(alpha * 100));
111 public final float getAlpha() { return alpha; }
113 public final void paint(final Graphics g) {
114 title.setText(makeTitle());
115 if (display.getLayer() == layer) {
116 setBackground(Color.green);
117 slider.setBackground(Color.green);
118 } else {
119 setBackground(color);
120 slider.setBackground(color);
122 super.paint(g);
125 public void mousePressed(final MouseEvent me) {
126 if (Utils.isPopupTrigger(me)) {
127 JPopupMenu popup = new JPopupMenu();
128 JMenuItem item = new JMenuItem("Reset layer coloring");
129 item.addActionListener(new ActionListener() {
130 public void actionPerformed(ActionEvent ae) {
131 display.resetLayerColors();
134 popup.add(item);
135 item = new JMenuItem("Reset all layer alphas");
136 item.addActionListener(new ActionListener() {
137 public void actionPerformed(ActionEvent ae) {
138 display.resetLayerAlphas();
141 popup.add(item);
142 popup.show(this, me.getX(), me.getY());
143 return;
146 final int mod = me.getModifiers();
147 Utils.log2("mouse pressed : " + mod);
148 if (0 == (mod & Event.ALT_MASK) && 0 == (mod & Event.SHIFT_MASK)) {
149 // Would mess up translation of red/blue colors when scrolling
151 display.toLayer(this.layer);
152 setColor(Color.white);
153 display.setColorChannel(this.layer, this.color);
154 repaint();
156 } else if (display.getLayer() == this.layer) {
157 // do nothing
158 } else if (0 != (mod & Event.ALT_MASK)) {
159 if (this.color == Color.blue) {
160 // unset
161 setColor(Color.white);
162 } else {
163 // set as blue channel
164 setColor(Color.blue);
166 } else if (0 != (mod & Event.SHIFT_MASK)) {
167 if (this.color == Color.red) {
168 // unset
169 setColor(Color.white);
170 } else {
171 // set as red channel
172 setColor(Color.red);
175 display.setColorChannel(this.layer, this.color);
178 public void mouseReleased(MouseEvent me) {}
179 public void mouseEntered(MouseEvent me) {}
180 public void mouseExited (MouseEvent me) {}
181 public void mouseClicked(MouseEvent me) {}
183 public final String toString() {
184 return "Layer panel for " + layer.getTitle();