9100637ca1e42b130f8e5c692eb896c3d9431cd3
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalsdk.analytics.gmap.map.layer;
21
22 import java.awt.Color;
23 import java.awt.Font;
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;
35 import java.util.Map;
36 import java.util.Set;
37
38 import javax.servlet.http.HttpServletRequest;
39
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;
46
47
48 public class SwingLayer {
49         private Rectangle shape;
50         private NovaMap map;
51
52         public SwingLayer(NovaMap map) {
53                 this.map = map;
54         }
55         
56         public boolean paintLayer(HttpServletRequest request, Graphics2D g2d, Rectangle bounds, Rectangle2D mapArea, Graphics2D g2Legend) {
57                 return paintNodes(request, g2d, bounds, mapArea, g2Legend);
58         }
59
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();
64                 int legendSize = 0;
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();
74                 int textWidth = 0;
75                 int legendLength = 0;
76                 for (int i = 0; i < showListArr.length; i++) {
77                         legendLength = ((String)showListArr[i]).length();
78                         if(legendLength > textWidth) textWidth = legendLength;
79                 }
80                 Point2D point = null;
81             for (Iterator iterator = set.iterator(); iterator.hasNext();) {
82                         Map.Entry entry = (Map.Entry) iterator.next();
83                         NodeInfo nodeInfo = (NodeInfo) entry.getValue();
84
85                         String id1 = (String) request.getAttribute("server_process_id");
86                         String id2 = (String) request.getSession().getAttribute("server_process_id");
87                         
88                         if (!id1.equals(id2)) {
89                                 request.setAttribute("server_process_interrupted", true);
90                                 System.out.println("swing layer interrupted");
91                                 return false;
92                         }
93                         //System.out.println("%%%%%%%getImage. no of T1%%%%%%" + nodeInfo.getAttribute("x_sequence"));
94
95
96                         point = map.getPixelPos(nodeInfo.geoCoordinate.latitude, nodeInfo.geoCoordinate.longitude);
97
98                         if (!mapArea.contains(point.getX(), point.getY())) {
99                                 continue;
100                         }
101                         
102                         painted = true;
103
104                         g2d.setColor(colorProperties.getColor(nodeInfo.getNodeType()));
105                         
106                         Point2D xyPoint = map.getScreenPointFromPixel(point.getX(), point.getY());
107                         int width2 = (colorProperties.getSize(nodeInfo.getNodeType()) * width) / 5;
108
109                         if (shape == null) {
110                                 shape = new Rectangle((int) xyPoint.getX(), (int) xyPoint.getY(), width2, width2);
111                         }
112                         else {
113                                 shape.setRect((int) xyPoint.getX(), (int) xyPoint.getY(), width2, width2);
114                         }
115
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);
118                         }
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);
121                         }
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);
124                         }
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);
127                         }
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);
132                         }
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);
137                         }
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);
142                         }
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);
147                         } else {
148                                 g2d.drawRect((int) shape.getCenterX() - width2, (int) shape.getCenterY() - width2, width2, width2);
149                         }
150
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);
156                                 g2d.setFont(font);
157                                 g2d.setColor(Color.BLACK);
158                                 g2d.drawString("M", shape.x + width2 / 2, shape.y);
159                         }
160                         
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);
167 //                      }
168                 }
169         String legendName = "";
170         int baseY = 0;
171         baseY = (int)(20*showListArr.length) + 20;//+5;
172         int baseX = 0;
173                 if(map.isShowLegend()) {
174                         for (int i = showListArr.length-1; i>=0; i--) {
175                                 
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)
183                                         //if(i == 0){
184                                         g2Legend.setColor(Color.BLACK);
185                                         g2Legend.setFont(NovaMap.HEADER_FONT);
186                                         g2Legend.drawString("Legend", (int) (10), 10);
187                                 }
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);
194                                 
195                                 g2Legend.drawString(legendName.substring(0, legendName.indexOf("-")), (int) (10) + width+10, (int) (15*i)+10+20);
196                         }
197                 }
198                 
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);
205 */              
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);
214                 }
215                 
216                 //g2d.drawString("Hello", new Double(bounds.getMinX()).intValue()+20, new Double(bounds.getMaxY()).intValue()-50);
217                 FontMetrics metrics = g2d.getFontMetrics();
218
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);
224                 }
225
226                 g2d.setColor(oldColor);
227                 g2d.setStroke(oldStroke);
228                 
229                 return painted;
230         }
231         
232         private String nvl(String s) {
233                 return (s == null) ? "" : s;
234         }       
235 }