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.io.File; |
| 8 | import java.nio.file.DirectoryStream; |
| 9 | import java.nio.file.Files; |
| 10 | import java.nio.file.Path; |
| 11 | import java.nio.file.Paths; |
| 12 | import java.nio.file.attribute.BasicFileAttributes; |
| 13 | import javafx.event.Event; |
| 14 | import javafx.event.EventHandler; |
| 15 | import javafx.scene.Node; |
| 16 | import javafx.scene.control.TreeItem; |
| 17 | import javafx.scene.image.Image; |
| 18 | import javafx.scene.image.ImageView; |
| 19 | |
| 20 | /** |
| 21 | * |
| 22 | * @author Raghav Kashyap (raghavkashyap@paxterrasolutions.com) |
| 23 | |
| 24 | * TestON is free software: you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License as published by |
| 26 | * the Free Software Foundation, either version 2 of the License, or |
| 27 | * (at your option) any later version. |
| 28 | |
| 29 | * TestON is distributed in the hope that it will be useful, |
| 30 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 32 | * GNU General Public License for more details. |
| 33 | |
| 34 | * You should have received a copy of the GNU General Public License |
| 35 | * along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 36 | |
| 37 | */ |
| 38 | public class LoadDirectory extends TreeItem<String> { |
| 39 | |
| 40 | //this stores the full path to the file or directory |
| 41 | OFAFileOperations fileOperation = new OFAFileOperations(); |
| 42 | private String fullPath; |
| 43 | |
| 44 | public String getFullPath() { |
| 45 | return (this.fullPath); |
| 46 | } |
| 47 | private boolean isDirectory; |
| 48 | |
| 49 | public boolean isDirectory() { |
| 50 | return (this.isDirectory); |
| 51 | } |
| 52 | |
| 53 | public LoadDirectory(Path file) { |
| 54 | super(file.toString()); |
| 55 | this.fullPath = file.toString(); |
| 56 | |
| 57 | if (Files.isDirectory(file)) { |
| 58 | this.isDirectory = true; |
| 59 | |
| 60 | if ("common".equals(file.getFileName().toString())) { |
| 61 | Node rootIcon = new ImageView(new Image(getClass().getResourceAsStream("/images/project.jpeg"), 20, 20, true, true)); |
| 62 | rootIcon.setId("/images/project.jpeg"); |
| 63 | this.setGraphic(rootIcon); |
| 64 | } else if ("cli".equalsIgnoreCase(file.getFileName().toString())) { |
| 65 | Node rootIcon2 = new ImageView(new Image(getClass().getResourceAsStream("/images/terminal.png"), 20, 20, true, true)); |
| 66 | rootIcon2.setId("/images/terminal.png"); |
| 67 | this.setGraphic(rootIcon2); |
| 68 | } else if ("api".equals(file.getFileName().toString())) { |
| 69 | Node rootIcon3 = new ImageView(new Image(getClass().getResourceAsStream("/images/api.jpg"), 20, 20, true, true)); |
| 70 | rootIcon3.setId("/images/api.jpg"); |
| 71 | this.setGraphic(rootIcon3); |
| 72 | } else if ("tool".equals(file.getFileName().toString())) { |
| 73 | Node rootIcon4 = new ImageView(new Image(getClass().getResourceAsStream("/images/tool.jpg"), 20, 20, true, true)); |
| 74 | this.setGraphic(rootIcon4); |
| 75 | } else if ("remotesys".equals(file.getFileName().toString())) { |
| 76 | Node rootIcon5 = new ImageView(new Image(getClass().getResourceAsStream("/images/automatorui.png"), 20, 20, true, true)); |
| 77 | this.setGraphic(rootIcon5); |
| 78 | } else if ("emulator".equals(file.getFileName().toString())) { |
| 79 | Node rootIcon6 = new ImageView(new Image(getClass().getResourceAsStream("/images/emulator.jpg"), 20, 20, true, true)); |
| 80 | this.setGraphic(rootIcon6); |
| 81 | } else if ("controller".equals(file.getFileName().toString())) { |
| 82 | Node rootIcon6 = new ImageView(new Image(getClass().getResourceAsStream("/images/controller.jpg"), 20, 20, true, true)); |
| 83 | this.setGraphic(rootIcon6); |
| 84 | } else if ("remotetestbed".equals(file.getFileName().toString())) { |
| 85 | Node rootIcon5 = new ImageView(new Image(getClass().getResourceAsStream("/images/testbed.jpg"), 20, 20, true, true)); |
| 86 | this.setGraphic(rootIcon5); |
| 87 | } |
| 88 | |
| 89 | } else { |
| 90 | this.isDirectory = false; |
| 91 | String fileName = file.getFileName().toString(); |
| 92 | String ext = fileOperation.getExtension(fileName); |
| 93 | if (".py".equals(ext)) { |
| 94 | if ("fvtapidriver.py".equalsIgnoreCase(fileName)) { |
| 95 | Node rootIcon7 = new ImageView(new Image(getClass().getResourceAsStream("/images/flowvisor.png"), 20, 20, true, true)); |
| 96 | rootIcon7.setId("/images/flowvisor.png"); |
| 97 | this.setGraphic(rootIcon7); |
| 98 | } else if ("mininetclidriver.py".equalsIgnoreCase(fileName)) { |
| 99 | Node rootIcon8 = new ImageView(new Image(getClass().getResourceAsStream("/images/mininet.jpg"), 20, 20, true, true)); |
| 100 | rootIcon8.setId("/images/mininet.jpg"); |
| 101 | this.setGraphic(rootIcon8); |
| 102 | } else if ("poxclidriver.py".equalsIgnoreCase(fileName)) { |
| 103 | Node rootIcon9 = new ImageView(new Image(getClass().getResourceAsStream("/images/pox.jpg"), 20, 20, true, true)); |
| 104 | rootIcon9.setId("/images/pox.jpg"); |
| 105 | this.setGraphic(rootIcon9); |
| 106 | } else if ("dpctlclidriver.py".equals(fileName)) { |
| 107 | Node rootIcon10 = new ImageView(new Image(getClass().getResourceAsStream("/images/dpctl.jpg"), 20, 20, true, true)); |
| 108 | rootIcon10.setId("/images/dpctl.jpg"); |
| 109 | this.setGraphic(rootIcon10); |
| 110 | } else if ("hpswitchclidriver.py".equalsIgnoreCase(fileName)) { |
| 111 | Node rootIcon11 = new ImageView(new Image(getClass().getResourceAsStream("/images/hp.jpg"), 20, 20, true, true)); |
| 112 | rootIcon11.setId("/images/hp.jpg"); |
| 113 | this.setGraphic(rootIcon11); |
| 114 | } else if ("cisco.py".equalsIgnoreCase(fileName)) { |
| 115 | Node rootIcon12 = new ImageView(new Image(getClass().getResourceAsStream("/images/Cisco.png"), 20, 20, true, true)); |
| 116 | rootIcon12.setId("/images/Cisco.png"); |
| 117 | this.setGraphic(rootIcon12); |
| 118 | } else if ("flowvisorclidriver.py".equalsIgnoreCase(fileName)) { |
| 119 | Node rootIcon12 = new ImageView(new Image(getClass().getResourceAsStream("/images/flowvisor.png"), 20, 20, true, true)); |
| 120 | rootIcon12.setId("/images/flowvisor.png"); |
| 121 | this.setGraphic(rootIcon12); |
| 122 | } else if ("floodlightclidriver.py".equalsIgnoreCase(fileName)) { |
| 123 | Node rootIcon11 = new ImageView(new Image(getClass().getResourceAsStream("/images/floodlight.jpg"), 20, 20, true, true)); |
| 124 | rootIcon11.setId("/images/floodlight.jpg"); |
| 125 | this.setGraphic(rootIcon11); |
| 126 | } else if ("remotevmdriver.py".equalsIgnoreCase(fileName)) { |
| 127 | Node rootIcon11 = new ImageView(new Image(getClass().getResourceAsStream("/images/remotevm.jpg"), 20, 20, true, true)); |
| 128 | rootIcon11.setId("/images/remotevm.jpg"); |
| 129 | this.setGraphic(rootIcon11); |
| 130 | } else if ("remotepoxdriver.py".equalsIgnoreCase(fileName)) { |
| 131 | Node rootIcon11 = new ImageView(new Image(getClass().getResourceAsStream("/images/pox.jpg"), 20, 20, true, true)); |
| 132 | rootIcon11.setId("/images/pox.jpg"); |
| 133 | this.setGraphic(rootIcon11); |
| 134 | } else if ("flowvisordriver.py".equalsIgnoreCase(fileName)) { |
| 135 | Node rootIcon11 = new ImageView(new Image(getClass().getResourceAsStream("/images/flowvisor.png"), 20, 20, true, true)); |
| 136 | rootIcon11.setId("/images/flowvisor.png"); |
| 137 | this.setGraphic(rootIcon11); |
| 138 | } else if ("switchclidriver.py".equalsIgnoreCase(fileName)) { |
| 139 | Node rootIcon11 = new ImageView(new Image(getClass().getResourceAsStream("/images/switchVM.png"), 20, 20, true, true)); |
| 140 | rootIcon11.setId("/images/switchVM.png"); |
| 141 | this.setGraphic(rootIcon11); |
| 142 | } else { |
| 143 | Node rootIcon6 = new ImageView(new Image(getClass().getResourceAsStream("/images/defaultTerminal.png"), 20, 20, true, true)); |
| 144 | rootIcon6.setId("/images/defaultTerminal.png"); |
| 145 | this.setGraphic(rootIcon6); |
| 146 | } |
| 147 | } |
| 148 | //if you want different icons for different file types this is where you'd do it |
| 149 | } |
| 150 | if (!fullPath.endsWith(File.separator)) { |
| 151 | String finalValue = null; |
| 152 | String value = file.getFileName().toString(); |
| 153 | String ext = fileOperation.getExtension(value); |
| 154 | if (ext == null) { |
| 155 | this.setValue(value); |
| 156 | } else { |
| 157 | if (ext.equals(".py")) { |
| 158 | finalValue = value.replace(ext, ""); |
| 159 | } |
| 160 | this.setValue(finalValue); |
| 161 | } |
| 162 | |
| 163 | } |
| 164 | this.addEventHandler(TreeItem.branchExpandedEvent(), new EventHandler() { |
| 165 | @Override |
| 166 | public void handle(Event e) { |
| 167 | LoadDirectory source = (LoadDirectory) e.getSource(); |
| 168 | if (source.isDirectory() && source.isExpanded()) { |
| 169 | } |
| 170 | try { |
| 171 | if (source.getChildren().isEmpty()) { |
| 172 | Path path = Paths.get(source.getFullPath()); |
| 173 | BasicFileAttributes attribs = Files.readAttributes(path, BasicFileAttributes.class); |
| 174 | if (attribs.isDirectory()) { |
| 175 | DirectoryStream<Path> dir = Files.newDirectoryStream(path); |
| 176 | for (Path file : dir) { |
| 177 | LoadDirectory treeNode = new LoadDirectory(file); |
| 178 | if (treeNode.getValue() != null && !treeNode.getValue().equals("__init__") && !treeNode.getValue().equals("controllerdriver") && !treeNode.getValue().equals("apidriver") |
| 179 | && !treeNode.getValue().equals("clidriver") && !treeNode.getValue().equals("controllerdriver") && !treeNode.getValue().equals("emulatordriver") && !treeNode.getValue().equals("toolsdriver") && !treeNode.getValue().equals("remotesysdriver")) //source.getChildren().add(treeNode); |
| 180 | { |
| 181 | source.getChildren().add(treeNode); |
| 182 | } |
| 183 | treeNode.setExpanded(true); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | } catch (Exception ex) { |
| 189 | } |
| 190 | } |
| 191 | }); |
| 192 | this.addEventHandler(TreeItem.branchCollapsedEvent(), new EventHandler() { |
| 193 | @Override |
| 194 | public void handle(Event e) { |
| 195 | LoadDirectory source = (LoadDirectory) e.getSource(); |
| 196 | if (source.isDirectory() && !source.isExpanded()) { |
| 197 | ImageView iv = (ImageView) source.getGraphic(); |
| 198 | } |
| 199 | } |
| 200 | }); |
| 201 | |
| 202 | |
| 203 | } |
| 204 | } |