]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.maps/src/org/simantics/maps/debug/DebugTileProvider.java
Hide "enabled" column for non-component type tech type tables
[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.Transparency;
21 import java.awt.geom.Rectangle2D;
22 import java.awt.image.BufferedImage;
23 import java.net.URI;
24 import java.net.URISyntaxException;
25
26 import org.simantics.maps.ProvisionException;
27 import org.simantics.maps.internal.ImageUtil;
28 import org.simantics.maps.tile.ITileProvider;
29 import org.simantics.maps.tile.TileKey;
30
31 /**
32  * @author Tuukka Lehtonen
33  */
34 public class DebugTileProvider implements ITileProvider {
35
36     private final Rectangle2D EXTENT = new Rectangle2D.Double(-180, -90, 360, 180);
37
38     private int tileSize;
39
40     private URI source;
41
42     public DebugTileProvider(int tileSize) {
43         this.tileSize = tileSize;
44
45         try {
46             this.source = new URI("debug://localhost");
47         } catch (URISyntaxException e) {
48             throw new Error("Should never happen", e);
49         }
50     }
51
52     @Override
53     public URI getSource() {
54         return source;
55     }
56
57     @Override
58     public Rectangle2D getExtent() {
59         return (Rectangle2D) EXTENT.clone();
60     }
61
62     @Override
63     public Image get(TileKey key) throws ProvisionException {
64         int level = key.getLevel();
65         int x = key.getX();
66         int y = key.getY();
67
68         double xTiles = Math.pow(2, level + 1);
69         double yTiles = Math.pow(2, level);
70
71         if (level < 0)
72             throw new IllegalArgumentException("invalid tile level " + level + " (tile=" + key +")");
73         if (x < 0 || x >= (int) xTiles)
74             throw new IllegalArgumentException("tile x out of bounds " + x + " (tile=" + key +")");
75         if (y < 0 || y >= (int) yTiles)
76             throw new IllegalArgumentException("tile y out of bounds " + y + " (tile=" + key +")");
77
78         Rectangle2D r = new Rectangle2D.Double();
79
80         double minx = -180;
81         double miny = -90;
82         double w = 360;
83         double h = 180;
84
85         double xdelta = w / xTiles;
86         double ydelta = h / yTiles;
87         double xx = x;
88         double yy = yTiles - y - 1;
89
90         r.setFrame(
91                 minx + xdelta*xx,
92                 miny + ydelta*yy,
93                 xdelta,
94                 ydelta
95         );
96 //        System.out.println(yTiles + ", " + key.y + " => " + r);
97
98         BufferedImage image = ImageUtil.createScreenCompatibleImage(tileSize, tileSize, Transparency.OPAQUE);
99         Graphics2D g = image.createGraphics();
100         try {
101             g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
102             g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
103
104             g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
105
106             g.setColor(Color.BLACK);
107             g.fillRect(0, 0, tileSize, tileSize);
108             g.setColor(Color.LIGHT_GRAY);
109             g.fillRect(1, 1, tileSize - 1, tileSize - 1);
110             g.setColor(Color.BLACK);
111
112             drawCenteredString(g, tileSize + "x" + tileSize, tileSize, tileSize / 2 - 50);
113             drawCenteredString(g, "(" + x + ", " + y + ")(" + level + ")", tileSize, tileSize / 2 + 50);
114
115             drawCenteredString(g, r.getMaxY() + "", tileSize, 50);
116             drawCenteredString(g, r.getMinY() + "", tileSize, 500);
117             g.drawString(r.getMinX() + "", 20, tileSize / 2);
118             g.drawString(r.getMaxX() + "", 412, tileSize / 2);
119
120             // Simulate network delays
121             try { Thread.sleep(100); } catch (InterruptedException e) {}
122
123             return image;
124         } finally {
125             g.dispose();
126         }
127     }
128
129     private void drawCenteredString(Graphics2D g, String str, int width, int y) {
130         FontMetrics metrics = g.getFontMetrics();
131         Rectangle2D r2d = metrics.getStringBounds(str, g);
132         g.drawString(str, (int)(width / 2 - r2d.getWidth() / 2.0), y);
133     }
134
135 }