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.io.IOException; |
| 9 | import java.nio.file.DirectoryStream; |
| 10 | import java.nio.file.Files; |
| 11 | import java.nio.file.Path; |
| 12 | import java.nio.file.Paths; |
| 13 | import java.nio.file.attribute.BasicFileAttributes; |
| 14 | import javafx.event.Event; |
| 15 | import javafx.event.EventHandler; |
| 16 | import javafx.scene.Node; |
| 17 | import javafx.scene.control.TreeItem; |
| 18 | import javafx.scene.image.Image; |
| 19 | import javafx.scene.image.ImageView; |
| 20 | |
| 21 | /** |
| 22 | * |
| 23 | * @author Raghav Kashyap (raghavkashyap@paxterrasolutions.com) |
| 24 | |
| 25 | * TestON is free software: you can redistribute it and/or modify |
| 26 | * it under the terms of the GNU General Public License as published by |
| 27 | * the Free Software Foundation, either version 2 of the License, or |
| 28 | * (at your option) any later version. |
| 29 | |
| 30 | * TestON is distributed in the hope that it will be useful, |
| 31 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 32 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 33 | * GNU General Public License for more details. |
| 34 | |
| 35 | * You should have received a copy of the GNU General Public License |
| 36 | * along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 37 | |
| 38 | */ |
| 39 | public class OFALoadTree extends TreeItem<String> { |
| 40 | |
| 41 | OFAFileOperations fileOperation = new OFAFileOperations(); |
| 42 | public static Image folderCollapseImage = new Image("images/folder.jpg", 30.0, 30.0, true, true); |
| 43 | public static Image folderExpandImage = new Image("images/folder.jpg", 10.0, 10.0, true, true); |
| 44 | public static Image fileImage = new Image("images/File.png", 30.0, 30.0, true, true); |
| 45 | //this stores the full path to the file or directory |
| 46 | private String fullPath; |
| 47 | |
| 48 | public String getFullPath() { |
| 49 | return (this.fullPath); |
| 50 | } |
| 51 | private boolean isDirectory; |
| 52 | |
| 53 | public boolean isDirectory() { |
| 54 | return (this.isDirectory); |
| 55 | } |
| 56 | |
| 57 | public OFALoadTree(Path file) { |
| 58 | super(file.toString()); |
| 59 | this.fullPath = file.toString(); |
| 60 | //test if this is a directory and set the icon |
| 61 | if (Files.isDirectory(file)) { |
| 62 | this.isDirectory = true; |
| 63 | if ("common".equals(file.getFileName().toString())) { |
| 64 | Node rootIcon = new ImageView(new Image(getClass().getResourceAsStream("/images/project.jpeg"), 20, 20, true, true)); |
| 65 | this.setGraphic(rootIcon); |
| 66 | } else if ("cli".equals(file.getFileName().toString())) { |
| 67 | Node rootIcon2 = new ImageView(new Image(getClass().getResourceAsStream("/images/terminal.png"), 20, 20, true, true)); |
| 68 | rootIcon2.setId("/images/terminal.png"); |
| 69 | this.setGraphic(rootIcon2); |
| 70 | } else if ("api".equals(file.getFileName().toString())) { |
| 71 | Node rootIcon3 = new ImageView(new Image(getClass().getResourceAsStream("/images/www2.jpg"), 20, 20, true, true)); |
| 72 | this.setGraphic(rootIcon3); |
| 73 | } else if ("emulator".equals(file.getFileName().toString())) { |
| 74 | Node rootIcon4 = new ImageView(new Image(getClass().getResourceAsStream("/images/mobile.png"), 20, 20, true, true)); |
| 75 | this.setGraphic(rootIcon4); |
| 76 | } else if ("tool".equals(file.getFileName().toString())) { |
| 77 | Node rootIcon5 = new ImageView(new Image(getClass().getResourceAsStream("/images/automatorui.png"), 20, 20, true, true)); |
| 78 | this.setGraphic(rootIcon5); |
| 79 | } else if ("contoller".equals(file.getFileName().toString())) { |
| 80 | Node rootIcon6 = new ImageView(new Image(getClass().getResourceAsStream("/images/windows.jpg"), 20, 20, true, true)); |
| 81 | this.setGraphic(rootIcon6); |
| 82 | } else { |
| 83 | Node rootIcon = new ImageView(new Image(getClass().getResourceAsStream("/images/project.jpeg"), 20, 20, true, true)); |
| 84 | rootIcon.setId("/images/project.jpeg"); |
| 85 | this.setGraphic(rootIcon); |
| 86 | } |
| 87 | } else { |
| 88 | this.isDirectory = false; |
| 89 | String fileName = file.getFileName().toString(); |
| 90 | String ext = fileOperation.getExtension(fileName); |
| 91 | if (".ospk".equals(ext)) { |
| 92 | Node rootIcon5 = new ImageView(fileImage); |
| 93 | rootIcon5.setId(".ospk"); |
| 94 | this.setGraphic(rootIcon5); |
| 95 | |
| 96 | } else if (".params".equals(ext)) { |
| 97 | Node rootIcon1 = new ImageView(new Image(getClass().getResourceAsStream("/images/params.jpeg"), 30, 30, true, true)); |
| 98 | rootIcon1.setId(".params"); |
| 99 | setGraphic(rootIcon1); |
| 100 | |
| 101 | } else if (".topo".equals(ext)) { |
| 102 | Node rootIcon3 = new ImageView(new Image(getClass().getResourceAsStream("/images/tpl.png"), 30, 30, true, true)); |
| 103 | rootIcon3.setId(".topo"); |
| 104 | setGraphic(rootIcon3); |
| 105 | } else if (".log".equals(ext)) { |
| 106 | Node rootIcon3 = new ImageView(new Image(getClass().getResourceAsStream("/images/log.jpg"), 20, 20, true, true)); |
| 107 | rootIcon3.setId(".log"); |
| 108 | setGraphic(rootIcon3); |
| 109 | } else if (".rpt".equals(ext)) { |
| 110 | Node rootIcon3 = new ImageView(new Image(getClass().getResourceAsStream("/images/report.jpeg"), 20, 20, true, true)); |
| 111 | rootIcon3.setId(".rpt"); |
| 112 | setGraphic(rootIcon3); |
| 113 | } else if (".session".equals(ext)) { |
| 114 | Node rootIcon3 = new ImageView(new Image(getClass().getResourceAsStream("/images/session.png"), 20, 20, true, true)); |
| 115 | rootIcon3.setId(".session"); |
| 116 | setGraphic(rootIcon3); |
| 117 | } else if (".py".equals(ext)) { |
| 118 | Node rootIcon3 = new ImageView(new Image(getClass().getResourceAsStream("/images/py.jpg"), 20, 20, true, true)); |
| 119 | rootIcon3.setId(".py"); |
| 120 | setGraphic(rootIcon3); |
| 121 | } |
| 122 | |
| 123 | //if you want different icons for different file types this is where you'd do it |
| 124 | } |
| 125 | |
| 126 | //set the value |
| 127 | if (!fullPath.endsWith(File.separator)) { |
| 128 | //set the value (which is what is displayed in the tree) |
| 129 | String value = file.toString(); |
| 130 | int indexOf = value.lastIndexOf(File.separator); |
| 131 | if (indexOf > 0) { |
| 132 | this.setValue(value.substring(indexOf + 1)); |
| 133 | } else { |
| 134 | this.setValue(value); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | |
| 139 | this.addEventHandler(TreeItem.branchExpandedEvent(), new EventHandler() { |
| 140 | @Override |
| 141 | public void handle(Event e) { |
| 142 | OFALoadTree source = (OFALoadTree) e.getSource(); |
| 143 | if (source.isDirectory() && source.isExpanded()) { |
| 144 | } |
| 145 | try { |
| 146 | if (source.getChildren().isEmpty()) { |
| 147 | Path path = Paths.get(source.getFullPath()); |
| 148 | BasicFileAttributes attribs = Files.readAttributes(path, BasicFileAttributes.class); |
| 149 | if (attribs.isDirectory()) { |
| 150 | DirectoryStream<Path> dir = Files.newDirectoryStream(path); |
| 151 | for (Path file : dir) { |
| 152 | OFALoadTree treeNode = new OFALoadTree(file); |
| 153 | String fileExtension = fileOperation.getExtension(treeNode.getValue()); |
| 154 | String fileName = fileOperation.getFileName(treeNode.getValue()); |
| 155 | if (fileExtension == null || fileExtension.equals(".py") || fileExtension.equals(".ospk") || fileExtension.equals(".topo") || fileExtension.equals(".params") || fileExtension.equals(".log") || fileExtension.equals(".rpt") || fileExtension.equals(".session")) { |
| 156 | if (fileExtension == null) { |
| 157 | treeNode.setExpanded(true); |
| 158 | source.getChildren().add(treeNode); |
| 159 | } else { |
| 160 | if (fileExtension.matches(".(ospk|params|topo|py|log|rpt|session)")) { |
| 161 | String finalValue = treeNode.getValue().replace(fileExtension, ""); |
| 162 | treeNode.setValue(finalValue); |
| 163 | if (!treeNode.getValue().equals("__init__")) { |
| 164 | source.getChildren().add(treeNode); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | } catch (Exception ex) { |
| 174 | } |
| 175 | } |
| 176 | }); |
| 177 | } |
| 178 | } |