2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalsdk.analytics.gmap.map.layer;
22 import java.awt.Color;
24 import java.awt.FontMetrics;
25 import java.awt.Graphics2D;
26 import java.awt.Point;
27 import java.awt.Rectangle;
28 import java.awt.Stroke;
29 import java.awt.geom.Point2D;
30 import java.awt.geom.Rectangle2D;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.Iterator;
38 import javax.servlet.http.HttpServletRequest;
40 import org.openecomp.portalsdk.analytics.gmap.map.ColorProperties;
41 import org.openecomp.portalsdk.analytics.gmap.map.MapConstant;
42 import org.openecomp.portalsdk.analytics.gmap.map.NovaMap;
43 import org.openecomp.portalsdk.analytics.gmap.node.Node;
44 import org.openecomp.portalsdk.analytics.gmap.node.NodeInfo;
45 import org.openecomp.portalsdk.analytics.system.Globals;
48 public class SwingLayer {
49 private Rectangle shape;
52 public SwingLayer(NovaMap map) {
56 public boolean paintLayer(HttpServletRequest request, Graphics2D g2d, Rectangle bounds, Rectangle2D mapArea, Graphics2D g2Legend) {
57 return paintNodes(request, g2d, bounds, mapArea, g2Legend);
60 protected boolean paintNodes(HttpServletRequest request, Graphics2D g2d, Rectangle bounds, Rectangle2D mapArea, Graphics2D g2Legend) {
61 boolean painted = false;
62 Node node = map.getNode();
63 ColorProperties colorProperties = map.getColorProperties();
65 if(map.isShowLegend())
66 legendSize = map.getShowListSize();
67 Object showListArr[] = ((HashSet)map.getShowList()).toArray();
68 HashMap<String,NodeInfo> hashMap = node.getNodeCollection().getNodeCollection();
69 Set set = hashMap.entrySet();
70 int width = map.getShapeWidth();
71 ArrayList<String> visibleLabel = new ArrayList<String>(151);
72 Color oldColor = g2d.getColor();
73 Stroke oldStroke = g2d.getStroke();
76 for (int i = 0; i < showListArr.length; i++) {
77 legendLength = ((String)showListArr[i]).length();
78 if(legendLength > textWidth) textWidth = legendLength;
81 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
82 Map.Entry entry = (Map.Entry) iterator.next();
83 NodeInfo nodeInfo = (NodeInfo) entry.getValue();
85 String id1 = (String) request.getAttribute("server_process_id");
86 String id2 = (String) request.getSession().getAttribute("server_process_id");
88 if (!id1.equals(id2)) {
89 request.setAttribute("server_process_interrupted", true);
90 System.out.println("swing layer interrupted");
93 //System.out.println("%%%%%%%getImage. no of T1%%%%%%" + nodeInfo.getAttribute("x_sequence"));
96 point = map.getPixelPos(nodeInfo.geoCoordinate.latitude, nodeInfo.geoCoordinate.longitude);
98 if (!mapArea.contains(point.getX(), point.getY())) {
104 g2d.setColor(colorProperties.getColor(nodeInfo.getNodeType()));
106 Point2D xyPoint = map.getScreenPointFromPixel(point.getX(), point.getY());
107 int width2 = (colorProperties.getSize(nodeInfo.getNodeType()) * width) / 5;
110 shape = new Rectangle((int) xyPoint.getX(), (int) xyPoint.getY(), width2, width2);
113 shape.setRect((int) xyPoint.getX(), (int) xyPoint.getY(), width2, width2);
116 if (colorProperties.getShape(nodeInfo.getNodeType())!=null && colorProperties.getShape(nodeInfo.getNodeType()).equalsIgnoreCase(MapConstant.FILLED_SQUARE)) {
117 g2d.fillRect((int) shape.getCenterX() - width2, (int) shape.getCenterY() - width2, width2, width2);
119 else if (colorProperties.getShape(nodeInfo.getNodeType())!=null && colorProperties.getShape(nodeInfo.getNodeType()).equalsIgnoreCase(MapConstant.HOLLOW_SQUARE)) {
120 g2d.drawRect((int) shape.getCenterX() - width2, (int) shape.getCenterY() - width2, width2, width2);
122 else if (colorProperties.getShape(nodeInfo.getNodeType())!=null && colorProperties.getShape(nodeInfo.getNodeType()).equalsIgnoreCase(MapConstant.FILLED_CIRCLE)) {
123 g2d.fillOval((int) shape.getCenterX() - width2, (int) shape.getCenterY() - width2, width2, width2);
125 else if (colorProperties.getShape(nodeInfo.getNodeType())!=null && colorProperties.getShape(nodeInfo.getNodeType()).equalsIgnoreCase(MapConstant.HOLLOW_CIRCLE)) {
126 g2d.drawOval((int) shape.getCenterX() - width2, (int) shape.getCenterY() - width2, width2, width2);
128 else if (colorProperties.getShape(nodeInfo.getNodeType())!=null && colorProperties.getShape(nodeInfo.getNodeType()).equalsIgnoreCase(MapConstant.FILLED_TRIANGLE)) {
129 int[] xPoints = {(int) shape.getX(), (int) shape.getX() - width2 / 2, (int) shape.getX() + width2 / 2};
130 int[] yPoints = {(int) shape.getY() + width2 / 2, (int) shape.getY() - width2 / 2, (int) shape.getY() - width2 / 2};
131 g2d.fillPolygon(xPoints, yPoints, xPoints.length);
133 else if (colorProperties.getShape(nodeInfo.getNodeType())!=null && colorProperties.getShape(nodeInfo.getNodeType()).equalsIgnoreCase(MapConstant.HOLLOW_TRIANGLE)) {
134 int[] xPoints = {(int) shape.getX(), (int) shape.getX() - width2 / 2, (int) shape.getX() + width2 / 2};
135 int[] yPoints = {(int) shape.getY() + width2 / 2, (int) shape.getY() - width2 / 2, (int) shape.getY() - width2 / 2};
136 g2d.drawPolygon(xPoints, yPoints, xPoints.length);
138 else if (colorProperties.getShape(nodeInfo.getNodeType())!=null && colorProperties.getShape(nodeInfo.getNodeType()).equalsIgnoreCase(MapConstant.FILLED_DIAMOND)) {
139 int[] xPoints = {(int) shape.getX() - width2 / 2, (int) shape.getX(), (int) shape.getX() + width2 / 2, (int) shape.getX()};
140 int[] yPoints = {(int) shape.getY() , (int) shape.getY() - width2 / 2, (int) shape.getY(), (int) shape.getY() + width2 / 2};
141 g2d.fillPolygon(xPoints, yPoints, xPoints.length);
143 else if (colorProperties.getShape(nodeInfo.getNodeType())!=null && colorProperties.getShape(nodeInfo.getNodeType()).equalsIgnoreCase(MapConstant.HOLLOW_DIAMOND)) {
144 int[] xPoints = {(int) shape.getX() - width2 / 2, (int) shape.getX(), (int) shape.getX() + width2 / 2, (int) shape.getX()};
145 int[] yPoints = {(int) shape.getY() , (int) shape.getY() - width2 / 2, (int) shape.getY(), (int) shape.getY() + width2 / 2};
146 g2d.drawPolygon(xPoints, yPoints, xPoints.length);
148 g2d.drawRect((int) shape.getCenterX() - width2, (int) shape.getCenterY() - width2, width2, width2);
151 if (nodeInfo.isMoveable()) {
152 int fontSize = width / 2;
153 fontSize = fontSize > 14 ? 14 : fontSize;
154 fontSize = (colorProperties.getSize(nodeInfo.getNodeType()) * fontSize) / 5;
155 Font font = new Font("sans-serif", Font.BOLD, fontSize);
157 g2d.setColor(Color.BLACK);
158 g2d.drawString("M", shape.x + width2 / 2, shape.y);
161 // if (map.containsShowLabelList(nodeInfo.getNodeType())) {
162 // g2d.setColor(Color.BLACK);
163 // FontMetrics metrics = g2d.getFontMetrics();
164 // int x = shape.x - metrics.stringWidth(nodeInfo.getID()) / 2;
165 // int y = shape.y + width2 * 4 / 3;
166 // g2d.drawString(nodeInfo.getID(), x, y);
169 String legendName = "";
171 baseY = (int)(20*showListArr.length) + 20;//+5;
173 if(map.isShowLegend()) {
174 for (int i = showListArr.length-1; i>=0; i--) {
176 legendName = (String)showListArr[i];
177 //for(int i = 0; i < showListArr.length; i++ ) {
178 if(i == showListArr.length-1){
179 textWidth = (textWidth<="Legend".length())?"Legend".length():textWidth;
180 g2Legend.setColor(Color.WHITE);
181 //g2d.draw(new Rectangle2D.Double((int) bounds.getMaxX()*0.1, (int) bounds.getMaxY()*0.75*showListArr.length, (int) bounds.getMaxX()*0.75, (int) bounds.getMaxY()*0.75));
182 g2Legend.fill3DRect((int)(0), (int)(0), (int) bounds.getWidth() , (int)(baseY) , true); // (int)(bounds.getMaxX()*0.9)- (int)(bounds.getMaxX()*0.25)
184 g2Legend.setColor(Color.BLACK);
185 g2Legend.setFont(NovaMap.HEADER_FONT);
186 g2Legend.drawString("Legend", (int) (10), 10);
188 int[] xPointsL = {(int) (10 - width / 2), (int) (10), (int) (10 + width/2), (int) (10)};
189 int[] yPointsL = {(int) (15*i+5+20), (int) (15*i+5+20 - width / 2), (int) (15*i+5+20), (int) (15*i+5+20 + width / 2)};
190 g2Legend.setColor(colorProperties.getColor( ((String)showListArr[i]).toUpperCase()));
191 g2Legend.fillPolygon(xPointsL, yPointsL, xPointsL.length);
192 g2Legend.setFont(NovaMap.TEXT_FONT);
193 g2Legend.setColor(Color.BLACK);
195 g2Legend.drawString(legendName.substring(0, legendName.indexOf("-")), (int) (10) + width+10, (int) (15*i)+10+20);
199 /* g2d.drawString("0", (int) bounds.getMaxX()/2+20 + width+10, 0);
200 g2d.drawString("50", (int) bounds.getMaxX()/2+20 + width+10, 50);
201 g2d.drawString("100", (int) bounds.getMaxX()/2+20 + width+10, 100);
202 g2d.drawString("200", (int) bounds.getMaxX()/2+20 + width+10, 200);
203 g2d.drawString("400", (int) bounds.getMaxX()/2+20 + width+10, 400);
204 g2d.drawString("600", (int) bounds.getMaxX()/2+20 + width+10, 600);
206 // g2d.setFont(NovaMap.TEXT_FONT);
207 // g2d.setColor(Color.BLACK);
208 if(nvl(map.getDataLoaded()).trim().length() > 0) {
209 g2d.setColor(Color.WHITE);
210 g2d.fill3DRect(new Double(bounds.getMinX()).intValue(), new Double(bounds.getMaxY()).intValue()-30, (int) bounds.getWidth() , (int)(30) , true); // (int)(bounds.getMaxX()*0.9)- (int)(bounds.getMaxX()*0.25)
211 g2d.setColor(Color.RED);
212 g2d.setFont(NovaMap.HEADER_FONT);
213 g2d.drawString(Globals.getUserDefinedMessageForMemoryLimitReached() + " "+ map.getDataLoaded()+ " were downloaded to Map.", new Double(bounds.getMinX()).intValue()+80, new Double(bounds.getMaxY()).intValue()-15);
216 //g2d.drawString("Hello", new Double(bounds.getMinX()).intValue()+20, new Double(bounds.getMaxY()).intValue()-50);
217 FontMetrics metrics = g2d.getFontMetrics();
219 for (int i = 0; i < visibleLabel.size(); i++) {
220 String[] properties = visibleLabel.get(i).split(">>>");
221 int x = Integer.parseInt(properties[1]) - metrics.stringWidth(properties[0]) / 2;
222 int y = Integer.parseInt(properties[2]) + Integer.parseInt(properties[3]) * 4 / 3;
223 g2d.drawString(properties[0], x, y);
226 g2d.setColor(oldColor);
227 g2d.setStroke(oldStroke);
232 private String nvl(String s) {
233 return (s == null) ? "" : s;