public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { StringBuilder builder = new StringBuilder(string); for (int i = builder.length() - 1; i >= 0; i--) { int cp = builder.codePointAt(i); if (!Character.isDigit(cp) && cp != '-') { builder.deleteCharAt(i); if (Character.isSupplementaryCodePoint(cp)) { i--; builder.deleteCharAt(i); } } } super.insertString(fb, offset, builder.toString(), attr); }
@Override public void insertString(final int offs, final String str, final AttributeSet a) throws BadLocationException { // NavigatorLogger.printMessage("Offset:"+offs+" STr:"+str+"L:"+getLength()+"attr:"+a); if ((getLength() + str.length()) <= maxLength) { final char[] source = str.toCharArray(); final char[] result = new char[source.length]; int j = 0; for (int i = 0; i < result.length; i++) { if (Character.isDigit(source[i])) { result[j++] = source[i]; } else { toolkit.beep(); if (log.isDebugEnabled()) { log.debug("insertString: " + source[i]); // NOI18N } } } super.insertString(offs, new String(result, 0, j), a); checked = false; } else { toolkit.beep(); } if ((getLength()) == maxLength) { // getLength() ist schon aktualisiert if (bringFocus2Next == true) { checked = true; nextField.requestFocus(); } // NavigatorLogger.printMessage("Sprung"); // NavigatorLogger.printMessage(nextField); } }
private boolean isWordChar(int index) { try { char ch = document.getText(index, 1).charAt(0); return Character.isLetterOrDigit(ch); } catch (BadLocationException e) { return false; } }
public void insertString(int offset, String s, AttributeSet attributeSet) throws BadLocationException { if ((limit == 0 || getLength() + s.length() <= limit) && Character.isDigit(s.charAt(0))) { // if we haven't reached the limit, insert the string super.insertString(offset, s, attributeSet); } else { // otherwise, just lose the string java.awt.Toolkit.getDefaultToolkit().beep(); } }
// The insert string method public void insertString(int offs, String src, AttributeSet a) throws BadLocationException { char[] source = src.toCharArray(); char[] result = new char[source.length]; int j = 0; for (int i = 0; i < result.length; i++) { if (Character.isDigit(source[i])) result[j++] = source[i]; } super.insertString(offs, new String(result, 0, j), a); }
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { char[] source = str.toCharArray(); char[] result = new char[source.length]; int j = 0; for (int i = 0; i < result.length; i++) { if (Character.isDigit(source[i])) result[j++] = source[i]; else { toolkit.beep(); // System.err.println("insertString: " + source[i]); } } super.insertString(offs, new String(result, 0, j), a); }
// Initialize Board public void init() { // Create cells and handlers cells = new JTextField[gameBoard.boardSize * gameBoard.boardSize + 1]; // Redraw Panel boardPanel.removeAll(); boardPanel.setLayout(new GridLayout(gameBoard.boardSize, gameBoard.boardSize)); // Set layout JTextFilter TextFilter = new JTextFilter(3); JTextDocumentListener JTextDocFilter = new JTextDocumentListener(); for (int i = 1; i <= gameBoard.boardSize * gameBoard.boardSize; i++) { cells[i] = new JTextField(); ((AbstractDocument) cells[i].getDocument()).setDocumentFilter(TextFilter); ((AbstractDocument) cells[i].getDocument()).addDocumentListener(JTextDocFilter); ((AbstractDocument) cells[i].getDocument()).putProperty("index", i); cells[i].setHorizontalAlignment(JTextField.CENTER); cells[i].setFont(new Font("Agency FB", Font.BOLD, 24)); // Add elements to the grid content pane boardPanel.add(cells[i]); } // Initialize booleans gameOver = false; // Clear Board for (int i = 1; i <= (gameBoard.boardSize * gameBoard.boardSize); i++) { String ch = Integer.toString(this.gameBoard.cells[i]); char chr = '-'; if (ch.compareTo("0") == 0 || ch == Character.toString(chr)) { cells[i].setText(""); } else { cells[i].setText(ch); cells[i].setBackground(Color.lightGray); } } // gameBoard.out(); setVisible(true); this.boardPanel.repaint(); this.gameTimer.reset(); jButtonSOLVE.setEnabled(true); }
@Override public void replace( DocumentFilter.FilterBypass fp, int offset, int length, String string, AttributeSet aset) throws BadLocationException { Document doc = fp.getDocument(); String oldText = doc.getText(0, doc.getLength()); StringBuilder sb = new StringBuilder(oldText); sb.replace(offset, offset + length, oldText); int len = string.length(); boolean isValidInteger = true; for (int i = 0; i < len; i++) { if (!Character.isDigit(string.charAt(i))) { isValidInteger = false; break; } } if (isValidInteger && verifyText(sb.toString())) { super.replace(fp, offset, length, string, aset); } else Toolkit.getDefaultToolkit().beep(); }
private void doDoubleClick(MouseEvent evt, int line, int offset, int dot) throws BadLocationException { // Ignore empty lines if (getLineLength(line) == 0) return; try { int bracket = TextUtilities.findMatchingBracket(document, Math.max(0, dot - 1)); if (bracket != -1) { int mark = getMarkPosition(); // Hack if (bracket > mark) { bracket++; mark--; } select(mark, bracket); return; } } catch (BadLocationException bl) { bl.printStackTrace(); } // Ok, it's not a bracket... select the word String lineText = getLineText(line); char ch = lineText.charAt(Math.max(0, offset - 1)); String noWordSep = (String) document.getProperty("noWordSep"); if (noWordSep == null) noWordSep = ""; // If the user clicked on a non-letter char, // we select the surrounding non-letters boolean selectNoLetter = (!Character.isLetterOrDigit(ch) && noWordSep.indexOf(ch) == -1); int wordStart = 0; for (int i = offset - 1; i >= 0; i--) { ch = lineText.charAt(i); if (selectNoLetter ^ (!Character.isLetterOrDigit(ch) && noWordSep.indexOf(ch) == -1)) { wordStart = i + 1; break; } } int wordEnd = lineText.length(); for (int i = offset; i < lineText.length(); i++) { ch = lineText.charAt(i); if (selectNoLetter ^ (!Character.isLetterOrDigit(ch) && noWordSep.indexOf(ch) == -1)) { wordEnd = i; break; } } int lineStart = getLineStartOffset(line); select(lineStart + wordStart, lineStart + wordEnd); /* String lineText = getLineText(line); String noWordSep = (String)document.getProperty("noWordSep"); int wordStart = TextUtilities.findWordStart(lineText,offset,noWordSep); int wordEnd = TextUtilities.findWordEnd(lineText,offset,noWordSep); int lineStart = getLineStartOffset(line); select(lineStart + wordStart,lineStart + wordEnd); */ }
@Override public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException { for (int n = string.length(); n > 0; n--) { char c = string.charAt(n - 1); switch (filterType) { case ALPHA: if ((Character.isAlphabetic(c)) && (maxLength < 0 || this.textComponent.getText().length() < maxLength)) super.replace(fb, i, i1, String.valueOf(c), as); break; case NUMERIC: if ((Character.isDigit(c)) && (maxLength < 0 || this.textComponent.getText().length() < maxLength)) super.replace(fb, i, i1, String.valueOf(c), as); break; case ALPHANUMERIC: if ((Character.isAlphabetic(c) || Character.isDigit(c)) && (maxLength < 0 || this.textComponent.getText().length() < maxLength)) super.replace(fb, i, i1, String.valueOf(c), as); break; case ALPHA_SPACE: if ((Character.isAlphabetic(c) || c == ' ') && (maxLength < 0 || this.textComponent.getText().length() < maxLength)) super.replace(fb, i, i1, String.valueOf(c), as); break; case NUMERIC_SPACE: if ((Character.isDigit(c) || c == ' ') && (maxLength < 0 || this.textComponent.getText().length() < maxLength)) super.replace(fb, i, i1, String.valueOf(c), as); break; case ALPHANUMERIC_SPACE: if ((Character.isAlphabetic(c) || Character.isDigit(c) || c == ' ') && (maxLength < 0 || this.textComponent.getText().length() < maxLength)) super.replace(fb, i, i1, String.valueOf(c), as); break; case NO_HTML: if ((!isOneOfChars(c, '<', '>', '&')) && (maxLength < 0 || this.textComponent.getText().length() < maxLength)) super.replace(fb, i, i1, String.valueOf(c), as); break; case EMAIL: if ((Character.isAlphabetic(c) || Character.isDigit(c) || isOneOfChars( c, '.', '!', '@', '_', '-', '+', '#', '$', '?', '=', '%', '\'', '*', '^', '`', '{', '|', '}', '~')) && (maxLength < 0 || this.textComponent.getText().length() < maxLength)) super.replace(fb, i, i1, String.valueOf(c), as); break; case FILENAME: if ((Character.isAlphabetic(c) || Character.isDigit(c) || isOneOfChars(c, '_', '-')) && (maxLength < 0 || this.textComponent.getText().length() < maxLength)) super.replace(fb, i, i1, String.valueOf(c), as); break; default: if (maxLength < 0 || this.textComponent.getText().length() < maxLength) super.replace(fb, i, i1, String.valueOf(c), as); break; } } if (listener != null) listener.lengthChanged(textComponent.getText().length(), textComponent); }
/** * Checks whether the character is a delimiter. * * @param character the character to check * @return true if a delimiter */ public boolean isDelimiter(String character) { return Character.isWhitespace(character.charAt(0)) || (m_Delimiters.indexOf(character.charAt(0)) > -1); }
@Override protected int drawUnselectedText(java.awt.Graphics g, int x, int y, int p0, int p1) throws BadLocationException { Document doc = getDocument(); Segment segment = new Segment(); Segment token = getLineBuffer(); doc.getText(p0, p1 - p0, segment); int count = p1 - p0; int left = 0; int state = TEXT; int elementIndex = doc.getDefaultRootElement().getElementIndex(p0); AttributeSet lineAttributes = doc.getDefaultRootElement().getElement(elementIndex).getAttributes(); if (lineAttributes.isDefined("inComment")) { state = MULTILINECOMMENT; } for (int i = 0; i < count; i++) { // Starting in default text state. if (state == TEXT) { if (Character.isLetter(segment.array[i + segment.offset]) && Character.isLowerCase(segment.array[i + segment.offset])) { // flush now g.setColor(textColor); doc.getText(p0 + left, i - left, token); x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left); left = i; state = KEYWORD; } // Do nothing else { if (segment.array[i + segment.offset] == '/') { // flush now g.setColor(textColor); doc.getText(p0 + left, i - left, token); x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left); left = i; state = COMMENT; } else if (segment.array[i + segment.offset] == '"') { // flush now g.setColor(textColor); doc.getText(p0 + left, i - left, token); x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left); left = i; state = STRING; } } } else if (state == KEYWORD) { // Still if (Character.isLetter( segment .array[ i + segment .offset])) { // && Character.isLowerCase(segment.array[i+segment.offset])) } else { // flush now doc.getText(p0 + left, i - left, token); if (Keywords.isKeyword(token)) { g.setColor(keywordColor); } else { g.setColor(textColor); } x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left); left = i; state = TEXT; if (segment.array[i + segment.offset] == '/') { state = COMMENT; } else if (segment.array[i + segment.offset] == '"') { state = STRING; } } } else if (state == COMMENT) { if (segment.array[i + segment.offset] == '/') { break; } else if (segment.array[i + segment.offset] == '*') { state = MULTILINECOMMENT; } else { state = TEXT; } } else if (state == MULTILINECOMMENT) { if (i > 0 && segment.array[i + segment.offset] == '/' && segment.array[i + segment.offset - 1] == '*') { // flush now doc.getText(p0 + left, i + 1 - left, token); g.setColor(commentColor); x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left); left = i + 1; state = TEXT; } } else if (state == STRING) { if (segment.array[i + segment.offset] == '"') { // flush now doc.getText(p0 + left, i + 1 - left, token); g.setColor(stringColor); x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left); left = i + 1; state = TEXT; } } // Starting not in token } // end loop // Flush last doc.getText(p0 + left, p1 - p0 - left, token); if (state == KEYWORD) { if (Keywords.isKeyword(token)) { g.setColor(keywordColor); } else { g.setColor(textColor); } } else if (state == STRING) { g.setColor(stringColor); } else if (state == COMMENT && ((p1 - p0 - left) > 1)) { g.setColor(commentColor); } else if (state == MULTILINECOMMENT) { g.setColor(commentColor); } else { g.setColor(textColor); } x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left); return x; }