Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuehnel committed May 12, 2016
2 parents 83f610b + e4d4d03 commit 4fee442
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 41 deletions.
10 changes: 4 additions & 6 deletions src/main/java/org/matsim/contrib/josm/ImportAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@ public void actionPerformed(ActionEvent e) {

ImportDialog dialog = new ImportDialog();
JOptionPane pane = new JOptionPane(dialog, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);

JDialog dlg = pane.createDialog(Main.parent, tr("Import"));
dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dlg.setMinimumSize(new Dimension(800, 600));
dlg.setMinimumSize(new Dimension(400, 600));
dlg.setVisible(true);
if (pane.getValue() != null) {
if (((Integer) pane.getValue()) == JOptionPane.OK_OPTION) {
if (!dialog.networkPathButton.getText().equals("choose")) {
if (dialog.schedulePathButton.getText().equals("choose")) {
dialog.schedulePathButton.setText(null);
}
ImportTask task = new ImportTask(dialog.networkPathButton.getText(), dialog.schedulePathButton.getText(),
if (dialog.getNetworkFile()!=null) {
ImportTask task = new ImportTask(dialog.getNetworkFile(), dialog.getScheduleFile(),
dialog.getSelectedProjection());
Main.worker.execute(task);
}
Expand Down
95 changes: 77 additions & 18 deletions src/main/java/org/matsim/contrib/josm/ImportDialog.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
package org.matsim.contrib.josm;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.data.projection.Projection;
import org.openstreetmap.josm.data.projection.Projections;
import org.openstreetmap.josm.gui.preferences.projection.CodeProjectionChoice;
import org.openstreetmap.josm.gui.preferences.projection.ListProjectionChoice;
import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
import org.openstreetmap.josm.tools.GBC;
import org.openstreetmap.josm.tools.ImageProvider;

/**
* the import dialog
Expand All @@ -28,34 +38,53 @@
*/
@SuppressWarnings("serial")
class ImportDialog extends JPanel {
// the JOptionPane that contains this dialog. required for the closeDialog()
// method.

/**
* Holds the path of the import file
*/
final JLabel networkPath = new JLabel("network");
final JButton networkPathButton = new JButton("choose");
final JLabel schedulePath = new JLabel("transit schedule");
final JButton schedulePathButton = new JButton("choose");
final JLabel networkHeading = new JLabel("Network:");
final JLabel networkPath = new JLabel("...");
final JButton networkPathButton = new JButton(new ImageProvider("open.png").getResource().getImageIcon(new Dimension(10, 10)));
final JLabel schedulePathHeading = new JLabel("Transit Schedule:");
final JLabel schedulePath = new JLabel("...");
final JButton schedulePathButton = new JButton(new ImageProvider("open.png").getResource().getImageIcon(new Dimension(10, 10)));
final JLabel importSystemLabel = new JLabel("Origin System:");

final JComboBox<ProjectionChoice> importSystemCB = new JComboBox<>(ProjectionPreference.getProjectionChoices().toArray(new ProjectionChoice[] {}));
private Projection selectedProjection;
private JPanel projSubPrefPanelWrapper = new JPanel(new GridBagLayout());
private JPanel projSubPrefPanel;

private File networkFile = null;
private File scheduleFile = null;

public ImportDialog() {

networkHeading.setFont(networkHeading.getFont().deriveFont(Font.BOLD));
schedulePathHeading.setFont(networkHeading.getFont().deriveFont(Font.BOLD));
importSystemLabel.setFont(networkHeading.getFont().deriveFont(Font.BOLD));
networkPath.setBorder(BorderFactory.createEtchedBorder());
schedulePath.setBorder(BorderFactory.createEtchedBorder());

setLayout(new GridBagLayout());
add(networkPath, GBC.std());


add(networkHeading, GBC.eop());
add(networkPath, GBC.eop().fill(GridBagConstraints.HORIZONTAL) );
add(networkPathButton, GBC.eop());

JSeparator sep = new JSeparator(SwingConstants.HORIZONTAL);
sep.setPreferredSize(new Dimension(400,3));
add(sep, GBC.eop());

add(schedulePath, GBC.std());

add(schedulePathHeading, GBC.eop());
add(schedulePath, GBC.eop().fill(GridBagConstraints.HORIZONTAL) );
add(schedulePathButton, GBC.eop());

JSeparator sep2 = new JSeparator(SwingConstants.HORIZONTAL);
sep2.setPreferredSize(new Dimension(400,3));
add(sep2, GBC.eop());

JLabel importSystemLabel = new JLabel("origin system:");
add(importSystemLabel, GBC.std());
add(importSystemLabel, GBC.eop());

importSystemCB.addActionListener(new ActionListener() {
@Override
Expand All @@ -73,14 +102,20 @@ public void actionPerformed(ActionEvent e) {
networkPathButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser(System.getProperty("user.home"));
JFileChooser chooser;
if(networkFile == null) {
chooser = new JFileChooser(System.getProperty("user.home"));
} else {
chooser = new JFileChooser(networkFile.getAbsolutePath());
}
chooser.setApproveButtonText("Import");
chooser.setDialogTitle("MATSim-Import");
FileFilter filter = new FileNameExtensionFilter("Network-XML", "xml");
chooser.setFileFilter(filter);
int result = chooser.showOpenDialog(Main.parent);
if (result == JFileChooser.APPROVE_OPTION && chooser.getSelectedFile().getAbsolutePath() != null) {
networkPathButton.setText(chooser.getSelectedFile().getAbsolutePath());
networkFile = new File(chooser.getSelectedFile().getAbsolutePath());
networkPath.setText(networkFile.getName());
}
}
});
Expand All @@ -89,14 +124,20 @@ public void actionPerformed(ActionEvent e) {
schedulePathButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser(System.getProperty("user.home"));
JFileChooser chooser;
if(scheduleFile == null) {
chooser = new JFileChooser(System.getProperty("user.home"));
} else {
chooser = new JFileChooser(networkFile.getAbsolutePath());
}
chooser.setApproveButtonText("Import");
chooser.setDialogTitle("MATSim-Import");
FileFilter filter = new FileNameExtensionFilter("TransitSchedule-XML", "xml");
chooser.setFileFilter(filter);
int result = chooser.showOpenDialog(Main.parent);
if (result == JFileChooser.APPROVE_OPTION && chooser.getSelectedFile().getAbsolutePath() != null) {
schedulePathButton.setText(chooser.getSelectedFile().getAbsolutePath());
scheduleFile = new File(chooser.getSelectedFile().getAbsolutePath());
schedulePath.setText(scheduleFile.getName());
}
}
});
Expand All @@ -121,6 +162,11 @@ private void selectedProjectionChanged(final ProjectionChoice pc) {
public void actionPerformed(ActionEvent e) {
if((pc instanceof CodeProjectionChoice)) {
selectedProjection = Projections.getProjectionByCode(pc.getPreferences(projSubPrefPanel).iterator().next());
} else if(pc instanceof ListProjectionChoice) {
Collection<String> prefs = pc.getPreferences(projSubPrefPanel);
pc.setPreferences(prefs);
selectedProjection = pc.getProjection();
System.out.println(pc.toString()+" "+pc.getProjection().toCode());
}
}
};
Expand All @@ -131,14 +177,27 @@ public void actionPerformed(ActionEvent e) {
projSubPrefPanelWrapper.add(projSubPrefPanel, GBC.std().fill(GBC.BOTH).weight(1.0, 1.0));
revalidate();
repaint();
if(!(pc instanceof CodeProjectionChoice)) {
selectedProjection = pc.getProjection();
if(pc instanceof CodeProjectionChoice) {
selectedProjection = Projections.getProjectionByCode(pc.getPreferences(projSubPrefPanel).iterator().next());
} else if (pc instanceof ListProjectionChoice) {
Collection<String> prefs = pc.getPreferences(projSubPrefPanel);
pc.setPreferences(prefs);
}
selectedProjection = pc.getProjection();
}


public Projection getSelectedProjection() {
return selectedProjection;
}

public File getNetworkFile() {
return networkFile;
}

public File getScheduleFile() {
return scheduleFile;
}


}
6 changes: 4 additions & 2 deletions src/main/java/org/matsim/contrib/josm/ImportTask.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.matsim.contrib.josm;

import java.io.File;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.data.projection.Projection;
import org.openstreetmap.josm.gui.PleaseWaitRunnable;
Expand All @@ -17,9 +19,9 @@ class ImportTask extends PleaseWaitRunnable {
/**
* Creates a new Import task with the given <code>path</code>.
*/
public ImportTask(String networkPath, String schedulePath, Projection projection) {
public ImportTask(File network, File schedule, Projection projection) {
super("MATSim Import");
this.importer = new Importer(networkPath, schedulePath, projection);
this.importer = new Importer(network, schedule, projection);
}

/**
Expand Down
25 changes: 12 additions & 13 deletions src/main/java/org/matsim/contrib/josm/Importer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

class Importer {

private final String networkPath;
private final String schedulePath;
private final File network;
private final File schedule;
private Projection projection;
private MATSimLayer layer;

Expand All @@ -40,17 +40,17 @@ class Importer {
private EditableScenario sourceScenario;
private EditableScenario targetScenario;

public Importer(String networkPath, String schedulePath, Projection projection) {
this.networkPath = networkPath;
this.schedulePath = schedulePath;
public Importer(File network, File schedule, Projection projection) {
this.network = network;
this.schedule = schedule;
this.projection = projection;
}

public Importer(EditableScenario scenario, Projection projection) {
this.sourceScenario = scenario;
this.projection = projection;
this.networkPath = null;
this.schedulePath = null;
this.network = null;
this.schedule = null;
}

void run() {
Expand All @@ -65,8 +65,7 @@ void run() {
convertStops();
convertLines();
}
layer = new MATSimLayer(dataSet, networkPath == null ? MATSimLayer.createNewName() : networkPath, networkPath == null ? null : new File(
networkPath), targetScenario, way2Links, link2Segment, stopRelation2TransitStop);
layer = new MATSimLayer(dataSet, network == null ? MATSimLayer.createNewName() : network.getName(), network == null ? null : network, targetScenario, way2Links, link2Segment, stopRelation2TransitStop);
}

// Abuse fields in MATSim data structures to hold the "real" object ids.
Expand All @@ -92,14 +91,14 @@ private void copyIdsToOrigIds(EditableScenario sourceScenario) {

private EditableScenario readScenario() {
Config config = ConfigUtils.createConfig();
if (schedulePath != null) {
if (schedule != null) {
config.transit().setUseTransit(true);
}
EditableScenario tempScenario = EditableScenarioUtils.createScenario(config);
MatsimNetworkReader reader = new MatsimNetworkReader(tempScenario.getNetwork());
reader.readFile(networkPath);
if (schedulePath != null) {
new TransitScheduleReader(tempScenario).readFile(schedulePath);
reader.readFile(network.getAbsolutePath());
if (schedule != null) {
new TransitScheduleReader(tempScenario).readFile(schedule.getAbsolutePath());
}
return tempScenario;
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/matsim/contrib/josm/IOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openstreetmap.josm.data.osm.Relation;
import org.openstreetmap.josm.data.osm.Way;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -41,7 +42,7 @@ public void readNetworkWithoutTransit() {
URL url = getClass().getResource("/test-input/pt-tutorial/multimodalnetwork.xml");
Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
new NetworkReaderMatsimV1(scenario.getNetwork()).parse(url);
Importer importer = new Importer(url.getFile(), null, Main.getProjection());
Importer importer = new Importer(new File(url.getFile()), null, Main.getProjection());
importer.run();
MATSimLayer layer = importer.getLayer();
deleteAndUndeleteLinksOneByOne(scenario, layer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.matsim.pt.transitSchedule.api.TransitScheduleReader;
import org.openstreetmap.josm.Main;

import java.io.File;
import java.net.URL;

public class PtTutorialScenario {
Expand All @@ -25,7 +26,7 @@ public static Scenario scenario() {
}

public static MATSimLayer layer() {
Importer importer = new Importer(networkUrl.getFile(), transitScheduleUrl.getFile(), Main.getProjection());
Importer importer = new Importer(new File(networkUrl.getFile()), new File(transitScheduleUrl.getFile()), Main.getProjection());
importer.run();
return importer.getLayer();
}
Expand Down

0 comments on commit 4fee442

Please sign in to comment.