1. FxWindowApp.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | package lib.fxapp; import java.io.File; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.ListView; import javafx.scene.control.TextField; import javafx.stage.Stage; public class FxWindowApp extends Application { FxWindow window; Settings settings; TextField folder; ListView<String> list; @Override public void start(Stage stage) { initialize(stage); stage.setTitle("FxWindowApp V0.9.1"); stage.setScene(new Scene(window.getMainWindow(), 400, 300)); stage.show(); } public static void main(String[] args) { launch(args); } void initialize(Stage stage) { settings = new Settings(); window = new FxWindow(stage, 1); window.addMenu("File"); window.addMenuItem("Exit", runnableExit ); addFolderToolbar(); list = window.getDndIconListView(runnableListSelected); window.setMainWindow(list); } TextField addFolderToolbar() { if( settings.get("homeFolder") == null ) { settings.set("homeFolder", window.getHomeFolder()); } window.addToolBarItem( 0, window.getLabel( "Data Folder: ")); folder = window.getTextField(settings.get("homeFolder"), runnableFolderEdit); window.addToolBarItem( 0, folder, true); window.addToolBarItem( 0, window.getImageButton("/res/16/folder.png", runnableFolderButton)); window.addToolBarItem( 0, window.getImageButton("/res/16/up.png", runnableFolderUpButton)); return folder; } Runnable runnableExit = new Runnable() { @Override public void run() { if( window.yesno("Exit", "Do you want exit progrma ?") ) { System.exit(0); } } }; Runnable runnableFolderButton = new Runnable() { @Override public void run() { File file = window.getDirectoryDialog(); if( file != null ) { folder.setText(file.getPath()); } } }; Runnable runnableFolderUpButton = new Runnable() { @Override public void run() { String text = folder.getText(); if( text != null ) { File file = new File(text); if( file.exists() && file.isDirectory() && file.getParentFile().exists() ) { folder.setText(file.getParentFile().getPath()); } } } }; Runnable runnableFolderEdit = new Runnable() { @Override public void run() { String text = folder.getText(); if( text != null ) { File file = new File(folder.getText()); if( file.exists() && file.isDirectory() ) { settings.set("homeFolder", file.getPath()); settings.save(); list.getSelectionModel().clearSelection(); list.getItems().clear(); File[] files = file.listFiles(); for( File f : files ) { list.getItems().add( f.getPath() ); } list.refresh(); } } } }; Runnable runnableListSelected = new Runnable() { @Override public void run() { String text = list.getSelectionModel().getSelectedItem(); if( text != null ) { window.runAfter(100, () -> folder.setText(text)); } } }; } |
2. FxWindow.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | package lib.fxapp; import java.io.File; import java.util.List; import java.util.Optional; import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Node; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Button; import javafx.scene.control.ButtonType; import javafx.scene.control.ContentDisplay; import javafx.scene.control.Label; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.control.ScrollPane; import javafx.scene.control.SelectionMode; import javafx.scene.control.TextField; import javafx.scene.control.ToolBar; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeView; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.DragEvent; import javafx.scene.input.Dragboard; import javafx.scene.input.TransferMode; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.stage.DirectoryChooser; import javafx.stage.FileChooser; import javafx.stage.Modality; import javafx.stage.Stage; import javafx.stage.StageStyle; import javafx.util.Callback; import javafx.util.Duration; public class FxWindow { Stage mainStage; BorderPane mainWindow; MenuBar menuBar; Menu currMenu; ToolBar toolBar[]; Label statusText; public FxWindow(Stage stage, int numToolbar) { mainStage = stage; exitOnQuit(stage); menuBar = getMenuBar(); mainWindow = getBorderPane(); mainWindow.setTop(getToolBar(numToolbar)); mainWindow.setBottom(getStatusBar()); } public FxWindow(Stage stage) { this(stage, 0); } public BorderPane getMainWindow() { return mainWindow; } public void setMainWindow(Node node) { mainWindow.setCenter(node); } public void setLeftWindow(Node node) { mainWindow.setLeft(node); } public void setRightWindow(Node node) { mainWindow.setRight(node); } public void exitOnQuit(Stage stage) { stage.setOnCloseRequest( e -> System.exit(0) ); } public void hideWindowBorder(Stage stage) { stage.initStyle(StageStyle.UNDECORATED); } public void setMainTitle(Stage stage, String title) { stage.setTitle(title); } public void setMainIcon(Stage stage, String icon) { stage.getIcons().add(new Image(getClass().getResourceAsStream(icon))); } public void runLater(Runnable runnable) { Platform.runLater(() -> runnable.run()); } public Timeline runAfter(int msec, Runnable runnable) { Timeline timeline = new Timeline(new KeyFrame( Duration.millis(msec), e -> runnable.run())); timeline.play(); return timeline; } public Timeline runPeriodic(int msec, Runnable runnable) { Timeline timeline = new Timeline(new KeyFrame( Duration.millis(msec), e -> runnable.run())); timeline.setCycleCount(Animation.INDEFINITE); timeline.play(); return timeline; } public void stopPeriodic(Timeline timeline) { timeline.stop(); } public String getHomeFolder() { return System.getProperty("user.home"); } public String getWorkingFolder() { return System.getProperty("user.dir"); } public File[] getRootLists() { return File.listRoots(); } //------------------------------------------------------------ // Containers //------------------------------------------------------------ public BorderPane getBorderPane(int top, int right, int bottom, int left) { BorderPane pane = new BorderPane(); pane.setPadding(new Insets(top, right, bottom, left)); return pane; } public BorderPane getBorderPane() { return getBorderPane(0,0,0,0); } //------------------------------------------------------------ // Menu, Toolbar, Statusbar //------------------------------------------------------------ public MenuBar getMenuBar(int fontSize) { MenuBar menuBar = new MenuBar(); if( fontSize != 0 ) { menuBar.setStyle("-fx-font-size: " + fontSize + " pt;"); } return menuBar; } public MenuBar getMenuBar() { return getMenuBar(0); } public void addMenu(String title) { currMenu = new Menu(title); menuBar.getMenus().add(currMenu); } public MenuItem addMenuItem(String title, String iconFile, Runnable runnable) { MenuItem item = new MenuItem( title); if( iconFile != null ) { item.setGraphic(new ImageView(new Image(getClass().getResourceAsStream(iconFile)))); } item.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent t) { runnable.run(); } }); currMenu.getItems().add(item); return item; } public MenuItem addMenuItem(String title, Runnable runnable) { return addMenuItem(title, null, runnable); } public void addToolBarItem(int index, Node item, boolean extend) { if( extend ) { HBox.setHgrow(toolBar[index], Priority.ALWAYS); HBox.setHgrow(item, Priority.ALWAYS); } toolBar[index].getItems().add(item); } public void addToolBarItem(int index, Node item) { addToolBarItem(index, item,false); } public void addToolBarItem(Node item, boolean extend) { addToolBarItem(0, item, extend); } public void addToolBarItem(Node item) { addToolBarItem(0, item,false); } public Label getLabel(String text) { return new Label(text); } public VBox getToolBar(int numToolbar) { VBox box = new VBox(0); box.setAlignment(Pos.CENTER); box.setPadding(new Insets(0, 0, 0, 0)); box.getChildren().add(menuBar); if( numToolbar > 0 ) { toolBar = new ToolBar[numToolbar]; for( int i = 0; i < numToolbar; i++ ) { toolBar[i] = new ToolBar(); box.getChildren().add(toolBar[i]); } } return box; } public HBox getStatusBar() { statusText = new Label("Ready"); statusText.setAlignment(Pos.CENTER_LEFT); HBox hbox = new HBox(16); hbox.setAlignment(Pos.CENTER_LEFT); hbox.setPadding(new Insets(4, 4, 4, 4)); hbox.getChildren().add(statusText); return hbox; } public void setStatusTextSize(int size) { statusText.setFont(new Font(size)); } public void setStatusText(String text) { statusText.setText(text); } //------------------------------------------------------------ // Drag And Drop //------------------------------------------------------------ public void setDragAndDrop(Node item, FxRunnable runnable) { item.setOnDragOver(new EventHandler<DragEvent>() { @Override public void handle(DragEvent event) { if( event.getDragboard().hasFiles() ) { event.acceptTransferModes(TransferMode.COPY_OR_MOVE); } event.consume(); } }); item.setOnDragDropped(new EventHandler<DragEvent>() { @Override public void handle(DragEvent event) { Dragboard db = event.getDragboard(); boolean success = false; if( db.hasFiles() ) { success = true; List<File> files = db.getFiles(); runnable.run(files); } event.setDropCompleted(success); event.consume(); } }); } //------------------------------------------------------------ // Controls //------------------------------------------------------------ public Label getLabel( String text, int fontSize ) { Label label = new Label(text); label.setFont(new Font(fontSize)); return label; } public Button getButton(String text, String image, Runnable runnable) { Button item = new Button(); if( text != null ) { item.setText(text); } if( image != null ) { item.setGraphic(new ImageView(new Image(getClass().getResourceAsStream(image)))); if( text != null ) { item.setContentDisplay(ContentDisplay.TOP); } } if( runnable != null ) { item.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent t) { runnable.run(); } }); } return item; } public Button getImageButton(String image, Runnable runnable) { return getButton(null, image, runnable); } public TextField getTextField(Runnable runnable, String text, int fontSize) { TextField item = new TextField(text == null ? "" : text); if( fontSize != 0 ) { item.setFont(new Font(fontSize)); } if( runnable != null ) { item.textProperty().addListener((obs, oldText, newText) -> { runnable.run(); }); } setDragAndDrop(item, new FxRunnable() { @Override public void run(Object... object) { if( object[0] instanceof List<?> ) { @SuppressWarnings("unchecked") List<File> files = (List<File>) object[0]; item.setText( files.get(0).getPath() ); } } }); return item; } public TextField getTextField() { return getTextField( null, "", 0 ); } public TextField getTextField(String text) { return getTextField( null, text, 0 ); } public TextField getTextField(Runnable runnable) { return getTextField( runnable, "", 0 ); } public TextField getTextField(String text, Runnable runnable) { return getTextField( runnable, text, 0 ); } public ScrollPane getScrollPane(int fontSize) { ScrollPane item = new ScrollPane(); item.setPannable(true); if( fontSize > 0 ) { item.setStyle("-fx-font-size: " + fontSize + " pt;"); } return item; } public ImageView getScrollImageView(ScrollPane pane, FxRunnable runnable) { ImageView item = new ImageView(); // the following line allows detection of clicks on transparent // parts of the image: item.setPickOnBounds(true); if( runnable != null ) { item.setOnMouseClicked(e -> { runnable.run(e); }); } setDragAndDrop(item, new FxRunnable() { @Override public void run(Object... object) { if( object[0] instanceof List<?> ) { @SuppressWarnings("unchecked") List<File> files = (List<File>) object[0]; //TODO } } }); item.setPreserveRatio(true); item.fitWidthProperty().bind(pane.widthProperty()); pane.setContent(item); return item; } public void setImageViewSize(ImageView imageView) { //Image image = new Image("my/res/flower.png", 100, 100, false, false); imageView.setFitHeight(100); imageView.setFitWidth(100); imageView.setPreserveRatio(true); } public ListView<String> getListView(boolean icon, boolean dnd, int fontSize, Runnable runnable) { ListView<String> item = new ListView<String>(); if( fontSize != 0 ) { item.setStyle("-fx-font-size: " + fontSize + " pt;"); } if( icon ) { item.setCellFactory(new Callback<ListView<String>, ListCell<String>>() { @Override public ListCell<String> call(ListView<String> list) { return new ListCell<String>() { @Override public void updateItem(String item, boolean empty) { super.updateItem(item, empty); String image = null; if( empty == false ) { if( new File(item).isDirectory() ) { image = "/res/16/folder.png"; } else { image = "/res/16/file.png"; } setGraphic(new ImageView(new Image(FxWindow.class.getResourceAsStream(image)))); setText(item); } else { setGraphic(null); setText(null); } } }; } }); } if( dnd ) { setDragAndDrop(item, new FxRunnable() { @Override public void run(Object... object) { if( object[0] instanceof List<?> ) { @SuppressWarnings("unchecked") List<File> files = (List<File>) object[0]; for( File f : files ) { item.getItems().add( f.getPath() ); } } } }); } if( runnable != null ) { item.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() { public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { runnable.run(); } }); } return item; } public ListView<String> getListView(Runnable runnable) { return getListView(false,false,0,runnable); } public ListView<String> getDndListView(Runnable runnable) { return getListView(false,true,0,runnable); } public ListView<String> getIconListView(Runnable runnable) { return getListView(true,false,0,runnable); } public ListView<String> getDndIconListView(Runnable runnable) { return getListView(true,true,0,runnable); } public ListView<String> getDndIconListView() { return getListView(true,true,0,null); } public void enableMultiSelection(ListView<String> list) { list.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); } public TreeView<String> getTreeFileView(String baseFolder, int fontSize, Runnable runnable) { System.out.println("getTreeFileView(): " + baseFolder); TreeView<String> tree = new TreeView<>(); if( fontSize > 0 ) { tree.setStyle("-fx-font-size: " + fontSize + " pt;"); } tree.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<String>>() { @Override public void changed(ObservableValue<? extends TreeItem<String>> observable, TreeItem<String> oldValue, TreeItem<String> newValue) { runnable.run(); } }); Image icon = new Image(getClass().getResourceAsStream("/res/16/folder.png")); TreeItem<String> root = new TreeItem<> (new File(baseFolder).getName(), new ImageView(icon)); addToTreeView(root, baseFolder, fontSize, runnable); tree.setRoot(root); return tree; } public TreeItem<String> getTreeRootItem(TreeView<String> tree) { return tree.getRoot(); } public void setShowRoot(TreeView<String> tree) { tree.setShowRoot(false); } public String getSelectedTreePath(TreeView<String> tree) { TreeItem<String> item = tree.getSelectionModel().getSelectedItem(); StringBuilder sb = new StringBuilder(); sb.insert( 0, item.getValue() ); item = item.getParent(); while( item != null ) { sb.insert(0, "/"); sb.insert(0, item.getValue()); item = item.getParent(); } return sb.toString(); } private void addToTreeView(TreeItem<String> node, String folder, int fontSize, Runnable runnable) { System.out.println("addToTreeView(): Folder: " + node.getValue() + " -> folder: " + folder); File dir = new File(folder); File[] files = dir.listFiles(); for( File file : files ) { if( file.isDirectory() ) { System.out.println("addToTreeView(): Folder: " + file.getName() + " -> folder: " + folder); Image icon = new Image(getClass().getResourceAsStream("/res/16/folder.png")); TreeItem<String> item = new TreeItem<> ( file.getName(), new ImageView(icon)); item.setExpanded(true); node.getChildren().add(item); addToTreeView( item, file.getPath(), fontSize, runnable ); } else { System.out.println("addToTreeView(): File: " + file.getName() + " -> folder: " + folder); Image icon = new Image(getClass().getResourceAsStream("/res/16/file.png")); TreeItem<String> item = new TreeItem<> ( file.getName(), new ImageView(icon)); node.getChildren().add(item); } } } //------------------------------------------------------------ // Dialog //------------------------------------------------------------ public void alert(String title, String message) { Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle(title); alert.setHeaderText(null); alert.setContentText(message); alert.initModality(Modality.APPLICATION_MODAL); alert.initOwner(mainStage); alert.showAndWait(); } public boolean yesno(String title, String message) { Alert alert = new Alert(AlertType.CONFIRMATION); alert.setTitle(title); alert.setHeaderText(null); alert.setContentText(message); alert.initModality(Modality.APPLICATION_MODAL); alert.initOwner(mainStage); Optional<ButtonType> result = alert.showAndWait(); if (result.get() == ButtonType.OK ){ return true; } else { return false; } } public File getFileDialog() { FileChooser dialog = new FileChooser(); dialog.setTitle("Select File"); return dialog.showOpenDialog(null); } public File getDirectoryDialog() { DirectoryChooser dialog = new DirectoryChooser(); dialog.setTitle("Select Folder"); return dialog.showDialog(null); } } |
3. FxRunnable.java
1 2 3 | public interface FxRunnable { public void run(Object object); } |
4. Settings.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | package lib.fxapp; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Properties; public class Settings { Properties settings; public Settings() { settings = new Properties(); load(); } void load() { InputStream input = null; try { input = new FileInputStream("config.properties"); settings.load(input); } catch (IOException ex) { ex.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } } void save() { OutputStream output = null; try { output = new FileOutputStream("config.properties"); settings.store(output, null); } catch (IOException io) { io.printStackTrace(); } finally { if (output != null) { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } } void set(String name, String value) { settings.setProperty(name, value); } String get(String name) { return settings.getProperty(name); } } |
댓글 없음:
댓글 쓰기