Search in sources :

Example 81 with BadLocationException

use of javax.swing.text.BadLocationException in project jabref by JabRef.

the class PreviewPanelTransferHandler method createTransferable.

@Override
protected Transferable createTransferable(JComponent component) {
    if (component instanceof JEditorPane) {
        // this method should be called from the preview panel only
        // the default TransferHandler implementation is aware of HTML
        // and returns an appropriate Transferable
        // as textTransferHandler.createTransferable() is not available and
        // I don't know any other method, I do the HTML conversion by hand
        // First, get the HTML of the selected text
        JEditorPane editorPane = (JEditorPane) component;
        StringWriter stringWriter = new StringWriter();
        try {
            editorPane.getEditorKit().write(stringWriter, editorPane.getDocument(), editorPane.getSelectionStart(), editorPane.getSelectionEnd());
        } catch (BadLocationException | IOException e) {
            LOGGER.warn("Cannot write preview", e);
        }
        // Second, return the HTML (and text as fallback)
        return new HtmlTransferable(stringWriter.toString(), editorPane.getSelectedText());
    } else {
        // if not called from the preview panel, return an error string
        return new StringSelection(Localization.lang("Operation not supported"));
    }
}
Also used : StringWriter(java.io.StringWriter) JEditorPane(javax.swing.JEditorPane) IOException(java.io.IOException) BadLocationException(javax.swing.text.BadLocationException) StringSelection(java.awt.datatransfer.StringSelection)

Example 82 with BadLocationException

use of javax.swing.text.BadLocationException in project jabref by JabRef.

the class BasicAction method initRawPanel.

// Panel with text import functionality
private void initRawPanel() {
    rawPanel.setLayout(new BorderLayout());
    // Textarea
    textPane.setEditable(false);
    document = textPane.getStyledDocument();
    addStylesToDocument();
    try {
        document.insertString(0, "", document.getStyle("regular"));
    } catch (BadLocationException ex) {
        LOGGER.warn("Problem setting style", ex);
    }
    OverlayPanel testPanel = new OverlayPanel(textPane, Localization.lang("paste text here"));
    testPanel.setPreferredSize(new Dimension(450, 255));
    testPanel.setMaximumSize(new Dimension(450, Integer.MAX_VALUE));
    // Setup fields (required to be done before setting up popup menu)
    fieldList = new JList<>(getAllFields());
    fieldList.setCellRenderer(new SimpleCellRenderer(fieldList.getFont()));
    ListSelectionModel listSelectionModel = fieldList.getSelectionModel();
    listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    listSelectionModel.addListSelectionListener(new FieldListSelectionHandler());
    fieldList.addMouseListener(new FieldListMouseListener());
    // After the call to getAllFields
    initPopupMenuAndToolbar();
    //Add listener to components that can bring up popup menus.
    MouseListener popupListener = new PopupListener(inputMenu);
    textPane.addMouseListener(popupListener);
    testPanel.addMouseListener(popupListener);
    JPanel leftPanel = new JPanel(new BorderLayout());
    leftPanel.add(toolBar, BorderLayout.NORTH);
    leftPanel.add(testPanel, BorderLayout.CENTER);
    JPanel inputPanel = setUpFieldListPanel();
    // parse with FreeCite button
    parseWithFreeCiteButton.addActionListener(event -> {
        if (parseWithFreeCiteAndAddEntries()) {
            okPressed = false;
            dispose();
        }
    });
    rawPanel.add(leftPanel, BorderLayout.CENTER);
    rawPanel.add(inputPanel, BorderLayout.EAST);
    JLabel desc = new JLabel("<html><h3>" + Localization.lang("Plain text import") + "</h3><p>" + Localization.lang("This is a simple copy and paste dialog. First load or paste some text into " + "the text input area.<br>After that, you can mark text and assign it to a BibTeX field.") + "</p></html>");
    desc.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    rawPanel.add(desc, BorderLayout.SOUTH);
}
Also used : JPanel(javax.swing.JPanel) ListSelectionModel(javax.swing.ListSelectionModel) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) OverlayPanel(org.jabref.gui.util.component.OverlayPanel) MouseListener(java.awt.event.MouseListener) BorderLayout(java.awt.BorderLayout) BadLocationException(javax.swing.text.BadLocationException)

Example 83 with BadLocationException

use of javax.swing.text.BadLocationException in project pcgen by PCGen.

the class NotesView method handleBackspace.

//methods dealing with Key Events
private void handleBackspace() {
    // TODO: This sucks, clean it up
    Element elem;
    int pos = editor.getCaretPosition();
    StyledDocument htmlDoc = (ExtendedHTMLDocument) editor.getStyledDocument();
    try {
        if (pos > 0) {
            if ((editor.getSelectedText()) != null) {
                ExtendedHTMLEditorKit.delete(editor);
                return;
            }
            int sOffset = htmlDoc.getParagraphElement(pos).getStartOffset();
            if (sOffset == editor.getSelectionStart()) {
                if (ExtendedHTMLEditorKit.checkParentsTag(htmlDoc.getParagraphElement(editor.getCaretPosition()), HTML.Tag.LI)) {
                    elem = ExtendedHTMLEditorKit.getListItemParent(htmlDoc.getCharacterElement(editor.getCaretPosition()));
                    boolean content = false;
                    int so = elem.getStartOffset();
                    int eo = elem.getEndOffset();
                    if ((so + 1) < eo) {
                        char[] temp = editor.getText(so, eo - so).toCharArray();
                        for (char aTemp : temp) {
                            if (!Character.isWhitespace(aTemp)) {
                                content = true;
                            }
                        }
                    }
                    if (!content) {
                        elem.getParentElement();
                        ExtendedHTMLEditorKit.removeTag(editor, elem, true);
                        editor.setCaretPosition(sOffset - 1);
                        return;
                    }
                    editor.setCaretPosition(editor.getCaretPosition() - 1);
                    editor.moveCaretPosition(editor.getCaretPosition() - 2);
                    editor.replaceSelection("");
                    return;
                }
            }
            editor.replaceSelection("");
        }
    } catch (BadLocationException ble) {
        Logging.errorPrint(ble.getMessage(), ble);
    }
}
Also used : ExtendedHTMLDocument(gmgen.gui.ExtendedHTMLDocument) Element(javax.swing.text.Element) StyledDocument(javax.swing.text.StyledDocument) Point(java.awt.Point) BadLocationException(javax.swing.text.BadLocationException)

Example 84 with BadLocationException

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

the class MDocDate method startDateDialog.

//	getString
/**
	 *	Call Calendar Dialog
	 */
private void startDateDialog() {
    log.config("");
    //	Date Dialog
    String result = getText();
    Timestamp ts = null;
    try {
        ts = new Timestamp(m_format.parse(result).getTime());
    } catch (Exception pe) {
        ts = new Timestamp(System.currentTimeMillis());
    }
    ts = VDate.startCalendar(m_tc, ts, m_format, m_displayType, m_title);
    result = m_format.format(ts);
    //	move to field
    try {
        super.remove(0, getText().length());
        super.insertString(0, result, null);
    } catch (BadLocationException ble) {
        log.log(Level.SEVERE, "", ble);
    }
}
Also used : Timestamp(java.sql.Timestamp) BadLocationException(javax.swing.text.BadLocationException) BadLocationException(javax.swing.text.BadLocationException)

Example 85 with BadLocationException

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

the class FieldAutoCompleter method showPopup.

private void showPopup() {
    if (!isEnabled())
        return;
    log.finest("showPopup");
    popup.setVisible(false);
    if (textBox.isEnabled() && updateListData() && listBox.getModel().getSize() != 0) {
        if (!(textBox instanceof JTextField)) {
            textBox.getDocument().addDocumentListener(documentListener);
        }
        textBox.registerKeyboardAction(acceptAction, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_FOCUSED);
        int size = listBox.getModel().getSize();
        listBox.setVisibleRowCount(size < 10 ? size : 10);
        int x = 0;
        try {
            x = textBox.getUI().modelToView(textBox, 0).x;
        } catch (BadLocationException e) {
            // this should never happen!!!
            e.printStackTrace();
        }
        popup.setMinimumSize(new Dimension(textBox.getWidth(), 10));
        popup.show(textBox, x, textBox.getHeight());
    } else {
        popup.setVisible(false);
    }
    textBox.requestFocus();
}
Also used : Dimension(java.awt.Dimension) JTextField(javax.swing.JTextField) 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