/*
* Copyright 2009 Peter Karich, peat_hal ‘at’ users ‘dot’ sourceforge ‘dot’ net.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/
package de.timefinder.core.ui.command;
import de.timefinder.core.io.xml.XmlImport;
import de.timefinder.data.algo.DataPoolSettings;
import java.io.FileInputStream;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.richclient.application.Application;
import org.springframework.richclient.application.ApplicationWindow;
import org.springframework.richclient.application.statusbar.StatusBar;
import org.springframework.richclient.progress.ProgressMonitor;
/**
* This UI class provides the Import class to the user
*
* @author Peter Karich, peat_hal ‘at’ users ‘dot’ sourceforge ‘dot’ net
*/
public class ImportXmlFileCommand extends AbstractImportCommand {
public static final String ID = "importXmlFile";
private DataPoolSettings settings;
public ImportXmlFileCommand() {
super(ID);
}
@Autowired
public void setDataPoolSettings(DataPoolSettings set) {
this.settings = set;
}
/**
* Starts a new frame and close the active window.
*
* @see Application#close()
*/
@Override
protected void doOnce() {
checkIfDataIsAlreadyAvailable();
closeAllViews();
ApplicationWindow aw = getApplicationWindow();
final StatusBar bar = aw.getStatusBar();
final ProgressMonitor pm = bar.getProgressMonitor();
JFileChooser fc = new JFileChooser();
fc.setDialogTitle(tr.get(ID + ".fileDialog"));
int res = fc.showOpenDialog(aw.getControl());
if (res != JFileChooser.APPROVE_OPTION) {
bar.setMessage(tr.get(ID + ".noFileSelected"));
return;
}
file = fc.getSelectedFile();
if (!file.getName().endsWith(".xml")) {
int resultOP = JOptionPane.showConfirmDialog(null, tr.get(ID + ".confirmation.message"),
tr.get(ID + ".confirmation.title"), JOptionPane.OK_CANCEL_OPTION);
if (resultOP != JOptionPane.OK_OPTION) {
return;
}
}
pm.taskStarted(tr.get(ID + ".startTask"), 100);
MySwingWorker sw = new MySwingWorker(ID) {
@Override
protected void myconstruct() throws Exception {
bar.getProgressMonitor().worked(10);
XmlImport importer = new XmlImport(dataPool, settings,
new FileInputStream(file.getAbsolutePath()));
// TODO NOW
// importer.setStatusBar(bar);
importer.doWork();
bar.getProgressMonitor().worked(90);
}
@Override
protected void done() {
bar.getProgressMonitor().worked(90);
openAllViews();
bar.getProgressMonitor().worked(100);
super.done();
}
};
sw.execute();
}
}