Java Code Examples for javax.swing.text.BadLocationException#printStackTrace()

The following examples show how to use javax.swing.text.BadLocationException#printStackTrace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CheckAttributedTree.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
    int start = info.start;
    int end = info.end;
    if (start == -1 && end == -1)
        return;
    if (start == -1)
        start = end;
    if (end == -1)
        end = start;
    try {
        h.addHighlight(info.start, info.end,
                new DefaultHighlighter.DefaultHighlightPainter(c));
        if (info.pos != -1) {
            Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
            h.addHighlight(info.pos, info.pos + 1,
                new DefaultHighlighter.DefaultHighlightPainter(c2));
        }
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: TreePosTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
    int start = info.start;
    int end = info.end;
    if (start == -1 && end == -1)
        return;
    if (start == -1)
        start = end;
    if (end == -1)
        end = start;
    try {
        h.addHighlight(info.start, info.end,
                new DefaultHighlighter.DefaultHighlightPainter(c));
        if (info.pos != -1) {
            Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
            h.addHighlight(info.pos, info.pos + 1,
                new DefaultHighlighter.DefaultHighlightPainter(c2));
        }
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
 
Example 3
Source File: TreePosTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
    int start = info.start;
    int end = info.end;
    if (start == -1 && end == -1)
        return;
    if (start == -1)
        start = end;
    if (end == -1)
        end = start;
    try {
        h.addHighlight(info.start, info.end,
                new DefaultHighlighter.DefaultHighlightPainter(c));
        if (info.pos != -1) {
            Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
            h.addHighlight(info.pos, info.pos + 1,
                new DefaultHighlighter.DefaultHighlightPainter(c2));
        }
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: CheckAttributedTree.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
    int start = info.start;
    int end = info.end;
    if (start == -1 && end == -1)
        return;
    if (start == -1)
        start = end;
    if (end == -1)
        end = start;
    try {
        h.addHighlight(info.start, info.end,
                new DefaultHighlighter.DefaultHighlightPainter(c));
        if (info.pos != -1) {
            Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
            h.addHighlight(info.pos, info.pos + 1,
                new DefaultHighlighter.DefaultHighlightPainter(c2));
        }
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
 
Example 5
Source File: TextualDocumentView.java    From gate-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void insertUpdate(final javax.swing.event.DocumentEvent e) {
  // propagate the edit to the document
  try {
    // deactivate our own listener so we don't get cycles
    document.removeDocumentListener(gateDocListener);
    document.edit(
            new Long(e.getOffset()),
            new Long(e.getOffset()),
            new DocumentContentImpl(e.getDocument().getText(e.getOffset(),
                    e.getLength())));
  } catch(BadLocationException ble) {
    ble.printStackTrace(Err.getPrintWriter());
  } catch(InvalidOffsetException ioe) {
    ioe.printStackTrace(Err.getPrintWriter());
  } finally {
    // reactivate our listener
    document.addDocumentListener(gateDocListener);
  }
  // //update the offsets in the list
  // Component listView = annotationListView.getGUI();
  // if(listView != null) listView.repaint();
}
 
Example 6
Source File: LogArea.java    From gate-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Try and recover from a BadLocationException thrown when inserting a string
 * into the log area. This method must only be called on the AWT event
 * handling thread.
 */
private void handleBadLocationException(BadLocationException e,
    String textToInsert, Style style) {
  originalErr.println("BadLocationException encountered when writing to "
      + "the log area: " + e);
  originalErr.println("trying to recover...");

  Document newDocument = new DefaultStyledDocument();
  try {
    StringBuilder sb = new StringBuilder();
    sb.append("An error occurred when trying to write a message to the log area.  The log\n");
    sb.append("has been cleared to try and recover from this problem.\n\n");
    sb.append(textToInsert);

    newDocument.insertString(0, sb.toString(), style);
  } catch(BadLocationException e2) {
    // oh dear, all bets are off now...
    e2.printStackTrace(originalErr);
    return;
  }
  // replace the log area's document with the new one
  setDocument(newDocument);
}
 
Example 7
Source File: SourceCodePanel.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
void recomputeSize() {
  if (!initted) return;
  if (view == null) return;
  width = ICON_SIZE + 2 * LINE_NO_SPACE;
  try {
    int numLines = 1 + source.getLineOfOffset(source.getDocument().getEndPosition().getOffset() - 1);
    String str = Integer.toString(numLines);
    if (getShowLineNumbers()) {
      // Compute width based on whether we are drawing line numbers
      width += GraphicsUtilities.getStringWidth(str, getFontMetrics(getFont())) + LINE_NO_SPACE;
    }
    // FIXME: add on width for all icons (breakpoint, current line,
    // current line in caller frame)
    Dimension d = new Dimension(width, numLines * getFontMetrics(getFont()).getHeight());
    setSize(d);
    setPreferredSize(d);
  } catch (BadLocationException e) {
    e.printStackTrace();
  }
}
 
Example 8
Source File: JEditTextArea.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void updateBracketHighlight(int newCaretPosition) {
	if (newCaretPosition == 0) {
		bracketPosition = bracketLine = -1;
		return;
	}

	try {
		int offset = TextUtilities.findMatchingBracket(document, newCaretPosition - 1);
		if (offset != -1) {
			bracketLine = getLineOfOffset(offset);
			bracketPosition = offset - getLineStartOffset(bracketLine);
			return;
		}
	} catch (BadLocationException bl) {
		bl.printStackTrace();
	}

	bracketLine = bracketPosition = -1;
}
 
Example 9
Source File: CheckAttributedTree.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
    int start = info.start;
    int end = info.end;
    if (start == -1 && end == -1)
        return;
    if (start == -1)
        start = end;
    if (end == -1)
        end = start;
    try {
        h.addHighlight(info.start, info.end,
                new DefaultHighlighter.DefaultHighlightPainter(c));
        if (info.pos != -1) {
            Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
            h.addHighlight(info.pos, info.pos + 1,
                new DefaultHighlighter.DefaultHighlightPainter(c2));
        }
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: SourceCodePanel.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/** Line number is one-based */
public void showLineNumber(int lineNo) {
  try {
    int offset = source.getLineStartOffset(lineNo - 1);
    Rectangle rect = source.modelToView(offset);
    if (rect == null) {
      return;
    }
    source.scrollRectToVisible(rect);
  } catch (BadLocationException e) {
    e.printStackTrace();
  }
}
 
Example 11
Source File: SourceCodePanel.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/** Line number is one-based */
public void showLineNumber(int lineNo) {
  try {
    int offset = source.getLineStartOffset(lineNo - 1);
    Rectangle rect = source.modelToView(offset);
    if (rect == null) {
      return;
    }
    source.scrollRectToVisible(rect);
  } catch (BadLocationException e) {
    e.printStackTrace();
  }
}
 
Example 12
Source File: CLIOutput.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public int postAttributed(String text, int position, AttributeSet attribs) {
    Document doc = getDocument();
    setCaretPosition(position);

    try {
        doc.insertString(position, text, attribs);
        repaint();
    } catch (BadLocationException e) {
        e.printStackTrace();
    }

    return getCaretPosition();
}
 
Example 13
Source File: Utility.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Scrolls the specified text component so the text between the starting and ending index are visible.
 */
public static void scrollToText(JTextComponent textComponent, int startingIndex, int endingIndex) {
    try {
        Rectangle startingRectangle = textComponent.modelToView(startingIndex);
        Rectangle endDingRectangle = textComponent.modelToView(endingIndex);

        Rectangle totalBounds = startingRectangle.union(endDingRectangle);

        textComponent.scrollRectToVisible(totalBounds);
        textComponent.repaint();
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
 
Example 14
Source File: JEditTextArea.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Sets the entire text of this text area.
 */
public void setText(String text) {
	try {
		document.beginCompoundEdit();
		document.remove(0, document.getLength());
		document.insertString(0, text, null);
	} catch (BadLocationException bl) {
		bl.printStackTrace();
	} finally {
		document.endCompoundEdit();
	}
}
 
Example 15
Source File: SourceCodePanel.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/** Line number is one-based */
public void showLineNumber(int lineNo) {
  try {
    int offset = source.getLineStartOffset(lineNo - 1);
    Rectangle rect = source.modelToView(offset);
    if (rect == null) {
      return;
    }
    source.scrollRectToVisible(rect);
  } catch (BadLocationException e) {
    e.printStackTrace();
  }
}
 
Example 16
Source File: ActionFactory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
        try {
            Caret caret = target.getCaret();
            BaseDocument doc = Utilities.getDocument(target);
            int caretLine = Utilities.getLineOffset(doc, caret.getDot());
            AnnotationDesc aDesc = doc.getAnnotations().activateNextAnnotation(caretLine);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }
}
 
Example 17
Source File: REditorPane.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private void setIndexOfHrefAndText(HTMLDocument hdoc, int pos, String text, String hRef) {
    this.hRefIndex = 0;
    this.textIndex = 0;
    Iterator iterator = hdoc.getIterator(HTML.Tag.A);
    while (iterator.isValid()) {
        if (pos >= iterator.getStartOffset() && pos < iterator.getEndOffset()) {
            return;
        } else {
            AttributeSet attributes = iterator.getAttributes();
            if (attributes != null && attributes.getAttribute(HTML.Attribute.HREF) != null) {
                try {
                    String t = hdoc.getText(iterator.getStartOffset(), iterator.getEndOffset() - iterator.getStartOffset())
                            .trim();
                    String h = attributes.getAttribute(HTML.Attribute.HREF).toString();
                    if (t.equals(text)) {
                        this.textIndex++;
                    }
                    if (h.equals(hRef)) {
                        this.hRefIndex++;
                    }
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
            }
        }
        iterator.next();
    }
}
 
Example 18
Source File: SwingEditFavoriteInteraction.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void setDisplayNameTextToCommandLineText() {
    try {
        String text = fullCommandLineTextField.getDocument().getText(0,
                fullCommandLineTextField.getDocument().getLength());
        displayNameTextField.setText(text);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
 
Example 19
Source File: Opt4JAbout.java    From opt4j with MIT License 4 votes vote down vote up
@Override
public void startup() {
	Container content = this;

	content.setLayout(new BorderLayout());

	JLabel logoLabel = new JLabel(Icons.getIcon("img/top_logo.png"));
	logoLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
	JPanel logo = new JPanel(new BorderLayout());
	logo.setBackground(Color.WHITE);
	logo.add(logoLabel);

	content.add(logo, BorderLayout.PAGE_START);

	JTextPane license = new JTextPane();
	license.setEditable(false);
	final JScrollPane licenseScroll = new JScrollPane(license);
	licenseScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
	StyledDocument doc = license.getStyledDocument();

	Style regular = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

	try {
		doc.insertString(doc.getLength(), LICENSE_TEXT, regular);
	} catch (BadLocationException e) {
		e.printStackTrace();
	}
	license.setPreferredSize(new Dimension(360, 100));
	content.add(licenseScroll, BorderLayout.CENTER);

	SwingUtilities.invokeLater(() -> licenseScroll.getVerticalScrollBar().setValue(0));
	JPanel footer = new JPanel(new BorderLayout());
	footer.setBackground(Color.WHITE);

	// Add Copyright & Credits
	String copyright = "<html>Build " + Opt4J.getDateISO() + " <br /> Version " + Opt4J.getVersion()
			+ "   \u00a9 Opt4J.org 2007</html>";
	JLabel copyrightLabel = new JLabel(copyright);
	copyrightLabel.setHorizontalAlignment(SwingConstants.CENTER);
	copyrightLabel.setVerticalAlignment(SwingConstants.BOTTOM);
	copyrightLabel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
	footer.add(copyrightLabel, BorderLayout.EAST);

	String credits = "<html><p>Credits:<br />";
	for (String author : AUTHORS) {
		credits += author + "<br/>";
	}
	credits += "</p></html>";

	JLabel creditsLabel = new JLabel(credits);
	creditsLabel.setHorizontalAlignment(SwingConstants.CENTER);
	creditsLabel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
	footer.add(creditsLabel, BorderLayout.WEST);

	content.add(footer, BorderLayout.PAGE_END);
}
 
Example 20
Source File: JEditTextArea.java    From rapidminer-studio with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * Returns the specified substring of the document.
 * 
 * @param start
 *            The start offset
 * @param len
 *            The length of the substring
 * @return The substring, or null if the offsets are invalid
 */
public final String getText(int start, int len) {
	try {
		return document.getText(start, len);
	} catch (BadLocationException bl) {
		bl.printStackTrace();
		return null;
	}
}