001/**
002 * 
003 */
004package icy.canvas;
005
006import icy.gui.component.button.ColorChooserButton;
007import icy.gui.component.button.ColorChooserButton.ColorChangeListener;
008import icy.gui.component.button.IcyButton;
009import icy.gui.util.ComponentUtil;
010import icy.resource.ResourceUtil;
011import icy.resource.icon.IcyIcon;
012import icy.util.EventUtil;
013
014import java.awt.BorderLayout;
015import java.awt.Color;
016import java.awt.Font;
017import java.awt.GridBagConstraints;
018import java.awt.GridBagLayout;
019import java.awt.Insets;
020import java.awt.event.ActionEvent;
021import java.awt.event.ActionListener;
022import java.awt.event.MouseAdapter;
023import java.awt.event.MouseEvent;
024
025import javax.swing.JComboBox;
026import javax.swing.JLabel;
027import javax.swing.JPanel;
028
029/**
030 * Setting panel for Canvas2D
031 * 
032 * @author Stephane
033 */
034public class Canvas2DSettingPanel extends JPanel
035{
036    /**
037     * 
038     */
039    private static final long serialVersionUID = -6747952036222386920L;
040
041    final Canvas2D canvas2D;
042
043    /**
044     * gui
045     */
046    JComboBox zoomComboBox;
047    JComboBox rotationComboBox;
048
049    private IcyButton zoomFitImageButton;
050    private IcyButton centerImageButton;
051    private IcyButton zoomPlus;
052    private IcyButton zoomMinus;
053    private IcyButton rotateUnclock;
054    private IcyButton rotateClock;
055    ColorChooserButton bgColorButton;
056
057    public Canvas2DSettingPanel(Canvas2D cnv)
058    {
059        super();
060
061        this.canvas2D = cnv;
062
063        initialize();
064
065        // as scale isn't necessary changed (if already 100%)
066        zoomComboBox.setSelectedItem(Integer.toString((int) (canvas2D.getScaleX() * 100)));
067        bgColorButton.setColor(new Color(canvas2D.preferences.getInt(Canvas2D.ID_BG_COLOR, 0xFFFFFF)));
068        bgColorButton.setSelected(canvas2D.preferences.getBoolean(Canvas2D.ID_BG_COLOR_ENABLED, false));
069
070        zoomComboBox.addActionListener(new ActionListener()
071        {
072            @Override
073            public void actionPerformed(ActionEvent e)
074            {
075                if (!canvas2D.modifyingZoom)
076                {
077                    try
078                    {
079                        final double scale = Double.parseDouble((String) zoomComboBox.getSelectedItem()) / 100;
080
081                        // set mouse position on view center
082                        canvas2D.centerMouseOnView();
083                        // set new scale
084                        canvas2D.setScale(scale, scale, true, true);
085                    }
086                    catch (NumberFormatException E)
087                    {
088                        // ignore change
089                    }
090                }
091            }
092        });
093        rotationComboBox.addActionListener(new ActionListener()
094        {
095            @Override
096            public void actionPerformed(ActionEvent e)
097            {
098                if (!canvas2D.modifyingRotation)
099                {
100                    try
101                    {
102                        final double angle = Double.parseDouble((String) rotationComboBox.getSelectedItem());
103                        // we first apply modulo
104                        canvas2D.setRotation(canvas2D.getRotation(), false);
105                        // then set new angle
106                        canvas2D.setRotation((angle * Math.PI) / 180d, true);
107                    }
108                    catch (NumberFormatException E)
109                    {
110                        // ignore change
111                    }
112                }
113            }
114        });
115        zoomPlus.addActionListener(new ActionListener()
116        {
117            @Override
118            public void actionPerformed(ActionEvent e)
119            {
120                final double scale = canvas2D.smoothTransform.getDestValue(Canvas2D.SCALE_X) * 1.25;
121
122                // set mouse position on view center
123                canvas2D.centerMouseOnView();
124                // apply scale
125                canvas2D.setScale(scale, scale, true, true);
126            }
127        });
128        zoomMinus.addActionListener(new ActionListener()
129        {
130            @Override
131            public void actionPerformed(ActionEvent e)
132            {
133                final double scale = canvas2D.smoothTransform.getDestValue(Canvas2D.SCALE_X) * 0.8;
134
135                // set mouse position on view center
136                canvas2D.centerMouseOnView();
137                // apply scale
138                canvas2D.setScale(scale, scale, true, true);
139            }
140        });
141        rotateUnclock.addActionListener(new ActionListener()
142        {
143            @Override
144            public void actionPerformed(ActionEvent e)
145            {
146                canvas2D.setRotation(canvas2D.smoothTransform.getDestValue(Canvas2D.ROT) + (Math.PI / 8), true);
147            }
148        });
149        rotateClock.addActionListener(new ActionListener()
150        {
151            @Override
152            public void actionPerformed(ActionEvent e)
153            {
154                canvas2D.setRotation(canvas2D.smoothTransform.getDestValue(Canvas2D.ROT) - (Math.PI / 8), true);
155            }
156        });
157        zoomFitImageButton.addActionListener(new ActionListener()
158        {
159            @Override
160            public void actionPerformed(ActionEvent e)
161            {
162                canvas2D.fitCanvasToImage();
163            }
164        });
165        centerImageButton.addActionListener(new ActionListener()
166        {
167            @Override
168            public void actionPerformed(ActionEvent e)
169            {
170                canvas2D.centerImage();
171            }
172        });
173        bgColorButton.addMouseListener(new MouseAdapter()
174        {
175            @Override
176            public void mouseClicked(MouseEvent e)
177            {
178                if (EventUtil.isRightMouseButton(e))
179                {
180                    bgColorButton.setSelected(!bgColorButton.isSelected());
181                    canvas2D.backgroundColorEnabledChanged();
182                }
183            };
184        });
185        bgColorButton.addColorChangeListener(new ColorChangeListener()
186        {
187            @Override
188            public void colorChanged(ColorChooserButton source)
189            {
190                canvas2D.backgroundColorChanged();
191            }
192        });
193    }
194
195    private void initialize()
196    {
197        // subPanel.add(GuiUtil.createLineBoxPanel(Box.createHorizontalStrut(4),
198        // GuiUtil.createFixedWidthBoldLabel("Zoom", 70), zoomComboBox,
199        // GuiUtil.createFixedWidthBoldLabel("%", 20), Box.createHorizontalGlue(), zoomMinus,
200        // Box.createHorizontalStrut(4), zoomPlus, Box.createHorizontalStrut(4)));
201        // subPanel.add(Box.createVerticalStrut(4));
202        // subPanel.add(GuiUtil.createLineBoxPanel(Box.createHorizontalStrut(4),
203        // GuiUtil.createFixedWidthBoldLabel("Rotation", 70), rotationComboBox,
204        // GuiUtil.createFixedWidthBoldLabel("°", 20), Box.createHorizontalGlue(), rotateUnclock,
205        // Box.createHorizontalStrut(4), rotateClock, Box.createHorizontalStrut(4)));
206
207        setLayout(new BorderLayout());
208
209        JPanel panel = new JPanel();
210        add(panel, BorderLayout.SOUTH);
211        GridBagLayout gbl_panel = new GridBagLayout();
212        gbl_panel.columnWidths = new int[] {60, 0, 20, 0, 0, 8, 0, 0};
213        gbl_panel.rowHeights = new int[] {0, 0, 0};
214        gbl_panel.columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0};
215        gbl_panel.rowWeights = new double[] {0.0, 0.0, Double.MIN_VALUE};
216        panel.setLayout(gbl_panel);
217
218        JLabel label_1 = new JLabel("Zoom");
219        label_1.setFont(new Font("Tahoma", Font.BOLD, 11));
220        GridBagConstraints gbc_label_1 = new GridBagConstraints();
221        gbc_label_1.anchor = GridBagConstraints.EAST;
222        gbc_label_1.insets = new Insets(0, 0, 5, 5);
223        gbc_label_1.gridx = 0;
224        gbc_label_1.gridy = 0;
225        panel.add(label_1, gbc_label_1);
226
227        zoomComboBox = new JComboBox(new String[] {"10", "50", "100", "200", "400", "1000"});
228        zoomComboBox.setEditable(true);
229        zoomComboBox.setToolTipText("Select zoom factor");
230        zoomComboBox.setSelectedIndex(2);
231        ComponentUtil.setFixedWidth(zoomComboBox, 64);
232        GridBagConstraints gbc_zoomComboBox = new GridBagConstraints();
233        gbc_zoomComboBox.fill = GridBagConstraints.HORIZONTAL;
234        gbc_zoomComboBox.insets = new Insets(0, 0, 5, 1);
235        gbc_zoomComboBox.gridx = 1;
236        gbc_zoomComboBox.gridy = 0;
237        panel.add(zoomComboBox, gbc_zoomComboBox);
238
239        JLabel label_2 = new JLabel("%");
240        label_2.setFont(new Font("Tahoma", Font.BOLD, 11));
241        GridBagConstraints gbc_label_2 = new GridBagConstraints();
242        gbc_label_2.anchor = GridBagConstraints.WEST;
243        gbc_label_2.insets = new Insets(0, 0, 5, 5);
244        gbc_label_2.gridx = 2;
245        gbc_label_2.gridy = 0;
246        panel.add(label_2, gbc_label_2);
247
248        zoomMinus = new IcyButton(new IcyIcon(ResourceUtil.ICON_MINUS, Canvas2D.ICON_SIZE));
249        zoomMinus.setFlat(true);
250        zoomMinus.setToolTipText("Reduce zoom factor");
251        GridBagConstraints gbc_zoomMinus_1 = new GridBagConstraints();
252        gbc_zoomMinus_1.insets = new Insets(0, 0, 5, 1);
253        gbc_zoomMinus_1.gridx = 3;
254        gbc_zoomMinus_1.gridy = 0;
255        panel.add(zoomMinus, gbc_zoomMinus_1);
256
257        zoomPlus = new IcyButton(new IcyIcon(ResourceUtil.ICON_PLUS, Canvas2D.ICON_SIZE));
258        zoomPlus.setFlat(true);
259        zoomPlus.setToolTipText("Increase zoom factor");
260        GridBagConstraints gbc_zoomPlus_1 = new GridBagConstraints();
261        gbc_zoomPlus_1.insets = new Insets(0, 0, 5, 5);
262        gbc_zoomPlus_1.gridx = 4;
263        gbc_zoomPlus_1.gridy = 0;
264        panel.add(zoomPlus, gbc_zoomPlus_1);
265
266        JLabel label_3 = new JLabel("Rotation");
267        label_3.setFont(new Font("Tahoma", Font.BOLD, 11));
268        GridBagConstraints gbc_label_3 = new GridBagConstraints();
269        gbc_label_3.anchor = GridBagConstraints.EAST;
270        gbc_label_3.insets = new Insets(0, 0, 0, 5);
271        gbc_label_3.gridx = 0;
272        gbc_label_3.gridy = 1;
273        panel.add(label_3, gbc_label_3);
274
275        rotationComboBox = new JComboBox(new String[] {"0", "45", "90", "135", "180", "225", "270", "315"});
276        ComponentUtil.setFixedWidth(rotationComboBox, 64);
277        rotationComboBox.setEditable(true);
278        rotationComboBox.setToolTipText("Select rotation angle");
279        rotationComboBox.setSelectedIndex(0);
280        GridBagConstraints gbc_rotationComboBox = new GridBagConstraints();
281        gbc_rotationComboBox.fill = GridBagConstraints.HORIZONTAL;
282        gbc_rotationComboBox.insets = new Insets(0, 0, 0, 1);
283        gbc_rotationComboBox.gridx = 1;
284        gbc_rotationComboBox.gridy = 1;
285        panel.add(rotationComboBox, gbc_rotationComboBox);
286
287        JLabel label_4 = new JLabel("°");
288        label_4.setFont(new Font("Tahoma", Font.BOLD, 11));
289        GridBagConstraints gbc_label_4 = new GridBagConstraints();
290        gbc_label_4.anchor = GridBagConstraints.WEST;
291        gbc_label_4.insets = new Insets(0, 0, 0, 5);
292        gbc_label_4.gridx = 2;
293        gbc_label_4.gridy = 1;
294        panel.add(label_4, gbc_label_4);
295
296        rotateUnclock = new IcyButton(new IcyIcon(ResourceUtil.ICON_ROTATE_UNCLOCK, Canvas2D.ICON_SIZE));
297        rotateUnclock.setFlat(true);
298        rotateUnclock.setToolTipText("Rotate counter clockwise");
299        GridBagConstraints gbc_rotateUnclock_1 = new GridBagConstraints();
300        gbc_rotateUnclock_1.insets = new Insets(0, 0, 0, 1);
301        gbc_rotateUnclock_1.gridx = 3;
302        gbc_rotateUnclock_1.gridy = 1;
303        panel.add(rotateUnclock, gbc_rotateUnclock_1);
304
305        rotateClock = new IcyButton(new IcyIcon(ResourceUtil.ICON_ROTATE_CLOCK, Canvas2D.ICON_SIZE));
306        rotateClock.setFlat(true);
307        rotateClock.setToolTipText("Rotate clockwise");
308        GridBagConstraints gbc_rotateClock_1 = new GridBagConstraints();
309        gbc_rotateClock_1.insets = new Insets(0, 0, 0, 5);
310        gbc_rotateClock_1.gridx = 4;
311        gbc_rotateClock_1.gridy = 1;
312        panel.add(rotateClock, gbc_rotateClock_1);
313
314        zoomFitImageButton = new IcyButton(new IcyIcon(Canvas2D.ICON_FIT_IMAGE));
315        zoomFitImageButton.setFlat(true);
316        zoomFitImageButton.setToolTipText("Fit window to image size");
317        GridBagConstraints gbc_zoomFitImage = new GridBagConstraints();
318        gbc_zoomFitImage.insets = new Insets(0, 0, 0, 5);
319        gbc_zoomFitImage.gridx = 6;
320        gbc_zoomFitImage.gridy = 1;
321        panel.add(zoomFitImageButton, gbc_zoomFitImage);
322
323        centerImageButton = new IcyButton(new IcyIcon(Canvas2D.ICON_CENTER_IMAGE));
324        centerImageButton.setFlat(true);
325        centerImageButton.setToolTipText("Center image in window");
326        GridBagConstraints gbc_centerImageButton = new GridBagConstraints();
327        gbc_centerImageButton.gridx = 7;
328        gbc_centerImageButton.gridy = 1;
329        panel.add(centerImageButton, gbc_centerImageButton);
330
331        bgColorButton = new ColorChooserButton();
332        bgColorButton
333                .setToolTipText("Left click to change background color, right click to enable/disable background color");
334        GridBagConstraints gbc_bgColorButton = new GridBagConstraints();
335        gbc_bgColorButton.gridwidth = 2;
336        gbc_bgColorButton.fill = GridBagConstraints.HORIZONTAL;
337        gbc_bgColorButton.insets = new Insets(0, 0, 5, 0);
338        gbc_bgColorButton.gridx = 6;
339        gbc_bgColorButton.gridy = 0;
340        panel.add(bgColorButton, gbc_bgColorButton);
341
342    }
343
344    public void updateZoomState(String zoom)
345    {
346        // try to select current zoom level
347        zoomComboBox.setSelectedItem(zoom);
348    }
349
350    public void updateRotationState(String rotInfo)
351    {
352        // try to select current rotation angle
353        rotationComboBox.setSelectedItem(rotInfo);
354    }
355
356    public boolean isBackgroundColorEnabled()
357    {
358        return bgColorButton.isSelected();
359    }
360
361    public void setBackgroundColorEnabled(boolean value)
362    {
363        bgColorButton.setSelected(value);
364    }
365
366    public Color getBackgroundColor()
367    {
368        return bgColorButton.getColor();
369    }
370
371    public void setBackgroundColor(Color color)
372    {
373        bgColorButton.setColor(color);
374    }
375}