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 NewActorFrame 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 actorNameLabel;
063        private JLabel classNameLabel;
064        private JLabel introLabel1;
065        private JLabel introLabel2;
066        private JLabel introLabel3;
067        private JLabel introLabel4;
068        private JTextField actorNameTextField;
069        private JTextField classNameTextField;
070
071        private Vector listeners = new Vector();
072
073        /**
074         * Construct the editor dialog with the given Step.
075         * 
076         * @param parent
077         *            the parent frame for this dialog
078         */
079        public NewActorFrame(Component parent) {
080                super((java.awt.Frame) parent, "New Actor", true);
081                this.parent = parent;
082                init();
083                setVisible(false);
084        }
085
086        private void init() {
087                this.setName("New Actor");
088                actorNameLabel = new JLabel("Actor name");
089                classNameLabel = new JLabel("Class name");
090                introLabel1 = new JLabel(
091                                "<html><body><p>Enter the name of the actor that you "
092                                                + "would like to add.  Then enter the Java "
093                                                + "class name of the actor.  This class must "
094                                                + "be in the classpath before adding the actor.</p></body></html>");
095                okButton = new JButton("OK");
096                cancelButton = new JButton("Cancel");
097                actorNameTextField = new JTextField();
098                classNameTextField = new JTextField();
099
100                ActionHandler ahandler = new ActionHandler();
101                cancelButton.addActionListener(ahandler);
102                okButton.addActionListener(ahandler);
103
104                Container c = getContentPane();
105                c.setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
106                c.add(Box.createRigidArea(new Dimension(WIDE_SPACE, PAD)));
107
108                JPanel horizPanel = new JPanel();
109                horizPanel.setLayout(new BoxLayout(horizPanel, BoxLayout.Y_AXIS));
110
111                JPanel labelPanel = new JPanel();
112                // labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.X_AXIS));
113                // labelPanel.add(Box.createRigidArea(new Dimension(HORIZONTAL, PAD)));
114                // labelPanel.setPreferredSize(new Dimension(100, 20));
115                // labelPanel.add(Box.createRigidArea(new Dimension(HORIZONTAL, PAD)));
116                introLabel1.setPreferredSize(new Dimension(300, 80));
117                // labelPanel.add(Box.createRigidArea(new Dimension(HORIZONTAL, PAD)));
118                labelPanel.add(introLabel1);
119                // labelPanel.add(introLabel2);
120                // labelPanel.add(introLabel3);
121                // labelPanel.add(introLabel4);
122                // labelPanel.add(Box.createRigidArea(new Dimension(HORIZONTAL, PAD)));
123
124                JPanel actorNamePanel = new JPanel();
125                actorNameTextField.setColumns(15);
126                actorNamePanel
127                                .setLayout(new BoxLayout(actorNamePanel, BoxLayout.X_AXIS));
128                actorNamePanel.add(Box.createRigidArea(new Dimension(10, SPACE)));
129                actorNamePanel.add(actorNameLabel);
130                actorNamePanel.add(Box.createRigidArea(new Dimension(10, SPACE)));
131                actorNamePanel.add(actorNameTextField);
132                actorNamePanel.add(Box.createRigidArea(new Dimension(10, SPACE)));
133
134                JPanel classNamePanel = new JPanel();
135                classNameTextField.setColumns(15);
136                classNamePanel
137                                .setLayout(new BoxLayout(classNamePanel, BoxLayout.X_AXIS));
138                classNamePanel.add(Box.createRigidArea(new Dimension(10, SPACE)));
139                classNamePanel.add(classNameLabel);
140                classNamePanel.add(Box.createRigidArea(new Dimension(10, SPACE)));
141                classNamePanel.add(classNameTextField);
142                classNamePanel.add(Box.createRigidArea(new Dimension(10, SPACE)));
143
144                // horizPanel.add(labelPanel);
145                horizPanel.add(Box.createRigidArea(new Dimension(10, SPACE)));
146                horizPanel.add(actorNamePanel);
147                horizPanel.add(Box.createRigidArea(new Dimension(10, SPACE)));
148                horizPanel.add(classNamePanel);
149
150                JPanel buttonPanel = new JPanel();
151                buttonPanel.add(okButton);
152                buttonPanel.add(cancelButton);
153
154                c.add(labelPanel);
155                c.add(horizPanel);
156                c.add(Box.createRigidArea(new Dimension(20, SPACE)));
157                c.add(buttonPanel);
158
159                // put the window in the middle of its parent
160                if (parent != null) {
161                        int x = parent.getX();
162                        int y = parent.getY();
163                        int width = parent.getWidth();
164                        int height = parent.getHeight();
165                        setLocation(x + ((width / 2) - (this.getWidth() / 2)), y
166                                        + ((height / 2) - (this.getHeight() / 2)));
167                }
168
169                pack();
170        }
171
172        /**
173         * add an action listener that will be notified when the ok or cancel button
174         * is clicked.
175         */
176        public void addActionListener(ActionListener listener) {
177                listeners.addElement(listener);
178        }
179
180        /**
181         * get the folder name from the user input. don't call this until the
182         * actionlistener has been fired.
183         */
184        public String getActorName() {
185                return actorNameTextField.getText();
186        }
187
188        /**
189         * get the class name from the user input. don't call this until the
190         * actionlistener has been fired.
191         */
192        public String getClassName() {
193                return classNameTextField.getText();
194        }
195
196        /**
197         * get the concept name from the user input. don't call this until the
198         * actionlistener has been fired.
199         */
200        public String getConceptName() {
201                return normalizeConceptName();
202        }
203
204        /**
205         * handle ok button events
206         */
207        private void okButtonHandler(ActionEvent event) {
208                for (int i = 0; i < listeners.size(); i++) {
209                        ActionListener listener = (ActionListener) listeners.elementAt(i);
210                        listener.actionPerformed(new ActionEvent(this, 1,
211                                        "okbutton_clicked"));
212                }
213        }
214
215        /**
216         * handle cancle button events
217         */
218        private void cancelButtonHandler(ActionEvent event) {
219                for (int i = 0; i < listeners.size(); i++) {
220                        ActionListener listener = (ActionListener) listeners.elementAt(i);
221                        listener.actionPerformed(new ActionEvent(this, 2,
222                                        "cancelbutton_clicked"));
223                }
224        }
225
226        /**
227         * normalize the foldername for the concept name
228         */
229        private String normalizeConceptName() {
230                // take the spaces out of the folder name and put it as the
231                // concept name
232                return actorNameTextField.getText().replaceAll("\\s", "");
233        }
234
235        /**
236         * Listener used to detect button presses
237         */
238        private class ActionHandler implements ActionListener {
239                /**
240                 * Description of the Method
241                 * 
242                 * @param event
243                 *            Description of Parameter
244                 */
245                public void actionPerformed(ActionEvent event) {
246                        Object object = event.getSource();
247                        if (object == okButton) {
248                                okButtonHandler(event);
249                        } else if (object == cancelButton) {
250                                cancelButtonHandler(event);
251                        }
252                }
253        }
254}