diff --git a/src/main/java/org/matsim/contrib/josm/ImportAction.java b/src/main/java/org/matsim/contrib/josm/ImportAction.java index dc5b5ed..a11c259 100644 --- a/src/main/java/org/matsim/contrib/josm/ImportAction.java +++ b/src/main/java/org/matsim/contrib/josm/ImportAction.java @@ -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); } diff --git a/src/main/java/org/matsim/contrib/josm/ImportDialog.java b/src/main/java/org/matsim/contrib/josm/ImportDialog.java index 256d9b1..eca214b 100644 --- a/src/main/java/org/matsim/contrib/josm/ImportDialog.java +++ b/src/main/java/org/matsim/contrib/josm/ImportDialog.java @@ -1,24 +1,26 @@ package org.matsim.contrib.josm; -import static org.openstreetmap.josm.tools.I18n.tr; - +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.JOptionPane; 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.SystemOfMeasurement; -import org.openstreetmap.josm.data.coor.CoordinateFormat; import org.openstreetmap.josm.data.projection.Projection; import org.openstreetmap.josm.data.projection.Projections; import org.openstreetmap.josm.gui.preferences.projection.CodeProjectionChoice; @@ -26,6 +28,7 @@ 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 @@ -35,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 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 @@ -80,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()); } } }); @@ -96,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()); } } }); @@ -132,6 +166,7 @@ public void actionPerformed(ActionEvent e) { Collection prefs = pc.getPreferences(projSubPrefPanel); pc.setPreferences(prefs); selectedProjection = pc.getProjection(); + System.out.println(pc.toString()+" "+pc.getProjection().toCode()); } } }; @@ -156,5 +191,13 @@ public Projection getSelectedProjection() { return selectedProjection; } + public File getNetworkFile() { + return networkFile; + } + + public File getScheduleFile() { + return scheduleFile; + } + } diff --git a/src/main/java/org/matsim/contrib/josm/ImportTask.java b/src/main/java/org/matsim/contrib/josm/ImportTask.java index f1c4255..a4b54ae 100644 --- a/src/main/java/org/matsim/contrib/josm/ImportTask.java +++ b/src/main/java/org/matsim/contrib/josm/ImportTask.java @@ -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; @@ -17,9 +19,9 @@ class ImportTask extends PleaseWaitRunnable { /** * Creates a new Import task with the given path. */ - 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); } /** diff --git a/src/main/java/org/matsim/contrib/josm/Importer.java b/src/main/java/org/matsim/contrib/josm/Importer.java index 3f666cf..714d0f4 100644 --- a/src/main/java/org/matsim/contrib/josm/Importer.java +++ b/src/main/java/org/matsim/contrib/josm/Importer.java @@ -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; @@ -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() { @@ -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. @@ -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; } diff --git a/src/test/java/org/matsim/contrib/josm/IOTest.java b/src/test/java/org/matsim/contrib/josm/IOTest.java index 906fd11..fcff955 100644 --- a/src/test/java/org/matsim/contrib/josm/IOTest.java +++ b/src/test/java/org/matsim/contrib/josm/IOTest.java @@ -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; @@ -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); diff --git a/src/test/java/org/matsim/contrib/josm/PtTutorialScenario.java b/src/test/java/org/matsim/contrib/josm/PtTutorialScenario.java index 25194d6..0dd37bc 100644 --- a/src/test/java/org/matsim/contrib/josm/PtTutorialScenario.java +++ b/src/test/java/org/matsim/contrib/josm/PtTutorialScenario.java @@ -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 { @@ -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(); }