/**
     * Gets the displayed value, and uses the converter to convert the shared value to units this
     * instance is supposed to represent. If the displayed value and shared value are different,
     * then we turn off our document filter and update our value to match the shared value. We
     * re-enable the document filter afterwards.
     */
    @Override
    protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      try {
        StyledDocument doc = getStyledDocument();
        double actualValue = (multiplier.getDisplayValue(value.getValue()));
        String actualValueAsString = df.format(actualValue);

        String displayedValue;
        try {
          displayedValue = doc.getText(0, doc.getLength());
        } catch (BadLocationException e) {
          displayedValue = "";
        }
        if (displayedValue.isEmpty()) {
          displayedValue = "0";
        }
        double displayedValueAsDouble = Double.parseDouble(displayedValue);
        if (!actualValueAsString.equals(displayedValue)
            && displayedValueAsDouble != actualValue) // Allow user to enter trailing zeroes.
        {
          bypassFilterAndSetText(doc, actualValueAsString);
        }
      } catch (NumberFormatException e) {
        // Swallow it as it's OK for the user to try and enter non-numbers.
      }
    }
    protected void bypassFilterAndSetText(StyledDocument doc, String text) {
      try {
        filter.setUpdateValue(false);
        doc.remove(0, doc.getLength());
        doc.insertString(0, text, null);

      } catch (BadLocationException e) {
        java.awt.Toolkit.getDefaultToolkit().beep();
      } finally {
        filter.setUpdateValue(true);
      }
    }
Esempio n. 3
0
 void editorPane_keyPressed(KeyEvent e) {
   StyledDocument doc = editorPane.getStyledDocument();
   int pos = editorPane.getCaretPosition();
   int code = e.getKeyCode();
   Element el;
   switch (code) {
     case KeyEvent.VK_BACK_SPACE:
     case KeyEvent.VK_DELETE:
     case KeyEvent.VK_LEFT:
     case KeyEvent.VK_KP_LEFT:
       if (pos == 0) return;
       // we want to get the element to the left of position.
       el = doc.getCharacterElement(pos - 1);
       break;
     case KeyEvent.VK_RIGHT:
     case KeyEvent.VK_KP_RIGHT:
       // we want to get the element to the right of position.
       el = doc.getCharacterElement(pos + 1);
       break;
     default:
       return; // bail we don't handle it.
   }
   AttributeSet attr = el.getAttributes();
   String el_name = (String) attr.getAttribute(StyleConstants.NameAttribute);
   int el_range = el.getEndOffset() - el.getStartOffset() - 1;
   if (el_name.startsWith("Parameter") && StyleConstants.getComponent(attr) != null) {
     try {
       switch (code) {
         case KeyEvent.VK_BACK_SPACE:
         case KeyEvent.VK_DELETE:
           doc.remove(el.getStartOffset(), el_range);
           break;
         case KeyEvent.VK_LEFT:
         case KeyEvent.VK_KP_LEFT:
           editorPane.setCaretPosition(pos - el_range);
           break;
         case KeyEvent.VK_RIGHT:
         case KeyEvent.VK_KP_RIGHT:
           editorPane.setCaretPosition(pos + (el_range));
           break;
       }
     } catch (BadLocationException ex) {
     }
   }
 }
Esempio n. 4
0
  private void insertParameter(ConfigParamDescr descr, String format, int pos) {
    try {
      StyledDocument doc = (StyledDocument) editorPane.getDocument();

      // The component must first be wrapped in a style
      Style style = doc.addStyle("Parameter-" + numParameters, null);
      JLabel label = new JLabel(descr.getDisplayName());
      label.setAlignmentY(0.8f); // make sure we line up
      label.setFont(new Font("Helvetica", Font.PLAIN, 14));
      label.setForeground(Color.BLUE);
      label.setName(descr.getKey());
      label.setToolTipText("key: " + descr.getKey() + "    format: " + format);
      StyleConstants.setComponent(style, label);
      doc.insertString(pos, format, style);
      numParameters++;
    } catch (BadLocationException e) {
    }
  }