/**
   * Split a long line into several lines.
   *
   * @param origLine the original line
   * @return a string with new-line characters at the line-split locations.
   */
  protected String splitLine(String origLine) {
    StringBuilder newLines = new StringBuilder();

    // Determine the line's current indent. Any indent added below must be added to it.
    String currentIndent = "";
    for (int i = 0; i < origLine.length(); i++) {
      if (origLine.charAt(i) == '\u00a0') currentIndent += HARD_SPACE;
      else break;
    }

    // Add the words of the line to a line builder until adding a word would exceed the max allowed
    // length.
    StringBuilder line = new StringBuilder(currentIndent);
    String[] words = Util.splitWords(origLine, "[\u00a0 ]"); // either hard or soft space
    for (String word : words) {
      if (line.length() + 1 + word.length() + currentIndent.length() > this.maxLineLength) {
        if (newLines.length() == 0) currentIndent += INDENT; // indent continuation lines
        newLines.append(line.toString());
        line = new StringBuilder("\n").append(currentIndent);
      }

      // Add a space in front of the word if it's not the first word.
      if (!line.toString().endsWith(HARD_SPACE)) line.append(HARD_SPACE);
      line.append(word);
    }

    // Add the final words to the split lines.
    if (line.length() > 1) newLines.append(line.toString());

    return newLines.toString();
  }
Пример #2
0
  public void openLink(String link) {
    if (WWUtil.isEmpty(link)) return;

    try {
      try {
        // See if the link is a URL, and invoke the browser if it is
        URL url = new URL(link.replace(" ", "%20"));
        Desktop.getDesktop().browse(url.toURI());
        return;
      } catch (MalformedURLException ignored) { // just means that the link is not a URL
      }

      // It's not a URL, so see if it's a file and invoke the desktop to open it if it is.
      File file = new File(link);
      if (file.exists()) {
        Desktop.getDesktop().open(new File(link));
        return;
      }

      String message = "Cannot open resource. It's not a valid file or URL.";
      Util.getLogger().log(Level.SEVERE, message);
      this.showErrorDialog(null, "No Reconocido V\u00ednculo", message);
    } catch (UnsupportedOperationException e) {
      String message =
          "Unable to open resource.\n"
              + link
              + (e.getMessage() != null ? "\n" + e.getMessage() : "");
      Util.getLogger().log(Level.SEVERE, message, e);
      this.showErrorDialog(e, "Error Opening Resource", message);
    } catch (IOException e) {
      String message =
          "I/O error while opening resource.\n"
              + link
              + (e.getMessage() != null ? ".\n" + e.getMessage() : "");
      Util.getLogger().log(Level.SEVERE, message, e);
      this.showErrorDialog(e, "I/O Error", message);
    } catch (Exception e) {
      String message =
          "Error attempting to open resource.\n"
              + link
              + (e.getMessage() != null ? "\n" + e.getMessage() : "");
      Util.getLogger().log(Level.SEVERE, message);
      this.showMessageDialog(message, "Error Opening Resource", JOptionPane.ERROR_MESSAGE);
    }
  }
  /**
   * Determine the number of lines in the annotation text and the length of the longest line.
   *
   * @param annoText the annotation text.
   * @return the length of the longest line (width) and number of lines (height).
   */
  protected Dimension computeLengths(String annoText) {
    String[] lines = Util.splitLines(annoText);
    int lineLength = 0;
    for (String line : lines) {
      if (line.length() > lineLength) lineLength = line.length();
    }

    return new Dimension(
        lineLength + 5, lines.length + 1); // the 5 and 1 account for slight sizing discrepancies
  }
  /**
   * Split a collection of lines into a new collection whose lines are all less than the maximum
   * allowed line length.
   *
   * @param origText the original lines.
   * @return the new lines.
   */
  protected String splitLines(String origText) {
    StringBuilder newText = new StringBuilder();

    String[] lines = Util.splitLines(origText);
    for (String line : lines) {
      // Append the line to the output buffer if it's within size, other wise split it and append
      // the result.
      newText
          .append(line.length() <= this.maxLineLength ? line : this.splitLine(line))
          .append("\n");
    }

    return newText.toString();
  }
Пример #5
0
  public File chooseOutputFile(String defaultName, String suffixWithoutDot, String dialogTitle) {
    String defaultPath = this.getFileChooser().getCurrentDirectory().getPath();

    if (defaultName != null && defaultName.length() > 0)
      defaultPath += File.separatorChar + defaultName;

    if (suffixWithoutDot != null && suffixWithoutDot.length() > 0)
      defaultPath += "." + suffixWithoutDot;

    if (dialogTitle == null || dialogTitle.length() == 0) dialogTitle = "Choose Save Location";
    this.getFileChooser().setDialogTitle(dialogTitle);

    this.getFileChooser().setSelectedFile(new File(defaultPath));
    this.getFileChooser().setMultiSelectionEnabled(false);

    while (true) {
      int status = this.getFileChooser().showSaveDialog(this.getFrame());
      if (status != JFileChooser.APPROVE_OPTION) return null;

      File outFile = this.getFileChooser().getSelectedFile();
      if (outFile == null) {
        this.showMessageDialog("No location selected", "No Selection", JOptionPane.ERROR_MESSAGE);
        continue;
      }

      if (suffixWithoutDot != null && suffixWithoutDot.length() > 0)
        outFile = Util.ensureFileSuffix(outFile, suffixWithoutDot);

      if (outFile.exists()) {
        status = this.showConfirmFileOverwriteDialog(outFile);
        if (status == JOptionPane.NO_OPTION) continue;
        if (status == JOptionPane.CANCEL_OPTION) return null;
      }

      return outFile;
    }
  }
  public void selected(SelectEvent event) {
    try {
      if (event.isRollover()) {
        if (this.lastSelectedObject == event.getTopObject()) return; // same thing selected

        if (this.lastSelectedObject != null) {
          this.hideAnnotationPanel();
          this.lastSelectedObject = null;
        }

        if (event.getTopObject() != null && event.getTopObject() instanceof AVList) {
          String annoText =
              ((AVList) event.getTopObject()).getStringValue(Constants.INFO_PANEL_TEXT);
          if (!WWUtil.isEmpty(annoText)) {
            this.lastSelectedObject = (AVList) event.getTopObject();
            this.showAnnotationPanel(annoText);
          }
        }
      }
    } catch (Exception e) {
      // Wrap the handler in a try/catch to keep exceptions from bubbling up
      Util.getLogger().warning(e.getMessage() != null ? e.getMessage() : e.toString());
    }
  }
Пример #7
0
  public Frame getFrame() {
    AppFrame appFrame = (AppFrame) getRegisteredObject(Constants.APP_FRAME);
    if (appFrame != null) return appFrame.getFrame();

    return Util.findParentFrame((Container) getRegisteredObject(Constants.APP_PANEL));
  }