| 1 | package cadastre_fr;
|
|---|
| 2 |
|
|---|
| 3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 4 |
|
|---|
| 5 | import java.awt.event.ActionEvent;
|
|---|
| 6 | import java.awt.event.ActionListener;
|
|---|
| 7 | import javax.swing.*;
|
|---|
| 8 | import org.openstreetmap.josm.Main;
|
|---|
| 9 | import org.openstreetmap.josm.gui.preferences.PreferenceDialog;
|
|---|
| 10 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
|---|
| 11 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 12 | import org.openstreetmap.josm.tools.I18n;
|
|---|
| 13 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * Preference settings for the French Cadastre plugin
|
|---|
| 17 | *
|
|---|
| 18 | * @author Pieren <pieren3@gmail.com>
|
|---|
| 19 | */
|
|---|
| 20 | public class CadastrePreferenceSetting implements PreferenceSetting {
|
|---|
| 21 |
|
|---|
| 22 | static final int TRANS_MIN = 1;
|
|---|
| 23 | static final int TRANS_MAX = 10;
|
|---|
| 24 | private JSlider sliderTrans = new JSlider(JSlider.HORIZONTAL, TRANS_MIN, TRANS_MAX, TRANS_MAX);
|
|---|
| 25 |
|
|---|
| 26 | private JTextField sourcing = new JTextField(20);
|
|---|
| 27 |
|
|---|
| 28 | private JCheckBox alterColors = new JCheckBox(tr("Replace original background by JOSM background color."));
|
|---|
| 29 |
|
|---|
| 30 | private JCheckBox reversGrey = new JCheckBox(tr("Reverse grey colors (for black backgrounds)."));
|
|---|
| 31 |
|
|---|
| 32 | private JCheckBox transparency = new JCheckBox(tr("Set background transparent."));
|
|---|
| 33 |
|
|---|
| 34 | private JCheckBox drawBoundaries = new JCheckBox(tr("Draw boundaries of downloaded data."));
|
|---|
| 35 |
|
|---|
| 36 | private JRadioButton grabMultiplier1 = new JRadioButton("", true);
|
|---|
| 37 |
|
|---|
| 38 | private JRadioButton grabMultiplier2 = new JRadioButton("", true);
|
|---|
| 39 |
|
|---|
| 40 | private JRadioButton grabMultiplier3 = new JRadioButton("", true);
|
|---|
| 41 |
|
|---|
| 42 | private JRadioButton grabMultiplier4 = new JRadioButton("", true);
|
|---|
| 43 |
|
|---|
| 44 | static final int DEFAULT_SQUARE_SIZE = 100;
|
|---|
| 45 | private JTextField grabMultiplier4Size = new JTextField(5);
|
|---|
| 46 |
|
|---|
| 47 | private JCheckBox enableCache = new JCheckBox(tr("Enable automatic caching."));
|
|---|
| 48 |
|
|---|
| 49 | static final int DEFAULT_CACHE_SIZE = 500;
|
|---|
| 50 | JLabel jLabelCacheSize = new JLabel(tr("Max. cache size (in MB)"));
|
|---|
| 51 | private JTextField cacheSize = new JTextField(20);
|
|---|
| 52 |
|
|---|
| 53 | public void addGui(final PreferenceDialog gui) {
|
|---|
| 54 |
|
|---|
| 55 | String description = tr("A special handler of the French cadastre wms at www.cadastre.gouv.fr" + "<BR><BR>"
|
|---|
| 56 | + "Please read the Terms and Conditions of Use here (in French): <br>"
|
|---|
| 57 | + "<a href=\"http://www.cadastre.gouv.fr/scpc/html/CU_01_ConditionsGenerales_fr.html\"> "
|
|---|
| 58 | + "http://www.cadastre.gouv.fr/scpc/html/CU_01_ConditionsGenerales_fr.html</a> <BR>"
|
|---|
| 59 | + "before any upload of data created by this plugin.");
|
|---|
| 60 | JPanel cadastrewms = gui.createPreferenceTab("cadastrewms.gif", I18n.tr("French cadastre WMS"), description);
|
|---|
| 61 |
|
|---|
| 62 | // option to automatically set the source tag when uploading
|
|---|
| 63 | sourcing.setText(CadastrePlugin.source);
|
|---|
| 64 | sourcing.setToolTipText(tr("<html>Value of key \"source\" when autosourcing is enabled</html>"));
|
|---|
| 65 | JLabel jLabelSource = new JLabel(tr("Source"));
|
|---|
| 66 | cadastrewms.add(jLabelSource, GBC.eop().insets(0, 0, 0, 0));
|
|---|
| 67 | cadastrewms.add(sourcing, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5));
|
|---|
| 68 |
|
|---|
| 69 | // option to alter the original colors of the wms images
|
|---|
| 70 | alterColors.setSelected(Main.pref.getBoolean("cadastrewms.alterColors", false));
|
|---|
| 71 | alterColors.setToolTipText(tr("Replace the original white background by the backgound color defined in JOSM preferences."));
|
|---|
| 72 | cadastrewms.add(alterColors, GBC.eop().insets(0, 0, 0, 0));
|
|---|
| 73 |
|
|---|
| 74 | // option to reverse the grey colors (to see texts background)
|
|---|
| 75 | reversGrey.setSelected(Main.pref.getBoolean("cadastrewms.invertGrey", false));
|
|---|
| 76 | reversGrey.setToolTipText(tr("Invert the original black and white colors (and all intermediate greys). Useful for texts on dark backgrounds."));
|
|---|
| 77 | reversGrey.setEnabled(alterColors.isSelected());
|
|---|
| 78 | cadastrewms.add(reversGrey, GBC.eop().insets(00, 0, 0, 0));
|
|---|
| 79 |
|
|---|
| 80 | // option to enable transparency
|
|---|
| 81 | transparency.addActionListener(new ActionListener() {
|
|---|
| 82 | public void actionPerformed(ActionEvent e) {
|
|---|
| 83 | sliderTrans.setEnabled(transparency.isSelected());
|
|---|
| 84 | }
|
|---|
| 85 | });
|
|---|
| 86 | transparency.setSelected(Main.pref.getBoolean("cadastrewms.backgroundTransparent", false));
|
|---|
| 87 | transparency.setToolTipText(tr("Allows multiple layers stacking"));
|
|---|
| 88 | cadastrewms.add(transparency, GBC.eop().insets(0, 0, 0, 0));
|
|---|
| 89 |
|
|---|
| 90 | // slider for transparency level
|
|---|
| 91 | sliderTrans.setSnapToTicks(true);
|
|---|
| 92 | sliderTrans.setToolTipText(tr("Set WMS layers transparency. Right is opaque, left is transparent."));
|
|---|
| 93 | sliderTrans.setMajorTickSpacing(10);
|
|---|
| 94 | sliderTrans.setMinorTickSpacing(1);
|
|---|
| 95 | sliderTrans.setValue((int)(Float.parseFloat(Main.pref.get("cadastrewms.brightness", "1.0f"))*10));
|
|---|
| 96 | sliderTrans.setPaintTicks(true);
|
|---|
| 97 | sliderTrans.setPaintLabels(false);
|
|---|
| 98 | cadastrewms.add(sliderTrans, GBC.eol().fill(GBC.HORIZONTAL).insets(20, 0, 250, 0));
|
|---|
| 99 |
|
|---|
| 100 | // option to draw boundaries of downloaded data
|
|---|
| 101 | drawBoundaries.setSelected(Main.pref.getBoolean("cadastrewms.drawBoundaries", false));
|
|---|
| 102 | drawBoundaries.setToolTipText(tr("Draw a rectangle around downloaded data from WMS server."));
|
|---|
| 103 | cadastrewms.add(drawBoundaries, GBC.eop().insets(0, 0, 0, 5));
|
|---|
| 104 |
|
|---|
| 105 | // the downloaded images multiplier
|
|---|
| 106 | JLabel jLabelScale = new JLabel(tr("Image grab multiplier:"));
|
|---|
| 107 | cadastrewms.add(jLabelScale, GBC.std().insets(0, 5, 10, 0));
|
|---|
| 108 | ButtonGroup bg = new ButtonGroup();
|
|---|
| 109 | ActionListener multiplierActionListener = new ActionListener() {
|
|---|
| 110 | public void actionPerformed(ActionEvent actionEvent) {
|
|---|
| 111 | AbstractButton button = (AbstractButton) actionEvent.getSource();
|
|---|
| 112 | grabMultiplier4Size.setEnabled(button == grabMultiplier4);
|
|---|
| 113 | }
|
|---|
| 114 | };
|
|---|
| 115 | grabMultiplier1.setIcon(ImageProvider.get("preferences", "unsel_box_1"));
|
|---|
| 116 | grabMultiplier1.setSelectedIcon(ImageProvider.get("preferences", "sel_box_1"));
|
|---|
| 117 | grabMultiplier1.addActionListener( multiplierActionListener);
|
|---|
| 118 | grabMultiplier2.setIcon(ImageProvider.get("preferences", "unsel_box_2"));
|
|---|
| 119 | grabMultiplier2.setSelectedIcon(ImageProvider.get("preferences", "sel_box_2"));
|
|---|
| 120 | grabMultiplier2.addActionListener( multiplierActionListener);
|
|---|
| 121 | grabMultiplier2.setToolTipText(tr("Grab smaller images (higher quality but use more memory)"));
|
|---|
| 122 | grabMultiplier3.setIcon(ImageProvider.get("preferences", "unsel_box_3"));
|
|---|
| 123 | grabMultiplier3.setSelectedIcon(ImageProvider.get("preferences", "sel_box_3"));
|
|---|
| 124 | grabMultiplier3.addActionListener( multiplierActionListener);
|
|---|
| 125 | grabMultiplier3.setToolTipText(tr("Grab smaller images (higher quality but use more memory)"));
|
|---|
| 126 | grabMultiplier4.setIcon(ImageProvider.get("preferences", "unsel_box_4"));
|
|---|
| 127 | grabMultiplier4.setSelectedIcon(ImageProvider.get("preferences", "sel_box_4"));
|
|---|
| 128 | grabMultiplier4.addActionListener( multiplierActionListener);
|
|---|
| 129 | grabMultiplier4.setToolTipText(tr("Fixed size square (default is 100m)"));
|
|---|
| 130 | bg.add(grabMultiplier1);
|
|---|
| 131 | bg.add(grabMultiplier2);
|
|---|
| 132 | bg.add(grabMultiplier3);
|
|---|
| 133 | bg.add(grabMultiplier4);
|
|---|
| 134 | String currentScale = Main.pref.get("cadastrewms.scale", "1");
|
|---|
| 135 | if (currentScale.equals(Scale.X1.value))
|
|---|
| 136 | grabMultiplier1.setSelected(true);
|
|---|
| 137 | if (currentScale.equals(Scale.X2.value))
|
|---|
| 138 | grabMultiplier2.setSelected(true);
|
|---|
| 139 | if (currentScale.equals(Scale.X3.value))
|
|---|
| 140 | grabMultiplier3.setSelected(true);
|
|---|
| 141 | if (currentScale.equals(Scale.SQUARE_100M.value))
|
|---|
| 142 | grabMultiplier4.setSelected(true);
|
|---|
| 143 | cadastrewms.add(grabMultiplier1, GBC.std().insets(5, 0, 5, 0));
|
|---|
| 144 | cadastrewms.add(grabMultiplier2, GBC.std().insets(5, 0, 5, 0));
|
|---|
| 145 | cadastrewms.add(grabMultiplier3, GBC.std().insets(5, 0, 5, 0));
|
|---|
| 146 | cadastrewms.add(grabMultiplier4, GBC.std().insets(5, 0, 5, 0));
|
|---|
| 147 | int squareSize = getNumber("cadastrewms.squareSize", DEFAULT_SQUARE_SIZE);
|
|---|
| 148 | grabMultiplier4Size.setText(String.valueOf(squareSize));
|
|---|
| 149 | grabMultiplier4Size.setToolTipText(tr("Fixed size (from 25 to 1000 meters)"));
|
|---|
| 150 | grabMultiplier4Size.setEnabled(currentScale.equals(Scale.SQUARE_100M.value));
|
|---|
| 151 | cadastrewms.add(grabMultiplier4Size, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 5, 0, 5));
|
|---|
| 152 |
|
|---|
| 153 | // option to enable automatic caching
|
|---|
| 154 | enableCache.addActionListener(new ActionListener() {
|
|---|
| 155 | public void actionPerformed(ActionEvent e) {
|
|---|
| 156 | jLabelCacheSize.setEnabled(enableCache.isSelected());
|
|---|
| 157 | cacheSize.setEnabled(enableCache.isSelected());
|
|---|
| 158 | }
|
|---|
| 159 | });
|
|---|
| 160 | enableCache.setSelected(Main.pref.getBoolean("cadastrewms.enableCaching", true));
|
|---|
| 161 | enableCache.setToolTipText(tr("Replace the original white background by the backgound color defined in JOSM preferences."));
|
|---|
| 162 | cadastrewms.add(enableCache, GBC.eop().insets(0, 0, 0, 0));
|
|---|
| 163 |
|
|---|
| 164 | // option to fix the cache size(in MB)
|
|---|
| 165 | int size = getNumber("cadastrewms.cacheSize", DEFAULT_CACHE_SIZE);
|
|---|
| 166 | cacheSize.setText(String.valueOf(size));
|
|---|
| 167 | cacheSize.setToolTipText(tr("Oldest files are automatically deleted when this size is exceeded"));
|
|---|
| 168 | cadastrewms.add(jLabelCacheSize, GBC.std().insets(20, 0, 0, 0));
|
|---|
| 169 | cadastrewms.add(cacheSize, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 5, 0, 5));
|
|---|
| 170 |
|
|---|
| 171 | cadastrewms.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
|
|---|
| 172 |
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | public boolean ok() {
|
|---|
| 176 | Main.pref.put("cadastrewms.source", sourcing.getText());
|
|---|
| 177 | CadastrePlugin.source = sourcing.getText();
|
|---|
| 178 | Main.pref.put("cadastrewms.alterColors", alterColors.isSelected());
|
|---|
| 179 | Main.pref.put("cadastrewms.invertGrey", reversGrey.isSelected());
|
|---|
| 180 | Main.pref.put("cadastrewms.backgroundTransparent", transparency.isSelected());
|
|---|
| 181 | Main.pref.put("cadastrewms.brightness", Float.toString((float)sliderTrans.getValue()/10));
|
|---|
| 182 | Main.pref.put("cadastrewms.drawBoundaries", drawBoundaries.isSelected());
|
|---|
| 183 | if (grabMultiplier1.isSelected())
|
|---|
| 184 | Main.pref.put("cadastrewms.scale", Scale.X1.toString());
|
|---|
| 185 | else if (grabMultiplier2.isSelected())
|
|---|
| 186 | Main.pref.put("cadastrewms.scale", Scale.X2.toString());
|
|---|
| 187 | else if (grabMultiplier3.isSelected())
|
|---|
| 188 | Main.pref.put("cadastrewms.scale", Scale.X3.toString());
|
|---|
| 189 | else {
|
|---|
| 190 | Main.pref.put("cadastrewms.scale", Scale.SQUARE_100M.toString());
|
|---|
| 191 | try {
|
|---|
| 192 | int squareSize = Integer.parseInt(grabMultiplier4Size.getText());
|
|---|
| 193 | if (squareSize >= 25 && squareSize <= 1000)
|
|---|
| 194 | Main.pref.put("cadastrewms.squareSize", grabMultiplier4Size.getText());
|
|---|
| 195 | } catch (NumberFormatException e) { // ignore the last input
|
|---|
| 196 | }
|
|---|
| 197 | }
|
|---|
| 198 | Main.pref.put("cadastrewms.enableCaching", enableCache.isSelected());
|
|---|
| 199 |
|
|---|
| 200 | // spread data into objects instead of restarting the application
|
|---|
| 201 | try {
|
|---|
| 202 | CacheControl.cacheSize = Integer.parseInt(cacheSize.getText());
|
|---|
| 203 | Main.pref.put("cadastrewms.cacheSize", String.valueOf(CacheControl.cacheSize));
|
|---|
| 204 | } catch (NumberFormatException e) { // ignore the last input
|
|---|
| 205 | }
|
|---|
| 206 | CacheControl.cacheEnabled = enableCache.isSelected();
|
|---|
| 207 | CadastrePlugin.refreshConfiguration();
|
|---|
| 208 |
|
|---|
| 209 | return false;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | private int getNumber(String pref_parameter, int def_value) {
|
|---|
| 213 | try {
|
|---|
| 214 | return Integer.parseInt(Main.pref.get(pref_parameter, String.valueOf(def_value)));
|
|---|
| 215 | } catch (NumberFormatException e) {
|
|---|
| 216 | return def_value;
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|