Almost there, need to fix an rgb bug and add layer remove/add awareness hooks.
[trakem2.git] / ini / trakem2 / display / LayerPanel.java
blob8d3b29e1b375b43cf2d83633e84af2626bad9141
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.event.ChangeEvent;
34 import javax.swing.event.ChangeListener;
35 import java.awt.Event;
36 import java.awt.event.MouseListener;
37 import java.awt.event.MouseEvent;
38 import java.awt.Color;
39 import java.awt.Graphics;
40 import java.awt.Dimension;
43 public final class LayerPanel extends JPanel implements MouseListener {
45 private final JLabel title;
46 private final JSlider slider = new JSlider(javax.swing.SwingConstants.HORIZONTAL, 0, 100, 0);
47 private Color color = Color.white;
48 private float alpha = 0.0f; // for overlays
50 private final Display display;
51 protected final Layer layer;
53 public LayerPanel(final Display display, final Layer layer) {
54 this.display = display;
55 this.layer = layer;
57 this.slider.addChangeListener(new ChangeListener() {
58 public void stateChanged(final ChangeEvent ce) {
59 final float a = slider.getValue() / 100.0f;
60 setAlpha(a);
61 display.setLayerAlpha(LayerPanel.this, a);
63 });
65 this.title = new JLabel(makeTitle());
66 this.title.addMouseListener(this);
68 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
69 add(title);
70 add(slider);
72 final Dimension dim = new Dimension(250 - Display.scrollbar_width, DisplayablePanel.HEIGHT);
73 setMinimumSize(dim);
74 setMaximumSize(dim);
75 //setPreferredSize(dim);
77 addMouseListener(this);
78 setBackground(this.color);
79 setBorder(BorderFactory.createLineBorder(Color.black));
82 private final String makeTitle() {
83 return new StringBuffer().append(layer.getParent().indexOf(layer) + 1).append(':').append(' ').append(layer.getTitle()).toString();
86 public final void setColor(final Color color) {
87 this.color = color;
88 setBackground(color);
89 slider.setBackground(color);
90 if (Color.white == color) {
91 title.setForeground(Color.black);
92 } else {
93 title.setForeground(Color.white);
95 repaint();
98 public final Color getColor() { return color; }
100 public final void setAlpha(final float alpha) {
101 if (alpha < 0 || alpha > 1) return;
102 this.alpha = alpha;
103 display.repaint();
106 public final float getAlpha() { return alpha; }
108 public final void paint(final Graphics g) {
109 title.setText(makeTitle());
110 if (display.getLayer() == layer) {
111 setBackground(Color.green);
112 slider.setBackground(Color.green);
113 } else {
114 setBackground(color);
115 slider.setBackground(color);
117 super.paint(g);
120 public void mousePressed(final MouseEvent me) {
121 final int mod = me.getModifiers();
122 Utils.log2("mouse pressed : " + mod);
123 if (0 == (mod & Event.ALT_MASK) && 0 == (mod & Event.SHIFT_MASK)) {
124 display.toLayer(this.layer);
125 setColor(Color.white);
126 display.setColorChannel(this.layer, this.color);
127 repaint();
128 } else if (display.getLayer() == this.layer) {
129 // do nothing
130 } else if (0 != (mod & Event.ALT_MASK)) {
131 if (this.color == Color.blue) {
132 // unset
133 setColor(Color.white);
134 } else {
135 // set as blue channel
136 setColor(Color.blue);
138 } else if (0 != (mod & Event.SHIFT_MASK)) {
139 if (this.color == Color.red) {
140 // unset
141 setColor(Color.white);
142 } else {
143 // set as red channel
144 setColor(Color.red);
147 repaint();
148 display.setColorChannel(this.layer, this.color);
151 public void mouseReleased(MouseEvent me) {}
152 public void mouseEntered(MouseEvent me) {}
153 public void mouseExited (MouseEvent me) {}
154 public void mouseClicked(MouseEvent me) {}
156 public final String toString() {
157 return "Layer panel for " + layer.getTitle();