Пример #1
0
 private void setText(String text) {
   try {
     // remove all text and insert the completed string
     super.remove(0, getLength());
     super.insertString(0, text, null);
   } catch (BadLocationException e) {
     throw new RuntimeException(e.toString());
   }
 }
Пример #2
0
 public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
   // return immediately when selecting an item
   if (selecting) {
     return;
   }
   // insert the string into the document
   super.insertString(offs, str, a);
   // lookup and select a matching item
   Object item = lookupItem(getText(0, getLength()));
   if (item != null) {
     setSelectedItem(item);
   } else {
     // keep old item selected if there is no match
     item = comboBox.getSelectedItem();
     // imitate no insert (later on offs will be incremented by str.length(): selection won't move
     // forward)
     offs = offs - str.length();
     // provide feedback to the user that his input has been received but can not be accepted
     comboBox
         .getToolkit()
         .beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox);
   }
   setText(item.toString());
   // select the completed part
   highlightCompletedText(offs + str.length());
 }
Пример #3
0
    @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);
      }
    }
Пример #4
0
 public void remove(int offs, int len) throws BadLocationException {
   // return immediately when selecting an item
   if (selecting) {
     return;
   }
   if (hitBackspace) {
     // user hit backspace => move the selection backwards
     // old item keeps being selected
     if (offs > 0) {
       if (hitBackspaceOnSelection) {
         offs--;
       }
     } else {
       // User hit backspace with the cursor positioned on the start => beep
       comboBox.getToolkit().beep(); // when available use:
       // UIManager.getLookAndFeel().provideErrorFeedback(comboBox);
     }
     highlightCompletedText(offs);
   } else {
     super.remove(offs, len);
   }
 }
  @Override
  protected Document createDefaultModel() {
    PlainDocument d = (PlainDocument) super.createDefaultModel();

    d.setDocumentFilter(
        new DocumentFilter() {
          @Override
          public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
              throws BadLocationException {
            replace(fb, offset, 0, string, attr);
          }

          @Override
          public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
            replace(fb, offset, length, "", null);
          }

          @Override
          public void replace(
              FilterBypass fb, int replOffset, int replLen, String string, AttributeSet attrs)
              throws BadLocationException {
            String s = JMathTextField.this.getText();
            boolean insertedDummyChar = false;
            boolean insertedBrackets = false;
            String replStr = s.substring(replOffset, replOffset + replLen);

            if (string.matches(" |\\)")
                && replOffset + 1 <= s.length()
                && s.substring(replOffset, replOffset + 1).equals(string)) {
              JMathTextField.this.setCaretPosition(replOffset + 1);
              return;
            }

            if (string.isEmpty() && replStr.equals(" ")) {
              JMathTextField.this.setCaretPosition(replOffset);
              return;
            }

            string = string.replace(" ", "");
            if (string.isEmpty() && replStr.equals("(")) {
              int count = 1;
              while (replOffset + replLen < s.length()) {
                replLen++;
                replStr =
                    s.substring(
                        replOffset,
                        replOffset + replLen); // just update in case we need it later again
                if (replStr.charAt(0) == '(') count++;
                else if (replStr.charAt(0) == ')') count--;
                if (count == 0) break;
              }
            } else if (string.isEmpty() && replStr.equals(")")) {
              int count = -1;
              while (replOffset > 0) {
                replOffset--;
                replLen++;
                replStr = s.substring(replOffset, replOffset + replLen);
                if (replStr.charAt(0) == '(') count++;
                else if (replStr.charAt(0) == ')') count--;
                if (count == 0) break;
              }
            }

            if (string.matches("\\+|-|\\*|/|\\^|=")) {
              if (s.substring(replOffset + replLen).matches("( *(\\+|-|∙|/|\\^|=|\\)).*)|")) {
                string = string + "_";
                insertedDummyChar = true;
              } else if (s.substring(replOffset + replLen).matches(" .*")) {
                string = "_" + string;
                insertedDummyChar = true;
              }
            } else if (string.matches("\\(")) {
              if (Utils.reveresedString(s.substring(0, replOffset))
                      .matches("( )*([^\\+\\-∙/\\^= ].*|)")
                  || replStr.equals("_")) {
                string = "(_)";
                insertedDummyChar = true;
              } else if (!replStr.isEmpty()) {
                string = "(" + replStr + ")";
                insertedBrackets = true;
              } else return; // ignore this
            } else if (string.matches("\\)")) return; // ignore that

            // situation: A = B, press DEL -> make it A = _
            if (string.isEmpty()
                && Utils.reveresedString(s.substring(0, replOffset))
                    .matches("( )*(\\+|-|∙|/|\\^|=).*")
                && !replStr.matches("_|\\+|-|∙|/|\\^|=")) {
              string = "_";
              insertedDummyChar = true;
            } else if (string.isEmpty()
                && Utils.reveresedString(s.substring(0, replOffset))
                    .matches("( )*(\\+|-|∙|/|\\^|=).*")
                && replStr.equals("_")) {
              while (s.substring(replOffset - 1, replOffset).equals(" ")) {
                replOffset--;
                replLen++;
              }
              replOffset--;
              replLen++; // go just before the op
              // just update in case we need it later again
              // noinspection UnusedAssignment
              replStr = s.substring(replOffset, replOffset + replLen);
            }

            setNewString(
                fb, s.substring(0, replOffset) + string + s.substring(replOffset + replLen));

            if (insertedDummyChar) {
              int p = JMathTextField.this.getText().indexOf('_');
              if (p >= 0) {
                JMathTextField.this.setCaretPosition(p);
                JMathTextField.this.moveCaretPosition(p + 1);
              }
            } else if (insertedBrackets) {
              // move just before the ')'
              if (JMathTextField.this.getCaretPosition() > 0)
                JMathTextField.this.setCaretPosition(JMathTextField.this.getCaretPosition() - 1);
            }
          }

          synchronized void setNewString(FilterBypass fb, String tempStr)
              throws BadLocationException {
            operatorTree = OTParser.parse(tempStr, null);
            JMathTextField.this.updateByOpTree(fb);
          }
        });

    return d;
  }
 public void insertString(int offset, String str, AttributeSet attSet)
     throws BadLocationException {
   if (upperCase) str = str.toUpperCase();
   super.insertString(offset, str, attSet);
 }
Пример #7
0
 @Override
 public void remove(final int offs, final int len) throws BadLocationException {
   checked = false;
   super.remove(offs, len);
 }