001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.gui.viewer;
020
021import icy.canvas.IcyCanvas;
022import icy.gui.component.ColorComponent;
023import icy.gui.util.ComponentUtil;
024import icy.gui.util.GuiUtil;
025import icy.image.IcyBufferedImage;
026import icy.math.MathUtil;
027import icy.sequence.Sequence;
028import icy.type.collection.array.ArrayUtil;
029import icy.util.StringUtil;
030
031import java.awt.BorderLayout;
032import java.awt.Dimension;
033
034import javax.swing.BorderFactory;
035import javax.swing.Box;
036import javax.swing.BoxLayout;
037import javax.swing.JLabel;
038import javax.swing.JPanel;
039
040public class MouseImageInfosPanel extends JPanel
041{
042    /**
043     * 
044     */
045    private static final long serialVersionUID = -5994107451349072824L;
046
047    private static final int SIGNIFICANT_DIGIT = 5;
048
049    private final JLabel xLabel;
050    private final JLabel yLabel;
051    private final JLabel zLabel;
052    private final JLabel tLabel;
053    private final JLabel cLabel;
054    private final JLabel dataLabel;
055    private final JLabel xValue;
056    private final JLabel yValue;
057    private final JLabel zValue;
058    private final JLabel tValue;
059    private final JLabel cValue;
060    private final JLabel dataValue;
061
062    private final ColorComponent colorComp;
063
064    private boolean infoXVisible;
065    private boolean infoYVisible;
066    private boolean infoZVisible;
067    private boolean infoTVisible;
068    private boolean infoCVisible;
069    private boolean infoDataVisible;
070    private boolean infoColorVisible;
071
072    public MouseImageInfosPanel()
073    {
074        super(true);
075
076        infoXVisible = true;
077        infoYVisible = true;
078        infoTVisible = true;
079        infoZVisible = true;
080        infoCVisible = true;
081        infoDataVisible = true;
082        infoColorVisible = true;
083
084        colorComp = new ColorComponent();
085        ComponentUtil.setFixedSize(colorComp, new Dimension(30, 14));
086        xValue = new JLabel();
087        ComponentUtil.setFixedWidth(xValue, 58);
088        yValue = new JLabel();
089        ComponentUtil.setFixedWidth(yValue, 58);
090        zValue = new JLabel();
091        ComponentUtil.setFixedWidth(zValue, 40);
092        tValue = new JLabel();
093        ComponentUtil.setFixedWidth(tValue, 40);
094        cValue = new JLabel();
095        ComponentUtil.setFixedWidth(cValue, 40);
096        dataValue = new JLabel();
097        ComponentUtil.setFixedWidth(dataValue, 200);
098
099        xLabel = GuiUtil.createBoldLabel(" X  ");
100        yLabel = GuiUtil.createBoldLabel(" Y  ");
101        zLabel = GuiUtil.createBoldLabel(" Z  ");
102        tLabel = GuiUtil.createBoldLabel(" T  ");
103        cLabel = GuiUtil.createBoldLabel(" C  ");
104        dataLabel = GuiUtil.createBoldLabel(" Value  ");
105
106        final JPanel infosPanel = new JPanel();
107        infosPanel.setLayout(new BoxLayout(infosPanel, BoxLayout.LINE_AXIS));
108
109        infosPanel.add(colorComp);
110        infosPanel.add(Box.createHorizontalStrut(10));
111        infosPanel.add(xLabel);
112        infosPanel.add(xValue);
113        infosPanel.add(yLabel);
114        infosPanel.add(yValue);
115        infosPanel.add(zLabel);
116        infosPanel.add(zValue);
117        infosPanel.add(tLabel);
118        infosPanel.add(tValue);
119        infosPanel.add(cLabel);
120        infosPanel.add(cValue);
121        infosPanel.add(dataLabel);
122        infosPanel.add(dataValue);
123
124        setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
125        setLayout(new BorderLayout());
126
127        add(infosPanel, BorderLayout.WEST);
128        add(new JPanel(), BorderLayout.CENTER);
129
130        validate();
131
132        updateInfos(null);
133    }
134
135    /**
136     * @return the xInfoVisible
137     */
138    public boolean isInfoXVisible()
139    {
140        return infoXVisible;
141    }
142
143    /**
144     * @param value
145     *        the xInfoVisible to set
146     */
147    public void setInfoXVisible(boolean value)
148    {
149        if (infoXVisible != value)
150        {
151            infoXVisible = value;
152            xLabel.setVisible(value);
153            xValue.setVisible(value);
154        }
155    }
156
157    /**
158     * @return the yInfoVisible
159     */
160    public boolean isInfoYVisible()
161    {
162        return infoYVisible;
163    }
164
165    /**
166     * @param value
167     *        the yInfoVisible to set
168     */
169    public void setInfoYVisible(boolean value)
170    {
171        if (infoYVisible != value)
172        {
173            infoYVisible = value;
174            yLabel.setVisible(value);
175            yValue.setVisible(value);
176        }
177    }
178
179    /**
180     * @return the zInfoVisible
181     */
182    public boolean isInfoZVisible()
183    {
184        return infoZVisible;
185    }
186
187    /**
188     * @param value
189     *        the zInfoVisible to set
190     */
191    public void setInfoZVisible(boolean value)
192    {
193        if (infoZVisible != value)
194        {
195            infoZVisible = value;
196            zLabel.setVisible(value);
197            zValue.setVisible(value);
198        }
199    }
200
201    /**
202     * @return the tInfoVisible
203     */
204    public boolean isInfoTVisible()
205    {
206        return infoTVisible;
207    }
208
209    /**
210     * @param value
211     *        the tInfoVisible to set
212     */
213    public void setInfoTVisible(boolean value)
214    {
215        if (infoTVisible != value)
216        {
217            infoTVisible = value;
218            tLabel.setVisible(value);
219            tValue.setVisible(value);
220        }
221    }
222
223    /**
224     * @return the cInfoVisible
225     */
226    public boolean isInfoCVisible()
227    {
228        return infoCVisible;
229    }
230
231    /**
232     * @param value
233     *        the cInfoVisible to set
234     */
235    public void setInfoCVisible(boolean value)
236    {
237        if (infoCVisible != value)
238        {
239            infoCVisible = value;
240            cLabel.setVisible(value);
241            cValue.setVisible(value);
242        }
243    }
244
245    /**
246     * @return the dataInfoVisible
247     */
248    public boolean isInfoDataVisible()
249    {
250        return infoDataVisible;
251    }
252
253    /**
254     * @param value
255     *        the dataInfoVisible to set
256     */
257    public void setInfoDataVisible(boolean value)
258    {
259        if (infoDataVisible != value)
260        {
261            infoDataVisible = value;
262            dataLabel.setVisible(value);
263            dataValue.setVisible(value);
264        }
265    }
266
267    /**
268     * @return the colorInfoVisible
269     */
270    public boolean isInfoColorVisible()
271    {
272        return infoColorVisible;
273    }
274
275    /**
276     * @param value
277     *        the colorInfoVisible to set
278     */
279    public void setInfoColorVisible(boolean value)
280    {
281        if (infoColorVisible != value)
282        {
283            infoColorVisible = value;
284            colorComp.setVisible(value);
285        }
286    }
287
288    public void updateInfos(IcyCanvas canvas)
289    {
290        final Sequence seq;
291
292        if (canvas != null)
293            seq = canvas.getSequence();
294        else
295            seq = null;
296
297        if (seq != null)
298        {
299            final double x = canvas.getMouseImagePosX();
300            final double y = canvas.getMouseImagePosY();
301            final double z = canvas.getMouseImagePosZ();
302            final double t = canvas.getMouseImagePosT();
303            final double c = canvas.getMouseImagePosC();
304            final int xi = (int) x;
305            final int yi = (int) y;
306            final int zi = (int) z;
307            final int ti = (int) t;
308            final int ci = (int) c;
309
310            final String xs;
311            final String ys;
312            final String zs;
313            final String ts;
314            final String cs;
315
316            if (x == xi)
317                xs = StringUtil.toString(xi);
318            else
319                xs = StringUtil.toStringEx(x, SIGNIFICANT_DIGIT);
320            if (y == yi)
321                ys = StringUtil.toString(yi);
322            else
323                ys = StringUtil.toStringEx(y, SIGNIFICANT_DIGIT);
324            if (z == zi)
325                zs = StringUtil.toString(zi);
326            else
327                zs = StringUtil.toStringEx(z, SIGNIFICANT_DIGIT);
328            if (t == ti)
329                ts = StringUtil.toString(ti);
330            else
331                ts = StringUtil.toStringEx(t, SIGNIFICANT_DIGIT);
332            if (c == ci)
333                cs = StringUtil.toString(ci);
334            else
335                cs = StringUtil.toStringEx(c, SIGNIFICANT_DIGIT);
336
337            xValue.setText(xs);
338            yValue.setText(ys);
339            zValue.setText(zs);
340            tValue.setText(ts);
341            cValue.setText(cs);
342
343            xValue.setToolTipText(xValue.getText());
344            yValue.setToolTipText(yValue.getText());
345            zValue.setToolTipText(zValue.getText());
346            tValue.setToolTipText(tValue.getText());
347            cValue.setToolTipText(cValue.getText());
348
349            final IcyBufferedImage image = seq.getImage(ti, zi);
350
351            if ((image != null) && (image.isInside(xi, yi)))
352            {
353                try
354                {
355                    // FIXME : should take C value in account to retrieve single component color
356                    colorComp.setColor(image.getRGB(xi, yi, canvas.getLut()));
357
358                    if (ci == -1)
359                    {
360                        // all components values
361                        dataValue.setText(ArrayUtil.array1DToString(image.getDataCopyC(xi, yi), image.getDataType_()
362                                .isSigned(), false, " : ", 5));
363                    }
364                    else
365                    {
366                        // single component value
367                        final double v = image.getData(xi, yi, ci);
368                        final int vi = (int) v;
369                        final String vs;
370
371                        if (v == vi)
372                            vs = Integer.toString(vi);
373                        else
374                            vs = Double.toString(MathUtil.roundSignificant(v, SIGNIFICANT_DIGIT, true));
375
376                        dataValue.setText(vs);
377                    }
378                }
379                catch (Exception e)
380                {
381                    // ignore exception as we can have unsync update
382                }
383
384                dataValue.setToolTipText(dataValue.getText());
385            }
386            else
387            {
388                dataValue.setText("-");
389                colorComp.setColor(null);
390
391                dataValue.setToolTipText(null);
392            }
393        }
394        else
395        {
396            xValue.setText("-");
397            yValue.setText("-");
398            zValue.setText("-");
399            tValue.setText("-");
400            cValue.setText("-");
401            dataValue.setText("-");
402            colorComp.setColor(null);
403
404            xValue.setToolTipText(null);
405            yValue.setToolTipText(null);
406            zValue.setToolTipText(null);
407            tValue.setToolTipText(null);
408            cValue.setToolTipText(null);
409            dataValue.setToolTipText(null);
410        }
411    }
412}