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.sequence.tools;
020
021import icy.gui.component.button.IcyToggleButton;
022import icy.resource.ResourceUtil;
023import icy.resource.icon.IcyIcon;
024
025import java.awt.Color;
026import java.awt.GridBagConstraints;
027import java.awt.GridBagLayout;
028import java.awt.Insets;
029import java.awt.event.ActionEvent;
030import java.awt.event.ActionListener;
031
032import javax.swing.BorderFactory;
033import javax.swing.ButtonGroup;
034import javax.swing.ButtonModel;
035import javax.swing.JPanel;
036import javax.swing.SwingConstants;
037import javax.swing.event.EventListenerList;
038
039public class PositionAlignmentPanel extends JPanel
040{
041    /**
042     * 
043     */
044    private static final long serialVersionUID = -811970435734479103L;
045
046    public static class PositionBox extends IcyToggleButton
047    {
048        /**
049         * 
050         */
051        private static final long serialVersionUID = -6952409965950366299L;
052
053        public PositionBox()
054        {
055            super(new IcyIcon(ResourceUtil.ICON_NULL, 16));
056
057            setSelectedIcon(new IcyIcon(ResourceUtil.ICON_PICTURE, 16));
058            setBorder(BorderFactory.createLineBorder(Color.black));
059            setFocusPainted(false);
060        }
061    }
062
063    private PositionBox topLeftBox;
064    private PositionBox topBox;
065    private PositionBox topRightBox;
066    private PositionBox leftBox;
067    private PositionBox centerBox;
068    private PositionBox rightBox;
069    private PositionBox bottomLeftBox;
070    private PositionBox bottomBox;
071    private PositionBox bottomRightBox;
072
073    private final ButtonGroup positionGroup;
074
075    /**
076     * Create the panel.
077     */
078    public PositionAlignmentPanel()
079    {
080        super();
081
082        initialize();
083
084        positionGroup = new ButtonGroup();
085
086        positionGroup.add(topLeftBox);
087        positionGroup.add(topBox);
088        positionGroup.add(topRightBox);
089        positionGroup.add(leftBox);
090        positionGroup.add(centerBox);
091        positionGroup.add(rightBox);
092        positionGroup.add(bottomLeftBox);
093        positionGroup.add(bottomBox);
094        positionGroup.add(bottomRightBox);
095
096        centerBox.setSelected(true);
097
098        final ActionListener listener = new ActionListener()
099        {
100            @Override
101            public void actionPerformed(ActionEvent e)
102            {
103                // forward event
104                fireActionPerformed(e);
105            }
106        };
107
108        topLeftBox.addActionListener(listener);
109        topBox.addActionListener(listener);
110        topRightBox.addActionListener(listener);
111        leftBox.addActionListener(listener);
112        centerBox.addActionListener(listener);
113        rightBox.addActionListener(listener);
114        bottomLeftBox.addActionListener(listener);
115        bottomBox.addActionListener(listener);
116        bottomRightBox.addActionListener(listener);
117    }
118
119    @Override
120    public void setEnabled(boolean enabled)
121    {
122        super.setEnabled(enabled);
123
124        topLeftBox.setEnabled(enabled);
125        topBox.setEnabled(enabled);
126        topRightBox.setEnabled(enabled);
127        leftBox.setEnabled(enabled);
128        centerBox.setEnabled(enabled);
129        rightBox.setEnabled(enabled);
130        bottomLeftBox.setEnabled(enabled);
131        bottomBox.setEnabled(enabled);
132        bottomRightBox.setEnabled(enabled);
133    }
134
135    /**
136     * Returns the selected horizontal alignment.<br>
137     * Possible values are <code>SwingConstants.LEFT / CENTER / RIGHT</code>
138     **/
139    public int getXAlign()
140    {
141        final ButtonModel model = positionGroup.getSelection();
142
143        if ((model == topLeftBox.getModel()) || (model == leftBox.getModel()) || (model == bottomLeftBox.getModel()))
144            return SwingConstants.LEFT;
145        if ((model == topBox.getModel()) || (model == centerBox.getModel()) || (model == bottomBox.getModel()))
146            return SwingConstants.CENTER;
147
148        return SwingConstants.RIGHT;
149    }
150
151    /**
152     * Return the selected vertical alignment.<br>
153     * Possible values are <code>SwingConstants.TOP / CENTER / BOTTOM</code>
154     **/
155    public int getYAlign()
156    {
157        final ButtonModel model = positionGroup.getSelection();
158
159        if ((model == topLeftBox.getModel()) || (model == topBox.getModel()) || (model == topRightBox.getModel()))
160            return SwingConstants.TOP;
161        if ((model == leftBox.getModel()) || (model == centerBox.getModel()) || (model == rightBox.getModel()))
162            return SwingConstants.CENTER;
163
164        return SwingConstants.BOTTOM;
165    }
166
167    /**
168     * Notifies all listeners that have registered interest for
169     * notification on this event type. The event instance
170     * is lazily created using the <code>event</code> parameter.
171     * 
172     * @param event
173     *        the <code>ActionEvent</code> object
174     * @see EventListenerList
175     */
176    protected void fireActionPerformed(ActionEvent event)
177    {
178        for (ActionListener listener : getActionListeners())
179            listener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, event.getActionCommand(),
180                    event.getWhen(), event.getModifiers()));
181    }
182
183    /**
184     * Adds an <code>ActionListener</code> to the panel.
185     * 
186     * @param l
187     *        the <code>ActionListener</code> to be added
188     */
189    public void addActionListener(ActionListener l)
190    {
191        listenerList.add(ActionListener.class, l);
192    }
193
194    /**
195     * Removes an <code>ActionListener</code> from the panel.
196     * If the listener is the currently set <code>Action</code> for the button, then the
197     * <code>Action</code> is set to <code>null</code>.
198     * 
199     * @param l
200     *        the listener to be removed
201     */
202    public void removeActionListener(ActionListener l)
203    {
204        listenerList.remove(ActionListener.class, l);
205    }
206
207    /**
208     * Returns an array of all the <code>ActionListener</code>s added
209     * to this AbstractButton with addActionListener().
210     * 
211     * @return all of the <code>ActionListener</code>s added or an empty
212     *         array if no listeners have been added
213     * @since 1.4
214     */
215    public ActionListener[] getActionListeners()
216    {
217        return listenerList.getListeners(ActionListener.class);
218    }
219
220    private void initialize()
221    {
222        GridBagLayout gridBagLayout = new GridBagLayout();
223        gridBagLayout.columnWidths = new int[] {0, 0, 0, 0};
224        gridBagLayout.rowHeights = new int[] {0, 0, 0, 0};
225        gridBagLayout.columnWeights = new double[] {0.0, 0.0, 0.0, Double.MIN_VALUE};
226        gridBagLayout.rowWeights = new double[] {0.0, 0.0, 0.0, Double.MIN_VALUE};
227        setLayout(gridBagLayout);
228
229        topLeftBox = new PositionBox();
230        topLeftBox.setToolTipText("Align content to top left");
231        GridBagConstraints gbc_topLeftBox = new GridBagConstraints();
232        gbc_topLeftBox.insets = new Insets(0, 0, 5, 5);
233        gbc_topLeftBox.gridx = 0;
234        gbc_topLeftBox.gridy = 0;
235        add(topLeftBox, gbc_topLeftBox);
236
237        topBox = new PositionBox();
238        topBox.setToolTipText("Align content to top");
239        GridBagConstraints gbc_topBox = new GridBagConstraints();
240        gbc_topBox.insets = new Insets(0, 0, 5, 5);
241        gbc_topBox.gridx = 1;
242        gbc_topBox.gridy = 0;
243        add(topBox, gbc_topBox);
244
245        topRightBox = new PositionBox();
246        topRightBox.setToolTipText("Align content to top right");
247        GridBagConstraints gbc_topRightBox = new GridBagConstraints();
248        gbc_topRightBox.insets = new Insets(0, 0, 5, 0);
249        gbc_topRightBox.gridx = 2;
250        gbc_topRightBox.gridy = 0;
251        add(topRightBox, gbc_topRightBox);
252
253        leftBox = new PositionBox();
254        leftBox.setToolTipText("Align content to left");
255        GridBagConstraints gbc_leftBox = new GridBagConstraints();
256        gbc_leftBox.insets = new Insets(0, 0, 5, 5);
257        gbc_leftBox.gridx = 0;
258        gbc_leftBox.gridy = 1;
259        add(leftBox, gbc_leftBox);
260
261        centerBox = new PositionBox();
262        centerBox.setToolTipText("Align content to center");
263        GridBagConstraints gbc_centerBox = new GridBagConstraints();
264        gbc_centerBox.insets = new Insets(0, 0, 5, 5);
265        gbc_centerBox.gridx = 1;
266        gbc_centerBox.gridy = 1;
267        add(centerBox, gbc_centerBox);
268
269        rightBox = new PositionBox();
270        rightBox.setToolTipText("Align content to right");
271        GridBagConstraints gbc_rightBox = new GridBagConstraints();
272        gbc_rightBox.insets = new Insets(0, 0, 5, 0);
273        gbc_rightBox.gridx = 2;
274        gbc_rightBox.gridy = 1;
275        add(rightBox, gbc_rightBox);
276
277        bottomLeftBox = new PositionBox();
278        bottomLeftBox.setToolTipText("Align content to bottom left");
279        GridBagConstraints gbc_bottomLeftBox = new GridBagConstraints();
280        gbc_bottomLeftBox.insets = new Insets(0, 0, 0, 5);
281        gbc_bottomLeftBox.gridx = 0;
282        gbc_bottomLeftBox.gridy = 2;
283        add(bottomLeftBox, gbc_bottomLeftBox);
284
285        bottomBox = new PositionBox();
286        bottomBox.setToolTipText("Align content to bottom");
287        GridBagConstraints gbc_bottomBox = new GridBagConstraints();
288        gbc_bottomBox.insets = new Insets(0, 0, 0, 5);
289        gbc_bottomBox.gridx = 1;
290        gbc_bottomBox.gridy = 2;
291        add(bottomBox, gbc_bottomBox);
292
293        bottomRightBox = new PositionBox();
294        bottomRightBox.setToolTipText("Align content to bottom right");
295        GridBagConstraints gbc_bottomRightBox = new GridBagConstraints();
296        gbc_bottomRightBox.gridx = 2;
297        gbc_bottomRightBox.gridy = 2;
298        add(bottomRightBox, gbc_bottomRightBox);
299    }
300
301}