| 1 | package cadastre_fr;
|
|---|
| 2 |
|
|---|
| 3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 4 |
|
|---|
| 5 | import java.awt.GridBagLayout;
|
|---|
| 6 | import java.awt.event.ActionEvent;
|
|---|
| 7 | import java.util.ArrayList;
|
|---|
| 8 |
|
|---|
| 9 | import javax.swing.JComboBox;
|
|---|
| 10 | import javax.swing.JLabel;
|
|---|
| 11 | import javax.swing.JOptionPane;
|
|---|
| 12 | import javax.swing.JPanel;
|
|---|
| 13 | import javax.swing.JTextField;
|
|---|
| 14 |
|
|---|
| 15 | import org.openstreetmap.josm.Main;
|
|---|
| 16 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 17 | import org.openstreetmap.josm.data.projection.Lambert;
|
|---|
| 18 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 19 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 20 |
|
|---|
| 21 | public class MenuActionNewLocation extends JosmAction {
|
|---|
| 22 |
|
|---|
| 23 | private static final long serialVersionUID = 1L;
|
|---|
| 24 |
|
|---|
| 25 | public MenuActionNewLocation() {
|
|---|
| 26 | super(tr("Change location"), "cadastre_small", tr("Set a new location for the next request"), null, false);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | public void actionPerformed(ActionEvent e) {
|
|---|
| 30 | WMSLayer wmsLayer = addNewLayer(new ArrayList<WMSLayer>());
|
|---|
| 31 | if (wmsLayer != null)
|
|---|
| 32 | DownloadWMSTask.download(wmsLayer);
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | public WMSLayer addNewLayer(ArrayList<WMSLayer> existingLayers) {
|
|---|
| 36 | if (Main.map == null) {
|
|---|
| 37 | JOptionPane.showMessageDialog(Main.parent,
|
|---|
| 38 | tr("Open a layer first (GPX, OSM, cache)"));
|
|---|
| 39 | return null;
|
|---|
| 40 | /*
|
|---|
| 41 | } else if (existingLayers != null && existingLayers.size() > 0) {
|
|---|
| 42 | JOptionPane.showMessageDialog(Main.parent,
|
|---|
| 43 | tr("Select one cadastre layer first"));
|
|---|
| 44 | return null;*/
|
|---|
| 45 | } else {
|
|---|
| 46 | String location = "";
|
|---|
| 47 | String codeDepartement = "";
|
|---|
| 48 | String codeCommune = "";
|
|---|
| 49 | boolean resetCookie = false;
|
|---|
| 50 | JLabel labelSectionNewLocation = new JLabel(tr("Add a new layer"));
|
|---|
| 51 | JPanel p = new JPanel(new GridBagLayout());
|
|---|
| 52 | JLabel labelLocation = new JLabel(tr("Location"));
|
|---|
| 53 | final JTextField inputTown = new JTextField( Main.pref.get("cadastrewms.location") );
|
|---|
| 54 | inputTown.setToolTipText(tr("<html>Enter the town,village or city name.<br>"
|
|---|
| 55 | + "Use the syntax and punctuation known by www.cadastre.gouv.fr .</html>"));
|
|---|
| 56 | JComboBox inputWMSList = null;
|
|---|
| 57 |
|
|---|
| 58 | p.add(labelSectionNewLocation, GBC.eol());
|
|---|
| 59 | p.add(labelLocation, GBC.std().insets(10, 0, 0, 0));
|
|---|
| 60 | p.add(inputTown, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
|
|---|
| 61 | JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null) {
|
|---|
| 62 | private static final long serialVersionUID = 1L;
|
|---|
| 63 |
|
|---|
| 64 | @Override
|
|---|
| 65 | public void selectInitialValue() {
|
|---|
| 66 | inputTown.requestFocusInWindow();
|
|---|
| 67 | inputTown.selectAll();
|
|---|
| 68 | }
|
|---|
| 69 | };
|
|---|
| 70 | pane.createDialog(Main.parent, tr("Add new layer")).setVisible(true);
|
|---|
| 71 | if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
|
|---|
| 72 | return null;
|
|---|
| 73 |
|
|---|
| 74 | WMSLayer wmsLayer = null;
|
|---|
| 75 | if (!inputTown.getText().equals("")) {
|
|---|
| 76 | location = inputTown.getText().toUpperCase();
|
|---|
| 77 | resetCookie = true;
|
|---|
| 78 | Main.pref.put("cadastrewms.location", location);
|
|---|
| 79 | Main.pref.put("cadastrewms.codeCommune", codeCommune);
|
|---|
| 80 | for (Layer l : Main.map.mapView.getAllLayers()) {
|
|---|
| 81 | if (l instanceof WMSLayer && l.name.equalsIgnoreCase(location + codeDepartement)) {
|
|---|
| 82 | return null;
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | // add the layer if it doesn't exist
|
|---|
| 86 | wmsLayer = new WMSLayer(location, codeCommune, Lambert.layoutZone);
|
|---|
| 87 | Main.main.addLayer(wmsLayer);
|
|---|
| 88 | System.out.println("Add new layer with Location:" + inputTown.getText());
|
|---|
| 89 | } else if (existingLayers != null && existingLayers.size() > 0 && inputWMSList.getSelectedIndex() > 0) {
|
|---|
| 90 | wmsLayer = existingLayers.get(inputWMSList.getSelectedIndex()-1);
|
|---|
| 91 | resetCookie = true;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | if (resetCookie)
|
|---|
| 95 | CadastrePlugin.cadastreGrabber.getWmsInterface().resetCookieIfNewLayer(wmsLayer.name);
|
|---|
| 96 | return wmsLayer;
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | }
|
|---|