javax.swing.text.TabStop Java Examples

The following examples show how to use javax.swing.text.TabStop. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OurSyntaxDocument.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/** Changes the font and tabsize for the document. */
public final void do_setFont(String fontName, int fontSize, int tabSize) {
    if (tabSize < 1)
        tabSize = 1;
    else if (tabSize > 100)
        tabSize = 100;
    if (fontName.equals(this.font) && fontSize == this.fontSize && tabSize == this.tabSize)
        return;
    this.font = fontName;
    this.fontSize = fontSize;
    this.tabSize = tabSize;
    for (MutableAttributeSet s : all) {
        StyleConstants.setFontFamily(s, fontName);
        StyleConstants.setFontSize(s, fontSize);
    }
    do_reapplyAll();
    BufferedImage im = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB); // this
                                                                             // is
                                                                             // used
                                                                             // to
                                                                             // derive
                                                                             // the
                                                                             // tab
                                                                             // width
    int gap = tabSize * im.createGraphics().getFontMetrics(new Font(fontName, Font.PLAIN, fontSize)).charWidth('X');
    TabStop[] pos = new TabStop[100];
    for (int i = 0; i < 100; i++) {
        pos[i] = new TabStop(i * gap + gap);
    }
    StyleConstants.setTabSet(tabset, new TabSet(pos));
    setParagraphAttributes(0, getLength(), tabset, false);
}
 
Example #2
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
@Override public void install(JEditorPane c) {
  FontMetrics fm = c.getFontMetrics(c.getFont());
  int tabLength = fm.charWidth('m') * 4;
  TabStop[] tabs = new TabStop[100];
  for (int j = 0; j < tabs.length; j++) {
    tabs[j] = new TabStop((j + 1f) * tabLength);
  }
  TabSet tabSet = new TabSet(tabs);
  StyleConstants.setTabSet(ATTRS, tabSet);
  super.install(c);
}
 
Example #3
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  JTextPane textPane = new JTextPane();
  textPane.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
  FontMetrics fm = textPane.getFontMetrics(textPane.getFont());
  float charWidth = fm.charWidth('m');
  float tabWidth = charWidth * 4f;
  TabStop[] tabs = new TabStop[10];
  for (int j = 0; j < tabs.length; j++) {
    tabs[j] = createTabStop((j + 1) * tabWidth);
  }
  TabSet tabSet = new TabSet(tabs);
  // MutableAttributeSet attributes = new SimpleAttributeSet();
  MutableAttributeSet attributes = textPane.getStyle(StyleContext.DEFAULT_STYLE);
  StyleConstants.setTabSet(attributes, tabSet);
  // int length = textPane.getDocument().getLength();
  // textPane.getStyledDocument().setParagraphAttributes(0, length, attributes, false);
  textPane.setParagraphAttributes(attributes, false);
  textPane.setText("JTextPane\n0123\n\t4567\n\t\t89ab\n");

  JTextArea textArea = new JTextArea();
  textArea.setTabSize(4);
  textArea.setText("JTextArea\n0123\n\t4567\n\t\t89ab\n");

  add(new JScrollPane(textArea), BorderLayout.NORTH);
  add(new JScrollPane(textPane));
  setPreferredSize(new Dimension(320, 240));
}
 
Example #4
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void updateUI() {
  super.updateUI();
  setOpaque(false);
  putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
  EventQueue.invokeLater(() -> {
    // MutableAttributeSet attr = new SimpleAttributeSet();
    Style attr = getStyle(StyleContext.DEFAULT_STYLE);
    TabStop[] ts = {new TabStop(25f, TabStop.ALIGN_DECIMAL, TabStop.LEAD_NONE)};
    StyleConstants.setTabSet(attr, new TabSet(ts));
    setParagraphAttributes(attr, false);
  });
}
 
Example #5
Source File: JavaSourceDocument.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public JavaSourceDocument(String title, Reader in, SourceFile theSource) throws IOException {
    doc = new DefaultStyledDocument();
    this.title = title;
    this.sourceFile = theSource;
    Debug.println("Created JavaSourceDocument for " + title);
    try {
        dek.read(in, doc, 0);
    } catch (BadLocationException e) {
        throw new RuntimeException(e);
    }
    in.close();
    doc.putProperty(Document.TitleProperty, title);
    //        root = doc.getDefaultRootElement();
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    FontMetrics fontMetrics = toolkit.getFontMetrics(sourceFont);
    TabStop[] tabs = new TabStop[50];
    float width = fontMetrics.stringWidth(" ");

    int tabSize = GUISaveState.getInstance().getTabSize();
    for (int i = 0; i < tabs.length; i++) {
        tabs[i] = new TabStop(width * (tabSize + tabSize * i));
    }
    TAB_SET = new TabSet(tabs);
    StyleConstants.setTabSet(commentAttributes, TAB_SET);
    StyleConstants.setTabSet(javadocAttributes, TAB_SET);

    StyleConstants.setTabSet(quotesAttributes, TAB_SET);

    StyleConstants.setTabSet(keywordsAttributes, TAB_SET);

    StyleConstants.setTabSet(commentAttributes, TAB_SET);

    StyleConstants.setTabSet(whiteAttributes, TAB_SET);
    StyleConstants.setFontFamily(whiteAttributes, sourceFont.getFamily());
    StyleConstants.setFontSize(whiteAttributes, sourceFont.getSize());
    StyleConstants.setLeftIndent(whiteAttributes, NumberedParagraphView.NUMBERS_WIDTH);

    doc.setParagraphAttributes(0, doc.getLength(), whiteAttributes, true);
    JavaScanner parser = new JavaScanner(new DocumentCharacterIterator(doc));
    while (parser.next() != JavaScanner.EOF) {
        int kind = parser.getKind();
        switch (kind) {
        case JavaScanner.COMMENT:
            doc.setCharacterAttributes(parser.getStartPosition(), parser.getLength(), commentAttributes, true);
            break;

        case JavaScanner.KEYWORD:
            doc.setCharacterAttributes(parser.getStartPosition(), parser.getLength(), keywordsAttributes, true);
            break;

        case JavaScanner.JAVADOC:
            doc.setCharacterAttributes(parser.getStartPosition(), parser.getLength(), javadocAttributes, true);
            break;

        case JavaScanner.QUOTE:
            doc.setCharacterAttributes(parser.getStartPosition(), parser.getLength(), quotesAttributes, true);
            break;

        default:
            break;
        }

    }

}
 
Example #6
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
private static TabStop createTabStop(float pos) {
  return new TabStop(pos);
}
 
Example #7
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  JCheckBox check = new JCheckBox("vertical grid lines", true);
  check.addActionListener(e -> repaint());

  JTextPane textPane = new JTextPane() {
    @Override protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (check.isSelected()) {
        int ox = getInsets().left;
        int h = getHeight();
        g.setColor(Color.RED);
        g.drawLine(ox, 0, ox, h);
        g.drawLine(ox + 100, 0, ox + 100, h);
        g.drawLine(ox + 200, 0, ox + 200, h);
        g.drawLine(ox + 300, 0, ox + 300, h);
        g.setColor(Color.ORANGE);
        g.drawLine(ox + 50, 0, ox + 50, h);
        g.drawLine(ox + 150, 0, ox + 150, h);
        g.drawLine(ox + 250, 0, ox + 250, h);
      }
    }
  };
  textPane.setText(String.join("\n",
      String.join("\t", "LEFT1", "CENTER1", "RIGHT1", "3.14"),
      String.join("\t", "LEFT22", "CENTER22", "RIGHT22", "12.3"),
      String.join("\t", "LEFT333", "CENTER333", "RIGHT333", "123.45"),
      String.join("\t", "LEFT4444", "CENTER4444", "RIGHT4444", "0.9876")));

  // MutableAttributeSet attr = new SimpleAttributeSet();
  Style attr = textPane.getStyle(StyleContext.DEFAULT_STYLE);
  StyleConstants.setTabSet(attr, new TabSet(new TabStop[] {
      new TabStop(0f, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE),
      new TabStop(100f, TabStop.ALIGN_CENTER, TabStop.LEAD_NONE),
      new TabStop(200f, TabStop.ALIGN_RIGHT, TabStop.LEAD_NONE),
      new TabStop(250f, TabStop.ALIGN_DECIMAL, TabStop.LEAD_NONE)
      // new TabStop(300f, TabStop.ALIGN_BAR, TabStop.LEAD_NONE)
  }));
  textPane.setParagraphAttributes(attr, false);
  // textPane.getStyledDocument().setParagraphAttributes(0, textPane.getDocument().getLength(), attr, false);

  add(new JScrollPane(textPane));
  add(check, BorderLayout.SOUTH);
  setPreferredSize(new Dimension(320, 240));
}