001 // License: GPL. For details, see LICENSE file. 002 package org.openstreetmap.josm.io.session; 003 004 import static org.openstreetmap.josm.tools.I18n.tr; 005 006 import java.awt.CardLayout; 007 import java.awt.Font; 008 import java.awt.GridBagLayout; 009 import java.awt.Insets; 010 import java.awt.event.ActionEvent; 011 import java.awt.event.ActionListener; 012 import java.awt.event.ItemEvent; 013 import java.awt.event.ItemListener; 014 import java.io.File; 015 import java.io.IOException; 016 import java.io.OutputStream; 017 import java.io.OutputStreamWriter; 018 import java.io.PrintWriter; 019 import java.io.UnsupportedEncodingException; 020 import java.io.Writer; 021 import java.net.MalformedURLException; 022 import java.net.URI; 023 import java.net.URL; 024 import java.util.Collection; 025 import java.util.Collections; 026 027 import javax.swing.AbstractAction; 028 import javax.swing.ButtonGroup; 029 import javax.swing.JButton; 030 import javax.swing.JCheckBox; 031 import javax.swing.JLabel; 032 import javax.swing.JPanel; 033 import javax.swing.JRadioButton; 034 import javax.swing.JTextField; 035 import javax.swing.SwingConstants; 036 037 import org.openstreetmap.josm.actions.SaveAction; 038 import org.openstreetmap.josm.gui.layer.Layer; 039 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 040 import org.openstreetmap.josm.gui.util.GuiHelper; 041 import org.openstreetmap.josm.io.OsmWriter; 042 import org.openstreetmap.josm.io.OsmWriterFactory; 043 import org.openstreetmap.josm.io.session.SessionWriter.ExportSupport; 044 import org.openstreetmap.josm.tools.GBC; 045 import org.openstreetmap.josm.tools.ImageProvider; 046 import org.w3c.dom.Element; 047 048 public class OsmDataSessionExporter implements SessionLayerExporter { 049 050 private OsmDataLayer layer; 051 052 public OsmDataSessionExporter(OsmDataLayer layer) { 053 this.layer = layer; 054 } 055 056 public OsmDataSessionExporter() { 057 } 058 059 public OsmDataSessionExporter newInstance(OsmDataLayer layer) { 060 return new OsmDataSessionExporter(layer); 061 } 062 063 @Override 064 public Collection<Layer> getDependencies() { 065 return Collections.emptySet(); 066 } 067 068 private class LayerSaveAction extends AbstractAction { 069 public LayerSaveAction() { 070 putValue(SMALL_ICON, new ImageProvider("save").setWidth(16).get()); 071 putValue(SHORT_DESCRIPTION, tr("Layer contains unsaved data - save to file.")); 072 updateEnabledState(); 073 } 074 075 public void actionPerformed(ActionEvent e) { 076 SaveAction.getInstance().doSave(layer); 077 updateEnabledState(); 078 } 079 080 public void updateEnabledState() { 081 setEnabled(layer.requiresSaveToFile()); 082 } 083 } 084 085 private JRadioButton link, include; 086 private JCheckBox export; 087 088 @Override 089 public JPanel getExportPanel() { 090 final JPanel p = new JPanel(new GridBagLayout()); 091 JPanel topRow = new JPanel(new GridBagLayout()); 092 export = new JCheckBox(); 093 export.setSelected(true); 094 final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEFT); 095 lbl.setToolTipText(layer.getToolTipText()); 096 097 JLabel lblData = new JLabel(tr("Data:")); 098 /* I18n: Refer to a OSM data file in session file */ link = new JRadioButton(tr("local file")); 099 link.putClientProperty("actionname", "link"); 100 link.setToolTipText(tr("Link to a OSM data file on your local disk.")); 101 /* I18n: Include OSM data in session file */ include = new JRadioButton(tr("include")); 102 include.setToolTipText(tr("Include OSM data in the .joz session file.")); 103 include.putClientProperty("actionname", "include"); 104 ButtonGroup group = new ButtonGroup(); 105 group.add(link); 106 group.add(include); 107 108 JPanel cardLink = new JPanel(new GridBagLayout()); 109 final File file = layer.getAssociatedFile(); 110 final LayerSaveAction saveAction = new LayerSaveAction(); 111 final JButton save = new JButton(saveAction); 112 if (file != null) { 113 JTextField tf = new JTextField(); 114 tf.setText(file.getPath()); 115 tf.setEditable(false); 116 cardLink.add(tf, GBC.std()); 117 save.setMargin(new Insets(0,0,0,0)); 118 cardLink.add(save, GBC.eol().insets(2,0,0,0)); 119 } else { 120 cardLink.add(new JLabel(tr("No file association")), GBC.eol()); 121 } 122 123 JPanel cardInclude = new JPanel(new GridBagLayout()); 124 JLabel lblIncl = new JLabel(tr("OSM data will be included in the session file.")); 125 lblIncl.setFont(lblIncl.getFont().deriveFont(Font.PLAIN)); 126 cardInclude.add(lblIncl, GBC.eol().fill(GBC.HORIZONTAL)); 127 128 final CardLayout cl = new CardLayout(); 129 final JPanel cards = new JPanel(cl); 130 cards.add(cardLink, "link"); 131 cards.add(cardInclude, "include"); 132 133 if (file != null) { 134 link.setSelected(true); 135 } else { 136 link.setEnabled(false); 137 link.setToolTipText(tr("No file association")); 138 include.setSelected(true); 139 cl.show(cards, "include"); 140 } 141 142 link.addActionListener(new ActionListener() { 143 public void actionPerformed(ActionEvent e) { 144 cl.show(cards, "link"); 145 } 146 }); 147 include.addActionListener(new ActionListener() { 148 public void actionPerformed(ActionEvent e) { 149 cl.show(cards, "include"); 150 } 151 }); 152 153 topRow.add(export, GBC.std()); 154 topRow.add(lbl, GBC.std()); 155 topRow.add(GBC.glue(1,0), GBC.std().fill(GBC.HORIZONTAL)); 156 p.add(topRow, GBC.eol().fill(GBC.HORIZONTAL)); 157 p.add(lblData, GBC.std().insets(10,0,0,0)); 158 p.add(link, GBC.std()); 159 p.add(include, GBC.eol()); 160 p.add(cards, GBC.eol().insets(15,0,3,3)); 161 162 export.addItemListener(new ItemListener() { 163 public void itemStateChanged(ItemEvent e) { 164 if (e.getStateChange() == ItemEvent.DESELECTED) { 165 GuiHelper.setEnabledRec(p, false); 166 export.setEnabled(true); 167 } else { 168 GuiHelper.setEnabledRec(p, true); 169 save.setEnabled(saveAction.isEnabled()); 170 link.setEnabled(file != null); 171 } 172 } 173 }); 174 return p; 175 } 176 177 @Override 178 public boolean shallExport() { 179 return export.isSelected(); 180 } 181 182 @Override 183 public boolean requiresZip() { 184 return include.isSelected(); 185 } 186 187 @Override 188 public Element export(ExportSupport support) throws IOException { 189 Element layerEl = support.createElement("layer"); 190 layerEl.setAttribute("type", "osm-data"); 191 layerEl.setAttribute("version", "0.1"); 192 193 Element file = support.createElement("file"); 194 layerEl.appendChild(file); 195 196 if (requiresZip()) { 197 String zipPath = "layers/" + String.format("%02d", support.getLayerIndex()) + "/data.osm"; 198 file.appendChild(support.createTextNode(zipPath)); 199 addDataFile(support.getOutputStreamZip(zipPath)); 200 } else { 201 URI uri = layer.getAssociatedFile().toURI(); 202 URL url = null; 203 try { 204 url = uri.toURL(); 205 } catch (MalformedURLException e) { 206 throw new IOException(e); 207 } 208 file.appendChild(support.createTextNode(url.toString())); 209 } 210 return layerEl; 211 } 212 213 protected void addDataFile(OutputStream out) throws IOException { 214 Writer writer = null; 215 try { 216 writer = new OutputStreamWriter(out, "UTF-8"); 217 } catch (UnsupportedEncodingException e) { 218 throw new RuntimeException(e); 219 } 220 OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, layer.data.getVersion()); 221 layer.data.getReadLock().lock(); 222 try { 223 w.writeLayer(layer); 224 w.flush(); 225 } finally { 226 layer.data.getReadLock().unlock(); 227 } 228 } 229 } 230