use of javax.swing.text.BadLocationException in project mc-dev by Bukkit.
the class ServerGUI method a.
public void a(JTextArea jtextarea, JScrollPane jscrollpane, String s) {
if (!SwingUtilities.isEventDispatchThread()) {
SwingUtilities.invokeLater(new ServerGuiInvokeRunnable(this, jtextarea, jscrollpane, s));
} else {
Document document = jtextarea.getDocument();
JScrollBar jscrollbar = jscrollpane.getVerticalScrollBar();
boolean flag = false;
if (jscrollpane.getViewport().getView() == jtextarea) {
flag = (double) jscrollbar.getValue() + jscrollbar.getSize().getHeight() + (double) (a.getSize() * 4) > (double) jscrollbar.getMaximum();
}
try {
document.insertString(document.getLength(), s, (AttributeSet) null);
} catch (BadLocationException badlocationexception) {
;
}
if (flag) {
jscrollbar.setValue(Integer.MAX_VALUE);
}
}
}
use of javax.swing.text.BadLocationException in project enclojure by EricThorsen.
the class ClojureCodeCompletionItem method doSubstitute.
private void doSubstitute(final JTextComponent component, final String toAdd, final int backOffset) {
final StyledDocument doc = (StyledDocument) component.getDocument();
class AtomicChange implements Runnable {
public void run() {
int caretOffset = component.getCaretPosition();
String cname = component.getClass().getName();
Boolean _isReplPanel = false;
if (cname.equals("javax.swing.JEditorPane"))
_isReplPanel = true;
String value = getText();
String javaList = "";
if (toAdd != null) {
value += toAdd;
}
try {
String c = component.getText(caretOffset - 1, 1);
if (//java package containing the Class at the end
!_isClojure && _isNamespaceOrPkg) {
value = setValueForClass(value, component);
javaList = getJavaImportListStr(value);
if ((javaList.contains("(")) && (!_isReplPanel)) {
int messageret = (JOptionPane.showConfirmDialog(component, "Do you want to add the import " + value + " to your ns imports?", "add import ?", 0));
if (messageret == 0) {
PersistentArrayMap entry = addImportList(component, javaList);
//component.setCaretPosition(caretOffset + javaList.length());
value = getClassPart(value);
if (entry != null) {
String origNS = (String) entry.get(Keyword.intern(Symbol.create("orignodestr")));
String newNS = (String) entry.get(Keyword.intern(Symbol.create("newnodestr")));
int insertOffset = newNS.length() - origNS.length();
_carretOffset = _carretOffset + insertOffset;
_dotOffset = _dotOffset + insertOffset;
}
}
}
}
if (!c.equals("/") && (_layout != backSlashAfterParen) && (_layout != backSlashNoParen)) {
switch(_layout) {
case LowercaseAfterParen:
doc.remove(_dotOffset, _carretOffset - _dotOffset);
break;
case UppercaseAfterParen:
doc.remove(_dotOffset, _carretOffset - _dotOffset);
break;
case UppercaseAfterParenWithDot:
if (//java method
_isMethodOrFunction && (!_isClojure)) {
if (_isConstructor) {
value = "";
} else if (Character.isLetter(value.charAt(0))) {
{
doc.remove(_dotOffset, _carretOffset - _dotOffset);
value = "." + value;
}
}
} else
doc.remove(_dotOffset, _carretOffset - _dotOffset);
break;
case LowercaseAfterParenWithDot:
if (//java method
_isMethodOrFunction && (!_isClojure)) {
if (_isConstructor) {
value = "";
} else if (Character.isLetter(value.charAt(0))) {
{
doc.remove(_dotOffset, _carretOffset - _dotOffset);
value = "." + value;
}
}
} else
doc.remove(_dotOffset, _carretOffset - _dotOffset);
break;
case backSlashAfterParen:
doc.remove(_dotOffset, _carretOffset - _dotOffset);
break;
case FirstDotAfterParen:
doc.remove(_dotOffset, _carretOffset - _dotOffset);
break;
case FirstDotNoParen:
doc.remove(_dotOffset, _carretOffset - _dotOffset);
break;
case UppercaseNoParen:
doc.remove(_dotOffset, _carretOffset - _dotOffset);
break;
case UppercaseNoParenWithDot:
if (//java method
_isMethodOrFunction && (!_isClojure)) {
if (_isConstructor) {
value = "";
} else if (Character.isLetter(value.charAt(0))) {
{
doc.remove(_dotOffset, _carretOffset - _dotOffset);
value = "." + value;
}
}
} else
doc.remove(_dotOffset, _carretOffset - _dotOffset);
break;
case LowercaseNoParenWithDot:
if (//java method
_isMethodOrFunction && (!_isClojure)) {
if (_isConstructor) {
value = "";
} else if (Character.isLetter(value.charAt(0))) {
{
doc.remove(_dotOffset, _carretOffset - _dotOffset);
value = "." + value;
}
}
} else
doc.remove(_dotOffset, _carretOffset - _dotOffset);
break;
case LowercaseNoParen:
doc.remove(_dotOffset, _carretOffset - _dotOffset);
break;
}
doc.insertString(_dotOffset, value, null);
} else {
if (!c.equals("/")) {
int backSlashOffset = indexOfBackSlash(component);
if (backSlashOffset == -1)
backSlashOffset = caretOffset;
if (_isStatic || _layout == backSlashAfterParen || _layout == backSlashNoParen) {
if (_isClojure && _isMethodOrFunction) {
if (isInCurrentNamespace(_fullclassname, component)) {
doc.remove(_dotOffset, _carretOffset - _dotOffset);
doc.insertString(_dotOffset, setValue(value), null);
} else {
doc.remove(backSlashOffset + 1, caretOffset - backSlashOffset - 1);
doc.insertString(backSlashOffset + 1, value, null);
}
} else {
doc.remove(backSlashOffset + 1, caretOffset - backSlashOffset - 1);
doc.insertString(backSlashOffset + 1, value, null);
}
} else {
doc.remove(_dotOffset, _carretOffset - _dotOffset);
doc.insertString(_dotOffset, setValue(value), null);
}
} else if (_isStatic || _layout == backSlashAfterParen || _layout == backSlashNoParen)
if (_isClojure && _isMethodOrFunction) {
if (isInCurrentNamespace(_fullclassname, component)) {
doc.remove(_dotOffset, _carretOffset - _dotOffset);
doc.insertString(_dotOffset, setValue(value), null);
} else
doc.insertString(_carretOffset, value, null);
} else
doc.insertString(_carretOffset, value, null);
else {
doc.remove(_dotOffset, _carretOffset - _dotOffset);
doc.insertString(_dotOffset, setValue(value), null);
}
}
component.setCaretPosition(component.getCaretPosition() - backOffset);
} catch (BadLocationException e) {
LOG.log(Level.FINEST, e.getMessage());
}
}
}
AtomicChange change = new AtomicChange();
try {
NbDocument.runAtomicAsUser(doc, change);
} catch (BadLocationException ex) {
LOG.log(Level.FINEST, ex.getMessage());
}
}
use of javax.swing.text.BadLocationException in project Botnak by Gocnak.
the class ListenerUserChat method showSuggestion.
protected void showSuggestion() {
hideSuggestion();
int position = userChat.getCaretPosition();
Point location;
try {
location = userChat.modelToView(position).getLocation();
} catch (BadLocationException e2) {
e2.printStackTrace();
return;
}
String text = userChat.getText();
int start = Math.max(0, position - 1);
while (start > 0) {
if (!(text.charAt(start) == '@' || text.charAt(start) == ' ')) {
start--;
} else {
break;
}
}
if (start > position) {
return;
}
String subText = text.substring(start, position).toLowerCase();
if ("".equals(subText)) {
//|| !subText.contains("@")) {
shouldShow = false;
return;
}
String subWord = subText.replace("@", "");
subWord = subWord.trim();
if (subWord.length() < 2) {
return;
}
User[] users = Settings.channelManager.getUsers(subWord);
if (users.length > 0) {
ArrayList<String> names = new ArrayList<>();
for (User u : users) {
names.add(u.getLowerNick());
}
suggestion = new SuggestionPanel(position, subWord, location, names.toArray(new String[names.size()]));
SwingUtilities.invokeLater(userChat::requestFocusInWindow);
}
}
use of javax.swing.text.BadLocationException in project enclojure by EricThorsen.
the class ReplPanel method resultReceived.
public void resultReceived(JEditorPane pane, String result) {
Document doc = (Document) pane.getDocument();
try {
if (pane != _replEditorPane)
result = "\n" + result;
doc.insertString(doc.getLength(), result, null);
pane.setCaretPosition(doc.getLength());
} catch (BadLocationException ex) {
Logger.getLogger(ReplPanel.class.getName()).log(Level.SEVERE, null, ex);
}
if (pane == jEditorPaneOut && result.contains("Listening for transport dt_socket at address")) {
//Listening for transport dt_socket at address: 58896
String portString = result.substring(result.indexOf(":") + 1);
_debugPort = Integer.parseInt(portString.trim());
}
if (pane == _replEditorPane) {
_promptPos = _replEditorPane.getDocument().getLength();
jTabbedPane1.setSelectedIndex(0);
_undoManager.die();
}
}
use of javax.swing.text.BadLocationException in project intellij-community by JetBrains.
the class SwingHelper method scrollToReference.
public static boolean scrollToReference(JEditorPane view, String reference) {
reference = StringUtil.trimStart(reference, "#");
List<String> toCheck = Arrays.asList("a", "h1", "h2", "h3", "h4");
Document document = view.getDocument();
if (document instanceof HTMLDocument) {
List<Element> list = new ArrayList<>();
for (Element root : document.getRootElements()) {
getAllElements(root, list, toCheck);
}
for (Element element : list) {
AttributeSet attributes = element.getAttributes();
String nm = (String) attributes.getAttribute(HTML.Attribute.NAME);
if (nm == null)
nm = (String) attributes.getAttribute(HTML.Attribute.ID);
if ((nm != null) && nm.equals(reference)) {
try {
int pos = element.getStartOffset();
Rectangle r = view.modelToView(pos);
if (r != null) {
Rectangle vis = view.getVisibleRect();
r.y -= 5;
r.height = vis.height;
view.scrollRectToVisible(r);
return true;
}
} catch (BadLocationException ex) {
//ignore
}
}
}
}
return false;
}
Aggregations