예제 #1
0
 /**
  * @param str
  * @param severity
  */
 public void addLine(String str, int severity) {
   Style style = null;
   switch (severity) {
     case 0:
       str = str + "\n";
       style = send;
       break;
     case 1:
       str = "(Info) " + str + "\n";
       style = send;
       break;
     case 2:
       str = "(Warning) " + str + "\n";
       style = send;
       break;
     case 3:
       str = "(Error)" + str + "\n";
       style = send;
       break;
   }
   try {
     clean();
     document.insertString(document.getLength(), str, null);
     document.setParagraphAttributes(parnum, str.length(), style, false);
     parnum += str.length();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  /*
   * Adds a field to the format being composed
   */
  public void addField() {
    DefaultMutableTreeNode node =
        (DefaultMutableTreeNode) availableFieldsComp.getTree().getLastSelectedPathComponent();
    if (node == null
        || !node.isLeaf()
        || !(node.getUserObject() instanceof DataObjDataFieldWrapper)) {
      return; // not really a field that can be added, just empty or a string
    }

    Object obj = node.getUserObject();
    if (obj instanceof DataObjDataFieldWrapper) {
      DataObjDataFieldWrapper wrapper = (DataObjDataFieldWrapper) obj;
      String sep = sepText.getText();
      if (StringUtils.isNotEmpty(sep)) {
        try {
          DefaultStyledDocument doc = (DefaultStyledDocument) formatEditor.getStyledDocument();
          if (doc.getLength() > 0) {
            doc.insertString(doc.getLength(), sep, null);
          }
        } catch (BadLocationException ble) {
        }
      }
      insertFieldIntoTextEditor(wrapper);
      setHasChanged(true);
    }
  }
예제 #3
0
 private void appendText(final DefaultStyledDocument doc, final String text, final Style style) {
   try {
     doc.insertString(doc.getLength(), text, style);
   } catch (final BadLocationException e) {
     throw new RuntimeException(e);
   }
 }
예제 #4
0
  /**
   * Override to apply syntax highlighting after the document has been updated.
   *
   * @param offset the offset
   * @param str the string to insert
   * @param a the attribute set, can be null
   * @throws BadLocationException if offset is invalid
   */
  public void insertString(int offset, String str, AttributeSet a) throws BadLocationException {
    if (m_AddMatchingEndBlocks && (m_BlockStart.length() > 0) && str.equals(m_BlockStart))
      str = addMatchingBlockEnd(offset);
    else if (m_UseBlanks && str.equals("\t")) str = m_Indentation;

    super.insertString(offset, str, a);
    processChangedLines(offset, str.length());
  }
  public void displayString(String string) {
    String logMessage = sdf.format(cal.getTime()) + " " + string + '\n';

    try {
      doc.insertString(doc.getLength(), logMessage, null);
    } catch (BadLocationException e) {
      e.printStackTrace();
    }
  }
예제 #6
0
 @Override
 public void insertString(final int offs, final String str, final AttributeSet a)
     throws BadLocationException {
   synchronized (doclock) {
     super.insertString(offs, str, a);
     color(offs, str.length());
     documentReader.update(offs, str.length());
   }
 }
예제 #7
0
파일: TerminalPanel.java 프로젝트: npk/lcmc
 /** Is called while a string is inserted. It checks if a cheat code is in the string. */
 @Override
 public void insertString(int offs, final String s, final AttributeSet a)
     throws BadLocationException {
   mPosLock.lock();
   if (offs < commandOffset) {
     terminalArea.setCaretPosition(commandOffset);
     offs = commandOffset;
   }
   if (userCommand) {
     if (editEnabled) {
       if (s.charAt(s.length() - 1) == '\n') {
         final int end = terminalArea.getDocument().getLength();
         super.insertString(end, "\n", commandColor);
         final String command = (getText(commandOffset, end - commandOffset) + s).trim();
         prevLine = end + 1;
         pos = end;
         maxPos = end;
         execCommand(command);
       } else {
         super.insertString(offs, s, commandColor);
       }
     }
     for (final String cheat : CHEATS_MAP.keySet()) {
       int cheatPos = CHEATS_MAP.get(cheat);
       if (s.equals(cheat.substring(cheatPos, cheatPos + 1))) {
         cheatPos++;
         CHEATS_MAP.put(cheat, cheatPos);
         if (cheatPos == cheat.length()) {
           for (final String ch : CHEATS_MAP.keySet()) {
             CHEATS_MAP.put(ch, 0);
           }
           startCheat(cheat);
         }
       } else {
         CHEATS_MAP.put(cheat, 0);
       }
     }
   } else {
     super.insertString(offs, s, a);
   }
   mPosLock.unlock();
 }
 public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
   if ((getLength() + str.length()) <= size) {
     super.insertString(offs, str, a);
   } else {
     System.err.println(
         "Tried to make field "
             + (getLength() + str.length())
             + " characters long when max length is "
             + size);
     Toolkit.getDefaultToolkit().beep();
   }
 }
예제 #9
0
  public void addStyledText(String text, String style, boolean index) {
    Style s = _hexStyledDoc.getStyle(style);
    if (s == null) {
      PacketSamurai.getUserInterface().log("WARNING: Missing style for partType: " + style);
      style = "base";
    }

    try {
      _hexStyledDoc.insertString(_hexStyledDoc.getLength(), text, _hexStyledDoc.getStyle(style));
    } catch (BadLocationException e) {
      e.printStackTrace();
    }
  }
  public void testRemoveRedundantTagsFromDoc() throws BadLocationException {
    m_doc.insertString(0, "Foobar", SimpleAttributeSet.EMPTY);

    SimpleAttributeSet actualAttr = new SimpleAttributeSet();
    actualAttr.addAttribute(StyleConstants.Bold, Boolean.TRUE);
    m_doc.setCharacterAttributes(1, 2, actualAttr, true);

    actualAttr = new SimpleAttributeSet();
    actualAttr.addAttribute(StyleConstants.Bold, Boolean.TRUE);
    m_doc.setCharacterAttributes(3, 2, actualAttr, true);

    assertEquals("F<b>ooba</b>r", FormattedText.formatted(m_doc).getFormatted());
  }
예제 #11
0
    @Subscribe
    public void displayChatMessage(ChatEvent ev) {
        ReceivedChatMessage fm = createReceivedMessage(ev);
        formattedMessages.addLast(fm);

        if (client.getState() == JFrame.ICONIFIED) {
            messageReceivedWhileIconified = true;
        } else {
            setForceFocus();
        }

        DefaultStyledDocument doc = new DefaultStyledDocument();
        int offset = 0;
        try {
            for (ReceivedChatMessage msg : formattedMessages) {
                SimpleAttributeSet attrs = new SimpleAttributeSet();
                ColorConstants.setForeground(attrs, msg.color);
                String nick = msg.nickname;
                String text = msg.ev.getText();
                doc.insertString(offset, nick + ": ", attrs);
                offset += nick.length() + 2;

                Color textColor = client.getTheme().getTextColor();
                if (textColor == null) {
                    attrs = null;
                } else {
                    attrs = new SimpleAttributeSet();
                    ColorConstants.setForeground(attrs, client.getTheme().getTextColor());
                }
                doc.insertString(offset, text + "\n", attrs);
                offset += text.length() + 1;
            }
        } catch (BadLocationException e) {
            logger.error(e.getMessage(), e); //should never happen
        }
        messagesPane.setDocument(doc);
        repaint();
    }
  void appendCommand(String command, CommandMessageTypeEnum type) {
    try {
      int pos = commanddoc.getLength();

      switch (type) {
        case OutgoingCommand:
          commanddoc.insertString(pos, command + "\n", commandStyle);
          break;
        case Response:
          commanddoc.insertString(pos, command + "\n", responseStyle);
          break;
        case IncomingCommand:
          commanddoc.insertString(pos, command + "\n", inCommandStyle);
          break;
        default:
          commanddoc.insertString(pos, command + "\n", errorStyle);
          break;
      }

      textCommand.scrollRectToVisible(new Rectangle(0, textCommand.getBounds(null).height, 1, 1));
    } catch (BadLocationException ex) {
    }
  }
  @SuppressWarnings({"LeakingThisInConstructor", "BooleanConstructorCall"})
  public ConversationTopComponent() {
    initComponents();
    //		setName(Bundle.CTL_ConversationTopComponent());
    //		setToolTipText(Bundle.HINT_ConversationTopComponent());
    m_commandHistory = new Vector<String>();

    StyleContext sc = new StyleContext();
    commanddoc = new DefaultStyledDocument(sc);

    responseStyle = sc.addStyle("Response", null);
    responseStyle.addAttribute(StyleConstants.Foreground, Color.black);
    responseStyle.addAttribute(StyleConstants.FontSize, 16);
    responseStyle.addAttribute(StyleConstants.FontFamily, "serif");
    responseStyle.addAttribute(StyleConstants.Bold, false);

    errorStyle = sc.addStyle("Error", null);
    errorStyle.addAttribute(StyleConstants.Foreground, Color.red);
    errorStyle.addAttribute(StyleConstants.FontSize, 16);
    errorStyle.addAttribute(StyleConstants.FontFamily, "serif");
    errorStyle.addAttribute(StyleConstants.Bold, true);

    commandStyle = sc.addStyle("Command", null);
    commandStyle.addAttribute(StyleConstants.Foreground, Color.blue);
    commandStyle.addAttribute(StyleConstants.FontSize, 16);
    commandStyle.addAttribute(StyleConstants.FontFamily, "serif");
    commandStyle.addAttribute(StyleConstants.Bold, false);

    inCommandStyle = sc.addStyle("InCommand", null);
    inCommandStyle.addAttribute(StyleConstants.Foreground, Color.green);
    inCommandStyle.addAttribute(StyleConstants.FontSize, 16);
    inCommandStyle.addAttribute(StyleConstants.FontFamily, "serif");
    inCommandStyle.addAttribute(StyleConstants.Bold, false);

    warningStyle = sc.addStyle("Warning", null);
    warningStyle.addAttribute(StyleConstants.Foreground, Color.yellow);
    warningStyle.addAttribute(StyleConstants.FontSize, 16);
    warningStyle.addAttribute(StyleConstants.FontFamily, "serif");
    warningStyle.addAttribute(StyleConstants.Bold, true);

    textCommand.setDocument(commanddoc);

    try {
      commanddoc.insertString(0, "Pronto!\n\n", responseStyle);
    } catch (BadLocationException ex) {
      Exceptions.printStackTrace(ex);
    }
  }
  /**
   * Asserts that the style is still set correctly on a styled document after encoding the document
   * into a string representation and decoding it back into a styled document again.
   */
  public void assertStyleRoundtrip(Object style) throws BadLocationException {
    m_doc.insertString(0, "Foobar Test", SimpleAttributeSet.EMPTY);

    SimpleAttributeSet actualAttr = new SimpleAttributeSet();
    actualAttr.addAttribute(style, Boolean.TRUE);
    m_doc.setCharacterAttributes(2, 2, actualAttr, true);

    StyledDocument doc = roundtrip(m_doc);

    AttributeSet exceptedAttr = doc.getCharacterElement(2).getAttributes();
    Boolean hasStyle = (Boolean) exceptedAttr.getAttribute(style);
    if (hasStyle != null) {
      assertTrue(hasStyle.booleanValue());
    } else {
      fail("Style not set");
    }
  }
  void listCommands() {
    if (ServiceManager.Current != null) {
      Scriptable scope = ServiceManager.Current.getScope();

      Object[] objs = scope.getIds();

      for (int i = 0; i < objs.length; i++) {
        Object obj = objs[i];

        try {
          int pos = commanddoc.getLength();

          commanddoc.insertString(pos, obj.toString() + "\n", responseStyle);
        } catch (BadLocationException ex) {
          Exceptions.printStackTrace(ex);
        }
      }
    }
  }
예제 #16
0
 private void addLineBreaksToHexDump(byte[] id, byte[] data) {
   // add linefeeds to the dump
   int lnCount = _hexDumpArea.getText().length() / 48;
   int rest = _hexDumpArea.getText().length() % 48;
   for (int i = 1; i <= lnCount; i++) {
     int pos = i * 67 - 20;
     try {
       int idx = i - 1;
       String ansci =
           idx == 0
               ? (Util.toAnsci(id, 0, id.length) + Util.toAnsci(data, 0, 16 - id.length))
               : Util.toAnsci(data, idx * 16 - id.length, idx * 16 + 16 - id.length);
       _hexStyledDoc.replace(pos, 1, "   " + ansci + "\n", _hexStyledDoc.getStyle("base"));
       // _hexStyledDoc.remove(pos,1);
       // _hexStyledDoc.insertString(pos, "\n", _hexStyledDoc.getStyle("base"));
     } catch (BadLocationException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
     }
   }
   // rest
   if (rest != 0) {
     try {
       int pos = lnCount * 67 + rest;
       String space = "";
       int spaceCount = 48 - rest;
       while (spaceCount-- > 0) space += " ";
       String ansci =
           lnCount == 0
               ? (Util.toAnsci(id, 0, id.length) + Util.toAnsci(data, 0, data.length - id.length))
               : Util.toAnsci(data, lnCount * 16 - id.length, data.length - id.length);
       _hexStyledDoc.insertString(pos, space + "  " + ansci, _hexStyledDoc.getStyle("base"));
     } catch (BadLocationException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
     }
   }
 }
 @Override
 public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
   super.insertString(offs, str, a);
   checkDocument();
 }
  void sendCommand() {
    {
      String command = editCommand.getText();

      if (command != null && !command.isEmpty()) {
        // attempt to execute the command
        if (m_context == null) {
          m_context = Context.enter();
          // m_scope = m_context.initStandardObjects();

          // load up all services
          if (ServiceManager.Current != null) ServiceManager.Current.applyServices(m_context);
        }

        if (command.equals("?")) {
          listCommands();
          editCommand.setText("");
          return;
        }

        try {
          int pos = commanddoc.getLength();

          commanddoc.insertString(pos, command + "\n", commandStyle);

          Object ret =
              m_context.evaluateString(
                  ServiceManager.Current.getScope(), command, "Script", 1, null);

          // commands that succeed will be placed in the command list
          m_commandHistory.add(command);
          m_commandIndex = m_commandHistory.size();

          if (ret != null) {
            pos = commanddoc.getLength();

            commanddoc.insertString(pos, ret.toString() + "\n", responseStyle);
          }
        } catch (EcmaError ee) {
          try {
            int pos = commanddoc.getLength();

            commanddoc.insertString(pos, ee.details() + "\n", errorStyle);
          } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);
          }

          ee.details();
        } catch (Exception ex) {
          int pos = commanddoc.getLength();

          try {
            commanddoc.insertString(pos, ex.getMessage() + "\n", errorStyle);
          } catch (BadLocationException ex1) {
            Exceptions.printStackTrace(ex1);
          }
        }

        editCommand.setText("");
      }
    }
  }
  public void testFormattedStringWithNoFormatRoundtrip() throws BadLocationException {
    m_doc.insertString(0, "Foobar", SimpleAttributeSet.EMPTY);

    StyledDocument doc = roundtrip(m_doc);
    assertEquals("Foobar", doc.getText(0, doc.getLength()));
  }
  public void testEscapeCharsThatAreUsedForMarkupInFormattedText() throws BadLocationException {
    m_doc.insertString(0, "Foo<b>ba</b>r", SimpleAttributeSet.EMPTY);

    StyledDocument doc = roundtrip(m_doc);
    assertEquals("Foo<b>ba</b>r", doc.getText(0, doc.getLength()));
  }
  public void testEscapeCharsThatAreUsedForMarkupInDocumentText() throws BadLocationException {
    m_doc.insertString(0, "Foo<b>ba</b>r", SimpleAttributeSet.EMPTY);

    String encoding = FormattedText.formatted(m_doc).getFormatted();
    assertEquals("Foo<b>ba</b>r", FormattedText.formatted(encoding).getUnformatted());
  }