Skip to content
This repository has been archived by the owner on Jul 11, 2018. It is now read-only.

Commit

Permalink
using swing-tasks for importing files
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Fichtner (pfichtner) committed May 13, 2014
1 parent a3a1759 commit b794d7d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 22 deletions.
22 changes: 19 additions & 3 deletions gpx-ui/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>gpx.ui</artifactId>
Expand All @@ -17,6 +18,14 @@
<version>0.0.3-SNAPSHOT</version>
</parent>

<repositories>
<repository>
<id>project.local</id>
<name>project</name>
<url>file:${project.basedir}/local-repo</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.github.pfichtner.jrunalyser</groupId>
Expand All @@ -28,6 +37,11 @@
<artifactId>TaskDialog</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.eknet.swing.task</groupId>
<artifactId>swing-tasks</artifactId>
<version>1.0.2</version>
</dependency>

<dependency>
<groupId>com.github.pfichtner.jrunalyser</groupId>
Expand Down Expand Up @@ -158,13 +172,15 @@
<transformers>
<!-- This bit sets the main class for the executable jar as you otherwise -->
<!-- would with the assembly plugin -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.github.pfichtner.jrunalyser.ui.dock.Dock</Main-Class>
</manifestEntries>
</transformer>
<!-- This bit merges the various META-INF/services files -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static org.eknet.swing.task.Mode.BLOCKING;

import java.awt.Component;
import java.awt.Dimension;
Expand All @@ -17,6 +18,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
Expand All @@ -33,6 +35,11 @@
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

import org.eknet.swing.task.AbstractTask;
import org.eknet.swing.task.TaskManager;
import org.eknet.swing.task.Tracker;
import org.eknet.swing.task.impl.TaskManagerImpl;
import org.eknet.swing.task.ui.TaskGlassPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -314,6 +321,9 @@ public void windowClosing(WindowEvent e) {
// center
frame.setLocationRelativeTo(null);
frame.setVisible(true);

TaskManager tm = new TaskManagerImpl();
frame.setGlassPane(new TaskGlassPane(tm));
}

private static void initInBackground(final DatasourceFascade dsf) {
Expand Down Expand Up @@ -461,17 +471,11 @@ private static Collection<Callable<Track>> createCallables(
result.add(new Callable<Track>() {
@Override
public Track call() throws IOException {
try {
log.debug("Loading {}", file); //$NON-NLS-1$
Track addedTrack;
addedTrack = dataSource.addTrack(file);
log.info("{} loaded", file); //$NON-NLS-1$
return addedTrack;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw e;
}
log.debug("Loading {}", file); //$NON-NLS-1$
Track addedTrack;
addedTrack = dataSource.addTrack(file);
log.info("{} loaded", file); //$NON-NLS-1$
return addedTrack;
}
});

Expand Down Expand Up @@ -548,22 +552,64 @@ private static JMenu createFileMenu(
JMenu jMenu = new JMenu(TITLE);
JMenuItem menuItem = new JMenuItem(
i18n.getText("com.github.pfichtner.jrunalyser.ui.dock.Dock.miAddGpx.title")); //$NON-NLS-1$
// TODO Should this be done inside a SwingWorker?

menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
for (File selectedFile : chooser.getSelectedFiles()) {
try {
Track track = GpxUnmarshaller
.loadTrack(selectedFile);
datasourceFascade.addTrack(track);
} catch (IOException e) {
throw Throwables.propagate(e);
File[] selectedFiles = chooser.getSelectedFiles();

Component glassPane = parent.getGlassPane();
if (glassPane instanceof TaskGlassPane) {
TaskGlassPane taskGlassPane = (TaskGlassPane) glassPane;
TaskManager tm = taskGlassPane.getTaskManager();

int cpus = Runtime.getRuntime().availableProcessors();
List<List<File>> partitions = Lists.partition(
Arrays.asList(selectedFiles),
(selectedFiles.length + cpus - 1) / cpus);
for (final List<File> files : partitions) {
tm.create(
new AbstractTask<Void, Void>(
i18n.getText("com.github.pfichtner.jrunalyser.ui.dock.Dock.importDialog.title"), BLOCKING) { //$NON-NLS-1$
@Override
public Void execute(
Tracker<Void> tracker) {
int i = 0;
for (File file : files) {
tracker.setProgress(0,
files.size(), i++);
tracker.setPhase(i18n
.getText(
"com.github.pfichtner.jrunalyser.ui.dock.Dock.importDialog.format", file)); //$NON-NLS-1$
try {
datasourceFascade
.addTrack(GpxUnmarshaller
.loadTrack(file));
} catch (IOException e) {
throw Throwables
.propagate(e);
}
}
return null;

}
}).execute();
}

} else {
for (File selectedFile : selectedFiles) {
try {
datasourceFascade.addTrack(GpxUnmarshaller
.loadTrack(selectedFile));
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
}

}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
com.github.pfichtner.jrunalyser.ui.dock.Dock.title=JRunalyser
com.github.pfichtner.jrunalyser.ui.dock.Dock.miAddGpx.title=Add GPX
com.github.pfichtner.jrunalyser.ui.dock.Dock.mWindows.title=Windows

com.github.pfichtner.jrunalyser.ui.dock.Dock.importDialog.title=Import tracks
com.github.pfichtner.jrunalyser.ui.dock.Dock.importDialog.format=Importing %s
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
com.github.pfichtner.jrunalyser.ui.dock.Dock.title=JRunalyser
com.github.pfichtner.jrunalyser.ui.dock.Dock.miAddGpx.title=GPX hinzufügen
com.github.pfichtner.jrunalyser.ui.dock.Dock.mWindows.title=Fenster

com.github.pfichtner.jrunalyser.ui.dock.Dock.importDialog.title=Importiere Tracks
com.github.pfichtner.jrunalyser.ui.dock.Dock.importDialog.format=Importiere %s

0 comments on commit b794d7d

Please sign in to comment.