Changeset 13712 in osm for applications


Ignore:
Timestamp:
2009-02-14T01:13:09+01:00 (15 years ago)
Author:
lvarga
Message:

Added preferences tab
Added possibility to activate/deactivate autozoom and autoload of tiles
Added setting for max zoom level
Done some small refactoring
In plan is to add button for selecting local tiles repository (someone is
using it in this way also)

Location:
applications/editors/josm/plugins/slippymap
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapLayer.java

    r13625 r13712  
    4444{
    4545        public int currentZoomLevel = 14;
    46     ArrayList<HashMap<SlippyMapKey, SlippyMapTile>> tileStorage = null;
     46    //ArrayList<HashMap<SlippyMapKey, SlippyMapTile>> tileStorage = null;
     47        private HashMap<SlippyMapKey, SlippyMapTile>[] tileStorage = null;
    4748
    4849    Point[][]                                  pixelpos    = new Point[21][21];
     
    5960    {
    6061        super(tr("Slippy Map"));
    61         background = true;
     62        background = true;
    6263
    6364        clearTileStorage();
     
    178179     */
    179180    public void increaseZoomLevel() {
    180         //TODO max lvl should be in preferences...
    181         if(currentZoomLevel < 17) {
     181        if(currentZoomLevel < SlippyMapPreferences.getMaxZoomLvl()) {
    182182                currentZoomLevel++;
    183                 loadAllTiles();
     183                //if(SlippyMapPreferences.getAutoloadTiles()) {
     184                //      loadAllTiles();
     185                //}
    184186        }
    185187    }
     
    191193        if(currentZoomLevel > 10) {
    192194                currentZoomLevel--;
    193                 loadAllTiles();
     195                //if(SlippyMapPreferences.getAutoloadTiles()) {
     196                //      loadAllTiles();
     197                //}
    194198        }
    195199    }
    196200   
    197     private void clearTileStorage()
    198     {
    199         tileStorage = new ArrayList<HashMap<SlippyMapKey, SlippyMapTile>>(20);
    200 
    201         for (int i = 0; i < 18; i++)
    202             tileStorage.add(new HashMap<SlippyMapKey, SlippyMapTile>());
     201    public void clearTileStorage()
     202    {
     203        int maxZoom = SlippyMapPreferences.getMaxZoomLvl();
     204        // +1 because of array indexed from 0.
     205        tileStorage = new HashMap[maxZoom+1];
     206
     207        for (int i = 0; i < maxZoom+1; i++)
     208            tileStorage[i] = new HashMap<SlippyMapKey, SlippyMapTile>();
    203209    }
    204210
     
    240246                SlippyMapKey key = new SlippyMapKey(x,y);
    241247
    242                 SlippyMapTile tile = tileStorage.get(currentZoomLevel).get(key);
     248                SlippyMapTile tile = tileStorage[currentZoomLevel].get(key);
    243249
    244250                if (tile == null)
    245                     tileStorage.get(currentZoomLevel).put(key,
     251                    tileStorage[currentZoomLevel].put(key,
    246252                            tile = new SlippyMapTile(x, y, currentZoomLevel));
    247253
     
    321327            {
    322328                SlippyMapKey key = new SlippyMapKey(x,y);
    323                 SlippyMapTile tile = tileStorage.get(currentZoomLevel).get(key);
     329                SlippyMapTile tile;
     330                try
     331                {
     332                        tile = tileStorage[currentZoomLevel].get(key);
     333                } catch (IndexOutOfBoundsException ex) {
     334                        throw new RuntimeException("currentZoomLevel=" + currentZoomLevel + " and tile storage array have just size=" + tileStorage.length + " and maxZoomLvl in preferences is " + SlippyMapPreferences.getMaxZoomLvl() + ".", ex);
     335                }
    324336
    325337                if (tile == null)
    326338                {
    327                     tileStorage.get(currentZoomLevel).put(key,
    328                             tile = new SlippyMapTile(x, y, currentZoomLevel));
     339                        tile = new SlippyMapTile(x, y, currentZoomLevel);
     340                    tileStorage[currentZoomLevel].put(key, tile);
     341                    if(SlippyMapPreferences.getAutoloadTiles()) {
     342                        //TODO probably do on background
     343                        tile.loadImage();
     344                        }
    329345                }
    330346                Image img = tile.getImage();
     
    357373                SlippyMapKey key = new SlippyMapKey(x,y);
    358374                int texty = p.y + 2 + fontHeight;
    359                 SlippyMapTile tile = tileStorage.get(currentZoomLevel).get(key);
     375                SlippyMapTile tile = tileStorage[currentZoomLevel].get(key);
    360376                p = pixelpos[x - z12x0 + 1][y - z12y0 + 2];
    361377                g.drawString("x=" + x + " y=" + y + " z=" + currentZoomLevel + "", p.x + 2, texty);
     
    398414
    399415        if((z12x1 - z12x0 < 2) || (z12y1 - z12y0 < 2)) {
    400                 increaseZoomLevel();
     416                if(SlippyMapPreferences.getAutozoom()) {
     417                        increaseZoomLevel();
     418                }
    401419                this.paint(oldg, mv);
    402420        }
    403421       
    404422        if((z12x1 - z12x0 > 6) || (z12y1 - z12y0 > 6)) {
    405                 decreaseZoomLevel();
     423                if(SlippyMapPreferences.getAutozoom()) {
     424                        decreaseZoomLevel();
     425                }
    406426                this.paint(oldg, mv);
    407427        }
     
    438458
    439459        SlippyMapKey key = new SlippyMapKey(tilex,tiley);
    440         SlippyMapTile tile = tileStorage.get(currentZoomLevel).get(key);
     460        SlippyMapTile tile = tileStorage[currentZoomLevel].get(key);
    441461        if (tile == null)
    442             tileStorage.get(currentZoomLevel).put(key,
     462            tileStorage[currentZoomLevel].put(key,
    443463                    tile = new SlippyMapTile(tilex, tiley, currentZoomLevel));
    444464        return tile;
  • applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapPreferenceSetting.java

    r13625 r13712  
    33import static org.openstreetmap.josm.tools.I18n.tr;
    44
     5import javax.swing.Box;
     6import javax.swing.JCheckBox;
    57import javax.swing.JComboBox;
    68import javax.swing.JLabel;
     9import javax.swing.JPanel;
     10import javax.swing.JSpinner;
    711
    8 import org.openstreetmap.josm.Main;
    912import org.openstreetmap.josm.gui.preferences.PreferenceDialog;
    1013import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
     
    2225     */
    2326    private JComboBox tileSourceCombo;
     27   
     28    private JCheckBox autozoomActive = new JCheckBox(tr("autozoom"));
     29    private JCheckBox autoloadTiles = new JCheckBox(tr("autoload tiles"));
     30    private JSpinner maxZoomLvl = new JSpinner();
    2431
    2532    public void addGui(PreferenceDialog gui)
    2633    {
     34        //String description = tr("A plugin that adds to JOSM new layer. This layer could render external tiles.");
     35        JPanel slippymapTab = gui.createPreferenceTab("slippymap.png", tr("SlippyMap"), tr("Settings for the SlippyMap plugin."));
     36
     37        JPanel mapUrlPanel = new JPanel();
    2738        String[] allMapUrls = SlippyMapPreferences.getAllMapUrls();
    2839        tileSourceCombo = new JComboBox(allMapUrls);
    29 
     40        tileSourceCombo.setEditable(true);
    3041        String source = SlippyMapPreferences.getMapUrl();
    31 
    32         for (int i = 0; i < allMapUrls.length; i++)
    33         {
    34 //            System.err.println("Comparing '" + source + "' to '"
    35 //                   + allMapUrls[i]);
    36 
    37             if (source.equals(allMapUrls[i]))
    38             {
    39                 tileSourceCombo.setSelectedIndex(i);
    40                 break;
    41             }
    42         }
    43 
    44         gui.display.add(new JLabel(tr("Tile Sources")), GBC.std());
    45         gui.display.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
    46         gui.display.add(tileSourceCombo, GBC.eol().fill(GBC.HORIZONTAL));
     42        tileSourceCombo.setSelectedItem(source);
     43        mapUrlPanel.add(new JLabel(tr("Tile Sources")), GBC.std());
     44        mapUrlPanel.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
     45        mapUrlPanel.add(tileSourceCombo, GBC.eol().fill(GBC.HORIZONTAL));
     46       
     47        JPanel autozoomPanel = new JPanel();
     48        autozoomPanel.add(new JLabel(tr("Auto zoom: ")), GBC.std());
     49        autozoomPanel.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
     50        autozoomPanel.add(autozoomActive, GBC.eol().fill(GBC.HORIZONTAL));
     51       
     52        JPanel autoloadPanel = new JPanel();
     53        autoloadPanel.add(new JLabel(tr("Autoload Tiles: ")), GBC.std());
     54        autoloadPanel.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
     55        autoloadPanel.add(autoloadTiles, GBC.eol().fill(GBC.HORIZONTAL));
     56       
     57        JPanel maxZoomLvlPanel = new JPanel();
     58        maxZoomLvlPanel.add(new JLabel(tr("Max zoom lvl: ")), GBC.std());
     59        maxZoomLvlPanel.add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
     60        maxZoomLvlPanel.add(this.maxZoomLvl, GBC.eol().fill(GBC.HORIZONTAL));
     61       
     62        slippymapTab.add(mapUrlPanel, GBC.eol().fill(GBC.HORIZONTAL));
     63        slippymapTab.add(autozoomPanel, GBC.eol().fill(GBC.HORIZONTAL));
     64        slippymapTab.add(autoloadPanel, GBC.eol().fill(GBC.HORIZONTAL));
     65        slippymapTab.add(maxZoomLvlPanel, GBC.eol().fill(GBC.HORIZONTAL));
     66        slippymapTab.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
     67       
     68        this.loadSettings();
    4769    }
    4870
    4971    /**
     72     * <p>
     73     * Load settings from {@link SlippyMapPreferences} class. Loaded preferences are stored to local GUI components.
     74     * Actualy this method loads and sets this params:<br>
     75     * <ul>
     76     *  <li>autozoom - {@link #autozoomActive} - {@link SlippyMapPreferences#getAutozoom()}</li>
     77     *  <li>autoload - {@link #autoloadTiles} - {@link SlippyMapPreferences#getAutoloadTiles()}</li>
     78     *  <li>maxZoomLvl - {@link #maxZoomLvl} - {@link SlippyMapPreferences#getMaxZoomLvl()}</li>
     79     * </ul>
     80     * </p>
     81     */
     82    private void loadSettings() {
     83        this.autozoomActive.setSelected(SlippyMapPreferences.getAutozoom());
     84        this.autoloadTiles.setSelected(SlippyMapPreferences.getAutoloadTiles());
     85        this.maxZoomLvl.setValue(SlippyMapPreferences.getMaxZoomLvl());
     86    }
     87   
     88    /**
     89     * <p>
    5090     * Someone pressed the "ok" button
     91     * </p>
     92     * <p>
     93     * This method saves actual state from GUI objects to actual preferences.
     94     * </p>
    5195     */
    5296    public boolean ok()
    5397    {
    54         Main.pref.put(SlippyMapPreferences.PREFERENCE_TILE_URL, tileSourceCombo.getSelectedItem().toString());
    55         //restart isnt required
     98        SlippyMapPreferences.setMapUrl(this.tileSourceCombo.getSelectedItem().toString());
     99        SlippyMapPreferences.setAutozoom(this.autozoomActive.isSelected());
     100        SlippyMapPreferences.setAutoloadTiles(this.autoloadTiles.isSelected());
     101        SlippyMapPreferences.setMaxZoomLvl((Integer)this.maxZoomLvl.getValue());
     102        //restart isn't required
    56103        return false;
    57104    }
  • applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapPreferences.java

    r13625 r13712  
    1212public class SlippyMapPreferences
    1313{
    14     public static String PREFERENCE_PREFIX   = "slippymap";
     14    public static final String PREFERENCE_PREFIX   = "slippymap";
    1515
    16     public static String PREFERENCE_TILE_URL = PREFERENCE_PREFIX + ".tile_url";
     16    private static final String PREFERENCE_TILE_URL = PREFERENCE_PREFIX + ".tile_url";
     17    private static final String PREFERENCE_AUTOZOOM = PREFERENCE_PREFIX + ".autozoom";
     18    private static final String PREFERENCE_AUTOLOADTILES = PREFERENCE_PREFIX + ".autoload_tiles";
     19    private static final String PREFERENCE_MAX_ZOOM_LVL = PREFERENCE_PREFIX + ".max_zoom_lvl";
    1720   
    1821    public static String getMapUrl()
     
    2831        return url;
    2932    }
     33   
     34    public static void setMapUrl(String mapUrl) {
     35        Main.pref.put(SlippyMapPreferences.PREFERENCE_TILE_URL, mapUrl);
     36    }
     37   
     38    public static boolean getAutozoom()
     39    {
     40        String autozoom = Main.pref.get(PREFERENCE_AUTOZOOM);
    3041
     42        if (autozoom == null || "".equals(autozoom))
     43        {
     44                autozoom = "true";
     45            Main.pref.put(PREFERENCE_AUTOZOOM, autozoom);
     46        }
     47
     48        return Boolean.parseBoolean(autozoom);
     49    }
     50   
     51    public static void setAutozoom(boolean autozoom) {
     52        Main.pref.put(SlippyMapPreferences.PREFERENCE_AUTOZOOM, autozoom);
     53    }
     54   
     55    public static boolean getAutoloadTiles()
     56    {
     57        String autoloadTiles = Main.pref.get(PREFERENCE_AUTOLOADTILES);
     58
     59        if (autoloadTiles == null || "".equals(autoloadTiles))
     60        {
     61                autoloadTiles = "true";
     62            Main.pref.put(PREFERENCE_AUTOLOADTILES, autoloadTiles);
     63        }
     64
     65        return Boolean.parseBoolean(autoloadTiles);
     66    }
     67   
     68    public static void setAutoloadTiles(boolean autoloadTiles) {
     69        Main.pref.put(SlippyMapPreferences.PREFERENCE_AUTOLOADTILES, autoloadTiles);
     70    }
     71
     72    public static int getMaxZoomLvl()
     73    {
     74        String maxZoomLvl = Main.pref.get(PREFERENCE_MAX_ZOOM_LVL);
     75
     76        if (maxZoomLvl == null || "".equals(maxZoomLvl))
     77        {
     78                maxZoomLvl = "17";
     79            Main.pref.put(PREFERENCE_MAX_ZOOM_LVL, maxZoomLvl);
     80        }
     81
     82        int navrat;
     83        try {
     84                navrat = Integer.parseInt(maxZoomLvl);
     85        } catch (Exception ex) {
     86                throw new RuntimeException("Problem while converting string to int. Converting value of prefetrences " + PREFERENCE_MAX_ZOOM_LVL + ". Value=\"" + maxZoomLvl + "\". Should be an integer. Error: " + ex.getMessage(), ex);
     87        }
     88        if(navrat > 30) {
     89                System.err.println("MaxZoomLvl shouldnt be more than 30! Setting to 30.");
     90                navrat = 30;
     91        }
     92        return navrat;
     93    }
     94   
     95    public static void setMaxZoomLvl(int maxZoomLvl) {
     96        if(maxZoomLvl > 30) {
     97                System.err.println("MaxZoomLvl shouldnt be more than 30! Setting to 30.");
     98                maxZoomLvl = 30;
     99        }
     100        Main.pref.put(SlippyMapPreferences.PREFERENCE_MAX_ZOOM_LVL, "" + maxZoomLvl);
     101    }
     102   
    31103    public static String[] getAllMapUrls()
    32104    {
Note: See TracChangeset for help on using the changeset viewer.