Esempio n. 1
0
 private void insertCover() {
   ImageResource image = tableView.getSelectionModel().getSelectedItem();
   book.setCoverImage(image);
   new CoverpageBookProcessor().processBook(book);
   Resource coverPage = book.getGuide().getCoverPage();
   book.getSpine().addResource(coverPage, 0);
   bookBrowserManager.refreshBookBrowser();
   bookBrowserManager.selectTextItem(coverPage);
   stage.close();
 }
Esempio n. 2
0
 private void insertClip(Clip clip) {
   CodeEditor editor = currentEditor.getValue();
   EditorRange selectedRange = editor.getSelection();
   String insertedClip = selectedRange.getSelection().replaceAll("^(.*)$", clip.getContent());
   editor.replaceSelection(insertedClip);
   book.setBookIsChanged(true);
 }
Esempio n. 3
0
 private void refresh() {
   List<ImageResource> imageResources = new ArrayList<>();
   List<Resource> resources =
       book.getResources()
           .getResourcesByMediaTypes(
               new MediaType[] {MediaType.GIF, MediaType.PNG, MediaType.SVG, MediaType.JPG});
   for (Resource resource : resources) {
     imageResources.add((ImageResource) resource);
   }
   tableView.setItems(FXCollections.observableList(imageResources));
   tableView.getSelectionModel().select(0);
 }
Esempio n. 4
0
 private void beautifyOrRepairHTML(String mode) {
   logger.info("beautifying html");
   try {
     CodeEditor editor = currentEditor.getValue();
     EditorPosition currentCursorPosition = editor.getEditorCursorPosition();
     String code = editor.getCode();
     if (currentEditorIsXHTML.get()) {
       try {
         Resource resource = currentXHTMLResource.get();
         resource.setData(code.getBytes("UTF-8"));
         switch (mode) {
           case "format":
             code = formatAsXHTML(code);
             break;
           case "repair":
             code = repairXHTML(code);
             break;
         }
         resource.setData(code.getBytes("UTF-8"));
       } catch (UnsupportedEncodingException e) {
         // never happens
       }
     }
     editor.setCode(code);
     editor.setEditorCursorPosition(currentCursorPosition);
     editor.scrollTo(currentCursorPosition);
     book.setBookIsChanged(true);
   } catch (IOException | JDOMException e) {
     logger.error("", e);
     Dialogs.create()
         .owner(tabPane)
         .title("Formatierung nicht möglich")
         .message(
             "Kann Datei nicht formatieren. Bitte die Fehlermeldung an den Hersteller weitergeben.")
         .showException(e);
   }
 }
Esempio n. 5
0
  public boolean splitXHTMLFile() {
    boolean result = false;
    if (currentEditor.getValue().getMediaType().equals(MediaType.XHTML)) {
      XHTMLCodeEditor xhtmlCodeEditor = (XHTMLCodeEditor) currentEditor.getValue();
      EditorPosition pos = xhtmlCodeEditor.getEditorCursorPosition();
      XMLTagPair pair =
          xhtmlCodeEditor.findSurroundingTags(
              token -> {
                String type = token.getType();
                if ("tag".equals(type)) {
                  String content = token.getContent();
                  if ("head".equals(content) || "body".equals(content) || "html".equals(content)) {
                    return true;
                  }
                }
                return false;
              });
      if (pair == null
          || "head".equals(pair.getTagName())
          || "html".equals(pair.getTagName())
          || StringUtils.isEmpty(pair.getTagName())) {
        Dialogs.create()
            .owner(tabPane)
            .title("Teilung nicht möglich")
            .message(
                "Kann Datei nicht an dieser Position teilen. Eine Teilung ist nur innerhalb des XHTML-Bodys möglich.")
            .showWarning();
        return false;
      }
      logger.debug("umgebendes pair " + pair);
      // wir sind innerhalb des Body
      int index = xhtmlCodeEditor.getIndexFromPosition(pos);
      try {
        String originalCode = xhtmlCodeEditor.getCode();
        org.jdom2.Document originalDocument = XHTMLUtils.parseXHTMLDocument(originalCode);
        List<Content> originalHeadContent = getOriginalHeadContent(originalDocument);

        byte[] frontPart = originalCode.substring(0, index).getBytes("UTF-8");
        Resource oldResource = currentXHTMLResource.getValue();
        oldResource.setData(frontPart);
        HtmlCleanerBookProcessor processor = new HtmlCleanerBookProcessor();
        processor.processResource(oldResource);
        xhtmlCodeEditor.setCode(new String(oldResource.getData(), "UTF-8"));

        byte[] backPart =
            originalCode.substring(index, originalCode.length() - 1).getBytes("UTF-8");
        String fileName = book.getNextStandardFileName(MediaType.XHTML);
        Resource resource = MediaType.XHTML.getResourceFactory().createResource("Text/" + fileName);
        byte[] backPartXHTML = XHTMLUtils.repairWithHead(backPart, originalHeadContent);
        resource.setData(backPartXHTML);

        int spineIndex = book.getSpine().getResourceIndex(oldResource);
        book.addSpineResource(resource, spineIndex + 1);
        openFileInEditor(resource, MediaType.XHTML);

        bookBrowserManager.refreshBookBrowser();
        needsRefresh.setValue(true);
        needsRefresh.setValue(false);
      } catch (IOException | JDOMException | ResourceDataException e) {
        logger.error("", e);
        Dialogs.create()
            .owner(tabPane)
            .title("Teilung nicht möglich")
            .message("Kann Datei nicht teilen. Bitte Fehlermeldung an den Hersteller übermitteln.")
            .showException(e);
      }

      result = true;
    }
    return result;
  }
Esempio n. 6
0
  public void openFileInEditor(Resource resource, MediaType mediaType)
      throws IllegalArgumentException {
    if (!isTabAlreadyOpen(resource)) {
      Tab tab = new Tab();
      tab.setClosable(true);
      if (resource == null) {
        Dialogs.create()
            .owner(tabPane)
            .title("Datei nicht vorhanden")
            .message(
                "Die angeforderte Datei ist nicht vorhanden und kann deshalb nicht geöffnet werden.")
            .showError();
        return;
      }
      tab.setText(resource.getFileName());
      resource
          .hrefProperty()
          .addListener(
              (observable, oldValue, newValue) -> {
                tab.setText(resource.getFileName());
              });

      String content = "";
      try {
        content = new String(resource.getData(), resource.getInputEncoding());
      } catch (IOException e) {
        logger.error("", e);
      }

      CodeEditor editor;
      if (mediaType.equals(MediaType.CSS)) {
        editor = new CssCodeEditor();
        editor.setContextMenu(contextMenuCSS);
      } else if (mediaType.equals(MediaType.XHTML)) {
        editor = new XHTMLCodeEditor();
        editor.setContextMenu(contextMenuXHTML);
      } else if (mediaType.equals(MediaType.XML)) {
        editor = new XMLCodeEditor();
        editor.setContextMenu(contextMenuXML);
      } else {
        throw new IllegalArgumentException("no editor for mediatype " + mediaType.getName());
      }

      tab.setContent((Node) editor);
      tab.setUserData(resource);
      tabPane.getTabs().add(tab);
      tabPane.getSelectionModel().select(tab);

      final String code = content;
      editor
          .stateProperty()
          .addListener(
              new ChangeListener<Worker.State>() {
                @Override
                public void changed(
                    ObservableValue<? extends Worker.State> observable,
                    Worker.State oldValue,
                    Worker.State newValue) {
                  if (newValue.equals(Worker.State.SUCCEEDED)) {
                    openingEditorTab = true;
                    editor.setCode(code);
                    editor.setCodeEditorSize(
                        ((AnchorPane) editor).getWidth() - 20,
                        ((AnchorPane) editor).getHeight() - 20);
                    ((AnchorPane) editor)
                        .widthProperty()
                        .addListener(
                            new ChangeListener<Number>() {
                              @Override
                              public void changed(
                                  ObservableValue<? extends Number> observable,
                                  Number oldValue,
                                  Number newValue) {
                                editor.setCodeEditorSize(
                                    newValue.doubleValue() - 20,
                                    ((AnchorPane) editor).getHeight() - 20);
                              }
                            });
                    ((AnchorPane) editor)
                        .heightProperty()
                        .addListener(
                            new ChangeListener<Number>() {
                              @Override
                              public void changed(
                                  ObservableValue<? extends Number> observable,
                                  Number oldValue,
                                  Number newValue) {
                                editor.setCodeEditorSize(
                                    ((AnchorPane) editor).getWidth() - 20,
                                    newValue.doubleValue() - 20);
                              }
                            });
                    editor.setCodeEditorSize(
                        ((AnchorPane) editor).getWidth() - 20,
                        ((AnchorPane) editor).getHeight() - 20);
                    ((AnchorPane) editor)
                        .widthProperty()
                        .addListener(
                            new ChangeListener<Number>() {
                              @Override
                              public void changed(
                                  ObservableValue<? extends Number> observable,
                                  Number oldValue,
                                  Number newValue) {
                                editor.setCodeEditorSize(
                                    newValue.doubleValue() - 20,
                                    ((AnchorPane) editor).getHeight() - 20);
                              }
                            });
                    ((AnchorPane) editor)
                        .heightProperty()
                        .addListener(
                            new ChangeListener<Number>() {
                              @Override
                              public void changed(
                                  ObservableValue<? extends Number> observable,
                                  Number oldValue,
                                  Number newValue) {
                                editor.setCodeEditorSize(
                                    ((AnchorPane) editor).getWidth() - 20,
                                    newValue.doubleValue() - 20);
                              }
                            });
                    openingEditorTab = false;
                  }
                }
              });

      editor
          .cursorPositionProperty()
          .addListener(
              (observable, oldValue, newValue) -> {
                cursorPosLabelProperty.set(newValue.getLine() + ":" + newValue.getColumn());
              });

      editor
          .codeProperty()
          .addListener(
              (observable1, oldValue, newValue) -> {
                if (openingEditorTab) {
                  return;
                }
                if (currentEditor.getValue().getMediaType().equals(MediaType.XHTML)) {
                  try {
                    currentXHTMLResource.get().setData(newValue.getBytes("UTF-8"));
                  } catch (UnsupportedEncodingException e) {
                    // never happens
                  }
                } else if (currentEditor.getValue().getMediaType().equals(MediaType.CSS)) {
                  try {
                    currentCssResource.get().setData(newValue.getBytes("UTF-8"));
                  } catch (UnsupportedEncodingException e) {
                    // never happens
                  }
                } else if (currentEditor.getValue().getMediaType().equals(MediaType.XML)) {
                  try {
                    currentXMLResource.get().setData(newValue.getBytes("UTF-8"));
                    if (((XMLResource) resource).isValidXML()
                        && MediaType.OPF.equals(resource.getMediaType())) {
                      PackageDocumentReader.read(resource, book);
                    }
                  } catch (JDOMException | IOException e) {
                    logger.error("", e);
                  }
                }

                if (scheduledService.getState().equals(Worker.State.READY)) {
                  scheduledService.start();
                } else {
                  scheduledService.restart();
                }
                book.setBookIsChanged(true);
              });
    }
  }
Esempio n. 7
0
  public void init() {
    currentEditor.addListener(
        (observable, oldValue, newValue) -> {
          canUndo.unbind();
          canRedo.unbind();
          if (newValue != null) {
            currentEditorIsXHTML.setValue(
                currentEditor.getValue().getMediaType().equals(MediaType.XHTML));
            canUndo.bind(currentEditor.getValue().canUndoProperty());
            canRedo.bind(currentEditor.getValue().canRedoProperty());
          } else {
            currentEditorIsXHTML.setValue(false);
            canUndo.setValue(false);
            canRedo.setValue(false);
          }
        });

    MenuItem separatorItem = new SeparatorMenuItem();
    // Html menu
    contextMenuXHTML = new ContextMenu();
    contextMenuXHTML.setAutoFix(true);
    contextMenuXHTML.setAutoHide(true);

    Menu clipsItem = new Menu("Clips");
    clipManager
        .getClipsRoot()
        .addEventHandler(
            TreeItem.<Clip>childrenModificationEvent(),
            event -> {
              clipsItem.getItems().clear();
              writeClipMenuItemChildren(clipManager.getClipsRoot(), clipsItem);
            });
    contextMenuXHTML.getItems().add(clipsItem);
    contextMenuXHTML.getItems().add(separatorItem);

    MenuItem itemRepairHTML = new MenuItem("HTML reparieren");
    itemRepairHTML.setOnAction(
        e -> {
          beautifyOrRepairHTML("repair");
        });
    contextMenuXHTML.getItems().add(itemRepairHTML);

    MenuItem itemBeautifyHTML = new MenuItem("HTML formatieren");
    itemBeautifyHTML.setOnAction(
        e -> {
          beautifyOrRepairHTML("format");
        });
    contextMenuXHTML.getItems().add(itemBeautifyHTML);

    contextMenuXHTML.getItems().add(separatorItem);

    MenuItem openInExternalBrowserItem = new MenuItem("In externem Browser öffnen");
    openInExternalBrowserItem.setOnAction(
        e -> {
          openInExternalBrowser(currentEditor);
        });
    contextMenuXHTML.getItems().add(openInExternalBrowserItem);

    // XML menu
    contextMenuXML = new ContextMenu();
    contextMenuXML.setAutoFix(true);
    contextMenuXML.setAutoHide(true);

    MenuItem generateUuidMenuItem = new MenuItem("Neue UUID generieren");
    generateUuidMenuItem.setOnAction(
        e -> {
          book.getMetadata().generateNewUuid();
          bookBrowserManager.refreshOpf();
        });
    contextMenuXML.getItems().add(generateUuidMenuItem);
    currentXMLResource.addListener(
        new ChangeListener<Resource>() {
          @Override
          public void changed(
              ObservableValue<? extends Resource> observable,
              Resource oldValue,
              Resource newValue) {
            if (newValue != null
                && currentXMLResource.get().mediaTypeProperty().getValue().equals(MediaType.OPF)) {
              generateUuidMenuItem.visibleProperty().setValue(true);
            } else {
              generateUuidMenuItem.visibleProperty().setValue(false);
            }
          }
        });

    MenuItem separatorItem2 = new SeparatorMenuItem();
    contextMenuXML.getItems().add(separatorItem2);
    separatorItem2.visibleProperty().bind(generateUuidMenuItem.visibleProperty());

    MenuItem itemRepairXML = new MenuItem("XML reparieren");
    itemRepairXML.setOnAction(
        e -> {
          beautifyOrRepairXML("repair");
        });
    contextMenuXML.getItems().add(itemRepairXML);

    MenuItem itemBeautifyXML = new MenuItem("XML formatieren");
    itemBeautifyXML.setOnAction(
        e -> {
          beautifyOrRepairXML("format");
        });
    contextMenuXML.getItems().add(itemBeautifyXML);

    // css menu
    contextMenuCSS = new ContextMenu();
    contextMenuCSS.setAutoFix(true);
    contextMenuCSS.setAutoHide(true);
    MenuItem formatCSSOneLineItem = new MenuItem("Styles in je einer Zeile formatieren");
    formatCSSOneLineItem.setOnAction(e -> beautifyCSS("one_line"));
    contextMenuCSS.getItems().add(formatCSSOneLineItem);

    MenuItem formatCSSMultipleLinesItem = new MenuItem("Styles in mehreren Zeilen formatieren");
    formatCSSMultipleLinesItem.setOnAction(e -> beautifyCSS("multiple_lines"));
    contextMenuCSS.getItems().add(formatCSSMultipleLinesItem);
  }