admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * To change this template, choose Tools | Templates |
| 3 | * and open the template in the editor. |
| 4 | */ |
| 5 | package tai_ofa; |
| 6 | |
| 7 | import java.util.ArrayList; |
| 8 | import java.util.HashMap; |
| 9 | import java.util.Iterator; |
| 10 | import java.util.Map; |
| 11 | import java.util.Set; |
| 12 | import javafx.beans.property.DoubleProperty; |
| 13 | import javafx.collections.ObservableList; |
| 14 | import javafx.event.ActionEvent; |
| 15 | import javafx.event.EventHandler; |
| 16 | import javafx.scene.Cursor; |
| 17 | import javafx.scene.Node; |
| 18 | import javafx.scene.control.Button; |
| 19 | import javafx.scene.input.MouseButton; |
| 20 | import javafx.scene.input.MouseDragEvent; |
| 21 | import javafx.scene.input.MouseEvent; |
| 22 | import javafx.scene.layout.Pane; |
| 23 | import javafx.scene.layout.VBox; |
| 24 | import javafx.scene.paint.Color; |
| 25 | import javafx.scene.shape.Circle; |
| 26 | import javafx.scene.shape.Line; |
| 27 | import javafx.scene.shape.StrokeLineCap; |
| 28 | import javafx.stage.Stage; |
| 29 | |
| 30 | /** |
| 31 | * |
| 32 | * @author Raghav Kashyap (raghavkashyap@paxterrasolutions.com) |
| 33 | |
| 34 | * TestON is free software: you can redistribute it and/or modify |
| 35 | * it under the terms of the GNU General Public License as published by |
| 36 | * the Free Software Foundation, either version 2 of the License, or |
| 37 | * (at your option) any later version. |
| 38 | |
| 39 | * TestON is distributed in the hope that it will be useful, |
| 40 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 41 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 42 | * GNU General Public License for more details. |
| 43 | |
| 44 | * You should have received a copy of the GNU General Public License |
| 45 | * along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 46 | |
| 47 | */ |
| 48 | class OFAAnchorInsideImageNode { |
| 49 | |
| 50 | Double increaseBindX; |
| 51 | Double increaseBindY; |
| 52 | /*Double addX and addY is passed as an argument as binding coordinates are different for Wizard |
| 53 | and toplogyNode(In the editor)*/ |
| 54 | |
| 55 | public OFAAnchorInsideImageNode(Double addX, Double addY) { |
| 56 | increaseBindX = addX; |
| 57 | increaseBindY = addY; |
| 58 | } |
| 59 | boolean anchorFlag = false; |
| 60 | OFATopologyLink topologyLink = new OFATopologyLink(); |
| 61 | ArrayList<HashMap<String, String>> arrayOfLinkTopologyHash = new ArrayList<HashMap<String, String>>(); |
| 62 | |
| 63 | public void anchorsInsideImage(final TopologyWizard.Anchor anchor, final double bindLineStartx, final double bindLineStarty, final double bindLineEndx, final double bindLineEndy, final Pane hb, final DraggableNode content, |
| 64 | final VBox hbox, final Line con, final ArrayList<String> draggedImagesName, final ArrayList<HashMap<String, String>> arrayOfInterFaceHash, final HashMap<String, String> linkTopologyHash, final HashMap<Node, String> anchorNodeHash) { |
| 65 | final Line con11 = new Line(); |
| 66 | |
| 67 | anchor.setOnDragDetected(new EventHandler<MouseEvent>() { |
| 68 | @Override |
| 69 | public void handle(MouseEvent arg0) { |
| 70 | anchor.startFullDrag(); |
| 71 | enableDrag(anchor); |
| 72 | } |
| 73 | }); |
| 74 | |
| 75 | anchor.setOnMouseDragReleased(new EventHandler<MouseDragEvent>() { |
| 76 | @Override |
| 77 | public void handle(MouseDragEvent arg0) { |
| 78 | ObservableList<Node> allNodesCanvas = hb.getChildren(); |
| 79 | boolean flag = false; |
| 80 | String startNode = null; |
| 81 | String endNode = null; |
| 82 | try { |
| 83 | for (Node node : allNodesCanvas) { |
| 84 | Double x = node.getLayoutX() + increaseBindX; |
| 85 | Double y = node.getLayoutY() + increaseBindY; |
| 86 | if (node.toString().startsWith("DraggableNode")) { |
| 87 | for (int i = 0; i <= 80; i++) { |
| 88 | for (int j = 0; j <= 100; j++) { |
| 89 | Double x1 = node.getLayoutX() + increaseBindX; |
| 90 | Double y1 = node.getLayoutY() + increaseBindY; |
| 91 | Double x11 = x1 + i; |
| 92 | Double y11 = y1 + j; |
| 93 | if (x11 == arg0.getSceneX()) { |
| 94 | if (y11 == arg0.getSceneY()) { |
| 95 | con11.setStrokeLineCap(StrokeLineCap.ROUND); |
| 96 | con11.setStroke(Color.MIDNIGHTBLUE); |
| 97 | con11.setStrokeWidth(2.0); |
| 98 | con11.startXProperty().bind(content.layoutXProperty().add(bindLineStartx)); |
| 99 | con11.startYProperty().bind(content.layoutYProperty().add(bindLineStarty)); |
| 100 | startNode = ((Integer) content.hashCode()).toString(); |
| 101 | con11.endXProperty().bind(node.layoutXProperty().add(bindLineEndx)); |
| 102 | con11.endYProperty().bind(node.layoutYProperty().add(bindLineEndy)); |
| 103 | endNode = ((Integer) node.hashCode()).toString(); |
| 104 | con11.setId(startNode + "," + endNode); |
| 105 | hbox.setStyle("-fx-border-color: Transparent"); |
| 106 | con.setVisible(false); |
| 107 | anchor.setVisible(false); |
| 108 | hb.getChildren().add(con11); |
| 109 | flag = true; |
| 110 | |
| 111 | Map<Node, String> map = anchorNodeHash; |
| 112 | Iterator<Map.Entry<Node, String>> entries = map.entrySet().iterator(); |
| 113 | while (entries.hasNext()) { |
| 114 | Map.Entry<Node, String> entry = entries.next(); |
| 115 | entry.getKey(); |
| 116 | hb.getChildren().remove(entry.getKey()); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | } |
| 124 | |
| 125 | |
| 126 | } |
| 127 | if (flag == false) { |
| 128 | Map<Node, String> map = anchorNodeHash; |
| 129 | Iterator<Map.Entry<Node, String>> entries = map.entrySet().iterator(); |
| 130 | while (entries.hasNext()) { |
| 131 | Map.Entry<Node, String> entry = entries.next(); |
| 132 | entry.getKey(); |
| 133 | hb.getChildren().remove(entry.getKey()); |
| 134 | anchorFlag = false; |
| 135 | } |
| 136 | } |
| 137 | } catch (Exception e) { |
| 138 | } |
| 139 | } |
| 140 | }); |
| 141 | con11.setOnMouseEntered(new EventHandler<MouseEvent>() { |
| 142 | @Override |
| 143 | public void handle(MouseEvent arg0) { |
| 144 | con11.setStroke(Color.GOLD); |
| 145 | } |
| 146 | }); |
| 147 | con11.setOnMouseExited(new EventHandler<MouseEvent>() { |
| 148 | @Override |
| 149 | public void handle(MouseEvent arg0) { |
| 150 | con11.setStroke(Color.MIDNIGHTBLUE); |
| 151 | } |
| 152 | }); |
| 153 | final DraggableNode contentLine = new DraggableNode(); |
| 154 | con11.setOnMouseClicked(new EventHandler<MouseEvent>() { |
| 155 | @Override |
| 156 | public void handle(MouseEvent arg0) { |
| 157 | if (arg0.getClickCount() == 2) { |
| 158 | topologyLink.start(new Stage()); |
| 159 | if (arrayOfLinkTopologyHash.isEmpty()) { |
| 160 | for (HashMap<String, String> linkHash : arrayOfLinkTopologyHash) { |
| 161 | Set linkSet = linkHash.entrySet(); |
| 162 | Iterator linkHashDetailIterator = linkSet.iterator(); |
| 163 | while (linkHashDetailIterator.hasNext()) { |
| 164 | |
| 165 | Map.Entry linkMap = (Map.Entry) linkHashDetailIterator.next(); |
| 166 | if (linkMap.getKey().toString().equals(con11.getId())) { |
| 167 | String[] linkValues = linkMap.getValue().toString().split("_"); |
| 168 | topologyLink.nameText.setText(linkValues[0]); |
| 169 | topologyLink.typeText.setText(linkValues[1]); |
| 170 | topologyLink.devicesInTopoEditor.setEditable(true); |
| 171 | topologyLink.devicesInTopoEditor.getSelectionModel().select(linkValues[2]); |
| 172 | topologyLink.interfaceList2.setEditable(true); |
| 173 | topologyLink.interfaceList2.getSelectionModel().select(linkValues[3]); |
| 174 | topologyLink.destDevicesInTopoEditor.setEditable(true); |
| 175 | topologyLink.destDevicesInTopoEditor.getSelectionModel().select(linkValues[4]); |
| 176 | topologyLink.interfaceList4.setEditable(true); |
| 177 | topologyLink.interfaceList4.getSelectionModel().select(linkValues[5]); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | for (String string : draggedImagesName) { |
| 185 | topologyLink.devicesInTopoEditor.getItems().add(string); |
| 186 | topologyLink.destDevicesInTopoEditor.getItems().add(string); |
| 187 | } |
| 188 | topologyLink.devicesInTopoEditor.setOnAction(new EventHandler<ActionEvent>() { |
| 189 | @Override |
| 190 | public void handle(ActionEvent arg0) { |
| 191 | topologyLink.interfaceList2.getItems().clear(); |
| 192 | try { |
| 193 | for (HashMap<String, String> interFaceDetail : arrayOfInterFaceHash) { |
| 194 | Set set = interFaceDetail.entrySet(); |
| 195 | Iterator interFaceHashDetailIterator = set.iterator(); |
| 196 | while (interFaceHashDetailIterator.hasNext()) { |
| 197 | Map.Entry me = (Map.Entry) interFaceHashDetailIterator.next(); |
| 198 | String[] deviceNameAndiniterFaceValue = me.getValue().toString().split("\\_"); |
| 199 | if (deviceNameAndiniterFaceValue[1].equals(topologyLink.devicesInTopoEditor.getSelectionModel().getSelectedItem())) { |
| 200 | if (!me.getKey().toString().equals("")) { |
| 201 | topologyLink.interfaceList2.getItems().add(me.getKey().toString()); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } catch (Exception e) { |
| 207 | } |
| 208 | } |
| 209 | }); |
| 210 | |
| 211 | topologyLink.destDevicesInTopoEditor.setOnAction(new EventHandler<ActionEvent>() { |
| 212 | @Override |
| 213 | public void handle(ActionEvent arg0) { |
| 214 | |
| 215 | topologyLink.interfaceList4.getItems().clear(); |
| 216 | try { |
| 217 | for (HashMap<String, String> interFaceDetail : arrayOfInterFaceHash) { |
| 218 | Set set = interFaceDetail.entrySet(); |
| 219 | Iterator interFaceHashDetailIterator = set.iterator(); |
| 220 | while (interFaceHashDetailIterator.hasNext()) { |
| 221 | Map.Entry me = (Map.Entry) interFaceHashDetailIterator.next(); |
| 222 | String[] deviceNameAndiniterFaceValue = me.getValue().toString().split("\\_"); |
| 223 | if (deviceNameAndiniterFaceValue[1].equals(topologyLink.destDevicesInTopoEditor.getSelectionModel().getSelectedItem())) { |
| 224 | if (!me.getKey().toString().equals("")) { |
| 225 | topologyLink.interfaceList4.getItems().add(me.getKey().toString()); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } catch (Exception e) { |
| 231 | } |
| 232 | } |
| 233 | }); |
| 234 | topologyLink.finishSelectedLink.setOnAction(new EventHandler<ActionEvent>() { |
| 235 | @Override |
| 236 | public void handle(ActionEvent arg0) { |
| 237 | con11.setId(topologyLink.nameText.getText() + "_" + con11.getStartX() + "_" + con11.getStartY() + "_" + con11.getEndX() + "_" + con11.getEndY()); |
| 238 | String detailedString = topologyLink.nameText.getText() + "_" + topologyLink.typeText.getText() + "_" + topologyLink.devicesInTopoEditor.getSelectionModel().getSelectedItem() + "_" + topologyLink.interfaceList2.getSelectionModel().getSelectedItem() + "_" + topologyLink.destDevicesInTopoEditor.getSelectionModel().getSelectedItem() + "_" + topologyLink.interfaceList4.getSelectionModel().getSelectedItem() + "_"; |
| 239 | linkTopologyHash.put(con11.getId(), detailedString); |
| 240 | arrayOfLinkTopologyHash = new ArrayList<HashMap<String, String>>(); |
| 241 | arrayOfLinkTopologyHash.add(linkTopologyHash); |
| 242 | topologyLink.copyStage.close(); |
| 243 | } |
| 244 | }); |
| 245 | } else if (arg0.getButton() == MouseButton.SECONDARY) { |
| 246 | } |
| 247 | } |
| 248 | }); |
| 249 | } |
| 250 | |
| 251 | public void closeNodeOnCanvas(final Button closeButton, final VBox hbox, final Pane hb, final DraggableNode content) { |
| 252 | final ArrayList<Node> removeNodes = new ArrayList<Node>(); |
| 253 | |
| 254 | closeButton.setOnAction(new EventHandler<ActionEvent>() { |
| 255 | @Override |
| 256 | public void handle(ActionEvent arg0) { |
| 257 | Node parent = hbox.getParent(); |
| 258 | ObservableList<Node> allCurrentNode = hb.getChildren(); |
| 259 | for (Node node : allCurrentNode) { |
| 260 | if (node.toString().contains("Line")) { |
| 261 | if (!node.toString().matches("Line[id=Line[id=null,null]]")) { |
| 262 | if (node.getId() != null) { |
| 263 | String[] startLineNode = node.getId().split(","); |
| 264 | Integer nodeHash = content.hashCode(); |
| 265 | if (nodeHash.toString().equals(startLineNode[0])) { |
| 266 | removeNodes.add(node); |
| 267 | } |
| 268 | if (startLineNode.length == 2) { |
| 269 | if (nodeHash.toString().equals(startLineNode[1])) { |
| 270 | removeNodes.add(node); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | for (Node removenode : removeNodes) { |
| 279 | hb.getChildren().remove(removenode); |
| 280 | } |
| 281 | |
| 282 | hb.getChildren().remove(content); |
| 283 | } |
| 284 | }); |
| 285 | } |
| 286 | |
| 287 | private void enableDrag(final Circle circle) { |
| 288 | final OFAAnchorInsideImageNode.Delta dragDelta = new OFAAnchorInsideImageNode.Delta(); |
| 289 | |
| 290 | circle.setOnMousePressed(new EventHandler<MouseEvent>() { |
| 291 | @Override |
| 292 | public void handle(MouseEvent mouseEvent) { |
| 293 | dragDelta.x = circle.getCenterX() - mouseEvent.getX(); |
| 294 | dragDelta.y = circle.getCenterY() - mouseEvent.getY(); |
| 295 | circle.getScene().setCursor(Cursor.MOVE); |
| 296 | } |
| 297 | }); |
| 298 | |
| 299 | circle.setOnMouseReleased(new EventHandler<MouseEvent>() { |
| 300 | @Override |
| 301 | public void handle(MouseEvent mouseEvent) { |
| 302 | } |
| 303 | }); |
| 304 | |
| 305 | circle.setOnMouseDragged(new EventHandler<MouseEvent>() { |
| 306 | @Override |
| 307 | public void handle(MouseEvent mouseEvent) { |
| 308 | circle.setCenterX(mouseEvent.getX() + dragDelta.x); |
| 309 | circle.setCenterY(mouseEvent.getY() + dragDelta.y); |
| 310 | } |
| 311 | }); |
| 312 | |
| 313 | circle.setOnMouseEntered(new EventHandler<MouseEvent>() { |
| 314 | @Override |
| 315 | public void handle(MouseEvent mouseEvent) { |
| 316 | if (!mouseEvent.isPrimaryButtonDown()) { |
| 317 | circle.getScene().setCursor(Cursor.HAND); |
| 318 | } |
| 319 | } |
| 320 | }); |
| 321 | |
| 322 | circle.setOnMouseExited(new EventHandler<MouseEvent>() { |
| 323 | @Override |
| 324 | public void handle(MouseEvent mouseEvent) { |
| 325 | if (!mouseEvent.isPrimaryButtonDown()) { |
| 326 | } |
| 327 | } |
| 328 | }); |
| 329 | } |
| 330 | |
| 331 | class Anchor extends Circle { |
| 332 | |
| 333 | Anchor(String id, DoubleProperty x, DoubleProperty y) { |
| 334 | super(x.get(), y.get(), 7); |
| 335 | setId(id); |
| 336 | setFill(Color.ANTIQUEWHITE.deriveColor(1, 1, 1, 0.75)); |
| 337 | setStroke(Color.GREY); |
| 338 | x.bind(centerXProperty()); |
| 339 | y.bind(centerYProperty()); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | class Delta { |
| 344 | |
| 345 | double x, y; |
| 346 | } |
| 347 | } |