use of javax.swing.text.BadLocationException in project processing by processing.
the class CompletionPanel method insertSelection.
/**
* Created the popup list to be displayed
* @param position
* @param items
* @return
private JList<CompletionCandidate> createSuggestionList(final int position,
final DefaultListModel<CompletionCandidate> items) {
JList<CompletionCandidate> list = new JList<CompletionCandidate>(items);
//list.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
insertSelection(MOUSE_COMPLETION);
setInvisible();
}
}
});
list.setCellRenderer(new CustomListRenderer());
list.setFocusable(false);
return list;
}
*/
/*
// possibly defunct
private boolean updateList(final DefaultListModel<CompletionCandidate> items, String newSubword,
final Point location, int position) {
this.subWord = new String(newSubword);
if (subWord.indexOf('.') != -1)
this.subWord = subWord.substring(subWord.lastIndexOf('.') + 1);
insertionPosition = position;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
scrollPane.getViewport().removeAll();
completionList.setModel(items);
completionList.setSelectedIndex(0);
scrollPane.setViewportView(completionList);
popupMenu.setPopupSize(calcWidth(), calcHeight(items.getSize()));
//log("Suggestion updated" + System.nanoTime());
textarea.requestFocusInWindow();
popupMenu.show(textarea, location.x, textarea.getBaseline(0, 0)
+ location.y);
completionList.validate();
scrollPane.validate();
popupMenu.validate();
}
});
return true;
}
*/
/**
* Inserts the CompletionCandidate chosen from the suggestion list
* @param completionSource - whether being completed via keypress or mouse click.
* @return true - if code was successfully inserted at the caret position
*/
protected boolean insertSelection(int completionSource) {
if (completionList.getSelectedValue() != null) {
try {
// If user types 'abc.', subword becomes '.' and null is returned
String currentSubword = fetchCurrentSubword();
int currentSubwordLen = (currentSubword == null) ? 0 : currentSubword.length();
//logE(currentSubword + " <= subword,len => " + currentSubword.length());
String selectedSuggestion = completionList.getSelectedValue().getCompletionString();
if (currentSubword != null) {
selectedSuggestion = selectedSuggestion.substring(currentSubwordLen);
} else {
currentSubword = "";
}
String completionString = completionList.getSelectedValue().getCompletionString();
if (selectedSuggestion.endsWith(" )")) {
// selectedSuggestion = ")";
if (completionString.endsWith(" )")) {
completionString = completionString.substring(0, completionString.length() - 2) + ")";
}
}
boolean mouseClickOnOverloadedMethods = false;
if (completionSource == MOUSE_COMPLETION) {
// They have completion strings as 'foo('. See #2755
if (completionString.endsWith("(")) {
mouseClickOnOverloadedMethods = true;
}
}
Messages.loge(subWord + " <= subword, Inserting suggestion=> " + selectedSuggestion + " Current sub: " + currentSubword);
if (currentSubword.length() > 0) {
textarea.getDocument().remove(insertionPosition - currentSubwordLen, currentSubwordLen);
}
textarea.getDocument().insertString(insertionPosition - currentSubwordLen, completionString, null);
if (selectedSuggestion.endsWith(")") && !selectedSuggestion.endsWith("()")) {
// place the caret between '( and first ','
int x = selectedSuggestion.indexOf(',');
if (x == -1) {
// the case of single param methods, containing no ','
// just before ')'
textarea.setCaretPosition(textarea.getCaretPosition() - 1);
} else {
textarea.setCaretPosition(insertionPosition + x);
}
}
Messages.log("Suggestion inserted: " + System.currentTimeMillis());
if (completionList.getSelectedValue().getLabel().contains("...")) {
// log("No hide");
// Why not hide it? Coz this is the case of
// overloaded methods. See #2755
} else {
setInvisible();
}
if (mouseClickOnOverloadedMethods) {
// See #2755
((JavaTextArea) editor.getTextArea()).fetchPhrase();
}
return true;
} catch (BadLocationException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
setInvisible();
}
return false;
}
use of javax.swing.text.BadLocationException in project processing by processing.
the class LineID method startTracking.
/**
* Attach a {@link Document} to enable line number tracking when editing.
* The position to track is before the first non-whitespace character on the
* line. Edits happening before that position will cause the line number to
* update accordingly. Multiple {@link #startTracking} calls will replace
* the tracked document. Whoever wants a tracked line should track it and
* add itself as listener if necessary.
* ({@link LineHighlight}, {@link LineBreakpoint})
*
* @param doc the {@link Document} to use for line number tracking
*/
public synchronized void startTracking(Document doc) {
//System.out.println("tracking: " + this);
if (doc == null) {
// null arg
return;
}
if (doc == this.doc) {
// already tracking that doc
return;
}
try {
Element line = doc.getDefaultRootElement().getElement(lineIdx);
if (line == null) {
// line doesn't exist
return;
}
String lineText = doc.getText(line.getStartOffset(), line.getEndOffset() - line.getStartOffset());
// set tracking position at (=before) first non-white space character on line,
// or, if the line consists of entirely white spaces, just before the newline
// character
pos = doc.createPosition(line.getStartOffset() + nonWhiteSpaceOffset(lineText));
this.doc = doc;
doc.addDocumentListener(this);
} catch (BadLocationException ex) {
Messages.loge(null, ex);
pos = null;
this.doc = null;
}
}
use of javax.swing.text.BadLocationException in project jadx by skylot.
the class SearchBar method search.
private void search(int direction) {
String searchText = searchField.getText();
if (searchText == null || searchText.length() == 0 || rTextArea.getText() == null) {
return;
}
boolean forward = direction >= 0;
boolean matchCase = matchCaseCB.isSelected();
boolean regex = regexCB.isSelected();
boolean wholeWord = wholeWordCB.isSelected();
SearchContext context = new SearchContext();
context.setSearchFor(searchText);
context.setMatchCase(matchCase);
context.setRegularExpression(regex);
context.setSearchForward(forward);
context.setWholeWord(wholeWord);
context.setMarkAll(markAllCB.isSelected());
// TODO hack: move cursor before previous search for not jump to next occurrence
if (direction == 0 && !COLOR_BG_ERROR.equals(searchField.getBackground())) {
try {
int caretPos = rTextArea.getCaretPosition();
int lineNum = rTextArea.getLineOfOffset(caretPos) - 1;
if (lineNum > 1) {
rTextArea.setCaretPosition(rTextArea.getLineStartOffset(lineNum));
}
} catch (BadLocationException e) {
LOG.error("Caret move error", e);
}
}
SearchResult result = SearchEngine.find(rTextArea, context);
if (!result.wasFound()) {
int pos = SearchEngine.getNextMatchPos(searchText, rTextArea.getText(), forward, matchCase, wholeWord);
if (pos != -1) {
rTextArea.setCaretPosition(forward ? 0 : rTextArea.getDocument().getLength() - 1);
search(direction);
searchField.setBackground(COLOR_BG_WARN);
return;
}
searchField.setBackground(COLOR_BG_ERROR);
} else {
searchField.setBackground(COLOR_BG_NORMAL);
}
}
use of javax.swing.text.BadLocationException in project jadx by skylot.
the class CodeArea method centerCurrentLine.
public void centerCurrentLine() {
JViewport viewport = (JViewport) SwingUtilities.getAncestorOfClass(JViewport.class, this);
if (viewport == null) {
return;
}
try {
Rectangle r = modelToView(getCaretPosition());
if (r == null) {
return;
}
int extentHeight = viewport.getExtentSize().height;
Dimension viewSize = viewport.getViewSize();
if (viewSize == null) {
return;
}
int viewHeight = viewSize.height;
int y = Math.max(0, r.y - extentHeight / 2);
y = Math.min(y, viewHeight - extentHeight);
viewport.setViewPosition(new Point(0, y));
} catch (BadLocationException e) {
LOG.debug("Can't center current line", e);
}
}
use of javax.swing.text.BadLocationException in project jadx by skylot.
the class CodeArea method getJavaNodeAtOffset.
static JavaNode getJavaNodeAtOffset(JClass jCls, RSyntaxTextArea textArea, int offset) {
try {
int line = textArea.getLineOfOffset(offset);
int lineOffset = offset - textArea.getLineStartOffset(line);
return jCls.getCls().getJavaNodeAtPosition(line + 1, lineOffset + 1);
} catch (BadLocationException e) {
LOG.error("Can't get java node by offset", e);
}
return null;
}
Aggregations