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.system.Language;
029:
030: import java.awt.BorderLayout;
031: import java.awt.GridBagConstraints;
032: import java.awt.GridBagLayout;
033: import java.awt.Point;
034: import java.awt.event.ActionEvent;
035: import java.awt.event.ActionListener;
036: import java.awt.event.KeyEvent;
037: import java.awt.event.KeyListener;
038:
039: import java.io.File;
040: import java.io.IOException;
041:
042: import javax.swing.JButton;
043: import javax.swing.JDialog;
044: import javax.swing.JLabel;
045: import javax.swing.JPanel;
046: import javax.swing.JTextField;
047: import javax.swing.border.EmptyBorder;
048:
049: /**
050: * Dialog for saving a session with a new name.
051: *
052: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
053: * @version 1.0
054: */
055: public class SaveAsSessionDialog extends JDialog implements
056: ActionListener, KeyListener {
057: /** The CleverPHL instance. */
058: private CleverPHL cleverPHL;
059:
060: /** The ok button. */
061: private JButton okButton;
062:
063: /** The cancel button. */
064: private JButton cancelButton;
065:
066: /** The text field of the sessions name. */
067: private JTextField nameField;
068:
069: /** Whether the ok button has been pressed or the cancel button. */
070: private boolean pressedOK;
071:
072: /**
073: * Creates a new dialog (visible).
074: *
075: * @param cleverPHL the CleverPHL instance
076: */
077: public SaveAsSessionDialog(CleverPHL cleverPHL) {
078: super (cleverPHL.getMainFrame(), true);
079: this .cleverPHL = cleverPHL;
080: setName(cleverPHL.getCustomization().getString(
081: "Components.JacaretoComponent", "JACARETO_COMPONENT"));
082:
083: Language language = cleverPHL.getLanguage();
084: File sessionsDir = cleverPHL.getFiles().getDir(
085: "CLEVERPHL_SESSIONS_DIR");
086: SessionList sessionList = cleverPHL.getSessionList();
087:
088: pressedOK = false;
089:
090: //setLocationRelativeTo ( cleverPHL.getMainFrame() );
091: setTitle(language
092: .getString("CleverPHL.SaveAsSessionDialog.Title"));
093:
094: // The panel with the text fields
095: JPanel textFieldPanel = new JPanel();
096: textFieldPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
097: textFieldPanel.setLayout(new GridBagLayout());
098:
099: GridBagConstraints c = new GridBagConstraints();
100: c.gridx = 0;
101: c.gridy = 0;
102: c.weightx = 40;
103: c.ipadx = 5;
104: c.anchor = GridBagConstraints.WEST;
105:
106: //c.insets = new Insets (5,5,5,5);
107: JLabel nameFieldLabel = new JLabel(language
108: .getString("CleverPHL.SaveAsSessionDialog.Name"));
109: textFieldPanel.add(nameFieldLabel, c);
110: c.gridx = 1;
111: c.weightx = 60;
112: nameField = new JTextField(20);
113:
114: String sessionName = "";
115: String sessionDir = "";
116:
117: try {
118: for (int i = 0;; i++) {
119: sessionName = language
120: .getString("CleverPHL.SaveAsSessionDialog.InitialPrefix")
121: + i;
122: sessionDir = sessionsDir.getCanonicalPath()
123: + File.separator + sessionName;
124:
125: if (!(new File(sessionDir)).exists()
126: && !sessionList.contains(sessionName)) {
127: break;
128: }
129: }
130: } catch (IOException i) {
131: sessionName = "DefaultSession";
132:
133: // dir?
134: }
135:
136: nameField.setText(sessionName);
137: nameField.selectAll();
138: nameField.addKeyListener(this );
139:
140: textFieldPanel.add(nameField, c);
141: nameFieldLabel.setLabelFor(nameField);
142:
143: // The panel with the two buttons
144: JPanel buttonPanel = new JPanel();
145: okButton = new JButton(language.getString("General.Ok"));
146: okButton.addActionListener(this );
147: cancelButton = new JButton(language.getString("General.Cancel"));
148: cancelButton.addActionListener(this );
149: buttonPanel.add(okButton);
150: buttonPanel.add(cancelButton);
151: buttonPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
152:
153: // Add all to the content pane
154: getContentPane().setLayout(new BorderLayout());
155: getContentPane().add(textFieldPanel, BorderLayout.NORTH);
156: getContentPane().add(buttonPanel, BorderLayout.CENTER);
157:
158: Point ownerLoc = cleverPHL.getMainFrame().getLocation();
159: setLocation(((int) ownerLoc.getX()) + 30, ((int) ownerLoc
160: .getY()) + 30);
161:
162: //Does not work!
163: //okButton.requestFocus();
164: pack();
165: setResizable(false);
166: setVisible(true);
167: }
168:
169: /**
170: * Called when one of the two buttons has been pressed.
171: *
172: * @param e the action event
173: */
174: public void actionPerformed(ActionEvent e) {
175: if (e.getSource() == okButton) {
176: evaluateEntry();
177: } else if (e.getSource() == cancelButton) {
178: pressedOK = false;
179: setVisible(false);
180: }
181: }
182:
183: /**
184: * {@inheritDoc}
185: */
186: public void keyPressed(KeyEvent event) {
187: }
188:
189: /**
190: * {@inheritDoc}
191: */
192: public void keyTyped(KeyEvent event) {
193: }
194:
195: /**
196: * {@inheritDoc}
197: */
198: public void keyReleased(KeyEvent event) {
199: if (event.getKeyCode() == KeyEvent.VK_ENTER) {
200: evaluateEntry();
201: }
202: }
203:
204: private void evaluateEntry() {
205: Language language = cleverPHL.getLanguage();
206: String name = nameField.getText();
207:
208: if (name.equals("")) {
209: new CleverPHLMsgDialog(
210: cleverPHL,
211: this ,
212: language.getString("General.Error"),
213: language
214: .getString("CleverPHL.SaveAsSessionDialog.Error.NoName"),
215: language.getString("General.Ok"));
216: } else {
217: try {
218: String sessionDir = cleverPHL.getFiles().getDir(
219: "CLEVERPHL_SESSIONS_DIR").getCanonicalPath()
220: + File.separator + name;
221:
222: if ((new File(sessionDir)).exists()
223: || cleverPHL.getSessionList().contains(name)) {
224: new CleverPHLMsgDialog(
225: cleverPHL,
226: this ,
227: language.getString("General.Error"),
228: language
229: .getString("CleverPHL.SaveAsSessionDialog.Error.NameTwice"),
230: language.getString("General.Ok"));
231:
232: return;
233: }
234: } catch (IOException i) {
235: ;
236: }
237:
238: pressedOK = true;
239: setVisible(false);
240: }
241: }
242:
243: /**
244: * Returns whether the ok button has been pressed to close the dialog or the cancel button
245: *
246: * @return <code>true</code> if the ok button has been pressed, <code>false</code> if the
247: * cancel button has been pressed.
248: */
249: public boolean pressedOK() {
250: return pressedOK;
251: }
252:
253: /**
254: * Returns the content of the name field.
255: *
256: * @return the sessions name, or <code>null</code> if the cancel button has been pressed.
257: */
258: public String getSessionName() {
259: if (pressedOK()) {
260: return nameField.getText();
261: }
262:
263: return null;
264: }
265: }
|