Java tutorial
/* * blue - object composition environment for csound * Copyright (C) 2012 * Steven Yi <stevenyi@gmail.com> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package blue.orchestra.editor.blueSynthBuilder; import blue.orchestra.blueSynthBuilder.BSBGraphicInterface; import blue.orchestra.blueSynthBuilder.BSBObject; import java.util.Iterator; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import javax.swing.text.Element; import javax.swing.text.JTextComponent; import javax.swing.text.StyledDocument; import org.apache.commons.lang3.StringUtils; import org.netbeans.api.editor.mimelookup.MimeRegistration; import org.netbeans.spi.editor.completion.CompletionProvider; import org.netbeans.spi.editor.completion.CompletionResultSet; import org.netbeans.spi.editor.completion.CompletionTask; import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery; import org.netbeans.spi.editor.completion.support.AsyncCompletionTask; import org.openide.util.Exceptions; /** * * @author stevenyi */ public class BSBCompletionProvider implements CompletionProvider { BSBGraphicInterface bsbInterface = null; public void setBSBGraphicInterface(BSBGraphicInterface bsbInterface) { this.bsbInterface = bsbInterface; } @Override public CompletionTask createTask(int queryType, JTextComponent component) { if (queryType != CompletionProvider.COMPLETION_QUERY_TYPE || bsbInterface == null) { return null; } return new AsyncCompletionTask(new AsyncCompletionQuery() { @Override protected void query(CompletionResultSet completionResultSet, Document document, int caretOffset) { String filter = null; int startOffset = caretOffset - 1; try { final StyledDocument bDoc = (StyledDocument) document; final int lineStartOffset = getRowFirstNonWhite(bDoc, caretOffset); final char[] line = bDoc.getText(lineStartOffset, caretOffset - lineStartOffset).toCharArray(); final int whiteOffset = indexOfWhite(line); filter = new String(line, whiteOffset + 1, line.length - whiteOffset - 1); if (whiteOffset > 0) { startOffset = lineStartOffset + whiteOffset + 1; } else { startOffset = lineStartOffset; } } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } if (filter != null && !filter.equals("")) { int index = filter.indexOf("<"); if (index >= 0) { filter = filter.substring(index + 1); Iterator<BSBObject> bsbObjects = bsbInterface.iterator(); while (bsbObjects.hasNext()) { BSBObject bsbObj = bsbObjects.next(); String[] keys = bsbObj.getReplacementKeys(); if (keys == null) { continue; } for (String key : keys) { if (StringUtils.containsWhitespace(key)) { continue; } if (filter.isEmpty() || key.startsWith(filter)) { completionResultSet.addItem(new BSBCompletionItem(bsbObj, key)); } } } } } completionResultSet.finish(); } }, component); } @Override public int getAutoQueryTypes(JTextComponent component, String typedText) { return 0; } static int getRowFirstNonWhite(StyledDocument doc, int offset) throws BadLocationException { Element lineElement = doc.getParagraphElement(offset); int start = lineElement.getStartOffset(); while (start + 1 < lineElement.getEndOffset()) { try { if (doc.getText(start, 1).charAt(0) != ' ') { break; } } catch (BadLocationException ex) { throw (BadLocationException) new BadLocationException( "calling getText(" + start + ", " + (start + 1) + ") on doc of length: " + doc.getLength(), start).initCause(ex); } start++; } return start; } static int indexOfWhite(char[] line) { int i = line.length; while (--i > -1) { final char c = line[i]; if (Character.isWhitespace(c)) { return i; } } return -1; } }