Search in sources :

Example 1 with BadLocationException

use of javax.swing.text.BadLocationException in project Openfire by igniterealtime.

the class Launcher method startApplication.

private synchronized void startApplication() {
    if (openfired == null) {
        try {
            File windowsExe = new File(binDir, "openfired.exe");
            File unixExe = new File(binDir, "openfired");
            if (windowsExe.exists()) {
                openfired = Runtime.getRuntime().exec(new String[] { windowsExe.toString() });
            } else if (unixExe.exists()) {
                openfired = Runtime.getRuntime().exec(new String[] { unixExe.toString() });
            } else {
                throw new FileNotFoundException();
            }
        } catch (Exception e) {
            // Try one more time using the jar and hope java is on the path
            try {
                File libDir = new File(binDir.getParentFile(), "lib").getAbsoluteFile();
                openfired = Runtime.getRuntime().exec(new String[] { "java", "-jar", new File(libDir, "startup.jar").toString() });
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "Launcher could not start,\n" + appName, "File not found", JOptionPane.ERROR_MESSAGE);
            }
        }
        final SimpleAttributeSet styles = new SimpleAttributeSet();
        SwingWorker<String, Void> inputWorker = new SwingWorker<String, Void>() {

            @Override
            public String doInBackground() {
                if (openfired != null) {
                    // Get the input stream and read from it
                    try (InputStream in = openfired.getInputStream()) {
                        int c;
                        while ((c = in.read()) != -1) {
                            try {
                                StyleConstants.setFontFamily(styles, "courier new");
                                pane.getDocument().insertString(pane.getDocument().getLength(), "" + (char) c, styles);
                            } catch (BadLocationException e) {
                            // Ignore.
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return "ok";
            }
        };
        inputWorker.execute();
        SwingWorker<String, Void> errorWorker = new SwingWorker<String, Void>() {

            @Override
            public String doInBackground() {
                if (openfired != null) {
                    // Get the input stream and read from it
                    try (InputStream in = openfired.getErrorStream()) {
                        int c;
                        while ((c = in.read()) != -1) {
                            try {
                                StyleConstants.setForeground(styles, Color.red);
                                pane.getDocument().insertString(pane.getDocument().getLength(), "" + (char) c, styles);
                            } catch (BadLocationException e) {
                            // Ignore.
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return "ok";
            }
        };
        errorWorker.execute();
        if (freshStart) {
            try {
                Thread.sleep(1000);
                cardLayout.show(cardPanel, "running");
            } catch (Exception ex) {
            // Ignore.
            }
            freshStart = false;
        } else {
            // Clear Text
            pane.setText("");
            cardLayout.show(cardPanel, "running");
        }
    }
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) SwingWorker(javax.swing.SwingWorker) IOException(java.io.IOException) File(java.io.File) BadLocationException(javax.swing.text.BadLocationException) FileNotFoundException(java.io.FileNotFoundException) AWTException(java.awt.AWTException) IOException(java.io.IOException) BadLocationException(javax.swing.text.BadLocationException)

Example 2 with BadLocationException

use of javax.swing.text.BadLocationException in project languagetool by languagetool-org.

the class ResultAreaHelper method setHeader.

private void setHeader(String txt) {
    HTMLDocument d = (HTMLDocument) statusPane.getDocument();
    Element e = d.getElement(HEADER);
    try {
        d.setInnerHTML(e, "<p class=\"grayed\">" + txt + "</p>");
    } catch (BadLocationException ex) {
        Tools.showError(ex);
    } catch (IOException ex) {
        Tools.showError(ex);
    }
}
Also used : HTMLDocument(javax.swing.text.html.HTMLDocument) Element(javax.swing.text.Element) IOException(java.io.IOException) BadLocationException(javax.swing.text.BadLocationException)

Example 3 with BadLocationException

use of javax.swing.text.BadLocationException in project languagetool by languagetool-org.

the class ResultAreaHelper method setMain.

private void setMain(String html) {
    HTMLDocument d = (HTMLDocument) statusPane.getDocument();
    Element e = d.getElement(MAIN);
    try {
        d.setInnerHTML(e, html);
    } catch (BadLocationException ex) {
        Tools.showError(ex);
    } catch (IOException ex) {
        Tools.showError(ex);
    }
}
Also used : HTMLDocument(javax.swing.text.html.HTMLDocument) Element(javax.swing.text.Element) IOException(java.io.IOException) BadLocationException(javax.swing.text.BadLocationException)

Example 4 with BadLocationException

use of javax.swing.text.BadLocationException in project processing by processing.

the class FindReplace method find.

// look for the next instance of the find string to be found
// once found, select it (and go to that line)
private boolean find(boolean wrap, boolean backwards) {
    String searchTerm = findField.getText();
    // this will catch "find next" being called when no search yet
    if (searchTerm.length() != 0) {
        String text = editor.getText();
        // Started work on find/replace across tabs. These two variables store
        // the original tab and selection position so that it knew when to stop
        // rotating through.
        Sketch sketch = editor.getSketch();
        int tabIndex = sketch.getCurrentCodeIndex();
        if (ignoreCase) {
            searchTerm = searchTerm.toLowerCase();
            text = text.toLowerCase();
        }
        int nextIndex;
        if (!backwards) {
            //int selectionStart = editor.textarea.getSelectionStart();
            int selectionEnd = editor.getSelectionStop();
            nextIndex = text.indexOf(searchTerm, selectionEnd);
            if (nextIndex == -1 && wrap && !allTabs) {
                // if wrapping, a second chance is ok, start from beginning
                nextIndex = text.indexOf(searchTerm, 0);
            } else if (nextIndex == -1 && allTabs) {
                // For searching in all tabs, wrapping always happens.
                int tempIndex = tabIndex;
                // Look for searchterm in all tabs.
                while (tabIndex <= sketch.getCodeCount() - 1) {
                    if (tabIndex == sketch.getCodeCount() - 1) {
                        // System.out.println("wrapping.");
                        tabIndex = -1;
                    } else if (tabIndex == sketch.getCodeCount() - 1) {
                        break;
                    }
                    try {
                        Document doc = sketch.getCode(tabIndex + 1).getDocument();
                        if (doc != null) {
                            // this thing has the latest changes
                            text = doc.getText(0, doc.getLength());
                        } else {
                            // not this thing.
                            text = sketch.getCode(tabIndex + 1).getProgram();
                        }
                    } catch (BadLocationException e) {
                        e.printStackTrace();
                    }
                    tabIndex++;
                    if (ignoreCase) {
                        text = text.toLowerCase();
                    }
                    nextIndex = text.indexOf(searchTerm, 0);
                    if (nextIndex != -1 || tabIndex == tempIndex) {
                        break;
                    }
                }
                // No tab switching should happen, restore tabIndex
                if (nextIndex == -1) {
                    tabIndex = tempIndex;
                }
            }
        } else {
            //int selectionStart = editor.textarea.getSelectionStart();
            int selectionStart = editor.getSelectionStart() - 1;
            if (selectionStart >= 0) {
                nextIndex = text.lastIndexOf(searchTerm, selectionStart);
            } else {
                nextIndex = -1;
            }
            if (wrap && !allTabs && nextIndex == -1) {
                // if wrapping, a second chance is ok, start from the end
                nextIndex = text.lastIndexOf(searchTerm);
            } else if (nextIndex == -1 && allTabs) {
                int tempIndex = tabIndex;
                // Look for search term in previous tabs.
                while (tabIndex >= 0) {
                    if (tabIndex == 0) {
                        //System.out.println("wrapping.");
                        tabIndex = sketch.getCodeCount();
                    } else if (tabIndex == 0) {
                        break;
                    }
                    try {
                        Document doc = sketch.getCode(tabIndex - 1).getDocument();
                        if (doc != null) {
                            // this thing has the latest changes
                            text = doc.getText(0, doc.getLength());
                        } else {
                            // not this thing.
                            text = sketch.getCode(tabIndex - 1).getProgram();
                        }
                    } catch (BadLocationException e) {
                        e.printStackTrace();
                    }
                    tabIndex--;
                    if (ignoreCase) {
                        text = text.toLowerCase();
                    }
                    nextIndex = text.lastIndexOf(searchTerm);
                    if (nextIndex != -1 || tabIndex == tempIndex) {
                        break;
                    }
                }
                // No tab switching should happen, restore tabIndex
                if (nextIndex == -1) {
                    tabIndex = tempIndex;
                }
            }
        }
        if (nextIndex != -1) {
            if (allTabs) {
                sketch.setCurrentCode(tabIndex);
            }
            editor.setSelection(nextIndex, nextIndex + searchTerm.length());
        } else {
        //Toolkit.getDefaultToolkit().beep();
        }
        if (nextIndex != -1) {
            setFound(true);
            return true;
        }
    }
    setFound(false);
    return false;
}
Also used : Sketch(processing.app.Sketch) Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Example 5 with BadLocationException

use of javax.swing.text.BadLocationException in project processing by processing.

the class CompositionTextManager method insertFullWidthSpace.

/**
   * Insert full width space
   */
public void insertFullWidthSpace() {
    initialCaretPosition = textArea.getCaretPosition();
    int layoutCaretPosition = initialCaretPosition;
    try {
        textArea.getDocument().insertString(layoutCaretPosition, " ", null);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
Also used : Point(java.awt.Point) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

BadLocationException (javax.swing.text.BadLocationException)88 Document (javax.swing.text.Document)24 Element (javax.swing.text.Element)13 Point (java.awt.Point)10 IOException (java.io.IOException)9 DefaultHighlighter (javax.swing.text.DefaultHighlighter)8 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)7 Highlighter (javax.swing.text.Highlighter)7 ArrayList (java.util.ArrayList)6 DocumentEvent (javax.swing.event.DocumentEvent)6 StyledDocument (javax.swing.text.StyledDocument)6 HighlightPainter (javax.swing.text.Highlighter.HighlightPainter)5 Dimension (java.awt.Dimension)4 Rectangle (java.awt.Rectangle)4 ActionEvent (java.awt.event.ActionEvent)4 ActionListener (java.awt.event.ActionListener)4 File (java.io.File)4 Date (java.util.Date)4 Style (javax.swing.text.Style)4 HTMLDocument (javax.swing.text.html.HTMLDocument)4