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"));
}
}
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);
}
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);
}
}
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);
}
}
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();
}
Aggregations