001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.cleverphl.gui;
025:
026: import jacareto.cleverphl.CleverPHL;
027: import jacareto.cleverphl.session.SessionList;
028: import jacareto.cleverphl.session.SessionType;
029: import jacareto.system.Language;
030:
031: import java.awt.BorderLayout;
032: import java.awt.GridBagConstraints;
033: import java.awt.GridBagLayout;
034: import java.awt.Insets;
035: import java.awt.Point;
036: import java.awt.event.ActionEvent;
037: import java.awt.event.ActionListener;
038: import java.awt.event.KeyEvent;
039: import java.awt.event.KeyListener;
040:
041: import java.io.File;
042: import java.io.IOException;
043:
044: import java.util.Hashtable;
045: import java.util.Iterator;
046:
047: import javax.swing.JButton;
048: import javax.swing.JComboBox;
049: import javax.swing.JDialog;
050: import javax.swing.JLabel;
051: import javax.swing.JPanel;
052: import javax.swing.JTextField;
053: import javax.swing.border.EmptyBorder;
054:
055: /**
056: * Dialog for the creation of a new session.
057: *
058: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
059: * @version 1.0
060: */
061: public class NewSessionDialog extends JDialog implements
062: ActionListener, KeyListener {
063: /** The CleverPHL instance. */
064: private CleverPHL cleverPHL;
065:
066: /** The ok button. */
067: private JButton okButton;
068:
069: /** The cancel button. */
070: private JButton cancelButton;
071:
072: /** The text field of the sessions name. */
073: private JTextField nameField;
074:
075: /** The combo box for the type. */
076: private JComboBox typeBox;
077:
078: /** Whether the ok button has been pressed or the cancel button. */
079: private boolean pressedOK;
080:
081: /** A hashtable which maps type names to types. */
082: private Hashtable namesToTypes;
083:
084: /**
085: * Creates a new dialog (visible).
086: *
087: * @param cleverPHL the CleverPHL instance
088: */
089: public NewSessionDialog(CleverPHL cleverPHL) {
090: super (cleverPHL.getMainFrame(), true);
091: this .cleverPHL = cleverPHL;
092: setName(cleverPHL.getCustomization().getString(
093: "Components.JacaretoComponent", "JACARETO_COMPONENT"));
094:
095: Language language = cleverPHL.getLanguage();
096: File sessionsDir = cleverPHL.getFiles().getDir(
097: "CLEVERPHL_SESSIONS_DIR");
098: SessionList sessionList = cleverPHL.getSessionList();
099:
100: pressedOK = false;
101:
102: //setLocationRelativeTo ( cleverPHL.getMainFrame() );
103: setTitle(language.getString("CleverPHL.NewSessionDialog.Title"));
104:
105: // The panel with the text fields
106: JPanel dataPanel = new JPanel();
107: dataPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
108: dataPanel.setLayout(new GridBagLayout());
109:
110: GridBagConstraints c = new GridBagConstraints();
111: c.gridx = 0;
112: c.gridy = 0;
113: c.weightx = 40;
114: c.weighty = 50;
115: c.ipadx = 5;
116: c.anchor = GridBagConstraints.WEST;
117: c.insets = new Insets(5, 5, 5, 5);
118:
119: JLabel nameFieldLabel = new JLabel(language
120: .getString("CleverPHL.NewSessionDialog.Name"));
121: dataPanel.add(nameFieldLabel, c);
122: c.gridx = 1;
123: c.weightx = 60;
124: nameField = new JTextField(20);
125:
126: String sessionName = "";
127: String sessionDir = "";
128:
129: try {
130: for (int i = 0;; i++) {
131: sessionName = language
132: .getString("CleverPHL.NewSessionDialog.InitialPrefix")
133: + i;
134: sessionDir = sessionsDir.getCanonicalPath()
135: + File.separator + sessionName;
136:
137: if (!(new File(sessionDir)).exists()
138: && !sessionList.contains(sessionName)) {
139: break;
140: }
141: }
142: } catch (IOException i) {
143: sessionName = "DefaultSession";
144:
145: // dir?
146: }
147:
148: nameField.setText(sessionName);
149: nameField.selectAll();
150: nameField.addKeyListener(this );
151: dataPanel.add(nameField, c);
152: nameFieldLabel.setLabelFor(nameField);
153:
154: // the combo box for the session type
155: c.gridx = 0;
156: c.gridy = 1;
157:
158: JLabel typeBoxLabel = new JLabel(language
159: .getString("CleverPHL.NewSessionDialog.Type"));
160: dataPanel.add(typeBoxLabel, c);
161: c.weightx = 40;
162: c.gridx = 1;
163: c.weightx = 60;
164: typeBox = new JComboBox();
165:
166: Iterator it = SessionType.types();
167: namesToTypes = new Hashtable();
168:
169: while (it.hasNext()) {
170: SessionType type = (SessionType) it.next();
171: String typeName = language
172: .getString("CleverPHL.SessionType." + type.getID());
173: typeBox.addItem(typeName);
174: namesToTypes.put(typeName, type);
175: }
176:
177: dataPanel.add(typeBox, c);
178: typeBoxLabel.setLabelFor(typeBox);
179:
180: // The panel with the two buttons
181: JPanel buttonPanel = new JPanel();
182: okButton = new JButton(language.getString("General.Ok"));
183: okButton.addActionListener(this );
184: cancelButton = new JButton(language.getString("General.Cancel"));
185: cancelButton.addActionListener(this );
186: buttonPanel.add(okButton);
187: buttonPanel.add(cancelButton);
188: buttonPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
189:
190: // Add all to the content pane
191: getContentPane().setLayout(new BorderLayout());
192: getContentPane().add(dataPanel, BorderLayout.NORTH);
193: getContentPane().add(buttonPanel, BorderLayout.CENTER);
194:
195: Point ownerLoc = cleverPHL.getMainFrame().getLocation();
196: setLocation(((int) ownerLoc.getX()) + 30, ((int) ownerLoc
197: .getY()) + 30);
198:
199: //Does not work!
200: //okButton.requestFocus();
201: pack();
202: setResizable(false);
203: setVisible(true);
204: }
205:
206: /**
207: * Called when one of the two buttons has been pressed.
208: *
209: * @param e the action event
210: */
211: public void actionPerformed(ActionEvent e) {
212: if (e.getSource() == okButton) {
213: evaluateEntry();
214: } else if (e.getSource() == cancelButton) {
215: pressedOK = false;
216: setVisible(false);
217: }
218: }
219:
220: public void keyPressed(KeyEvent event) {
221: }
222:
223: public void keyTyped(KeyEvent event) {
224: }
225:
226: public void keyReleased(KeyEvent event) {
227: if (event.getKeyCode() == KeyEvent.VK_ENTER) {
228: evaluateEntry();
229: }
230: }
231:
232: private void evaluateEntry() {
233: Language language = cleverPHL.getLanguage();
234: String name = nameField.getText();
235:
236: if (name.equals("")) {
237: new CleverPHLMsgDialog(
238: cleverPHL,
239: this ,
240: language.getString("General.Error"),
241: language
242: .getString("CleverPHL.NewSessionDialog.Error.NoName"),
243: language.getString("General.Ok"));
244: } else {
245: try {
246: String sessionDir = cleverPHL.getFiles().getDir(
247: "CLEVERPHL_SESSIONS_DIR").getCanonicalPath()
248: + File.separator + name;
249:
250: if ((new File(sessionDir)).exists()
251: || cleverPHL.getSessionList().contains(name)) {
252: new CleverPHLMsgDialog(
253: cleverPHL,
254: this ,
255: language.getString("General.Error"),
256: language
257: .getString("CleverPHL.NewSessionDialog.Error.NameTwice"),
258: language.getString("General.Ok"));
259:
260: return;
261: }
262: } catch (IOException i) {
263: ;
264: }
265:
266: pressedOK = true;
267: setVisible(false);
268: }
269: }
270:
271: /**
272: * Returns whether the ok button has been pressed to close the dialog or the cancel button
273: *
274: * @return <code>true</code> if the ok button has been pressed, <code>false</code> if the
275: * cancel button has been pressed.
276: */
277: public boolean pressedOK() {
278: return pressedOK;
279: }
280:
281: /**
282: * Returns the content of the name field.
283: *
284: * @return the sessions name, or <code>null</code> if the cancel button has been pressed.
285: */
286: public String getSessionName() {
287: if (pressedOK()) {
288: return nameField.getText();
289: }
290:
291: return null;
292: }
293:
294: /**
295: * Returns the session type.
296: *
297: * @return DOCUMENT ME!
298: */
299: public SessionType getSessionType() {
300: if (pressedOK()) {
301: return (SessionType) namesToTypes.get(typeBox
302: .getSelectedItem());
303: }
304:
305: return null;
306: }
307: }
|