use of javax.swing.text.BadLocationException in project intellij-community by JetBrains.
the class HtmlPanel method getSelectedText.
@Override
public String getSelectedText() {
Document doc = getDocument();
int start = getSelectionStart();
int end = getSelectionEnd();
try {
Position p0 = doc.createPosition(start);
Position p1 = doc.createPosition(end);
StringWriter sw = new StringWriter(p1.getOffset() - p0.getOffset());
getEditorKit().write(sw, doc, p0.getOffset(), p1.getOffset() - p0.getOffset());
return StringUtil.removeHtmlTags(sw.toString());
} catch (BadLocationException | IOException ignored) {
}
return super.getSelectedText();
}
use of javax.swing.text.BadLocationException in project intellij-community by JetBrains.
the class JBLabel method setCopyable.
/**
* In 'copyable' mode JBLabel has the same appearance but user can select text with mouse and copy it to clipboard with standard shortcut.
* By default JBLabel is NOT copyable
* @return 'this' (the same instance)
*/
public JBLabel setCopyable(boolean copyable) {
if (copyable ^ myEditorPane != null) {
if (myEditorPane == null) {
final JLabel ellipsisLabel = new JBLabel("...");
myIconLabel = new JLabel(getIcon());
myEditorPane = new JEditorPane() {
@Override
public void paint(Graphics g) {
Dimension size = getSize();
boolean paintEllipsis = getPreferredSize().width > size.width && !myMultiline;
if (!paintEllipsis) {
super.paint(g);
} else {
Dimension ellipsisSize = ellipsisLabel.getPreferredSize();
int endOffset = size.width - ellipsisSize.width;
try {
// do not paint half of the letter
endOffset = modelToView(viewToModel(new Point(endOffset, 0)) - 1).x;
} catch (BadLocationException ignore) {
}
Shape oldClip = g.getClip();
g.clipRect(0, 0, endOffset, size.height);
super.paint(g);
g.setClip(oldClip);
g.translate(endOffset, 0);
ellipsisLabel.setSize(ellipsisSize);
ellipsisLabel.paint(g);
g.translate(-endOffset, 0);
}
}
};
myEditorPane.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
if (myEditorPane == null)
return;
int caretPosition = myEditorPane.getCaretPosition();
myEditorPane.setSelectionStart(caretPosition);
myEditorPane.setSelectionEnd(caretPosition);
}
});
myEditorPane.setContentType("text/html");
myEditorPane.setEditable(false);
myEditorPane.setBackground(UIUtil.TRANSPARENT_COLOR);
myEditorPane.setOpaque(false);
myEditorPane.setBorder(null);
UIUtil.putClientProperty(myEditorPane, UIUtil.NOT_IN_HIERARCHY_COMPONENTS, Collections.singleton(ellipsisLabel));
myEditorPane.setEditorKit(UIUtil.getHTMLEditorKit());
updateStyle(myEditorPane);
myEditorPane.setText(getText());
checkMultiline();
myEditorPane.setCaretPosition(0);
updateLayout();
} else {
removeAll();
myEditorPane = null;
myIconLabel = null;
}
}
return this;
}
use of javax.swing.text.BadLocationException in project intellij-community by JetBrains.
the class FileTextFieldImpl method processChosenFromCompletion.
private void processChosenFromCompletion(boolean nameOnly) {
final LookupFile file = getSelectedFileFromCompletionPopup();
if (file == null)
return;
if (nameOnly) {
try {
final Document doc = myPathTextField.getDocument();
int caretPos = myPathTextField.getCaretPosition();
if (myFinder.getSeparator().equals(doc.getText(caretPos, 1))) {
for (; caretPos < doc.getLength(); caretPos++) {
final String eachChar = doc.getText(caretPos, 1);
if (!myFinder.getSeparator().equals(eachChar))
break;
}
}
int start = caretPos > 0 ? caretPos - 1 : caretPos;
while (start >= 0) {
final String each = doc.getText(start, 1);
if (myFinder.getSeparator().equals(each)) {
start++;
break;
}
start--;
}
int end = start < caretPos ? caretPos : start;
while (end <= doc.getLength()) {
final String each = doc.getText(end, 1);
if (myFinder.getSeparator().equals(each)) {
break;
}
end++;
}
if (end > doc.getLength()) {
end = doc.getLength();
}
if (start > end || start < 0 || end > doc.getLength()) {
setTextToFile(file);
} else {
replacePathComponent(file, caretPos, start, end);
}
} catch (BadLocationException e) {
LOG.error(e);
}
} else {
setTextToFile(file);
}
}
use of javax.swing.text.BadLocationException in project zaproxy by zaproxy.
the class CustomScanDialog method getInjectionPointList.
private JList<Highlight> getInjectionPointList() {
if (injectionPointList == null) {
injectionPointList = new JList<>(injectionPointModel);
injectionPointList.setCellRenderer(new ListCellRenderer<Highlight>() {
@Override
public Component getListCellRendererComponent(JList<? extends Highlight> list, Highlight hlt, int index, boolean isSelected, boolean cellHasFocus) {
String str = "";
try {
str = getRequestField().getText(hlt.getStartOffset(), hlt.getEndOffset() - hlt.getStartOffset());
if (str.length() > 8) {
// just show first 8 chrs (arbitrary limit;)
str = str.substring(0, 8) + "..";
}
} catch (BadLocationException e) {
// Ignore
}
return new JLabel("[" + hlt.getStartOffset() + "," + hlt.getEndOffset() + "]: " + str);
}
});
}
return injectionPointList;
}
use of javax.swing.text.BadLocationException in project zaproxy by zaproxy.
the class HttpPanelTextArea method highlightEntryParser.
// Parse the TextArea data and search the HighlightEntry strings
// Highlight all found strings
private void highlightEntryParser(HighlightSearchEntry entry) {
String text;
int lastPos = 0;
text = this.getText();
Highlighter hilite = this.getHighlighter();
HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(entry.getColor());
while ((lastPos = text.indexOf(entry.getToken(), lastPos)) > -1) {
try {
hilite.addHighlight(lastPos, lastPos + entry.getToken().length(), painter);
lastPos += entry.getToken().length();
} catch (BadLocationException e) {
log.warn("Could not highlight entry", e);
}
}
}
Aggregations