List of usage examples for javax.swing.text BadLocationException printStackTrace
public void printStackTrace()
From source file:Main.java
/** * Prints a <code>Document</code> using a monospaced font, word wrapping on * the characters ' ', '\t', '\n', ',', '.', and ';'. This method is * expected to be called from Printable 'print(Graphics g)' functions. * * @param g The graphics context to write to. * @param doc The <code>javax.swing.text.Document</code> to print. * @param fontSize the point size to use for the monospaced font. * @param pageIndex The page number to print. * @param pageFormat The format to print the page with. * @param tabSize The number of spaces to expand tabs to. * * @see #printDocumentMonospaced//from ww w.j av a 2 s . c o m */ public static int printDocumentMonospacedWordWrap(Graphics g, Document doc, int fontSize, int pageIndex, PageFormat pageFormat, int tabSize) { g.setColor(Color.BLACK); g.setFont(new Font("Monospaced", Font.PLAIN, fontSize)); // Initialize our static variables (these are used by our tab expander below). tabSizeInSpaces = tabSize; fm = g.getFontMetrics(); // Create our tab expander. //RPrintTabExpander tabExpander = new RPrintTabExpander(); // Get width and height of characters in this monospaced font. int fontWidth = fm.charWidth('w'); // Any character will do here, since font is monospaced. int fontHeight = fm.getHeight(); int MAX_CHARS_PER_LINE = (int) pageFormat.getImageableWidth() / fontWidth; int MAX_LINES_PER_PAGE = (int) pageFormat.getImageableHeight() / fontHeight; final int STARTING_LINE_NUMBER = MAX_LINES_PER_PAGE * pageIndex; // The (x,y) coordinate to print at (in pixels, not characters). // Since y is the baseline of where we'll start printing (not the top-left // corner), we offset it by the font's ascent ( + 1 just for good measure). xOffset = (int) pageFormat.getImageableX(); int y = (int) pageFormat.getImageableY() + fm.getAscent() + 1; // A counter to keep track of the number of lines that WOULD HAVE been // printed if we were printing all lines. int numPrintedLines = 0; // Keep going while there are more lines in the document. currentDocLineNumber = 0; // The line number of the document we're currently on. rootElement = doc.getDefaultRootElement(); // To shorten accesses in our loop. numDocLines = rootElement.getElementCount(); // The number of lines in our document. while (currentDocLineNumber < numDocLines) { // Get the line we are going to print. String curLineString; Element currentLine = rootElement.getElement(currentDocLineNumber); int startOffs = currentLine.getStartOffset(); try { curLineString = doc.getText(startOffs, currentLine.getEndOffset() - startOffs); } catch (BadLocationException ble) { // Never happens ble.printStackTrace(); return Printable.NO_SUCH_PAGE; } // Remove newlines, because they end up as boxes if you don't; this is a monospaced font. curLineString = curLineString.replaceAll("\n", ""); // Replace tabs with how many spaces they should be. if (tabSizeInSpaces == 0) { curLineString = curLineString.replaceAll("\t", ""); } else { int tabIndex = curLineString.indexOf('\t'); while (tabIndex > -1) { int spacesNeeded = tabSizeInSpaces - (tabIndex % tabSizeInSpaces); String replacementString = ""; for (int i = 0; i < spacesNeeded; i++) replacementString += ' '; // Note that "\t" is actually a regex for this method. curLineString = curLineString.replaceFirst("\t", replacementString); tabIndex = curLineString.indexOf('\t'); } } // If this document line is too long to fit on one printed line on the page, // break it up into multpile lines. while (curLineString.length() > MAX_CHARS_PER_LINE) { int breakPoint = getLineBreakPoint(curLineString, MAX_CHARS_PER_LINE) + 1; numPrintedLines++; if (numPrintedLines > STARTING_LINE_NUMBER) { g.drawString(curLineString.substring(0, breakPoint), xOffset, y); y += fontHeight; if (numPrintedLines == STARTING_LINE_NUMBER + MAX_LINES_PER_PAGE) return Printable.PAGE_EXISTS; } curLineString = curLineString.substring(breakPoint, curLineString.length()); } currentDocLineNumber += 1; // We have printed one more line from the document. numPrintedLines++; if (numPrintedLines > STARTING_LINE_NUMBER) { g.drawString(curLineString, xOffset, y); y += fontHeight; if (numPrintedLines == STARTING_LINE_NUMBER + MAX_LINES_PER_PAGE) return Printable.PAGE_EXISTS; } } // Now, the whole document has been "printed." Decide if this page had any text on it or not. if (numPrintedLines > STARTING_LINE_NUMBER) return Printable.PAGE_EXISTS; return Printable.NO_SUCH_PAGE; }
From source file:com.intuit.tank.tools.debugger.PanelBuilder.java
/** * @param debuggerActions/* ww w . jav a 2 s. co m*/ * @return */ static Component createContentPanel(final AgentDebuggerFrame frame) { JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true); pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); final RSyntaxTextArea scriptEditorTA = new RSyntaxTextArea(); frame.setScriptEditorTA(scriptEditorTA); scriptEditorTA.setSelectionColor(scriptEditorTA.getCurrentLineHighlightColor()); scriptEditorTA.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE); scriptEditorTA.setHyperlinksEnabled(false); scriptEditorTA.setEditable(false); scriptEditorTA.setEnabled(false); scriptEditorTA.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { scriptEditorTA.grabFocus(); try { int offs = scriptEditorTA.viewToModel(e.getPoint()); if (offs > -1) { int line = scriptEditorTA.getLineOfOffset(offs); if (frame.getSteps().size() > line) { frame.fireStepChanged(line); if (e.getClickCount() == 2 && !e.isPopupTrigger()) { // show step xml try { DebugStep debugStep = frame.getSteps().get(line); String text = JaxbUtil.marshall(debugStep.getStepRun()); StepDialog dlg = new StepDialog(frame, text, SyntaxConstants.SYNTAX_STYLE_XML); dlg.setVisible(true); } catch (JAXBException e1) { frame.showError("Error showing step xml: " + e); } } } } } catch (BadLocationException ble) { ble.printStackTrace(); // Never happens } } }); RTextScrollPane scriptEditorScrollPane = new RTextScrollPane(scriptEditorTA); frame.setScriptEditorScrollPane(scriptEditorScrollPane); scriptEditorScrollPane.setIconRowHeaderEnabled(true); scriptEditorScrollPane.getGutter() .setBookmarkIcon(ActionProducer.getIcon("bullet_blue.png", IconSize.SMALL)); scriptEditorScrollPane.getGutter() .setCurrentLineIcon(ActionProducer.getIcon("current_line.png", IconSize.SMALL)); scriptEditorScrollPane.getGutter().setBookmarkingEnabled(true); pane.setLeftComponent(scriptEditorScrollPane); pane.setRightComponent(createRightPanel(frame)); pane.setDividerLocation(300); pane.setResizeWeight(0.4D); return pane; }
From source file:Main.java
public Main() { setDefaultCloseOperation(DISPOSE_ON_CLOSE); setSize(400, 300);/*from w w w. ja va2 s .c om*/ Exception ex = new Exception(); AttributeSet attr = null; StyledDocument doc = text.getStyledDocument(); for (StackTraceElement trace : ex.getStackTrace()) { try { doc.insertString(doc.getLength(), trace.toString() + '\n', attr); } catch (BadLocationException ex1) { ex1.printStackTrace(); } } getContentPane().add(new JScrollPane(text)); }
From source file:Main.java
@Override public void insertString(FilterBypass fb, int offset, String text, AttributeSet aset) { try {//from ww w.j av a2 s . com super.insertString(fb, offset, text.replaceAll("\\D++", ""), aset); } catch (BadLocationException ble) { ble.printStackTrace(); } }
From source file:Main.java
@Override public void replace(FilterBypass fb, int offset, int len, String text, AttributeSet aset) { try {//from ww w . j av a 2s . c o m super.replace(fb, offset, len, text.replaceAll("\\D++", ""), aset); } catch (BadLocationException ble) { ble.printStackTrace(); } }
From source file:Main.java
public Main() { editorPane.setDocument(doc);/*from ww w . ja va2 s .co m*/ editorPane.setEditorKit(styledEditorKit); JScrollPane scrollpane = new JScrollPane(editorPane); scrollpane.setPreferredSize(new Dimension(500, 400)); JPanel comboPanel = new JPanel(); comboPanel.add(fontBox); setLayout(new BorderLayout()); add(scrollpane, BorderLayout.CENTER); add(comboPanel, BorderLayout.SOUTH); Document doc = editorPane.getDocument(); for (int i = 0; i < 20; i++) { int offset = doc.getLength(); String str = "This is line number: " + i + "\n"; try { doc.insertString(offset, str, null); } catch (BadLocationException e) { e.printStackTrace(); } } fontBox.addActionListener(e -> { int size = (Integer) fontBox.getSelectedItem(); Action fontAction = new StyledEditorKit.FontSizeAction(String.valueOf(size), size); fontAction.actionPerformed(e); }); }
From source file:JTextPaneDemo.java
protected void insertText(String text, AttributeSet set) { try {//from ww w . j av a 2s .c om textPane.getDocument().insertString(textPane.getDocument().getLength(), text, set); } catch (BadLocationException e) { e.printStackTrace(); } }
From source file:Main.java
private void initComponents() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextPane textPane = new JTextPane(); ((AbstractDocument) textPane.getDocument()).addDocumentListener(new DocumentListener() { @Override//from w w w . j a v a 2 s .co m public void insertUpdate(final DocumentEvent de) { SwingUtilities.invokeLater(new Runnable() { public void run() { try { StyledDocument doc = (StyledDocument) de.getDocument(); int start = Utilities.getRowStart(textPane, Math.max(0, de.getOffset() - 1)); int end = Utilities.getWordStart(textPane, de.getOffset() + de.getLength()); String text = doc.getText(start, end - start); for (String emoticon : imageTokens) { int i = text.indexOf(emoticon); while (i >= 0) { final SimpleAttributeSet attrs = new SimpleAttributeSet( doc.getCharacterElement(start + i).getAttributes()); if (StyleConstants.getIcon(attrs) == null) { switch (emoticon) { case imageToken: StyleConstants.setIcon(attrs, anImage); break; } doc.remove(start + i, emoticon.length()); doc.insertString(start + i, emoticon, attrs); } i = text.indexOf(emoticon, i + emoticon.length()); } } } catch (BadLocationException ex) { ex.printStackTrace(); } } }); } @Override public void removeUpdate(DocumentEvent e) { } @Override public void changedUpdate(DocumentEvent e) { } }); JScrollPane scrollPane = new JScrollPane(textPane); scrollPane.setPreferredSize(new Dimension(300, 300)); frame.add(scrollPane); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public TestPane() { setLayout(new BorderLayout()); JPanel searchPane = new JPanel(); searchPane.add(new JLabel("Find: ")); searchPane.add(findText);//from ww w .j a v a 2 s. c om add(searchPane, BorderLayout.NORTH); add(new JScrollPane(ta)); try (BufferedReader reader = new BufferedReader(new FileReader(new File("c:/Java_Dev/run.bat")))) { ta.read(reader, "Text"); } catch (Exception e) { e.printStackTrace(); } ta.setCaretPosition(0); keyTimer = new Timer(250, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String find = findText.getText(); Document document = ta.getDocument(); try { for (int index = 0; index + find.length() < document.getLength(); index++) { String match = document.getText(index, find.length()); if (find.equals(match)) { DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter( Color.YELLOW); ta.getHighlighter().addHighlight(index, index + find.length(), highlightPainter); } } } catch (BadLocationException exp) { exp.printStackTrace(); } } }); keyTimer.setRepeats(false); findText.getDocument().addDocumentListener(new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { keyTimer.restart(); } @Override public void removeUpdate(DocumentEvent e) { keyTimer.restart(); } @Override public void changedUpdate(DocumentEvent e) { keyTimer.restart(); } }); }
From source file:TextAreaDemo.java
public void insertUpdate(DocumentEvent ev) { if (ev.getLength() != 1) { return;/*from www .j a va 2 s . co m*/ } int pos = ev.getOffset(); String content = null; try { content = textArea.getText(0, pos + 1); } catch (BadLocationException e) { e.printStackTrace(); } // Find where the word starts int w; for (w = pos; w >= 0; w--) { if (!Character.isLetter(content.charAt(w))) { break; } } if (pos - w < 2) { // Too few chars return; } String prefix = content.substring(w + 1).toLowerCase(); int n = Collections.binarySearch(words, prefix); if (n < 0 && -n <= words.size()) { String match = words.get(-n - 1); if (match.startsWith(prefix)) { // A completion is found String completion = match.substring(pos - w); // We cannot modify Document from within notification, // so we submit a task that does the change later SwingUtilities.invokeLater(new CompletionTask(completion, pos + 1)); } } else { // Nothing found mode = Mode.INSERT; } }