001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.cipres.kepler;
031
032import java.awt.AWTEvent;
033import java.awt.BorderLayout;
034import java.awt.Component;
035import java.awt.Dimension;
036import java.awt.GridBagConstraints;
037import java.awt.GridBagLayout;
038import java.awt.Insets;
039import java.awt.Toolkit;
040import java.awt.event.ActionEvent;
041import java.awt.event.ActionListener;
042import java.awt.event.WindowAdapter;
043import java.awt.event.WindowEvent;
044import java.util.ArrayList;
045
046import javax.swing.JButton;
047import javax.swing.JCheckBox;
048import javax.swing.JDialog;
049import javax.swing.JFrame;
050import javax.swing.JLabel;
051import javax.swing.JPanel;
052import javax.swing.JScrollPane;
053
054import org.apache.log4j.Logger;
055
056public class SubsetChooser extends JFrame {
057
058        private static final long serialVersionUID = -4694408842783273897L;
059
060        private static Logger logger = Logger.getLogger(SubsetChooser.class
061                        .getName());
062
063        private JPanel pnlSubsetChooser = new JPanel();
064        private JPanel pnlMain;
065        private DisplayObject[] displayObjects;
066        private JCheckBox[] checkboxes;
067        private JDialog dialog;
068        private String headerText;
069
070        /*************************************************************
071         * CONSTRUCTORS
072         **************************************************************/
073        public SubsetChooser(DisplayObject[] displayObjects) throws Exception {
074                enableEvents(AWTEvent.WINDOW_EVENT_MASK);
075                this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
076                this.displayObjects = displayObjects;
077        }
078
079        public SubsetChooser(DisplayObject[] displayObjects, String headerText)
080                        throws Exception {
081
082                enableEvents(AWTEvent.WINDOW_EVENT_MASK);
083                this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
084
085                this.displayObjects = displayObjects;
086                this.headerText = headerText;
087
088        }
089
090        public JPanel getSubsetChooserPanel() throws Exception {
091                buildPanel(false);
092                return pnlMain;
093        }
094
095        public DisplayObject[] showSubsetChooserAsDialog() throws Exception {
096                buildPanel(true);
097                dialog = new JDialog();
098                dialog.getContentPane().add(this.pnlMain);
099                dialog.setModal(true);
100                dialog.pack();
101
102                dialog.addWindowListener(new WindowAdapter() {
103                        public void windowClosing(WindowEvent e) {
104                                closePanelAsDialog();
105                        }
106                });
107
108                // make sure dialog size is not > 80% of screen size
109                Dimension dialogSize = dialog.getSize();
110                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
111                dialogSize.height = Math.min(dialogSize.height,
112                                (int) (screenSize.height * .8));
113                dialogSize.width = Math.min(dialogSize.width,
114                                (int) (screenSize.width * .8));
115                dialog.setSize(dialogSize);
116
117                // Center the dialog
118                dialog.setLocation((screenSize.width - dialogSize.width) / 2,
119                                (screenSize.height - dialogSize.height) / 2);
120
121                dialog.show();
122                dialog.dispose();
123
124                return getSubset();
125        }
126
127        private void closePanelAsDialog() {
128                dialog.dispose();
129        }
130
131        public DisplayObject[] getSubset() {
132                ArrayList aL = new ArrayList();
133                for (int i = 0; i < checkboxes.length; i++) {
134                        if (checkboxes[i].isSelected()) {
135                                aL.add(this.displayObjects[i]);
136                                ;
137                        }
138                }
139                aL.trimToSize();
140                return (DisplayObject[]) aL.toArray(new DisplayObject[aL.size()]);
141        }
142
143        /*************************************************************
144         * PRIVATE METHODS
145         **************************************************************/
146
147        private void buildPanel(boolean bShowOkButton) throws Exception {
148
149                // ui elements
150                GridBagLayout gridBagLayout1 = new GridBagLayout();
151                JLabel lblItem = new JLabel("Item");
152                JButton btnSelectAll2 = new JButton("Select All");
153                JButton btnDeselectAll1 = new JButton("Deselect All");
154                JCheckBox checkBox = new JCheckBox("Checkbox1");
155                JButton btnSelectAll1 = new JButton("Select All");
156                JButton btnDeselectAll2 = new JButton("Deselect All");
157                JScrollPane jScrollPane = new JScrollPane();
158
159                // build content panes
160                pnlMain = new JPanel();
161                BorderLayout borderLayout = new BorderLayout();
162                pnlMain.setLayout(borderLayout);
163
164                pnlSubsetChooser.setLayout(gridBagLayout1);
165                this.getContentPane().add(jScrollPane, java.awt.BorderLayout.WEST);
166                jScrollPane.getViewport().add(pnlSubsetChooser);
167                pnlMain.add(jScrollPane, BorderLayout.WEST);
168                jScrollPane
169                                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
170                jScrollPane
171                                .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
172
173                int row = 0;
174
175                // add header text if set in ctor
176                if (headerText != null) {
177                        JLabel lblHeader = new JLabel(headerText);
178                        pnlSubsetChooser.add(lblHeader, new GridBagConstraints(1, row, 2,
179                                        1, 0.0, 0.0, GridBagConstraints.NORTH,
180                                        GridBagConstraints.NONE, new Insets(15, 15, 15, 0), 0, 0));
181                        row++;
182                }
183                // add top-row buttons
184                pnlSubsetChooser.add(btnSelectAll1, new GridBagConstraints(1, row, 1,
185                                1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE,
186                                new Insets(15, 15, 15, 0), 0, 0));
187
188                pnlSubsetChooser.add(btnDeselectAll1, new GridBagConstraints(2, row, 1,
189                                1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
190                                new Insets(15, 5, 15, 15), 0, 0));
191                row++;
192
193                // add item header
194                lblItem.setFont(new java.awt.Font("Dialog", 1, 14));
195                pnlSubsetChooser.add(lblItem, new GridBagConstraints(1, row, 2, 1, 0.0,
196                                0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
197                                new Insets(0, 15, 5, 0), 0, 0));
198
199                // add list of selectable items
200                checkboxes = new JCheckBox[displayObjects.length];
201                for (int i = 0; i < displayObjects.length; i++) {
202                        row++;
203                        checkBox = new JCheckBox(displayObjects[i].getName(), true);
204                        checkboxes[i] = checkBox;
205                        pnlSubsetChooser.add(checkBox, new GridBagConstraints(1, row, 2, 1,
206                                        0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
207                                        new Insets(0, 15, 0, 0), 0, 0));
208                }
209
210                // add bottom-row buttons
211                row++;
212                pnlSubsetChooser.add(btnSelectAll2, new GridBagConstraints(1, row, 1,
213                                1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
214                                new Insets(15, 15, 15, 0), 0, 0));
215
216                pnlSubsetChooser.add(btnDeselectAll2, new GridBagConstraints(2, row, 1,
217                                1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
218                                new Insets(15, 5, 15, 15), 0, 0));
219
220                if (bShowOkButton) {
221                        JButton btnOk = new JButton("Ok");
222                        row++;
223                        pnlSubsetChooser.add(btnOk, new GridBagConstraints(1, row, 1, 1,
224                                        0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
225                                        new Insets(0, 15, 15, 0), 0, 0));
226                        btnOk.addActionListener(new ActionListener() {
227                                public void actionPerformed(ActionEvent e) {
228                                        Component c = (Component) e.getSource();
229                                        if (c.hasFocus()) {
230                                                closePanelAsDialog();
231                                        }
232                                }
233                        });
234                }
235
236                // add action listeners
237                btnSelectAll1.addActionListener(new ActionListener() {
238                        public void actionPerformed(ActionEvent e) {
239                                Component c = (Component) e.getSource();
240                                if (c.hasFocus()) {
241                                        selectAll(true);
242                                }
243                        }
244                });
245
246                btnSelectAll2.addActionListener(new ActionListener() {
247                        public void actionPerformed(ActionEvent e) {
248                                Component c = (Component) e.getSource();
249                                if (c.hasFocus()) {
250                                        selectAll(true);
251                                }
252                        }
253                });
254
255                btnDeselectAll1.addActionListener(new ActionListener() {
256                        public void actionPerformed(ActionEvent e) {
257                                Component c = (Component) e.getSource();
258                                if (c.hasFocus()) {
259                                        selectAll(false);
260                                }
261                        }
262                });
263
264                btnDeselectAll2.addActionListener(new ActionListener() {
265                        public void actionPerformed(ActionEvent e) {
266                                Component c = (Component) e.getSource();
267                                if (c.hasFocus()) {
268                                        selectAll(false);
269                                }
270                        }
271                });
272
273        } // end buildPanel
274
275        private void selectAll(boolean bSelect) {
276                for (int i = 0; i < checkboxes.length; i++) {
277                        checkboxes[i].setSelected(bSelect);
278                }
279        }
280
281}