- Timestamp:
- 2016-01-15T21:25:13+01:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/io/session
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/session/AbstractSessionExporter.java
r9455 r9471 10 10 11 11 /** 12 * Genericsuperclass of all session layer exporters.12 * Abstract superclass of all session layer exporters. 13 13 * @param <T> Type of exported layer 14 14 * @since 9455 -
trunk/src/org/openstreetmap/josm/io/session/GpxTracksSessionExporter.java
r9455 r9471 2 2 package org.openstreetmap.josm.io.session; 3 3 4 import static org.openstreetmap.josm.tools.I18n.tr;5 6 import java.awt.CardLayout;7 import java.awt.Component;8 import java.awt.Font;9 import java.awt.GridBagLayout;10 import java.awt.event.ActionEvent;11 import java.awt.event.ActionListener;12 import java.awt.event.ItemEvent;13 import java.awt.event.ItemListener;14 import java.io.File;15 import java.io.IOException;16 4 import java.io.OutputStream; 17 5 import java.io.OutputStreamWriter; 18 6 import java.io.PrintWriter; 19 7 import java.io.Writer; 20 import java.net.MalformedURLException;21 import java.net.URI;22 import java.net.URL;23 8 import java.nio.charset.StandardCharsets; 24 9 25 import javax.swing.ButtonGroup;26 import javax.swing.JLabel;27 import javax.swing.JPanel;28 import javax.swing.JRadioButton;29 import javax.swing.SwingConstants;30 31 10 import org.openstreetmap.josm.gui.layer.GpxLayer; 32 import org.openstreetmap.josm.gui.util.GuiHelper;33 import org.openstreetmap.josm.gui.widgets.JosmTextField;34 11 import org.openstreetmap.josm.io.GpxWriter; 35 import org.openstreetmap.josm.io.session.SessionWriter.ExportSupport;36 import org.openstreetmap.josm.tools.GBC;37 import org.w3c.dom.Element;38 12 39 13 /** … … 41 15 * @since 5501 42 16 */ 43 public class GpxTracksSessionExporter extends AbstractSessionExporter<GpxLayer> { 44 45 private JRadioButton link; 46 private JRadioButton include; 17 public class GpxTracksSessionExporter extends GenericSessionExporter<GpxLayer> { 47 18 48 19 /** … … 51 22 */ 52 23 public GpxTracksSessionExporter(GpxLayer layer) { 53 super(layer );24 super(layer, "tracks", "0.1", "gpx"); 54 25 } 55 26 56 27 @Override 57 public Component getExportPanel() {58 final JPanel p = new JPanel(new GridBagLayout());59 JPanel topRow = new JPanel(new GridBagLayout());60 export.setSelected(true);61 final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEFT);62 lbl.setToolTipText(layer.getToolTipText());63 lbl.setLabelFor(export);64 JLabel lblData = new JLabel(tr("Data:"));65 /* I18n: Refer to a OSM data file in session file */ link = new JRadioButton(tr("local file"));66 link.putClientProperty("actionname", "link");67 link.setToolTipText(tr("Link to a GPX file on your local disk."));68 /* I18n: Include OSM data in session file */ include = new JRadioButton(tr("include"));69 include.setToolTipText(tr("Include GPX data in the .joz session file."));70 include.putClientProperty("actionname", "include");71 ButtonGroup group = new ButtonGroup();72 group.add(link);73 group.add(include);74 75 JPanel cardLink = new JPanel(new GridBagLayout());76 final File file = layer.getAssociatedFile();77 if (file != null && file.exists()) {78 JosmTextField tf = new JosmTextField();79 tf.setText(file.getPath());80 tf.setEditable(false);81 cardLink.add(tf, GBC.std());82 } else {83 cardLink.add(new JLabel(tr("No file association")), GBC.eol());84 }85 86 JPanel cardInclude = new JPanel(new GridBagLayout());87 JLabel lblIncl = new JLabel(tr("GPX data will be included in the session file."));88 lblIncl.setFont(lblIncl.getFont().deriveFont(Font.PLAIN));89 cardInclude.add(lblIncl, GBC.eol().fill(GBC.HORIZONTAL));90 91 final CardLayout cl = new CardLayout();92 final JPanel cards = new JPanel(cl);93 cards.add(cardLink, "link");94 cards.add(cardInclude, "include");95 96 if (file != null && file.exists()) {97 link.setSelected(true);98 } else {99 link.setEnabled(false);100 link.setToolTipText(tr("No file association"));101 include.setSelected(true);102 cl.show(cards, "include");103 }104 105 link.addActionListener(new ActionListener() {106 @Override107 public void actionPerformed(ActionEvent e) {108 cl.show(cards, "link");109 }110 });111 include.addActionListener(new ActionListener() {112 @Override113 public void actionPerformed(ActionEvent e) {114 cl.show(cards, "include");115 }116 });117 118 topRow.add(export, GBC.std());119 topRow.add(lbl, GBC.std());120 topRow.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));121 p.add(topRow, GBC.eol().fill(GBC.HORIZONTAL));122 p.add(lblData, GBC.std().insets(10, 0, 0, 0));123 p.add(link, GBC.std());124 p.add(include, GBC.eol());125 p.add(cards, GBC.eol().insets(15, 0, 3, 3));126 127 export.addItemListener(new ItemListener() {128 @Override129 public void itemStateChanged(ItemEvent e) {130 if (e.getStateChange() == ItemEvent.DESELECTED) {131 GuiHelper.setEnabledRec(p, false);132 export.setEnabled(true);133 } else {134 GuiHelper.setEnabledRec(p, true);135 link.setEnabled(file != null && file.exists());136 }137 }138 });139 return p;140 }141 142 @Override143 public boolean requiresZip() {144 return include.isSelected();145 }146 147 @Override148 public Element export(ExportSupport support) throws IOException {149 Element layerEl = support.createElement("layer");150 layerEl.setAttribute("type", "tracks");151 layerEl.setAttribute("version", "0.1");152 153 Element file = support.createElement("file");154 layerEl.appendChild(file);155 156 if (requiresZip()) {157 String zipPath = "layers/" + String.format("%02d", support.getLayerIndex()) + "/data.gpx";158 file.appendChild(support.createTextNode(zipPath));159 addDataFile(support.getOutputStreamZip(zipPath));160 } else {161 URI uri = layer.getAssociatedFile().toURI();162 URL url = null;163 try {164 url = uri.toURL();165 } catch (MalformedURLException e) {166 throw new IOException(e);167 }168 file.appendChild(support.createTextNode(url.toString()));169 }170 return layerEl;171 }172 173 28 @SuppressWarnings("resource") 174 29 protected void addDataFile(OutputStream out) { -
trunk/src/org/openstreetmap/josm/io/session/OsmDataSessionExporter.java
r9455 r9471 2 2 package org.openstreetmap.josm.io.session; 3 3 4 import static org.openstreetmap.josm.tools.I18n.tr;5 6 import java.awt.CardLayout;7 import java.awt.Font;8 import java.awt.GridBagLayout;9 import java.awt.Insets;10 import java.awt.event.ActionEvent;11 import java.awt.event.ActionListener;12 import java.awt.event.ItemEvent;13 import java.awt.event.ItemListener;14 import java.io.File;15 import java.io.IOException;16 4 import java.io.OutputStream; 17 5 import java.io.OutputStreamWriter; 18 6 import java.io.PrintWriter; 19 7 import java.io.Writer; 20 import java.net.MalformedURLException;21 import java.net.URI;22 import java.net.URL;23 8 import java.nio.charset.StandardCharsets; 24 9 25 import javax.swing.AbstractAction;26 import javax.swing.ButtonGroup;27 import javax.swing.JButton;28 import javax.swing.JLabel;29 import javax.swing.JPanel;30 import javax.swing.JRadioButton;31 import javax.swing.SwingConstants;32 33 import org.openstreetmap.josm.actions.SaveAction;34 10 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 35 import org.openstreetmap.josm.gui.util.GuiHelper;36 import org.openstreetmap.josm.gui.widgets.JosmTextField;37 11 import org.openstreetmap.josm.io.OsmWriter; 38 12 import org.openstreetmap.josm.io.OsmWriterFactory; 39 import org.openstreetmap.josm.io.session.SessionWriter.ExportSupport;40 import org.openstreetmap.josm.tools.GBC;41 import org.openstreetmap.josm.tools.ImageProvider;42 import org.w3c.dom.Element;43 13 44 14 /** … … 46 16 * @since 4685 47 17 */ 48 public class OsmDataSessionExporter extends AbstractSessionExporter<OsmDataLayer> { 49 50 private JRadioButton link; 51 private JRadioButton include; 18 public class OsmDataSessionExporter extends GenericSessionExporter<OsmDataLayer> { 52 19 53 20 /** … … 56 23 */ 57 24 public OsmDataSessionExporter(OsmDataLayer layer) { 58 super(layer); 59 } 60 61 private class LayerSaveAction extends AbstractAction { 62 /** 63 * Constructs a new {@code LayerSaveAction}. 64 */ 65 LayerSaveAction() { 66 putValue(SMALL_ICON, new ImageProvider("save").setWidth(16).get()); 67 putValue(SHORT_DESCRIPTION, layer.requiresSaveToFile() ? 68 tr("Layer contains unsaved data - save to file.") : 69 tr("Layer does not contain unsaved data.")); 70 updateEnabledState(); 71 } 72 73 @Override 74 public void actionPerformed(ActionEvent e) { 75 SaveAction.getInstance().doSave(layer); 76 updateEnabledState(); 77 } 78 79 public final void updateEnabledState() { 80 setEnabled(layer.requiresSaveToFile()); 81 } 25 super(layer, "osm-data", "0.1", "osm"); 82 26 } 83 27 84 28 @Override 85 public JPanel getExportPanel() {86 final JPanel p = new JPanel(new GridBagLayout());87 JPanel topRow = new JPanel(new GridBagLayout());88 export.setSelected(true);89 final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEFT);90 lbl.setToolTipText(layer.getToolTipText());91 lbl.setLabelFor(export);92 JLabel lblData = new JLabel(tr("Data:"));93 /* I18n: Refer to a OSM data file in session file */ link = new JRadioButton(tr("local file"));94 link.putClientProperty("actionname", "link");95 link.setToolTipText(tr("Link to a OSM data file on your local disk."));96 /* I18n: Include OSM data in session file */ include = new JRadioButton(tr("include"));97 include.setToolTipText(tr("Include OSM data in the .joz session file."));98 include.putClientProperty("actionname", "include");99 ButtonGroup group = new ButtonGroup();100 group.add(link);101 group.add(include);102 103 JPanel cardLink = new JPanel(new GridBagLayout());104 final File file = layer.getAssociatedFile();105 final LayerSaveAction saveAction = new LayerSaveAction();106 final JButton save = new JButton(saveAction);107 if (file != null && file.exists()) {108 JosmTextField tf = new JosmTextField();109 tf.setText(file.getPath());110 tf.setEditable(false);111 cardLink.add(tf, GBC.std());112 save.setMargin(new Insets(0, 0, 0, 0));113 cardLink.add(save, GBC.eol().insets(2, 0, 0, 0));114 } else {115 cardLink.add(new JLabel(tr("No file association")), GBC.eol());116 }117 118 JPanel cardInclude = new JPanel(new GridBagLayout());119 JLabel lblIncl = new JLabel(tr("OSM data will be included in the session file."));120 lblIncl.setFont(lblIncl.getFont().deriveFont(Font.PLAIN));121 cardInclude.add(lblIncl, GBC.eol().fill(GBC.HORIZONTAL));122 123 final CardLayout cl = new CardLayout();124 final JPanel cards = new JPanel(cl);125 cards.add(cardLink, "link");126 cards.add(cardInclude, "include");127 128 if (file != null && file.exists()) {129 link.setSelected(true);130 } else {131 link.setEnabled(false);132 link.setToolTipText(tr("No file association"));133 include.setSelected(true);134 cl.show(cards, "include");135 }136 137 link.addActionListener(new ActionListener() {138 @Override139 public void actionPerformed(ActionEvent e) {140 cl.show(cards, "link");141 }142 });143 include.addActionListener(new ActionListener() {144 @Override145 public void actionPerformed(ActionEvent e) {146 cl.show(cards, "include");147 }148 });149 150 topRow.add(export, GBC.std());151 topRow.add(lbl, GBC.std());152 topRow.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));153 p.add(topRow, GBC.eol().fill(GBC.HORIZONTAL));154 p.add(lblData, GBC.std().insets(10, 0, 0, 0));155 p.add(link, GBC.std());156 p.add(include, GBC.eol());157 p.add(cards, GBC.eol().insets(15, 0, 3, 3));158 159 export.addItemListener(new ItemListener() {160 @Override161 public void itemStateChanged(ItemEvent e) {162 if (e.getStateChange() == ItemEvent.DESELECTED) {163 GuiHelper.setEnabledRec(p, false);164 export.setEnabled(true);165 } else {166 GuiHelper.setEnabledRec(p, true);167 save.setEnabled(saveAction.isEnabled());168 link.setEnabled(file != null && file.exists());169 }170 }171 });172 return p;173 }174 175 @Override176 public boolean requiresZip() {177 return include.isSelected();178 }179 180 @Override181 public Element export(ExportSupport support) throws IOException {182 Element layerEl = support.createElement("layer");183 layerEl.setAttribute("type", "osm-data");184 layerEl.setAttribute("version", "0.1");185 186 Element file = support.createElement("file");187 layerEl.appendChild(file);188 189 if (requiresZip()) {190 String zipPath = "layers/" + String.format("%02d", support.getLayerIndex()) + "/data.osm";191 file.appendChild(support.createTextNode(zipPath));192 addDataFile(support.getOutputStreamZip(zipPath));193 } else {194 URI uri = layer.getAssociatedFile().toURI();195 URL url = null;196 try {197 url = uri.toURL();198 } catch (MalformedURLException e) {199 throw new IOException(e);200 }201 file.appendChild(support.createTextNode(url.toString()));202 }203 return layerEl;204 }205 206 29 protected void addDataFile(OutputStream out) { 207 30 Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
Note:
See TracChangeset
for help on using the changeset viewer.