]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.maps/src/org/simantics/maps/debug/DebugTileProvider.java
Share some projects for Simantics District
[simantics/district.git] / org.simantics.district.maps / src / org / simantics / maps / debug / DebugTileProvider.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.maps.debug;
13
14 import java.awt.Color;
15 import java.awt.Font;
16 import java.awt.FontMetrics;
17 import java.awt.Graphics2D;
18 import java.awt.Image;
19 import java.awt.RenderingHints;
20 import java.awt.geom.Rectangle2D;
21 import java.awt.image.BufferedImage;
22 import java.net.URI;
23 import java.net.URISyntaxException;
24
25 import org.simantics.maps.ProvisionException;
26 import org.simantics.maps.tile.ITileProvider;
27 import org.simantics.maps.tile.TileKey;
28
29 /**
30  * @author Tuukka Lehtonen
31  */
32 public class DebugTileProvider implements ITileProvider {
33
34     private final Rectangle2D EXTENT = new Rectangle2D.Double(-180, -90, 360, 180);
35
36     private int tileSize;
37
38     private URI source;
39
40     public DebugTileProvider(int tileSize) {
41         this.tileSize = tileSize;
42
43         try {
44             this.source = new URI("debug://localhost");
45         } catch (URISyntaxException e) {
46             throw new Error("Should never happen", e);
47         }
48     }
49
50     @Override
51     public URI getSource() {
52         return source;
53     }
54
55     @Override
56     public Rectangle2D getExtent() {
57         return (Rectangle2D) EXTENT.clone();
58     }
59
60     @Override
61     public Image get(TileKey key) throws ProvisionException {
62         int level = key.getLevel();
63         int x = key.getX();
64         int y = key.getY();
65
66         double xTiles = Math.pow(2, level + 1);
67         double yTiles = Math.pow(2, level);
68
69         if (level < 0)
70             throw new IllegalArgumentException("invalid tile level " + level + " (tile=" + key +")");
71         if (x < 0 || x >= (int) xTiles)
72             throw new IllegalArgumentException("tile x out of bounds " + x + " (tile=" + key +")");
73         if (y < 0 || y >= (int) yTiles)
74             throw new IllegalArgumentException("tile y out of bounds " + y + " (tile=" + key +")");
75
76         Rectangle2D r = new Rectangle2D.Double();
77
78         double minx = -180;
79         double miny = -90;
80         double w = 360;
81         double h = 180;
82
83         double xdelta = w / xTiles;
84         double ydelta = h / yTiles;
85         double xx = x;
86         double yy = yTiles - y - 1;
87
88         r.setFrame(
89                 minx + xdelta*xx,
90                 miny + ydelta*yy,
91                 xdelta,
92                 ydelta
93         );
94 //        System.out.println(yTiles + ", " + key.y + " => " + r);
95
96         BufferedImage image = new BufferedImage(tileSize, tileSize, BufferedImage.TYPE_3BYTE_BGR);
97         Graphics2D g = image.createGraphics();
98         try {
99             g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
100             g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
101
102             g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 32));
103
104             g.setColor(Color.BLACK);
105             g.fillRect(0, 0, tileSize, tileSize);
106             g.setColor(Color.LIGHT_GRAY);
107             g.fillRect(1, 1, tileSize - 1, tileSize - 1);
108             g.setColor(Color.BLACK);
109
110             drawCenteredString(g, tileSize + "x" + tileSize, tileSize, tileSize / 2 - 50);
111             drawCenteredString(g, "(" + x + ", " + y + ")(" + level + ")", tileSize, tileSize / 2 + 50);
112
113             drawCenteredString(g, r.getMaxY() + "", tileSize, 50);
114             drawCenteredString(g, r.getMinY() + "", tileSize, 500);
115             g.drawString(r.getMinX() + "", 20, tileSize / 2);
116             g.drawString(r.getMaxX() + "", 412, tileSize / 2);
117
118             // Simulate network delays
119             try { Thread.sleep(100); } catch (InterruptedException e) {}
120
121             return image;
122         } finally {
123             g.dispose();
124         }
125     }
126
127     private void drawCenteredString(Graphics2D g, String str, int width, int y) {
128         FontMetrics metrics = g.getFontMetrics();
129         Rectangle2D r2d = metrics.getStringBounds(str, g);
130         g.drawString(str, (int)(width / 2 - r2d.getWidth() / 2.0), y);
131     }
132
133 }