public void addImageTab(Path imagePath) { TabPane previewTabPane = controller.getPreviewTabPane(); ImageTab tab = new ImageTab(); tab.setPath(imagePath); tab.setText(imagePath.getFileName().toString()); if (previewTabPane.getTabs().contains(tab)) { previewTabPane.getSelectionModel().select(tab); return; } Image image = new Image(IOHelper.pathToUrl(imagePath)); ImageView imageView = new ImageView(image); imageView.setPreserveRatio(true); imageView.setFitWidth(previewTabPane.getWidth()); previewTabPane .widthProperty() .addListener( (observable, oldValue, newValue) -> { imageView.setFitWidth(previewTabPane.getWidth()); }); Tooltip tip = new Tooltip(imagePath.toString()); Tooltip.install(tab.getGraphic(), tip); ScrollPane scrollPane = new ScrollPane(); scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); scrollPane.setContent(imageView); scrollPane.addEventFilter( ScrollEvent.SCROLL, e -> { if (e.isControlDown() && e.getDeltaY() > 0) { // zoom in imageView.setFitWidth(imageView.getFitWidth() + 16.0); } else if (e.isControlDown() && e.getDeltaY() < 0) { // zoom out imageView.setFitWidth(imageView.getFitWidth() - 16.0); } }); tab.setContent(scrollPane); TabPane tabPane = previewTabPane; tabPane.getTabs().add(tab); tabPane.getSelectionModel().select(tab); }
public void addTab(Path path, Runnable... runnables) { ObservableList<String> recentFiles = controller.getRecentFilesList(); if (Files.notExists(path)) { recentFiles.remove(path.toString()); return; } ObservableList<Tab> tabs = controller.getTabPane().getTabs(); for (Tab tab : tabs) { MyTab myTab = (MyTab) tab; Path currentPath = myTab.getPath(); if (Objects.nonNull(currentPath)) if (currentPath.equals(path)) { myTab.select(); // Select already added tab return; } } AnchorPane anchorPane = new AnchorPane(); EditorPane editorPane = webviewService.createWebView(); MyTab tab = createTab(); tab.setEditorPane(editorPane); tab.setTabText(path.getFileName().toString()); editorPane.confirmHandler( param -> { if ("command:ready".equals(param)) { JSObject window = editorPane.getWindow(); window.setMember("afx", controller); window.call("updateOptions", new Object[] {}); Map<String, String> shortCuts = controller.getShortCuts(); Set<String> keySet = shortCuts.keySet(); for (String key : keySet) { window.call("addNewCommand", new Object[] {key, shortCuts.get(key)}); } if (Objects.isNull(path)) return true; threadService.runTaskLater( () -> { String content = IOHelper.readFile(path); threadService.runActionLater( () -> { window.call("changeEditorMode", path.toUri().toString()); window.call("setInitialized"); window.call("setEditorValue", new Object[] {content}); for (Runnable runnable : runnables) { runnable.run(); } }); }); } return false; }); threadService.runActionLater( () -> { TabPane tabPane = controller.getTabPane(); tabPane.getTabs().add(tab); tab.select(); }); Node editorVBox = editorService.createEditorVBox(editorPane, tab); controller.fitToParent(editorVBox); anchorPane.getChildren().add(editorVBox); tab.setContent(anchorPane); tab.setPath(path); Tooltip tip = new Tooltip(path.toString()); Tooltip.install(tab.getGraphic(), tip); recentFiles.remove(path.toString()); recentFiles.add(0, path.toString()); editorPane.focus(); }