001/*
002 * Copyright (c) 2003-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.kepler.gui;
031
032import java.awt.Component;
033import java.awt.Container;
034import java.awt.Dimension;
035import java.awt.event.ActionEvent;
036import java.awt.event.ActionListener;
037import java.util.Vector;
038
039import javax.swing.Box;
040import javax.swing.BoxLayout;
041import javax.swing.JButton;
042import javax.swing.JDialog;
043import javax.swing.JLabel;
044import javax.swing.JPanel;
045import javax.swing.JTextField;
046
047/**
048 * A component for editing Step information.
049 */
050public class NewFolderFrame extends JDialog {
051        // CONSTANTS
052        private final static int PAD = 5;
053        private final static int HORIZONTAL = 200;
054        private final static int SPACE = 3;
055        private final static int WIDE_SPACE = 10;
056        private final static int COLUMNS = 25;
057        private final static int ROWS = 4;
058
059        private Component parent;
060        private JButton cancelButton;
061        private JButton okButton;
062        private JLabel folderNameLabel;
063        private JLabel introLabel;
064        private JTextField folderNameTextField;
065
066        private Vector listeners = new Vector();
067
068        /**
069         * Construct the editor dialog with the given Step.
070         * 
071         * @param parent
072         *            the parent frame for this dialog
073         */
074        public NewFolderFrame(Component parent) {
075                super((java.awt.Frame) parent, "New Folder", true);
076                this.parent = parent;
077                init();
078                setVisible(false);
079        }
080
081        private void init() {
082                this.setName("New Folder");
083                folderNameLabel = new JLabel("Folder name");
084                introLabel = new JLabel("Enter the name of the new folder that you "
085                                + "would like to add.");
086                okButton = new JButton("OK");
087                cancelButton = new JButton("Cancel");
088                folderNameTextField = new JTextField();
089
090                ActionHandler ahandler = new ActionHandler();
091                cancelButton.addActionListener(ahandler);
092                okButton.addActionListener(ahandler);
093
094                Container c = getContentPane();
095                c.setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
096
097                c.add(Box.createRigidArea(new Dimension(WIDE_SPACE, PAD)));
098
099                // c.add(introLabel);
100
101                JPanel labelPanel = new JPanel();
102                labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.Y_AXIS));
103                labelPanel.add(Box.createRigidArea(new Dimension(HORIZONTAL, PAD)));
104
105                JPanel folderNamePanel = new JPanel();
106                folderNamePanel.setLayout(new BoxLayout(folderNamePanel,
107                                BoxLayout.X_AXIS));
108                folderNamePanel.add(Box.createRigidArea(new Dimension(20, SPACE)));
109                folderNamePanel.add(folderNameLabel);
110                folderNamePanel.add(Box.createRigidArea(new Dimension(10, SPACE)));
111                folderNameTextField.setColumns(15);
112                folderNamePanel.add(folderNameTextField);
113                folderNamePanel.add(Box.createRigidArea(new Dimension(20, SPACE)));
114                labelPanel.add(folderNamePanel);
115
116                JPanel buttonPanel = new JPanel();
117                buttonPanel.add(okButton);
118                buttonPanel.add(cancelButton);
119
120                c.add(labelPanel);
121                c.add(Box.createRigidArea(new Dimension(20, SPACE)));
122                c.add(buttonPanel);
123
124                // put the window in the middle of its parent
125                if (parent != null) {
126                        int x = parent.getX();
127                        int y = parent.getY();
128                        int width = parent.getWidth();
129                        int height = parent.getHeight();
130                        setLocation(x + ((width / 2) - (this.getWidth() / 2)), y
131                                        + ((height / 2) - (this.getHeight() / 2)));
132                }
133
134                pack();
135        }
136
137        /**
138         * add an action listener that will be notified when the ok or cancel button
139         * is clicked.
140         */
141        public void addActionListener(ActionListener listener) {
142                listeners.addElement(listener);
143        }
144
145        /**
146         * get the folder name from the user input. don't call this until the
147         * actionlistener has been fired.
148         */
149        public String getFolderName() {
150                return folderNameTextField.getText();
151        }
152
153        /**
154         * get the concept name from the user input. don't call this until the
155         * actionlistener has been fired.
156         */
157        public String getConceptName() {
158                return normalizeConceptName();
159        }
160
161        /**
162         * handle ok button events
163         */
164        private void okButtonHandler(ActionEvent event) {
165                for (int i = 0; i < listeners.size(); i++) {
166                        ActionListener listener = (ActionListener) listeners.elementAt(i);
167                        listener.actionPerformed(new ActionEvent(this, 1,
168                                        "okbutton_clicked"));
169                }
170        }
171
172        /**
173         * handle cancle button events
174         */
175        private void cancelButtonHandler(ActionEvent event) {
176                for (int i = 0; i < listeners.size(); i++) {
177                        ActionListener listener = (ActionListener) listeners.elementAt(i);
178                        listener.actionPerformed(new ActionEvent(this, 2,
179                                        "cancelbutton_clicked"));
180                }
181        }
182
183        /**
184         * normalize the foldername for the concept name
185         */
186        private String normalizeConceptName() {
187                // take the spaces out of the folder name and put it as the
188                // concept name
189                return folderNameTextField.getText().replaceAll("\\s", "");
190        }
191
192        /**
193         * Listener used to detect button presses
194         */
195        private class ActionHandler implements ActionListener {
196                /**
197                 * Description of the Method
198                 * 
199                 * @param event
200                 *            Description of Parameter
201                 */
202                public void actionPerformed(ActionEvent event) {
203                        Object object = event.getSource();
204                        if (object == okButton) {
205                                okButtonHandler(event);
206                        } else if (object == cancelButton) {
207                                cancelButtonHandler(event);
208                        }
209                }
210        }
211}