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.Environment;
029: import jacareto.system.Language;
030: import jacareto.toolkit.io.FileUtilities;
031:
032: import java.awt.BorderLayout;
033: import java.awt.Color;
034: import java.awt.Point;
035: import java.awt.event.ActionEvent;
036: import java.awt.event.ActionListener;
037: import java.awt.event.KeyEvent;
038: import java.awt.event.KeyListener;
039: import java.awt.event.MouseEvent;
040: import java.awt.event.MouseListener;
041:
042: import java.io.File;
043: import java.io.FileFilter;
044: import java.io.IOException;
045:
046: import java.util.Arrays;
047: import java.util.Vector;
048:
049: import javax.swing.JButton;
050: import javax.swing.JDialog;
051: import javax.swing.JList;
052: import javax.swing.JPanel;
053: import javax.swing.JScrollPane;
054: import javax.swing.border.EmptyBorder;
055: import javax.swing.border.LineBorder;
056: import javax.swing.border.TitledBorder;
057:
058: /**
059: * Dialog for loading sessions.
060: *
061: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
062: * @version 1.01
063: */
064: public class LoadSessionDialog extends JDialog implements
065: ActionListener, KeyListener {
066: /** The CleverPHL instance. */
067: private CleverPHL cleverPHL;
068:
069: /** The environment. */
070: private Environment env;
071:
072: /** The list of starters. */
073: private JList list;
074:
075: /** The name of the sessions which have been selected. */
076: private String[] selectedSessions;
077:
078: /** The ok button. */
079: private JButton okButton;
080:
081: /** The cancel button. */
082: private JButton cancelButton;
083:
084: /** The directory names. */
085: private Vector dirNames;
086:
087: /**
088: * Creates a new dialog (visible).
089: *
090: * @param cleverPHL the CleverPHL instance
091: */
092: public LoadSessionDialog(CleverPHL cleverPHL) {
093: super (cleverPHL.getMainFrame(), true);
094: this .cleverPHL = cleverPHL;
095: this .env = cleverPHL.getEnvironment();
096: selectedSessions = null;
097: setName(cleverPHL.getCustomization().getString(
098: "Components.JacaretoComponent", "JACARETO_COMPONENT"));
099:
100: Language language = env.getLanguage();
101:
102: setTitle(language
103: .getString("CleverPHL.LoadSessionDialog.Title"));
104:
105: // The session list
106: File sessionDir = cleverPHL.getFiles().getDir(
107: "CLEVERPHL_SESSIONS_DIR");
108: SessionList sessionList = cleverPHL.getSessionList();
109: File[] dirs = sessionDir.listFiles(new FileFilter() {
110: public boolean accept(File file) {
111: return file.isDirectory();
112: }
113: });
114:
115: dirNames = new Vector(dirs.length);
116:
117: for (int i = 0; i < dirs.length; i++) {
118: String sessionName = dirs[i].getName();
119:
120: if (!sessionList.contains(sessionName)) {
121: dirNames.add(sessionName);
122: }
123: }
124:
125: list = new JList(dirNames);
126: list.addKeyListener(this );
127:
128: //list.setSelectionMode ( DefaultListSelectionModel.SINGLE_SELECTION );
129: TitledBorder listBorder = new TitledBorder(new LineBorder(
130: Color.black), language
131: .getString("CleverPHL.LoadSessionDialog.ListLabel"));
132:
133: //list.setFixedCellWidth ((int) listBorder.getMinimumSize(this).getWidth());
134: list.setFixedCellWidth(200);
135: list.setVisibleRowCount(10);
136: list.addMouseListener(new ListMouseListener(this ));
137:
138: JScrollPane listScrollPane = new JScrollPane(list);
139: listScrollPane.setBorder(listBorder);
140:
141: JPanel leftPanelWrapper = new JPanel();
142: leftPanelWrapper.setLayout(new BorderLayout());
143: leftPanelWrapper.setBorder(new EmptyBorder(5, 5, 5, 5));
144: leftPanelWrapper.add(listScrollPane, BorderLayout.NORTH);
145:
146: // The panel with the two buttons
147: JPanel buttonPanel = new JPanel();
148: okButton = new JButton(language.getString("General.Ok"));
149: okButton.addActionListener(this );
150: cancelButton = new JButton(language.getString("General.Cancel"));
151: cancelButton.addActionListener(this );
152: buttonPanel.add(okButton);
153: buttonPanel.add(cancelButton);
154: buttonPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
155:
156: // Add all to the content pane
157: getContentPane().setLayout(new BorderLayout());
158: getContentPane().add(leftPanelWrapper, BorderLayout.CENTER);
159: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
160:
161: Point ownerLoc = cleverPHL.getMainFrame().getLocation();
162: setLocation(((int) ownerLoc.getX()) + 30, ((int) ownerLoc
163: .getY()) + 30);
164:
165: pack();
166: setResizable(false);
167: setVisible(true);
168: }
169:
170: /**
171: * Called when one of the two buttons has been pressed.
172: *
173: * @param e the action event
174: */
175: public void actionPerformed(ActionEvent e) {
176: Language language = cleverPHL.getLanguage();
177:
178: Object source = e.getSource();
179:
180: if (source == okButton) {
181: if (list.isSelectionEmpty()) {
182: new CleverPHLMsgDialog(
183: cleverPHL,
184: this ,
185: language.getString("General.Error"),
186: language
187: .getString("CleverPHL.LoadSessionDialog.Error.NoSelectedSession"),
188: language.getString("General.Ok"));
189: } else {
190: close();
191: }
192: } else if (source == cancelButton) {
193: dispose();
194: }
195: }
196:
197: public void keyPressed(KeyEvent event) {
198: int keyCode = event.getKeyCode();
199: Language language = cleverPHL.getLanguage();
200:
201: switch (keyCode) {
202: case KeyEvent.VK_DELETE:
203:
204: CleverPHLConfirmDialog confirmDialog = new CleverPHLConfirmDialog(
205: cleverPHL,
206: this ,
207: language
208: .getString("CleverPHL.LoadSessionDialog.DeleteTitle"),
209: language
210: .getString("CleverPHL.LoadSessionDialog.Msg.ReallyDelete"),
211: language.getString("General.Yes"), language
212: .getString("General.No"));
213:
214: if (confirmDialog.getValue() == CleverPHLConfirmDialog.YES) {
215: deleteSelected();
216: }
217:
218: break;
219: }
220: }
221:
222: public void keyReleased(KeyEvent event) {
223: }
224:
225: public void keyTyped(KeyEvent event) {
226: }
227:
228: /**
229: * Closes this dialog and stores the selected sessions for later retrieval.
230: */
231: private void close() {
232: if (list.isSelectionEmpty()) {
233: Language language = cleverPHL.getLanguage();
234: new CleverPHLMsgDialog(
235: cleverPHL,
236: this ,
237: language.getString("General.Error"),
238: language
239: .getString("CleverPHL.LoadSessionDialog.Error.NoSelectedSession"),
240: language.getString("General.Ok"));
241: } else {
242: setVisible(false);
243:
244: Object[] selectedValues = list.getSelectedValues();
245: selectedSessions = new String[selectedValues.length];
246:
247: for (int i = 0; i < selectedSessions.length; i++) {
248: selectedSessions[i] = (String) selectedValues[i];
249: }
250:
251: dispose();
252: }
253: }
254:
255: /**
256: * Deletes the selected sessions.
257: */
258: private void deleteSelected() {
259: // get the selected indices in the list
260: int[] indices = list.getSelectedIndices();
261: Arrays.sort(indices);
262:
263: // remove the directories with the given indices (from the last index to the first,
264: // otherwise the mapping between indices and directories to be deleted would change)
265: for (int i = indices.length - 1; i >= 0; i--) {
266: try {
267: String dirName = env.getFiles().getDir(
268: "CLEVERPHL_SESSIONS_DIR").getCanonicalPath()
269: + File.separator
270: + (String) dirNames.get(indices[i]);
271: FileUtilities.deleteDirectory(new File(dirName));
272: dirNames.remove(indices[i]);
273: } catch (IOException ioe) {
274: env.getLogger().error(ioe);
275: }
276: }
277:
278: // update the list view
279: list.clearSelection();
280: list.repaint();
281: }
282:
283: /**
284: * Returns the name of the selected sessiona after this dialog has been closed, or
285: * <code>null</code> if no session has been selected.
286: *
287: * @return DOCUMENT ME!
288: */
289: public String[] getSelectedSessions() {
290: return selectedSessions;
291: }
292:
293: /**
294: * Listener for the double click on the list elements.
295: */
296: static class ListMouseListener implements MouseListener {
297: //~ Instance fields ------------------------------------------------------------------------
298:
299: /** The dialog this listener belongs to. */
300: private LoadSessionDialog dialog;
301:
302: //~ Constructors ---------------------------------------------------------------------------
303:
304: /**
305: * Creates a new listener object.
306: *
307: * @param dialog DOCUMENT ME!
308: */
309: public ListMouseListener(LoadSessionDialog dialog) {
310: this .dialog = dialog;
311: }
312:
313: //~ Methods --------------------------------------------------------------------------------
314:
315: /**
316: * Calls the <code>notifyDoubleClick</code> method of the starter frame when the mouse has
317: * been double clicked.
318: *
319: * @param e the mouse event.
320: */
321: public void mouseClicked(MouseEvent e) {
322: if (e.getClickCount() == 2) {
323: dialog.close();
324: }
325: }
326:
327: public void mouseEntered(MouseEvent e) {
328: }
329:
330: public void mouseExited(MouseEvent e) {
331: }
332:
333: public void mousePressed(MouseEvent e) {
334: }
335:
336: public void mouseReleased(MouseEvent e) {
337: }
338: }
339: }
|