1 /****************************************************************************/ 2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo 3 // Copyright (C) 2016-2018 German Aerospace Center (DLR) and others. 4 // This program and the accompanying materials 5 // are made available under the terms of the Eclipse Public License v2.0 6 // which accompanies this distribution, and is available at 7 // http://www.eclipse.org/legal/epl-v20.html 8 // SPDX-License-Identifier: EPL-2.0 9 /****************************************************************************/ 10 /// @file Constants.java 11 /// @author Maximiliano Bottazzi 12 /// @date 2016 13 /// @version $Id$ 14 /// 15 // 16 /****************************************************************************/ 17 package de.dlr.ts.lisum.gui.options; 18 19 import de.dlr.ts.commons.javafx.buttons.tools.FileChooserCombo; 20 import de.dlr.ts.commons.javafx.buttonspanels.OkCancelButtonsPanel; 21 import de.dlr.ts.commons.javafx.messages.ConfirmationMessage; 22 import de.dlr.ts.commons.logger.DLRLogger; 23 import de.dlr.ts.lisum.gui.SystemProperties; 24 import java.io.File; 25 import java.io.FileInputStream; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import java.util.Optional; 29 import java.util.Properties; 30 import javafx.event.ActionEvent; 31 import javafx.scene.Node; 32 import javafx.scene.Scene; 33 import javafx.scene.control.ButtonType; 34 import javafx.scene.control.Label; 35 import javafx.scene.input.KeyCode; 36 import javafx.scene.input.KeyEvent; 37 import javafx.scene.layout.AnchorPane; 38 import javafx.scene.text.Font; 39 import javafx.scene.text.FontWeight; 40 import javafx.stage.Modality; 41 import javafx.stage.Stage; 42 import javafx.stage.StageStyle; 43 import javafx.stage.Window; 44 import org.apache.commons.io.FileUtils; 45 46 /** 47 * 48 * @author @author <a href="mailto:maximiliano.bottazzi@dlr.de">Maximiliano Bottazzi</a> 49 */ 50 public class WorkspaceWindow 51 { 52 private Scene scene; 53 private Stage stage; 54 55 private final AnchorPane root = new AnchorPane(); 56 private final OkCancelButtonsPanel okCancelButtons = new OkCancelButtonsPanel(); 57 58 private final String initialDirName = System.getProperty("user.home") + File.separator + "LiSuMWorkspace"; 59 private FileChooserCombo workspaceChooser; 60 61 private static File workspace = null; 62 private String storedWorkspace; 63 64 private final Properties props = new Properties(); 65 private static final String PERSISTENCE_FILE = "LiSuM_Workspace.properties"; 66 private static final String WORKSPACE_KEY = "workspace"; 67 private File metadata; 68 69 70 71 /** 72 * 73 */ WorkspaceWindow()74 public WorkspaceWindow() 75 { 76 } 77 78 /** 79 * 80 */ readFromFile()81 public void readFromFile() 82 { 83 String tmpdir = System.getProperty("java.io.tmpdir") + File.separator; 84 File persFile = new File(tmpdir + PERSISTENCE_FILE); 85 86 DLRLogger.finest(this, "Reading workspace path from " + persFile.getAbsolutePath()); 87 88 if(persFile.exists()) 89 { 90 try 91 { 92 props.load(new FileInputStream(persFile)); 93 storedWorkspace = props.getProperty(WORKSPACE_KEY); 94 95 if(storedWorkspace != null && !storedWorkspace.isEmpty()) 96 { 97 workspace = new File(storedWorkspace); 98 if(!workspace.exists()) 99 workspace = null; 100 } 101 else 102 storedWorkspace = initialDirName; 103 } 104 catch (IOException ex) 105 { 106 ex.printStackTrace(System.out); 107 } 108 } 109 } 110 111 /** 112 * 113 */ saveAndClose()114 private void saveAndClose() 115 { 116 117 if(workspaceChooser.getTextField().getText().isEmpty()) 118 workspace = null; 119 else 120 { 121 workspace = new File(workspaceChooser.getTextField().getText()); 122 boolean createPath = createPath(); 123 124 if(!createPath) 125 return; 126 } 127 128 try 129 { 130 if(workspace == null) 131 props.remove(WORKSPACE_KEY); 132 else 133 props.put(WORKSPACE_KEY, workspace.getAbsolutePath()); 134 135 String tmpdir = System.getProperty("java.io.tmpdir") + File.separator; 136 137 props.store(new FileOutputStream(tmpdir + PERSISTENCE_FILE), ""); 138 139 stage.close(); 140 } catch (IOException ex) 141 { 142 ex.printStackTrace(System.out); 143 } 144 } 145 146 /** 147 * 148 */ createPath()149 private boolean createPath() 150 { 151 boolean ok = true; 152 153 if(!workspace.exists()) 154 { 155 ConfirmationMessage cm = new ConfirmationMessage("Proceed?"); 156 cm.setHeader("The selected workspace does not exist and will be created."); 157 Optional<ButtonType> result = cm.showAndWait(); 158 159 if(result.get() == ButtonType.OK) 160 { 161 workspace.mkdirs(); 162 } 163 else 164 ok = false; 165 } 166 167 if(ok) 168 { 169 metadata = new File(workspace.getAbsolutePath() + File.separator + ".metadata"); 170 171 if(!metadata.exists()) 172 { 173 metadata.mkdirs(); 174 175 /** 176 * Copy sample simulation to new workspace 177 */ 178 File demoOrigin = new File("simulations" + File.separator + "sampleSimulation"); 179 File demoDest = new File(workspace.getAbsolutePath() + File.separator + "sampleSimulation"); 180 181 try 182 { 183 if(!demoDest.exists()) 184 FileUtils.copyDirectory(demoOrigin, demoDest); 185 } catch (IOException ex) 186 { 187 //ex.printStackTrace(); 188 } 189 } 190 } 191 192 return ok; 193 } 194 195 /** 196 * 197 * @param owner 198 */ show(Window owner)199 public void show(Window owner) 200 { 201 okCancelButtons.getOkButton().setOnAction((ActionEvent event) -> saveAndClose()); 202 okCancelButtons.getCancelButton().setOnAction((ActionEvent event) -> stage.close()); 203 //okButton.setDisable(true); 204 205 scene = new Scene(root, 670, 180); 206 stage = new Stage(); 207 stage.initStyle(StageStyle.UTILITY); 208 stage.setTitle(SystemProperties.getInstance().getSystemName()); 209 stage.setScene(scene); 210 stage.initModality(Modality.WINDOW_MODAL); 211 //stage.initOwner(MainProgram.getInstance().getScene().getWindow()); 212 stage.initOwner(owner); 213 stage.getIcons().add(SystemProperties.getInstance().getMainIcon()); 214 stage.setResizable(false); 215 216 stage.addEventHandler(KeyEvent.KEY_PRESSED, (KeyEvent event) -> 217 { 218 if(event.getCode() == KeyCode.ESCAPE) 219 stage.close(); 220 }); 221 222 /** 223 * 224 */ 225 AnchorPane pane = new AnchorPane(); 226 AnchorPane.setTopAnchor(pane, 0.); 227 AnchorPane.setLeftAnchor(pane, 0.); 228 AnchorPane.setRightAnchor(pane, 0.); 229 pane.setPrefHeight(60.); 230 pane.setStyle("-fx-background-color: #FFFFFF;"); 231 232 233 Label label = new Label("Select a directory as workspace"); 234 label.setFont(Font.font("Arial", FontWeight.BOLD, 14)); 235 AnchorPane.setTopAnchor(label, 9.); 236 AnchorPane.setLeftAnchor(label, 8.); 237 238 Label label2 = new Label("This is where you store your simulations and where LiSuM saves the system preferences."); 239 label2.setFont(Font.font(12)); 240 AnchorPane.setTopAnchor(label2, 32.); 241 AnchorPane.setLeftAnchor(label2, 16.); 242 243 pane.getChildren().addAll(label, label2); 244 245 /** 246 * 247 */ 248 File init; 249 if(workspace == null) 250 init = new File("."); 251 else 252 { 253 String parent = workspace.getParent(); 254 if(parent != null && !parent.isEmpty()) 255 init = new File(parent); 256 else 257 init = workspace; 258 } 259 260 workspaceChooser = new FileChooserCombo("Workspace:", new File(System.getProperty("user.home")), stage); 261 workspaceChooser.enableDefaultButton(false); 262 workspaceChooser.setLabelWidth(80); 263 workspaceChooser.setTextFieldWidth(440.); 264 workspaceChooser.getBrowseButton().setPrefWidth(90.); 265 workspaceChooser.setDirectoryChooser(true); 266 267 if(workspace == null) 268 workspaceChooser.getTextField().setText(initialDirName); 269 else 270 workspaceChooser.getTextField().setText(workspace.getAbsolutePath()); 271 272 Node workspaceChooserNode = workspaceChooser.getNode(); 273 AnchorPane.setTopAnchor(workspaceChooserNode, 90.); 274 AnchorPane.setLeftAnchor(workspaceChooserNode, 14.); 275 276 /** 277 * 278 */ 279 Node okCancelNode = okCancelButtons.getNode(); 280 AnchorPane.setBottomAnchor(okCancelNode, 0.); 281 AnchorPane.setLeftAnchor(okCancelNode, 0.); 282 AnchorPane.setRightAnchor(okCancelNode, 0.); 283 284 root.getChildren().addAll(okCancelNode, workspaceChooserNode, pane); 285 286 stage.showAndWait(); 287 } 288 289 /** 290 * 291 * @return 292 */ getWorkspace()293 public static File getWorkspace() 294 { 295 return workspace; 296 } 297 } 298