001package icy.gui.lut;
002
003import icy.gui.component.NumberTextField;
004import icy.gui.dialog.ActionDialog;
005import icy.gui.dialog.MessageDialog;
006import icy.image.lut.LUT.LUTChannel;
007import icy.main.Icy;
008import icy.sequence.Sequence;
009
010import java.awt.GridBagConstraints;
011import java.awt.GridBagLayout;
012import java.awt.Insets;
013import java.awt.event.ActionEvent;
014import java.awt.event.ActionListener;
015
016import javax.swing.JButton;
017import javax.swing.JLabel;
018
019public class ScalerBoundsSettingDialog extends ActionDialog
020{
021    final LUTChannel lutChannel;
022
023    NumberTextField rangeMinField;
024    NumberTextField rangeMaxField;
025    NumberTextField lowBoundField;
026    NumberTextField highBoundField;
027    private JButton defaultBtn;
028    private JLabel lblMinimum;
029    private JLabel lblMaximum;
030
031    public ScalerBoundsSettingDialog(LUTChannel lutChannel)
032    {
033        super("Histogram range");
034
035        this.lutChannel = lutChannel;
036
037        initialize();
038
039        rangeMinField.setNumericValue(lutChannel.getMinBound());
040        rangeMaxField.setNumericValue(lutChannel.getMaxBound());
041        lowBoundField.setNumericValue(lutChannel.getMin());
042        highBoundField.setNumericValue(lutChannel.getMax());
043
044        defaultBtn.addActionListener(new ActionListener()
045        {
046            @Override
047            public void actionPerformed(ActionEvent e)
048            {
049                final LUTChannel lc = ScalerBoundsSettingDialog.this.lutChannel;
050
051                // use active sequence
052                final Sequence sequence = Icy.getMainInterface().getActiveSequence();
053                if (sequence != null)
054                {
055                    final int ch = lc.getChannel();
056
057                    if (ch < sequence.getSizeC())
058                    {
059                        rangeMinField.setNumericValue(sequence.getChannelTypeMin(ch));
060                        rangeMaxField.setNumericValue(sequence.getChannelTypeMax(ch));
061                        lowBoundField.setNumericValue(sequence.getChannelMin(ch));
062                        highBoundField.setNumericValue(sequence.getChannelMax(ch));
063                    }
064                }
065            }
066        });
067
068        setOkAction(new ActionListener()
069        {
070            @Override
071            public void actionPerformed(ActionEvent e)
072            {
073                final double rangeMin = rangeMinField.getNumericValue();
074                final double rangeMax = rangeMaxField.getNumericValue();
075                final double lowBound = lowBoundField.getNumericValue();
076                final double highBound = highBoundField.getNumericValue();
077
078                if ((rangeMin >= rangeMax) || (lowBound >= highBound))
079                {
080                    MessageDialog.showDialog("Invalids settings",
081                            "The maximum range value should be above the minimum range value",
082                            MessageDialog.ERROR_MESSAGE);
083                    return;
084                }
085                if ((lowBound < rangeMin) || (highBound > rangeMax))
086                {
087                    MessageDialog.showDialog("Invalids settings", "The view range should be inside the full range",
088                            MessageDialog.ERROR_MESSAGE);
089                    return;
090                }
091
092                final LUTChannel lc = ScalerBoundsSettingDialog.this.lutChannel;
093
094                lc.setMinBound(rangeMin);
095                lc.setMaxBound(rangeMax);
096                lc.setMin(lowBound);
097                lc.setMax(highBound);
098
099                dispose();
100            }
101        });
102
103        setCloseAfterAction(false);
104    }
105
106    private void initialize()
107    {
108        GridBagLayout gridBagLayout = new GridBagLayout();
109        gridBagLayout.columnWidths = new int[] {60, 0, 0, 10, 0};
110        gridBagLayout.rowHeights = new int[] {0, 23, 0, 0};
111        gridBagLayout.columnWeights = new double[] {0.0, 1.0, 1.0, 0.0, Double.MIN_VALUE};
112        gridBagLayout.rowWeights = new double[] {0.0, 0.0, 0.0, Double.MIN_VALUE};
113        mainPanel.setLayout(gridBagLayout);
114
115        lblMinimum = new JLabel("Minimum");
116        GridBagConstraints gbc_lblMinimum = new GridBagConstraints();
117        gbc_lblMinimum.insets = new Insets(0, 0, 5, 5);
118        gbc_lblMinimum.gridx = 1;
119        gbc_lblMinimum.gridy = 0;
120        mainPanel.add(lblMinimum, gbc_lblMinimum);
121
122        lblMaximum = new JLabel("Maximum");
123        GridBagConstraints gbc_lblMaximum = new GridBagConstraints();
124        gbc_lblMaximum.insets = new Insets(0, 0, 5, 5);
125        gbc_lblMaximum.gridx = 2;
126        gbc_lblMaximum.gridy = 0;
127        mainPanel.add(lblMaximum, gbc_lblMaximum);
128
129        final JLabel lblRangeMin = new JLabel("Full range");
130        GridBagConstraints gbc_lblRangeMin = new GridBagConstraints();
131        gbc_lblRangeMin.anchor = GridBagConstraints.EAST;
132        gbc_lblRangeMin.insets = new Insets(0, 0, 5, 5);
133        gbc_lblRangeMin.gridx = 0;
134        gbc_lblRangeMin.gridy = 1;
135        mainPanel.add(lblRangeMin, gbc_lblRangeMin);
136
137        rangeMinField = new NumberTextField();
138        GridBagConstraints gbc_rangeMinField = new GridBagConstraints();
139        gbc_rangeMinField.insets = new Insets(0, 0, 5, 5);
140        gbc_rangeMinField.fill = GridBagConstraints.HORIZONTAL;
141        gbc_rangeMinField.gridx = 1;
142        gbc_rangeMinField.gridy = 1;
143        mainPanel.add(rangeMinField, gbc_rangeMinField);
144        rangeMinField.setColumns(10);
145
146        rangeMaxField = new NumberTextField();
147        GridBagConstraints gbc_rangeMaxField = new GridBagConstraints();
148        gbc_rangeMaxField.insets = new Insets(0, 0, 5, 5);
149        gbc_rangeMaxField.fill = GridBagConstraints.HORIZONTAL;
150        gbc_rangeMaxField.gridx = 2;
151        gbc_rangeMaxField.gridy = 1;
152        mainPanel.add(rangeMaxField, gbc_rangeMaxField);
153        rangeMaxField.setColumns(10);
154
155        final JLabel lblBoundMin = new JLabel("View range");
156        GridBagConstraints gbc_lblBoundMin = new GridBagConstraints();
157        gbc_lblBoundMin.anchor = GridBagConstraints.EAST;
158        gbc_lblBoundMin.insets = new Insets(0, 0, 0, 5);
159        gbc_lblBoundMin.gridx = 0;
160        gbc_lblBoundMin.gridy = 2;
161        mainPanel.add(lblBoundMin, gbc_lblBoundMin);
162
163        lowBoundField = new NumberTextField();
164        GridBagConstraints gbc_lowBoundField = new GridBagConstraints();
165        gbc_lowBoundField.insets = new Insets(0, 0, 0, 5);
166        gbc_lowBoundField.fill = GridBagConstraints.HORIZONTAL;
167        gbc_lowBoundField.gridx = 1;
168        gbc_lowBoundField.gridy = 2;
169        mainPanel.add(lowBoundField, gbc_lowBoundField);
170        lowBoundField.setColumns(10);
171
172        highBoundField = new NumberTextField();
173        GridBagConstraints gbc_highBoundField = new GridBagConstraints();
174        gbc_highBoundField.insets = new Insets(0, 0, 0, 5);
175        gbc_highBoundField.fill = GridBagConstraints.HORIZONTAL;
176        gbc_highBoundField.gridx = 2;
177        gbc_highBoundField.gridy = 2;
178        mainPanel.add(highBoundField, gbc_highBoundField);
179        highBoundField.setColumns(10);
180
181        defaultBtn = new JButton("Default");
182        GridBagConstraints gbc_defaultBtn = new GridBagConstraints();
183        gbc_defaultBtn.anchor = GridBagConstraints.EAST;
184        gbc_defaultBtn.insets = new Insets(0, 0, 0, 5);
185        gbc_defaultBtn.gridx = 1;
186        gbc_defaultBtn.gridy = 0;
187        buttonPanel.add(defaultBtn, gbc_defaultBtn);
188    }
189
190}