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());
  }
Esempio n. 2
0
  protected SimpleAttributeSet createAttributes() {
    // called from getAttributes()
    SimpleAttributeSet res = super.createAttributes();
    res.addAttribute(Constants.ATTRIBUTE_REPLACED_CONTENT, content);

    return res;
  }
  /** Handle Start Tag. */
  protected void handleStartTag(TagElement tag) {

    Element elem = tag.getElement();
    if (elem == dtd.body) {
      inbody++;
    } else if (elem == dtd.html) {
    } else if (elem == dtd.head) {
      inhead++;
    } else if (elem == dtd.title) {
      intitle++;
    } else if (elem == dtd.style) {
      instyle++;
    }
    if (debugFlag) {
      if (tag.fictional()) {
        debug("Start Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos());
      } else {
        debug(
            "Start Tag: "
                + tag.getHTMLTag()
                + " attributes: "
                + getAttributes()
                + " pos: "
                + getCurrentPos());
      }
    }
    if (tag.fictional()) {
      SimpleAttributeSet attrs = new SimpleAttributeSet();
      attrs.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED, Boolean.TRUE);
      callback.handleStartTag(tag.getHTMLTag(), attrs, getBlockStartPosition());
    } else {
      callback.handleStartTag(tag.getHTMLTag(), getAttributes(), getBlockStartPosition());
      flushAttributes();
    }
  }
  private final void commonBuild(
      List<ElementSpec> elements, ElementBox box, Object elementNameValue) {
    // when there are no special requirements to build an element, use this
    // one
    SimpleAttributeSet attr = new SimpleAttributeSet();
    attr.addAttribute(SwingBoxDocument.ElementNameAttribute, elementNameValue);
    attr.addAttribute(Constants.ATTRIBUTE_ANCHOR_REFERENCE, new Anchor());
    attr.addAttribute(Constants.ATTRIBUTE_BOX_REFERENCE, box);
    attr.addAttribute(Constants.ATTRIBUTE_ELEMENT_ID, box.getElement().getAttribute("id"));

    elements.add(new ElementSpec(attr, ElementSpec.StartTagType));

    buildElements(elements, box);

    elements.add(new ElementSpec(attr, ElementSpec.EndTagType));
  }
Esempio n. 5
0
 /**
  * Generate an attribute set for links.
  *
  * @param action the action to perform
  * @return the attribute set
  */
 public static SimpleAttributeSet getActionStyle(ActionListener action) {
   SimpleAttributeSet style = new SimpleAttributeSet();
   StyleConstants.setForeground(style, Color.blue);
   StyleConstants.setUnderline(style, true);
   StyleConstants.setFontFamily(style, font);
   StyleConstants.setFontSize(style, fontSize);
   style.addAttribute(ACTION_ATTRIBUTE, action);
   return style;
 }
Esempio n. 6
0
 @Override
 public AttributeSet getAttributeSet() {
   SimpleAttributeSet as = new SimpleAttributeSet();
   if (fontFamily != null) as.addAttribute(StyleConstants.FontFamily, fontFamily);
   if (fontSize != null) as.addAttribute(StyleConstants.FontSize, fontSize);
   if (foreground != null) as.addAttribute(StyleConstants.Foreground, foreground);
   if (background != null) as.addAttribute(StyleConstants.Background, background);
   if (italic != null) as.addAttribute(StyleConstants.Italic, italic);
   if (bold != null) as.addAttribute(StyleConstants.Bold, bold);
   if (underline != null) as.addAttribute(StyleConstants.Underline, underline);
   if (superscript != null) as.addAttribute(StyleConstants.Superscript, superscript);
   if (subscript != null) as.addAttribute(StyleConstants.Subscript, subscript);
   if (strikethrough != null) as.addAttribute(StyleConstants.StrikeThrough, strikethrough);
   return as;
 }
 public void setSelectionAttribute(Object key, Object value) {
   StyledDocument sd = (StyledDocument) editor.getDocument();
   int a = editor.getSelectionStart();
   int b = editor.getSelectionEnd();
   if (a == b) {
     rtf.getInputAttributes().addAttribute(key, value);
     return;
   }
   SimpleAttributeSet sas = new SimpleAttributeSet();
   sas.addAttribute(key, value);
   sd.setCharacterAttributes(a, b - a, sas, false);
 }
Esempio n. 8
0
  private synchronized void writeMessageText(String message) {
    SimpleAttributeSet attrTS = new SimpleAttributeSet();
    attrTS.addAttribute(ColorConstants.Foreground, Color.DARK_GRAY);
    attrTS.addAttribute(StyleConstants.Bold, true);
    logMsg.setValue("source", "ccu");
    logMsg.setValue("destination", "ccu");

    Document doc = msgTextArea.getDocument();
    try {
      SimpleAttributeSet attr = new SimpleAttributeSet();
      attr.addAttribute(ColorConstants.Foreground, Color.blue);
      doc.insertString(doc.getLength(), "[" + getTimeStamp() + "]: ", attrTS);

      attr = new SimpleAttributeSet();
      attr.addAttribute(ColorConstants.Foreground, Color.black);
      doc.insertString(doc.getLength(), message + "\n", attr);

      msgTextArea.setCaretPosition(doc.getLength());
    } catch (Exception e) {
    }
  }
Esempio n. 9
0
 protected void insertLineBreak() {
   try {
     int carretPosition = _textComponent.getCaretPosition();
     StyledDocument document = (StyledDocument) _textComponent.getDocument();
     SimpleAttributeSet attributes =
         new SimpleAttributeSet(document.getCharacterElement(carretPosition).getAttributes());
     attributes.addAttribute(LINE_BREAK_STRING, Boolean.TRUE);
     document.insertString(carretPosition, LINE_BREAK_STRING, attributes);
     _textComponent.setCaretPosition(carretPosition + 1);
   } catch (BadLocationException e) {
     throw new sneer.foundation.lang.exceptions.NotImplementedYet(e); // Fix Handle this exception.
   }
 }
Esempio n. 10
0
  private void prepareStyles() {
    StyledDocument doc = parent.getStyledDocument();
    plainStyle = doc.addStyle("PLAIN_STYLE", null);
    StyleConstants.setItalic(plainStyle, false);
    StyleConstants.setBold(plainStyle, false);

    boldStyle = doc.addStyle("BOLD_STYLE", null);
    StyleConstants.setBold(boldStyle, true);
    StyleConstants.setForeground(boldStyle, new Color(38, 128, 2));

    nonBoldStyle = doc.addStyle("NON_BOLD_STYLE", null);
    StyleConstants.setBold(nonBoldStyle, false);

    fontSizeStyle = doc.addStyle("FONT_SIZE", null);
    StyleConstants.setFontSize(fontSizeStyle, 40);

    black = new SimpleAttributeSet();
    black.addAttribute(StyleConstants.CharacterConstants.Foreground, Color.black);

    brackets = new SimpleAttributeSet();
    brackets.addAttribute(StyleConstants.CharacterConstants.Foreground, Color.BLACK);
  }
Esempio n. 11
0
 public void insertString(String sText, SimpleAttributeSet attributeSet) {
   int i = sText.indexOf("<u");
   if (i < 0) {
     i = sText.indexOf("<a");
     if (i < 0) {
       try {
         document.insertString(document.getLength(), sText, attributeSet);
       } catch (BadLocationException e) {
         e.printStackTrace();
       }
       return;
     } else {
       // <a>
       if (i > 0) {
         insertString(sText.substring(0, i), attributeSet);
         sText = sText.substring(i);
       }
       i = sText.indexOf("'");
       sText = sText.substring(i + 1);
       i = sText.indexOf("'");
       String sLink = sText.substring(0, i);
       i = sText.indexOf(">");
       sText = sText.substring(i + 1);
       i = sText.indexOf("</a>");
       SimpleAttributeSet attributeSet2 = new SimpleAttributeSet(attributeSet);
       StyleConstants.setForeground(attributeSet2, Color.BLUE);
       StyleConstants.setUnderline(attributeSet2, true);
       attributeSet2.addAttribute("link", sLink);
       insertString(sText.substring(0, i), attributeSet2);
       sText = sText.substring(i + "</a>".length());
       if (sText.length() > 0) {
         insertString(sText, attributeSet);
       }
     }
   } else {
     // <u>
     if (i > 0) {
       insertString(sText.substring(0, i), attributeSet);
       sText = sText.substring(i);
     }
     i = sText.indexOf("</u>");
     SimpleAttributeSet attributeSet2 = new SimpleAttributeSet(attributeSet);
     StyleConstants.setForeground(attributeSet2, Color.RED);
     insertString(sText.substring("<u>".length(), i), attributeSet2);
     sText = sText.substring(i + "</u>".length());
     if (sText.length() > 0) {
       insertString(sText, attributeSet);
     }
   }
 }
Esempio n. 12
0
 /**
  * get the value of this <code>AttributeComponent</code>
  *
  * @return the value selected from this component
  */
 public AttributeSet getValue() {
   final SimpleAttributeSet set = new SimpleAttributeSet();
   SizeSelectorPanel ssp;
   for (int i = 0; i < components.size(); i++) {
     ssp = (SizeSelectorPanel) components.elementAt(i);
     if (ssp.valueChanged()) {
       ca.setAttribute(i, ssp.getAttr());
     }
   }
   final String newValue = ca.getAttribute();
   if (!originalValue.equalsIgnoreCase(newValue)) {
     set.addAttribute(key, newValue);
     Util.styleSheet().addCSSAttribute(set, (CSS.Attribute) key, newValue);
   }
   return set;
 }
Esempio n. 13
0
 public AttributeSet getValue(final boolean includeUnchanged) {
   if (includeUnchanged) {
     final SimpleAttributeSet set = new SimpleAttributeSet();
     SizeSelectorPanel ssp;
     for (int i = 0; i < components.size(); i++) {
       ssp = (SizeSelectorPanel) components.elementAt(i);
       ca.setAttribute(i, ssp.getAttr());
     }
     final String newValue = ca.getAttribute();
     set.addAttribute(key, newValue);
     Util.styleSheet().addCSSAttribute(set, (CSS.Attribute) key, newValue);
     return set;
   } else {
     return getValue();
   }
 }
Esempio n. 14
0
  /** Handle Empty Tag. */
  protected void handleEmptyTag(TagElement tag) throws ChangedCharSetException {

    Element elem = tag.getElement();
    if (elem == dtd.meta && !ignoreCharSet) {
      SimpleAttributeSet atts = getAttributes();
      if (atts != null) {
        String content = (String) atts.getAttribute(HTML.Attribute.CONTENT);
        if (content != null) {
          if ("content-type"
              .equalsIgnoreCase((String) atts.getAttribute(HTML.Attribute.HTTPEQUIV))) {
            throw new ChangedCharSetException(content, false);
          } else if ("charset"
              .equalsIgnoreCase((String) atts.getAttribute(HTML.Attribute.HTTPEQUIV))) {
            throw new ChangedCharSetException(content, true);
          }
        }
      }
    }
    if (inbody != 0
        || elem == dtd.meta
        || elem == dtd.base
        || elem == dtd.isindex
        || elem == dtd.style
        || elem == dtd.link) {
      if (debugFlag) {
        if (tag.fictional()) {
          debug("Empty Tag: " + tag.getHTMLTag() + " pos: " + getCurrentPos());
        } else {
          debug(
              "Empty Tag: "
                  + tag.getHTMLTag()
                  + " attributes: "
                  + getAttributes()
                  + " pos: "
                  + getCurrentPos());
        }
      }
      if (tag.fictional()) {
        SimpleAttributeSet attrs = new SimpleAttributeSet();
        attrs.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED, Boolean.TRUE);
        callback.handleSimpleTag(tag.getHTMLTag(), attrs, getBlockStartPosition());
      } else {
        callback.handleSimpleTag(tag.getHTMLTag(), getAttributes(), getBlockStartPosition());
        flushAttributes();
      }
    }
  }
  /**
   * 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");
    }
  }
Esempio n. 16
0
 @Override
 public void path(Timing timing, String sPath, String s) {
   sText = null;
   if (sPath.equals("/ZTM/ZI/ZM") && timing == Timing.CHARACTERS) {
     attributeSet = new SimpleAttributeSet(attributeSet0);
     StyleConstants.setFontSize(attributeSet, 72);
     sText = s + "\n";
   } else if (sPath.equals("/ZTM/ZI/ZMLB") && timing == Timing.START) {
     attributeSet = null;
     sText = "\n";
   } else if (sPath.equals("/ZTM/ZI/ZMLB/XH") && timing == Timing.CHARACTERS) {
     attributeSet = new SimpleAttributeSet(attributeSet0);
     StyleConstants.setFontSize(attributeSet, 48);
     sText = s + ".\n";
   } else if (sPath.equals("/ZTM/ZI/ZMLB/YD/PYLB/PY") && timing == Timing.CHARACTERS) {
     attributeSet = null;
     sText = "拼音:" + s + " ";
   } else if (sPath.equals("/ZTM/ZI/ZMLB/YD/PYLB/ZY") && timing == Timing.CHARACTERS) {
     sText = "注音:" + s + " ";
   } else if (sPath.equals("/ZTM/ZI/ZMLB/YD/PYLB/DY") && timing == Timing.CHARACTERS) {
     sText = "\n";
   } else if (sPath.equals("/ZTM/ZI/ZMLB/YD/GY") && timing == Timing.CHARACTERS) {
     attributeSet = new SimpleAttributeSet(attributeSet0);
     StyleConstants.setFontSize(attributeSet, 24);
     sText = s;
   } else if (sPath.equals("/ZTM/ZI/ZMLB/YD") && timing == Timing.END) {
     sText = "\n";
   } else if (sPath.equals("/ZTM/ZI/ZMLB/GL") && timing == Timing.CHARACTERS) {
     attributeSet = new SimpleAttributeSet(attributeSet0);
     StyleConstants.setFontSize(attributeSet, 24);
     sText = s;
   } else if (sPath.equals("/ZTM/ZI/ZMLB/ZMSY") && timing == Timing.START) {
     attributeSet = null;
     sText = "\n";
   } else if (sPath.equals("/ZTM/ZI/ZMLB/ZMSY/SYLB/XH") && timing == Timing.CHARACTERS) {
     attributeSet = null;
     sText = "(" + s + ") ";
   } else if (sPath.equals("/ZTM/ZI/ZMLB/ZMSY/SYLB/SY") && timing == Timing.CHARACTERS) {
     sText = s + "\n";
   } else if (sPath.equals("/ZTM/ZI/ZMLB/ZMSY/SYLB/LZ") && timing == Timing.CHARACTERS) {
     attributeSet = new SimpleAttributeSet(attributeSet0);
     StyleConstants.setFontSize(attributeSet, 24);
     sText = s + "\n";
   } else if (sPath.equals("/ZTM/ZI/ZMLB/ZMSY/SYLB") && timing == Timing.END) {
     attributeSet = null;
     sText = "\n";
   } else if (sPath.equals("/ZTM/CI/CMLB/CM/CY") && timing == Timing.CHARACTERS) {
     attributeSet = new SimpleAttributeSet(attributeSet0);
     StyleConstants.setForeground(attributeSet, Color.BLUE);
     StyleConstants.setUnderline(attributeSet, true);
     attributeSet.addAttribute("link", s);
     sText = s;
   } else if (sPath.equals("/ZTM/CI/CMLB/CM/LJXH") && timing == Timing.CHARACTERS) {
     attributeSet = null;
     sText = " ";
   } else if (sPath.equals("/ZTM/CI/CMLB") && timing == Timing.END) {
     attributeSet = null;
     sText = "\n\n";
   } else if (sPath.equals("/ZTM/CI/CM/CY") && timing == Timing.CHARACTERS) {
     attributeSet = new SimpleAttributeSet(attributeSet0);
     StyleConstants.setFontSize(attributeSet, 48);
     sText = "【" + s + "】\n";
     wordPositionMap.put(s, document.getLength());
   } else if (sPath.equals("/ZTM/CI/CM/LJXH") && timing == Timing.CHARACTERS) {
   } else if (sPath.equals("/ZTM/CI/CM/CMSY/GL") && timing == Timing.CHARACTERS) {
     attributeSet = new SimpleAttributeSet(attributeSet0);
     StyleConstants.setFontSize(attributeSet, 24);
     sText = s + "\n";
   } else if (sPath.equals("/ZTM/CI/CM/CMSY/SYLB/XH") && timing == Timing.CHARACTERS) {
     attributeSet = null;
     sText = "(" + s + ") ";
   } else if (sPath.equals("/ZTM/CI/CM/CMSY/SYLB/SY") && timing == Timing.CHARACTERS) {
     attributeSet = null;
     sText = s + "\n";
   } else if (sPath.equals("/ZTM/CI/CM/CMSY/SYLB/LZ") && timing == Timing.CHARACTERS) {
     attributeSet = new SimpleAttributeSet(attributeSet0);
     StyleConstants.setFontSize(attributeSet, 24);
     sText = s + "\n";
   } else if (sPath.equals("/ZTM/CI/CM/CMSY") && timing == Timing.END) {
     attributeSet = null;
     sText = "\n";
   }
   if (sText != null) {
     insertString(sText, attributeSet == null ? attributeSet0 : attributeSet);
   }
 }
  public static void main() {
    // Main
    frame = new JFrame("Java Playground");
    frame.setSize(640, 480);
    // Make sure the divider is properly resized
    frame.addComponentListener(
        new ComponentAdapter() {
          public void componentResized(ComponentEvent c) {
            splitter.setDividerLocation(.8);
          }
        });
    // Make sure the JVM is reset on close
    frame.addWindowListener(
        new WindowAdapter() {
          public void windowClosed(WindowEvent w) {
            new FrameAction().kill();
          }
        });

    // Setting up the keybinding
    // Ctrl+k or Cmd+k -> compile
    bind(KeyEvent.VK_K);

    // Ctrl+e or Cmd+e -> console
    bind(KeyEvent.VK_E);

    // Save, New file, Open file, Print.
    // Currently UNUSED until I figure out how normal java files and playground files will
    // interface.
    bind(KeyEvent.VK_S);
    bind(KeyEvent.VK_N);
    bind(KeyEvent.VK_O);
    bind(KeyEvent.VK_P);

    // Binds the keys to the action defined in the FrameAction class.
    frame.getRootPane().getActionMap().put("console", new FrameAction());

    // The main panel for typing code in.
    text = new JTextPane();
    textScroll = new JScrollPane(text);
    textScroll.setBorder(null);
    textScroll.setPreferredSize(new Dimension(640, 480));

    // Document with syntax highlighting. Currently unfinished.
    doc = text.getStyledDocument();
    doc.addDocumentListener(
        new DocumentListener() {
          public void changedUpdate(DocumentEvent d) {}

          public void insertUpdate(DocumentEvent d) {}

          public void removeUpdate(DocumentEvent d) {}
        });

    ((AbstractDocument) doc).setDocumentFilter(new NewLineFilter());

    // The output log; a combination compiler warning/error/runtime error/output log.
    outputText = new JTextPane();
    outputScroll = new JScrollPane(outputText);
    outputScroll.setBorder(null);

    // "Constant" for the error font
    error = new SimpleAttributeSet();
    error.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE);
    error.addAttribute(StyleConstants.Foreground, Color.RED);

    // "Constant" for the warning message font
    warning = new SimpleAttributeSet();
    warning.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE);
    warning.addAttribute(StyleConstants.Foreground, Color.PINK);

    // "Constant" for the debugger error font
    progErr = new SimpleAttributeSet();
    progErr.addAttribute(StyleConstants.Foreground, Color.BLUE);

    // Print streams to redirect System.out and System.err.
    out = new TextOutputStream(outputText, null);
    err = new TextOutputStream(outputText, error);
    System.setOut(new PrintStream(out));
    System.setErr(new PrintStream(err));

    // Sets up the output log
    outputText.setEditable(false);
    outputScroll.setVisible(true);

    // File input/output setup
    chooser = new JFileChooser();

    // Setting up miscellaneous stuff
    compiler = ToolProvider.getSystemJavaCompiler();
    JVMrunning = false;
    redirectErr = null;
    redirectOut = null;
    redirectIn = null;

    // Sets up the splitter pane and opens the program up
    splitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT, textScroll, outputScroll);
    consoleDisplayed = false;
    splitter.remove(outputScroll); // Initially hides terminal until it is needed
    splitter.setOneTouchExpandable(true);
    frame.add(splitter);
    frame.setVisible(true);

    // Sets the divider to the proper one, for debugging
    // splitter.setDividerLocation(.8);
  }