Example #1
0
  public Optional<String> toImageBlock(List<File> dropFiles) {

    if (!current.currentPath().isPresent()) asciiDocController.saveDoc();

    Path currentPath = current.currentPath().map(Path::getParent).get();
    IOHelper.createDirectories(currentPath.resolve("images"));
    List<Path> paths =
        dropFiles
            .stream()
            .map(File::toPath)
            .filter(pathResolver::isImage)
            .collect(Collectors.toList());

    List<String> buffer = new LinkedList<>();

    for (Path path : paths) {
      Path targetImage = currentPath.resolve("images").resolve(path.getFileName());
      if (!path.equals(targetImage)) IOHelper.copy(path, targetImage);
      buffer.add(String.format("image::images/%s[]", path.getFileName()));
    }

    if (buffer.size() > 0) return Optional.of(String.join("\n", buffer));

    return Optional.empty();
  }
Example #2
0
  public Optional<String> toImageBlock(Image image) {

    if (!current.currentPath().isPresent()) asciiDocController.saveDoc();

    Path currentPath = current.currentPath().map(Path::getParent).get();
    IOHelper.createDirectories(currentPath.resolve("images"));

    List<String> buffer = new LinkedList<>();
    DateTimeFormatter dateTimeFormatter =
        DateTimeFormatter.ofPattern(asciiDocController.getClipboardImageFilePattern());
    Path path = Paths.get(dateTimeFormatter.format(LocalDateTime.now()));

    Path targetImage = currentPath.resolve("images").resolve(path.getFileName());

    try {
      BufferedImage fromFXImage = SwingFXUtils.fromFXImage(image, null);
      ImageIO.write(fromFXImage, "png", targetImage.toFile());

    } catch (Exception e) {
      logger.error("Problem occured while saving clipboard image {}", targetImage);
    }

    buffer.add(String.format("image::images/%s[]", path.getFileName()));

    if (buffer.size() > 0) return Optional.of(String.join("\n", buffer));

    return Optional.empty();
  }
Example #3
0
  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);
  }
Example #4
0
  public Optional<String> toIncludeBlock(List<File> dropFiles) {
    if (!current.currentPath().isPresent()) asciiDocController.saveDoc();

    Path currentPath = current.currentPath().map(Path::getParent).get();

    List<Path> files =
        dropFiles
            .stream()
            .map(File::toPath)
            .filter(p -> !Files.isDirectory(p))
            .collect(Collectors.toList());

    List<String> buffer = new LinkedList<>();

    for (Path path : files) {
      Path targetPath = currentPath.resolve(path.getFileName());
      if (!path.equals(targetPath)) IOHelper.copy(path, targetPath);
      buffer.add(String.format("include::%s[]", path.getFileName()));
    }

    if (buffer.size() > 0) return Optional.of(String.join("\n", buffer));

    return Optional.empty();
  }
Example #5
0
  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();
  }