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.Session;
028: import jacareto.filter.Filter;
029: import jacareto.filter.FilterList;
030: import jacareto.system.Customization;
031: import jacareto.system.Language;
032: import jacareto.toolkit.swing.ExtensionFileFilter;
033:
034: import java.awt.BorderLayout;
035: import java.awt.Color;
036: import java.awt.FlowLayout;
037: import java.awt.GridLayout;
038: import java.awt.Point;
039: import java.awt.event.ActionEvent;
040: import java.awt.event.ActionListener;
041: import java.awt.event.MouseEvent;
042: import java.awt.event.MouseListener;
043:
044: import java.io.File;
045: import java.io.IOException;
046:
047: import java.util.Vector;
048:
049: import javax.swing.ButtonGroup;
050: import javax.swing.DefaultListSelectionModel;
051: import javax.swing.JButton;
052: import javax.swing.JCheckBox;
053: import javax.swing.JDialog;
054: import javax.swing.JFileChooser;
055: import javax.swing.JList;
056: import javax.swing.JPanel;
057: import javax.swing.JRadioButton;
058: import javax.swing.JScrollPane;
059: import javax.swing.border.EmptyBorder;
060: import javax.swing.border.LineBorder;
061: import javax.swing.border.TitledBorder;
062:
063: /**
064: * Dialog for creating a new data set.
065: *
066: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
067: * @version 1.01
068: */
069: public class NewDataSetDialog extends JDialog implements ActionListener {
070: /** The CleverPHL instance. */
071: private CleverPHL cleverPHL;
072:
073: /** The language. */
074: private Language language;
075:
076: /** The list of filters. */
077: private JList list;
078:
079: /** The filter list. */
080: private FilterList filterList;
081:
082: /** The filter which has been selected. */
083: private Filter selectedFilter;
084:
085: /** The ok button. */
086: private JButton okButton;
087:
088: /** The cancel button. */
089: private JButton cancelButton;
090:
091: /** The radiobutton for using the actual record. */
092: private JRadioButton actualRecordButton;
093:
094: /** The radiobutton for loading records. */
095: private JRadioButton loadRecordsButton;
096: private ButtonGroup recordGroup;
097:
098: /** The list of the selected record files. */
099: private JList fileList;
100:
101: /** The vector of files. */
102: private Vector fileVector;
103:
104: /** The button for adding files. */
105: private JButton addButton;
106:
107: /** The button for removing files. */
108: private JButton removeButton;
109:
110: /** The button for clearing the file list. */
111: private JButton clearButton;
112:
113: /** checkbox: Whether or not file names should be added. */
114: private JCheckBox addFileNamesCheckBox;
115:
116: /**
117: * Creates a new dialog (visible).
118: *
119: * @param cleverPHL the CleverPHL instance
120: */
121: public NewDataSetDialog(CleverPHL cleverPHL) {
122: super (cleverPHL.getDataSetFrame(), true);
123: this .cleverPHL = cleverPHL;
124: setName(cleverPHL.getCustomization().getString(
125: "Components.JacaretoComponent", "JACARETO_COMPONENT"));
126: selectedFilter = null;
127: fileVector = new Vector();
128:
129: language = cleverPHL.getLanguage();
130:
131: Session session = cleverPHL.getSessionList().getActual();
132:
133: setTitle(language.getString("CleverPHL.NewDataSetDialog.Title"));
134:
135: // The filter list
136: filterList = session.getFilterList();
137: list = new JList(filterList);
138: list
139: .setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
140:
141: if (filterList.getSize() > 0) {
142: list.setSelectedIndex(0);
143: }
144:
145: TitledBorder listBorder = new TitledBorder(new LineBorder(
146: Color.black), language
147: .getString("CleverPHL.NewDataSetDialog.ListLabel"));
148:
149: //list.setFixedCellWidth ((int) listBorder.getMinimumSize(this).getWidth());
150: list.setFixedCellWidth(200);
151: list.setVisibleRowCount(10);
152: list.addMouseListener(new ListMouseListener(this ));
153:
154: JScrollPane listScrollPane = new JScrollPane(list);
155: listScrollPane.setBorder(listBorder);
156:
157: JPanel leftPanelWrapper = new JPanel();
158: leftPanelWrapper.setLayout(new BorderLayout());
159: leftPanelWrapper.setBorder(new EmptyBorder(5, 5, 5, 5));
160: leftPanelWrapper.add(listScrollPane, BorderLayout.NORTH);
161:
162: // The selection of the record(s)
163: actualRecordButton = new JRadioButton(language
164: .getString("CleverPHL.NewDataSetDialog.ActualRecord"));
165: actualRecordButton.setSelected(true);
166: actualRecordButton.addActionListener(new ActionListener() {
167: public void actionPerformed(ActionEvent e) {
168: addFileNamesCheckBox.setEnabled(false);
169: setFileComponentsEnabled(false);
170: }
171: });
172: loadRecordsButton = new JRadioButton(language
173: .getString("CleverPHL.NewDataSetDialog.LoadRecords"));
174: loadRecordsButton.addActionListener(new ActionListener() {
175: public void actionPerformed(ActionEvent e) {
176: addFileNamesCheckBox.setEnabled(true);
177: setFileComponentsEnabled(true);
178: }
179: });
180: recordGroup = new ButtonGroup();
181: recordGroup.add(actualRecordButton);
182: recordGroup.add(loadRecordsButton);
183:
184: JPanel rightPanel = new JPanel();
185: rightPanel.setLayout(new GridLayout(1, 2));
186: rightPanel.add(actualRecordButton);
187: rightPanel.add(loadRecordsButton);
188:
189: fileList = new JList(fileVector);
190:
191: JScrollPane fileListScrollPane = new JScrollPane(fileList);
192: TitledBorder fileListBorder = new TitledBorder(new LineBorder(
193: Color.black), language
194: .getString("CleverPHL.NewDataSetDialog.FileList"));
195: fileListScrollPane.setBorder(fileListBorder);
196:
197: addButton = new JButton(language
198: .getString("CleverPHL.NewDataSetDialog.Add"));
199: addButton.addActionListener(new ActionListener() {
200: public void actionPerformed(ActionEvent e) {
201: addFiles();
202: }
203: });
204: removeButton = new JButton(language
205: .getString("CleverPHL.NewDataSetDialog.Remove"));
206: removeButton.addActionListener(new ActionListener() {
207: public void actionPerformed(ActionEvent e) {
208: removeFiles();
209: }
210: });
211: clearButton = new JButton(language
212: .getString("CleverPHL.NewDataSetDialog.Clear"));
213: clearButton.addActionListener(new ActionListener() {
214: public void actionPerformed(ActionEvent e) {
215: fileVector.clear();
216: fileList.setListData(fileVector);
217: }
218: });
219:
220: JPanel fileButtonPanel = new JPanel();
221: fileButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
222: fileButtonPanel.add(addButton);
223: fileButtonPanel.add(removeButton);
224: fileButtonPanel.add(clearButton);
225:
226: JPanel optionsPanel = new JPanel(
227: new FlowLayout(FlowLayout.LEFT));
228: addFileNamesCheckBox = new JCheckBox(language
229: .getString("CleverPHL.NewDataSetDialog.AddFileNames"));
230: addFileNamesCheckBox.setSelected(cleverPHL.getCustomization()
231: .getBoolean(
232: "CleverPHL.NewDataSetDialog.AddFileNamesAsRow",
233: false));
234: addFileNamesCheckBox.setEnabled(loadRecordsButton.isSelected());
235: addFileNamesCheckBox.addActionListener(new ActionListener() {
236: public void actionPerformed(ActionEvent e) {
237: getCustomization().put(
238: "CleverPHL.NewDataSetDialog.AddFileNamesAsRow",
239: addFileNamesCheckBox.isSelected());
240: }
241: });
242: optionsPanel.add(addFileNamesCheckBox);
243:
244: JPanel optionsWrapper = new JPanel(new BorderLayout());
245: optionsWrapper.add(fileButtonPanel, BorderLayout.NORTH);
246: optionsWrapper.add(optionsPanel, BorderLayout.CENTER);
247:
248: JPanel rightPanelWrapper = new JPanel();
249: rightPanelWrapper.setLayout(new BorderLayout());
250: rightPanelWrapper.setBorder(new EmptyBorder(5, 5, 5, 5));
251: rightPanelWrapper.add(rightPanel, BorderLayout.NORTH);
252: rightPanelWrapper.add(fileListScrollPane, BorderLayout.CENTER);
253: rightPanelWrapper.add(optionsWrapper, BorderLayout.SOUTH);
254:
255: // The panel with the two buttons
256: JPanel buttonPanel = new JPanel();
257: okButton = new JButton(language.getString("General.Ok"));
258: okButton.addActionListener(this );
259: cancelButton = new JButton(language.getString("General.Cancel"));
260: cancelButton.addActionListener(this );
261: buttonPanel.add(okButton);
262: buttonPanel.add(cancelButton);
263: buttonPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
264:
265: // Add all to the content pane
266: getContentPane().setLayout(new BorderLayout());
267: getContentPane().add(leftPanelWrapper, BorderLayout.WEST);
268: getContentPane().add(rightPanelWrapper, BorderLayout.CENTER);
269: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
270:
271: Point ownerLoc = cleverPHL.getDataSetFrame().getLocation();
272: setLocation(((int) ownerLoc.getX()) + 30, ((int) ownerLoc
273: .getY()) + 30);
274:
275: pack();
276: setFileComponentsEnabled(false);
277: setResizable(false);
278: setVisible(true);
279: }
280:
281: /**
282: * Returns the customization.
283: *
284: * @return the custom
285: */
286: public Customization getCustomization() {
287: return cleverPHL.getCustomization();
288: }
289:
290: /**
291: * Called when one of the two buttons has been pressed.
292: *
293: * @param e the action event
294: */
295: public void actionPerformed(ActionEvent e) {
296: Object source = e.getSource();
297:
298: if (source == okButton) {
299: close();
300: } else {
301: selectedFilter = null;
302: dispose();
303: }
304: }
305:
306: /**
307: * Closes the dialog.
308: */
309: public void close() {
310: if (list.getSelectedIndex() > -1) {
311: selectedFilter = (Filter) filterList.getElementAt(list
312: .getSelectedIndex());
313: } else {
314: selectedFilter = null;
315: }
316:
317: dispose();
318: }
319:
320: /**
321: * Returns the selected filter, or <code>null</code> if no filter is selected
322: *
323: * @return DOCUMENT ME!
324: */
325: public Filter getSelectedFilter() {
326: return selectedFilter;
327: }
328:
329: /**
330: * Adds files to the file list.
331: */
332: private void addFiles() {
333: JFileChooser chooser = new JFileChooser(cleverPHL
334: .getWorkingDir());
335: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
336: chooser
337: .setDialogTitle(language
338: .getString("CleverPHL.NewDataSetDialog.FileChooser.Title"));
339: chooser
340: .setFileFilter(new ExtensionFileFilter(
341: "xml",
342: language
343: .getString("CleverPHL.NewDataSetDialog.XMLFileFilter.Description")));
344: chooser.setMultiSelectionEnabled(true);
345:
346: int returnVal = chooser
347: .showDialog(
348: this ,
349: language
350: .getString("CleverPHL.NewDataSetDialog.FileChooser.ApproveButtonText"));
351:
352: if (returnVal == JFileChooser.APPROVE_OPTION) {
353: File[] selectedFiles = chooser.getSelectedFiles();
354:
355: for (int i = 0; i < selectedFiles.length; i++) {
356: try {
357: fileVector.add(selectedFiles[i].getCanonicalPath());
358: } catch (IOException ioe) {
359: cleverPHL
360: .getLogger()
361: .debug(
362: language
363: .getString("CleverPHL.NewDataSetDialog.FileChooser.Msg.DeterminePathNotPossible"));
364: }
365: }
366:
367: fileList.setListData(fileVector);
368: cleverPHL.setWorkingDir(chooser.getCurrentDirectory());
369: }
370: }
371:
372: /**
373: * Removed the selected files from the file list
374: */
375: private void removeFiles() {
376: int[] selectedIndices = fileList.getSelectedIndices();
377:
378: if (selectedIndices.length > 0) {
379: for (int i = selectedIndices.length - 1; i >= 0; i--) {
380: fileVector.remove(selectedIndices[i]);
381: }
382:
383: fileList.setListData(fileVector);
384: }
385: }
386:
387: private void setFileComponentsEnabled(boolean isEnabled) {
388: fileList.setEnabled(isEnabled);
389: addButton.setEnabled(isEnabled);
390: removeButton.setEnabled(isEnabled);
391: clearButton.setEnabled(isEnabled);
392: }
393:
394: /**
395: * Returns whether the actual record should be used or not.
396: *
397: * @return DOCUMENT ME!
398: */
399: public boolean useActualRecord() {
400: return actualRecordButton.isSelected();
401: }
402:
403: /**
404: * Returns the filename list of the selected files
405: *
406: * @return DOCUMENT ME!
407: */
408: public String[] getFilenames() {
409: String[] result = new String[fileVector.size()];
410:
411: for (int i = 0; i < result.length; i++) {
412: result[i] = (String) fileVector.get(i);
413: }
414:
415: return result;
416: }
417:
418: /**
419: * Listener for the double click on the list elements.
420: */
421: static class ListMouseListener implements MouseListener {
422: //~ Instance fields ------------------------------------------------------------------------
423:
424: /** The dialog this listener belongs to. */
425: private NewDataSetDialog dialog;
426:
427: //~ Constructors ---------------------------------------------------------------------------
428:
429: /**
430: * Creates a new listener object.
431: *
432: * @param dialog DOCUMENT ME!
433: */
434: public ListMouseListener(NewDataSetDialog dialog) {
435: this .dialog = dialog;
436: }
437:
438: //~ Methods --------------------------------------------------------------------------------
439:
440: /**
441: * Calls the <code>notifyDoubleClick</code> method of the starter frame when the mouse has
442: * been double clicked.
443: *
444: * @param e the mouse event.
445: */
446: public void mouseClicked(MouseEvent e) {
447: if (e.getClickCount() == 2) {
448: dialog.close();
449: }
450: }
451:
452: public void mouseEntered(MouseEvent e) {
453: }
454:
455: public void mouseExited(MouseEvent e) {
456: }
457:
458: public void mousePressed(MouseEvent e) {
459: }
460:
461: public void mouseReleased(MouseEvent e) {
462: }
463: }
464: }
|