source: osm/applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/newlayer/NewLayerFromFileAction.java@ 27231

Last change on this file since 27231 was 27231, checked in by larry0ua, 14 years ago

'PicLayer - changed marker icon to make it more visible; #5451; #7124 - first stage'

File size: 7.6 KB
Line 
1/***************************************************************************
2 * Copyright (C) 2009 by Tomasz Stelmach *
3 * http://www.stelmach-online.net/ *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21package org.openstreetmap.josm.plugins.piclayer.actions.newlayer;
22
23import static org.openstreetmap.josm.tools.I18n.tr;
24
25import java.awt.event.ActionEvent;
26import java.io.File;
27import java.io.IOException;
28import java.util.ArrayList;
29import java.util.List;
30
31import javax.imageio.ImageIO;
32import javax.swing.JFileChooser;
33import javax.swing.JOptionPane;
34import javax.swing.filechooser.FileFilter;
35
36import org.openstreetmap.josm.Main;
37import org.openstreetmap.josm.actions.JosmAction;
38import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
39import org.openstreetmap.josm.gui.layer.Layer;
40import org.openstreetmap.josm.plugins.piclayer.layer.PicLayerAbstract;
41import org.openstreetmap.josm.plugins.piclayer.layer.PicLayerFromFile;
42import org.openstreetmap.josm.plugins.piclayer.layer.PicLayerFromKML;
43import org.openstreetmap.josm.plugins.piclayer.layer.kml.KMLGroundOverlay;
44import org.openstreetmap.josm.plugins.piclayer.layer.kml.KMLReader;
45
46/**
47 * Action responsible for creation of new layers based on image files.
48 */
49@SuppressWarnings("serial")
50public class NewLayerFromFileAction extends JosmAction {
51
52 String m_lastdirprefname = "piclayer.lastdir";
53
54 /**
55 * Provides filtering of only image files.
56 */
57 private class ImageFileFilter extends FileFilter {
58
59 private String[] supportedExtensions;
60
61 public ImageFileFilter() {
62 List<String> extensions = new ArrayList<String>();
63 extensions.add("zip");
64 extensions.add("kml");
65 for (String ext : ImageIO.getReaderFormatNames())
66 extensions.add(ext);
67 supportedExtensions = extensions.toArray(new String[0]);
68 }
69
70 @Override
71 public boolean accept(File f) {
72 if ( f.isDirectory() )
73 return true;
74
75 String fileExtension = PicLayerFromFile.getFileExtension(f);
76
77 // Unfortunately, getReaderFormatNames does not always return ALL extensions in
78 // both lower and upper case, so we can not do a search in the array
79 for (String e: supportedExtensions)
80 if ( e.equalsIgnoreCase(fileExtension) ) {
81 return true;
82 }
83
84 return false;
85 }
86
87
88 @Override
89 public String getDescription() {
90 return tr("Supported image files, *.zip, *.kml");
91 }
92
93 }
94
95 /**
96 * Constructor...
97 */
98 public NewLayerFromFileAction() {
99 super(tr("New picture layer from file..."), "layericon24", null, null, false);
100 }
101
102 /**
103 * Action handler
104 */
105 @Override
106 public void actionPerformed(ActionEvent arg0) {
107
108 // Choose a file
109 JFileChooser fc = new JFileChooser(Main.pref.get(m_lastdirprefname));
110 fc.setAcceptAllFileFilterUsed( true );
111 fc.setFileFilter( new ImageFileFilter() );
112
113 fc.setMultiSelectionEnabled(true);
114 int result = fc.showOpenDialog( Main.parent );
115
116 // Create a layer?
117 if ( result == JFileChooser.APPROVE_OPTION ) {
118 // The first loaded layer will be placed at the top of any other layer of the same class,
119 // or at the bottom of the stack if there is no such layer yet
120 // The next layers we load will be placed one after the other after this first layer
121 int newLayerPos = Main.map.mapView.getAllLayers().size();
122 for(Layer l : Main.map.mapView.getLayersOfType(PicLayerAbstract.class)) {
123 int pos = Main.map.mapView.getLayerPos(l);
124 if (pos < newLayerPos) newLayerPos = pos;
125 }
126
127 for(File file : fc.getSelectedFiles() ) {
128 // TODO: we need a progress bar here, it can take quite some time
129
130 Main.pref.put(m_lastdirprefname, file.getParent());
131
132 // Create layer from file
133 if ("kml".equalsIgnoreCase(PicLayerFromFile.getFileExtension(file))) {
134 KMLReader kml = new KMLReader(file);
135 kml.process();
136 JOptionPane.showMessageDialog(null, tr("KML calibration is in beta stage and may produce incorrectly calibrated layers!\nPlease use http://josm.openstreetmap.de/ticket/5451 to upload your KMLs that were calibrated incorrectly."));
137 for (KMLGroundOverlay overlay : kml.getGroundOverlays()) {
138 //TODO: zoom to whole picture, not only the last
139 addNewLayerFromKML(file, overlay, newLayerPos);
140 }
141
142 } else {
143 addNewLayerFromFile(file, newLayerPos, fc.getSelectedFiles().length == 1);
144
145 }
146 }
147 }
148}
149
150 private void addNewLayerFromFile(File file, int newLayerPos, boolean isZoomToLayer) {
151 try {
152 PicLayerFromFile layer = new PicLayerFromFile( file );
153 layer.initialize();
154
155 placeLayer(layer, newLayerPos, isZoomToLayer);
156 }
157 catch (IOException e) {
158 // Failed
159 System.out.println( "NewLayerFromFileAction::actionPerformed - " + e.getMessage() );
160 JOptionPane.showMessageDialog(null, e.getMessage() );
161 }
162 }
163
164 private void placeLayer(PicLayerAbstract layer, int newLayerPos, boolean isZoomToLayer) throws IOException {
165 // Add layer only if successfully initialized
166
167 Main.main.addLayer( layer );
168 Main.map.mapView.moveLayer(layer, newLayerPos++);
169
170 if ( isZoomToLayer && Main.pref.getInteger("piclayer.zoom-on-load", 1) != 0 ) {
171 // if we are loading a single picture file, zoom on it, so that the user can see something
172 BoundingXYVisitor v = new BoundingXYVisitor();
173 layer.visitBoundingBox(v);
174 Main.map.mapView.recalculateCenterScale(v);
175 }
176 }
177 private void addNewLayerFromKML(File root, KMLGroundOverlay overlay, int newLayerPos) {
178 try {
179 PicLayerFromKML layer = new PicLayerFromKML(root, overlay);
180 layer.initialize();
181
182 placeLayer(layer, newLayerPos, true);
183 } catch (IOException e) {
184 // Failed
185 System.out.println( "NewLayerFromFileAction::actionPerformed - " + e.getMessage() );
186 JOptionPane.showMessageDialog(null, e.getMessage() );
187 }
188 }
189}
Note: See TracBrowser for help on using the repository browser.