All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.cloudfarming.client.geoviewer.jxmap.map.cache.OfflineTile Maven / Gradle / Ivy

Go to download

AgroSense geoviewer JXMap implementation. Contains a map/geoviewer TopComponent based on the JXMap classes from swingx.

The newest version!
/**
 * Copyright (C) 2008-2012 AgroSense Foundation.
 *
 * AgroSense is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
 * this software, see the FLOSS License Exception
 * .
 *
 * AgroSense is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with AgroSense.  If not, see .
 */
package nl.cloudfarming.client.geoviewer.jxmap.map.cache;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.lang.ref.SoftReference;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.jdesktop.swingx.mapviewer.Tile;
import org.openide.util.NbBundle;

/**
 * This class was created, because original Tile class has some important fields declared as private
 *
 * @author Frantisek Post
 */
@NbBundle.Messages("offline tile no data available=No data available")
public class OfflineTile extends Tile {

    private static final int TILE_SIZE = 256;
    private static Color offlineTileBackground = loadColor("OfflineTile.background", new Color(240, 240, 240));
    private static Color offlineTileForeground = loadColor("OfflineTile.foreground", Color.BLACK);
    
    private SoftReference image;
    private boolean loaded = false;
    private OfflineTileFactory tileFactory;
    private String url;
    private boolean offline = false;

    public OfflineTile(int x, int y, int zoom, String url, OfflineTileFactory tileFactory) {
        super(x, y, zoom);
        this.url = url;
        this.tileFactory = tileFactory;
        setLoading(false);
    }

    @Override
    public String getURL() {
        return url;
    }

    @Override
    public synchronized boolean isLoaded() {
        return loaded;
    }

    public synchronized void setLoaded(boolean loaded) {
        boolean old = isLoaded();
        this.loaded = loaded;
        firePropertyChange("loaded", old, isLoaded());
    }

    public void setImage(SoftReference image) {
        this.image = image;
    }

    @Override
    public BufferedImage getImage() {
        BufferedImage img = image.get();
        if (img == null) {
            setLoaded(false);
            tileFactory.startLoading(this);
        }

        return img;
    }

    public void createOfflineImage() {
        BufferedImage offlineImage = new BufferedImage(TILE_SIZE, TILE_SIZE, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = offlineImage.createGraphics();
        graphics.setColor(offlineTileBackground);
        graphics.fillRect(0, 0, TILE_SIZE, TILE_SIZE);
        graphics.setColor(offlineTileForeground);
        String message = nl.cloudfarming.client.geoviewer.jxmap.map.cache.Bundle.offline_tile_no_data_available();
        int textWidth = graphics.getFontMetrics().stringWidth(message);
        int textHeight = graphics.getFontMetrics().getHeight();

        graphics.drawString(message, (TILE_SIZE - textWidth) / 2, (TILE_SIZE - textHeight) / 2);
        setImage(new SoftReference(offlineImage));
        offline = true;
    }

    public boolean isOffline() {
        return offline;
    }

    void firePropertyChangeOnEDT(final String propertyName, final Object oldValue, final Object newValue) {
        if (!EventQueue.isDispatchThread()) {
            SwingUtilities.invokeLater(new Runnable() {
                
                @Override
                public void run() {
                    firePropertyChange(propertyName, oldValue, newValue);
                }
                
            });
        }
    }
    
    private static Color loadColor(String colorName, Color defaultColor) {
        Color color = UIManager.getColor(colorName);
        if (color == null) {
            color = defaultColor;
        }
        return color;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy