1 /* 2 * Copyright (C) 2016 3 * Deutsches Zentrum fuer Luft- und Raumfahrt e.V. 4 * Institut fuer Verkehrssystemtechnik 5 * 6 * German Aerospace Center 7 * Institute of Transportation Systems 8 * 9 */ 10 package de.dlr.ts.commons.javafx.buttons.tools; 11 12 import de.dlr.ts.commons.javafx.buttons.CustomButton; 13 import java.io.File; 14 import javafx.event.ActionEvent; 15 import javafx.scene.Node; 16 import javafx.scene.control.Label; 17 import javafx.scene.control.TextField; 18 import javafx.scene.layout.AnchorPane; 19 import javafx.scene.text.Font; 20 import javafx.stage.DirectoryChooser; 21 import javafx.stage.FileChooser; 22 import javafx.stage.Window; 23 24 /** 25 * 26 * @author @author <a href="mailto:maximiliano.bottazzi@dlr.de">Maximiliano Bottazzi</a> 27 */ 28 public class FileChooserCombo 29 { 30 private AnchorPane root = new AnchorPane(); 31 32 private Label label = new Label(); 33 private TextField textField = new TextField(); 34 private CustomButton browseButton = new CustomButton("Browse..."); 35 private CustomButton defaultButton = new CustomButton("Default"); 36 37 private File selectedFile = null; 38 private FileChooser fileChooser = new FileChooser(); 39 private DirectoryChooser directoryChooser = new DirectoryChooser(); 40 41 private static File initialDirectory = null; 42 private boolean defaultButtonEnabled = true; 43 44 private double labelWidth = 95d; 45 private double textFieldWidth = 260d; 46 private Window ownerWindow; 47 48 49 50 /** 51 * 52 * @param fontSize 53 */ setFontSize(double fontSize)54 public void setFontSize(double fontSize) 55 { 56 label.setFont(Font.font(fontSize)); 57 textField.setFont(Font.font(fontSize)); 58 browseButton.setFont(Font.font(fontSize)); 59 defaultButton.setFont(Font.font(fontSize)); 60 } 61 62 /** 63 * 64 * @param labelText 65 * @param initialDirectory 66 * @param ownerWindow 67 */ FileChooserCombo(String labelText, final File initialDirectory, Window ownerWindow)68 public FileChooserCombo(String labelText, final File initialDirectory, Window ownerWindow) 69 { 70 FileChooserCombo.initialDirectory = initialDirectory; 71 72 label.setText(labelText); 73 setLayout(); 74 75 browseButton.setOnAction((ActionEvent event) -> 76 { 77 fileChooser.setInitialDirectory(FileChooserCombo.initialDirectory); 78 selectedFile = fileChooser.showOpenDialog(ownerWindow); 79 80 if(selectedFile != null) 81 { 82 textField.setText(selectedFile.getAbsolutePath()); 83 FileChooserCombo.initialDirectory = selectedFile.getParentFile(); 84 } 85 }); 86 } 87 88 /** 89 * 90 * @return 91 */ getTextFieldWidth()92 public double getTextFieldWidth() 93 { 94 return textFieldWidth; 95 } 96 97 /** 98 * 99 * @return 100 */ getLabelWidth()101 public double getLabelWidth() 102 { 103 return labelWidth; 104 } 105 106 /** 107 * 108 * @param width 109 */ setLabelWidth(double width)110 public void setLabelWidth(double width) 111 { 112 this.labelWidth = width; 113 setLayout(); 114 } 115 116 /** 117 * 118 * @param textFieldWidth 119 */ setTextFieldWidth(double textFieldWidth)120 public void setTextFieldWidth(double textFieldWidth) 121 { 122 this.textFieldWidth = textFieldWidth; 123 setLayout(); 124 } 125 126 /** 127 * 128 * @param enable 129 */ enableDefaultButton(boolean enable)130 public void enableDefaultButton(boolean enable) 131 { 132 defaultButtonEnabled = enable; 133 setLayout(); 134 } 135 136 /** 137 * 138 */ setLayout()139 private void setLayout() 140 { 141 root.getChildren().clear(); 142 143 double width = labelWidth + 5d + textFieldWidth + 5d + 65d; 144 145 root.setPrefSize(width, 25.); 146 147 browseButton.setPrefWidth(65.); 148 AnchorPane.setTopAnchor(browseButton, 0.); 149 AnchorPane.setLeftAnchor(browseButton, labelWidth + 5d + textFieldWidth + 5d); 150 AnchorPane.setBottomAnchor(browseButton, 0.); 151 152 textField.setPrefWidth(textFieldWidth); 153 AnchorPane.setTopAnchor(textField, 0.); 154 AnchorPane.setLeftAnchor(textField, labelWidth + 5d); 155 //AnchorPane.setRightAnchor(textField, 140.); 156 AnchorPane.setBottomAnchor(textField, 0.); 157 158 label.setPrefWidth(labelWidth); 159 AnchorPane.setTopAnchor(label, 0.); 160 AnchorPane.setLeftAnchor(label, 0.); 161 AnchorPane.setBottomAnchor(label, 0.); 162 163 root.getChildren().addAll(label, textField, browseButton); 164 165 if(defaultButtonEnabled) 166 { 167 defaultButton.setPrefWidth(65.); 168 AnchorPane.setTopAnchor(defaultButton, 0.); 169 AnchorPane.setLeftAnchor(defaultButton, width + 5d); 170 AnchorPane.setBottomAnchor(defaultButton, 0.); 171 172 root.getChildren().add(defaultButton); 173 174 width += 5d + 65d; 175 } 176 } 177 178 /** 179 * 180 * @param directory 181 */ setDirectoryChooser(boolean directory)182 public void setDirectoryChooser(boolean directory) 183 { 184 if(directory) 185 { 186 browseButton.setOnAction((ActionEvent event) -> 187 { 188 fileChooser.setInitialDirectory(FileChooserCombo.initialDirectory); 189 directoryChooser.setInitialDirectory(initialDirectory); 190 selectedFile = directoryChooser.showDialog(ownerWindow); 191 192 if(selectedFile != null) 193 { 194 textField.setText(selectedFile.getAbsolutePath()); 195 FileChooserCombo.initialDirectory = selectedFile.getParentFile(); 196 } 197 }); 198 } 199 } 200 201 /** 202 * 203 * @param defaultValue 204 */ setDefaultValue(String defaultValue)205 public void setDefaultValue(String defaultValue) 206 { 207 defaultButton.setOnAction((ActionEvent event) -> textField.setText(defaultValue)); 208 } 209 210 /** 211 * 212 * @return 213 */ getNode()214 public Node getNode() 215 { 216 return root; 217 } 218 219 /** 220 * 221 * @param dir 222 */ setInitialDirectory(File dir)223 public void setInitialDirectory(File dir) 224 { 225 initialDirectory = dir; 226 } 227 228 /** 229 * 230 * @return 231 */ getSelectedFile()232 public File getSelectedFile() 233 { 234 return selectedFile; 235 } 236 237 /** 238 * 239 * @return 240 */ getBrowseButton()241 public CustomButton getBrowseButton() 242 { 243 return browseButton; 244 } 245 246 /** 247 * 248 * @return 249 */ getDefaultButton()250 public CustomButton getDefaultButton() 251 { 252 return defaultButton; 253 } 254 255 /** 256 * 257 * @return 258 */ getTextField()259 public TextField getTextField() 260 { 261 return textField; 262 } 263 264 /** 265 * 266 * @return 267 */ getFileChooser()268 public FileChooser getFileChooser() 269 { 270 return fileChooser; 271 } 272 273 } 274