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        private JRadioButton link, include;
052        private JCheckBox export;
053    
054        public OsmDataSessionExporter(OsmDataLayer layer) {
055            this.layer = layer;
056        }
057    
058        public OsmDataSessionExporter() {
059        }
060    
061        @Override
062        public Collection<Layer> getDependencies() {
063            return Collections.emptySet();
064        }
065    
066        private class LayerSaveAction extends AbstractAction {
067            public LayerSaveAction() {
068                putValue(SMALL_ICON, new ImageProvider("save").setWidth(16).get());
069                putValue(SHORT_DESCRIPTION, layer.requiresSaveToFile() ? 
070                        tr("Layer contains unsaved data - save to file.") :
071                        tr("Layer does not contain unsaved data."));
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        @Override
086        public JPanel getExportPanel() {
087            final JPanel p = new JPanel(new GridBagLayout());
088            JPanel topRow = new JPanel(new GridBagLayout());
089            export = new JCheckBox();
090            export.setSelected(true);
091            final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEFT);
092            lbl.setToolTipText(layer.getToolTipText());
093    
094            JLabel lblData = new JLabel(tr("Data:"));
095            /* I18n: Refer to a OSM data file in session file */ link = new JRadioButton(tr("local file"));
096            link.putClientProperty("actionname", "link");
097            link.setToolTipText(tr("Link to a OSM data file on your local disk."));
098            /* I18n: Include OSM data in session file */ include = new JRadioButton(tr("include"));
099            include.setToolTipText(tr("Include OSM data in the .joz session file."));
100            include.putClientProperty("actionname", "include");
101            ButtonGroup group = new ButtonGroup();
102            group.add(link);
103            group.add(include);
104    
105            JPanel cardLink = new JPanel(new GridBagLayout());
106            final File file = layer.getAssociatedFile();
107            final LayerSaveAction saveAction = new LayerSaveAction();
108            final JButton save = new JButton(saveAction);
109            if (file != null) {
110                JTextField tf = new JTextField();
111                tf.setText(file.getPath());
112                tf.setEditable(false);
113                cardLink.add(tf, GBC.std());
114                save.setMargin(new Insets(0,0,0,0));
115                cardLink.add(save, GBC.eol().insets(2,0,0,0));
116            } else {
117                cardLink.add(new JLabel(tr("No file association")), GBC.eol());
118            }
119    
120            JPanel cardInclude = new JPanel(new GridBagLayout());
121            JLabel lblIncl = new JLabel(tr("OSM data will be included in the session file."));
122            lblIncl.setFont(lblIncl.getFont().deriveFont(Font.PLAIN));
123            cardInclude.add(lblIncl, GBC.eol().fill(GBC.HORIZONTAL));
124    
125            final CardLayout cl = new CardLayout();
126            final JPanel cards = new JPanel(cl);
127            cards.add(cardLink, "link");
128            cards.add(cardInclude, "include");
129    
130            if (file != null) {
131                link.setSelected(true);
132            } else {
133                link.setEnabled(false);
134                link.setToolTipText(tr("No file association"));
135                include.setSelected(true);
136                cl.show(cards, "include");
137            }
138    
139            link.addActionListener(new ActionListener() {
140                public void actionPerformed(ActionEvent e) {
141                    cl.show(cards, "link");
142                }
143            });
144            include.addActionListener(new ActionListener() {
145                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                public void itemStateChanged(ItemEvent e) {
161                    if (e.getStateChange() == ItemEvent.DESELECTED) {
162                        GuiHelper.setEnabledRec(p, false);
163                        export.setEnabled(true);
164                    } else {
165                        GuiHelper.setEnabledRec(p, true);
166                        save.setEnabled(saveAction.isEnabled());
167                        link.setEnabled(file != null);
168                    }
169                }
170            });
171            return p;
172        }
173    
174        @Override
175        public boolean shallExport() {
176            return export.isSelected();
177        }
178    
179        @Override
180        public boolean requiresZip() {
181            return include.isSelected();
182        }
183    
184        @Override
185        public Element export(ExportSupport support) throws IOException {
186            Element layerEl = support.createElement("layer");
187            layerEl.setAttribute("type", "osm-data");
188            layerEl.setAttribute("version", "0.1");
189    
190            Element file = support.createElement("file");
191            layerEl.appendChild(file);
192    
193            if (requiresZip()) {
194                String zipPath = "layers/" + String.format("%02d", support.getLayerIndex()) + "/data.osm";
195                file.appendChild(support.createTextNode(zipPath));
196                addDataFile(support.getOutputStreamZip(zipPath));
197            } else {
198                URI uri = layer.getAssociatedFile().toURI();
199                URL url = null;
200                try {
201                    url = uri.toURL();
202                } catch (MalformedURLException e) {
203                    throw new IOException(e);
204                }
205                file.appendChild(support.createTextNode(url.toString()));
206            }
207            return layerEl;
208        }
209    
210        protected void addDataFile(OutputStream out) throws IOException {
211            Writer writer = null;
212            try {
213                writer = new OutputStreamWriter(out, "UTF-8");
214            } catch (UnsupportedEncodingException e) {
215                throw new RuntimeException(e);
216            }
217            OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, layer.data.getVersion());
218            layer.data.getReadLock().lock();
219            try {
220                w.writeLayer(layer);
221                w.flush();
222            } finally {
223                layer.data.getReadLock().unlock();
224            }
225        }
226    }
227