/** 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); }
public void setTabs(int charactersPerTab) { FontMetrics fm = getFontMetrics(getFont()); int charWidth = fm.charWidth('w'); int tabWidth = charWidth * charactersPerTab; TabStop[] tabs = new TabStop[10]; for (int j = 0; j < tabs.length; j++) { int tab = j + 1; tabs[j] = new TabStop(tab * tabWidth); } TabSet tabSet = new TabSet(tabs); SimpleAttributeSet attributes = new SimpleAttributeSet(); StyleConstants.setTabSet(attributes, tabSet); }
private void setupTabStops() { // Ugly piece of code; needed because default tab indentation is too // big. StyleContext sc = StyleContext.getDefaultStyleContext(); Toolkit t = Toolkit.getDefaultToolkit(); FontMetrics fm = t.getFontMetrics(new Font("Courier", Font.PLAIN, 14)); int cw = fm.stringWidth(" "); final TabStop[] tabStops = new TabStop[60]; for (int i = 0; i < tabStops.length; i++) tabStops[i] = new TabStop((i + 1) * cw); TabSet tabs = new TabSet(tabStops); AttributeSet paraSet = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabs); pane.setParagraphAttributes(paraSet, false); }
/** 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); }
private void setTabSize(int size) { TabStop[] tabs = new TabStop[1]; tabs[0] = new TabStop(size, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE); // tabs[1] = new TabStop(100, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE); // tabs[2] = new TabStop(200, TabStop.ALIGN_CENTER, TabStop.LEAD_NONE); // tabs[3] = new TabStop(300, TabStop.ALIGN_DECIMAL, TabStop.LEAD_NONE); TabSet tabset = new TabSet(tabs); StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset); textPane.setParagraphAttributes(aset, false); }
@Before public void setUp(){ task = new BattleTask(); // GUI setup JFrame demo = new JFrame(); demo.setSize(400, 300); demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); textpane = new JTextPane(); TabStop[] tabs = new TabStop[1]; tabs[0] = new TabStop(40, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE); TabSet tabset = new TabSet(tabs); StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset); textpane.setParagraphAttributes(aset, false); textpane.setEditable(false); JTextField input = new JTextField(); InputListener listener = new InputListener(); input.addActionListener(listener); demo.getContentPane().add(BorderLayout.CENTER, textpane); demo.getContentPane().add(BorderLayout.SOUTH, input); demo.setVisible(true); input.requestFocusInWindow(); }
@Override public float nextTabStop(float x, int tabOffset) { float tabBase = this.getTabBase(); // If the text isn't left justified, offset by 10 pixels! if(createRow().getAlignment(View.X_AXIS) != 0) return x + 10.0f; /* * This hack mimics the following, which is a private variable! ARGH! * * if (justification != StyleConstants.ALIGN_LEFT) * return x + 10.0f; */ x -= tabBase; TabSet tabs = getTabSet(); if (tabs == null) { // a tab every 72 pixels. return (tabBase + (((int) x / 72 + 1) * 72)); } TabStop tab = tabs.getTabAfter(x + .01f); if (tab == null) { // no tab, do a default of 5 pixels. // Should this cause a wrapping of the line? return tabBase + x + 5.0f; } int alignment = tab.getAlignment(); int offset; switch (alignment) { default: case TabStop.ALIGN_LEFT: // Simple case, left tab. return tabBase + tab.getPosition(); case TabStop.ALIGN_BAR: // PENDING: what does this mean? return tabBase + tab.getPosition(); case TabStop.ALIGN_RIGHT: case TabStop.ALIGN_CENTER: offset = findOffsetToCharactersInString(mutantTabChars, tabOffset + 1); break; case TabStop.ALIGN_DECIMAL: offset = findOffsetToCharactersInString(mutantTabDecimalChars, tabOffset + 1); break; } if (offset == -1) { offset = getEndOffset(); } float charsSize = getPartialSize(tabOffset + 1, offset); switch (alignment) { case TabStop.ALIGN_RIGHT: case TabStop.ALIGN_DECIMAL: // right and decimal are treated the same way, the new // position will be the location of the tab less the // partialSize. return tabBase + Math.max(x, tab.getPosition() - charsSize); case TabStop.ALIGN_CENTER: // Similar to right, but half the partialSize. return tabBase + Math.max(x, tab.getPosition() - charsSize / 2.0f); } // will never get here! return x; }
@Override public void insertString(DocumentFilter.FilterBypass fb, int offset, String str, AttributeSet attr) throws BadLocationException { if(!isEditable()) {return;} if(!flagInsert || offset!=lastPosition) {undo.valider(); flagInsert=true;} flagRemove=false; if(str.equals("\t")) { try { insererHTML("<span class='tab'> </span>", offset, Tag.SPAN); return; } catch (IOException ex) { Logger.getLogger(JMathTextPane.class.getName()).log(Level.SEVERE, null, ex); } } MutableAttributeSet att; //HACK en cas d'écriture proche d'un Component. On va chercher un style qui convient int pos = offset; do { att = new SimpleAttributeSet(getHTMLdoc().getCharacterElement(pos).getAttributes()); pos--; if(pos<0 && att.getAttribute(StyleConstants.ComponentAttribute)!=null) {att = new SimpleAttributeSet(defaultAttributeSet);} } while(pos>=0 && att.getAttribute(StyleConstants.ComponentAttribute)!=null); if(str.equals("\n") && editeurKit!=null && keepStyleUpdated) {//on réinitialise le style après un retour à le ligne. getEditeurKit().reset(); // getInputAttributes().addAttributes(getEditeurKit().getStyleAttributes()); } if(editeurKit!=null) { att.addAttributes(getEditeurKit().getStyleAttributes()); att.removeAttribute(HTML.Attribute.COLOR);//"color", HTML.Attribute.COLOR et "foreground" sont 3 attributs redondants, mais différents. att.addAttribute("color", ColorManager.getRGBHexa(getEditeurKit().getForeground()));//HACK car addAttribute ne remplace pas l'attribut color du html par l'attribut foreground du java } //Capture des caractères spéciaux if(str.equals("/")) { replaceBy(fb, offset, str, att, "÷"); return; } else if(str.equals("*")) { replaceBy(fb, offset, str, att, "×"); return; }//XXX ajouter ici le remplacements des caractères littéraux // attrs = att.copyAttributes(); //remplacerReturn(fb, offset, length, str, attrs); // remplacerEspaces(fb, offset, length, str, attrs); fb.insertString(offset, str, att); if(str.equals("\n") && keepStyleUpdated) {//on réinitialise le style après un retour à le ligne. //Fixe la taille des tabulations StyleContext sc = StyleContext.getDefaultStyleContext(); TabSet tabs = new TabSet(new TabStop[] { new TabStop(TAB_SIZE) }); AttributeSet paraSet = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabs); setParagraphAttributes(paraSet, false); new HTMLEditorKit.AlignmentAction("toLeft", StyleConstants.ALIGN_LEFT).actionPerformed(null); new HTMLEditorKit.ForegroundAction("toBlack", getDefaultForeground()).actionPerformed(null); undo.valider(); } lastPosition = getCaretPosition(); }
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; } } }
private void initialize() { final int espacesByTab=3; final int maxTabsNumber=28; //Calculate size and height BufferedImage dummy= new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); Graphics g= dummy.getGraphics(); regularFont=new Font("Monospaced", Font.PLAIN,size); charSize = g.getFontMetrics(regularFont).charWidth('0'); charHeight = g.getFontMetrics(regularFont).getHeight(); int tabSize = charSize * espacesByTab; TabStop[] tabs = new TabStop[maxTabsNumber]; for (int j = 0; j < tabs.length; j++){ tabs[j] = new TabStop( (j+1) * tabSize , TabStop.ALIGN_LEFT, TabStop.LEAD_NONE); } TabSet tabSet = new TabSet(tabs); regular = new SimpleAttributeSet(); StyleConstants.setFontFamily(regular, "Monospaced"); StyleConstants.setFontSize(regular, size); StyleConstants.ColorConstants.setForeground(regular, Color.black); StyleConstants.ColorConstants.setBackground(regular, backgroud); StyleConstants.setTabSet(regular, tabSet); regularBackground = new SimpleAttributeSet(); StyleConstants.ColorConstants.setBackground(regularBackground, backgroud); comment = new SimpleAttributeSet(); comment.addAttributes(regular); StyleConstants.ColorConstants.setForeground(comment, new Color(0x78B078)); //green string = new SimpleAttributeSet(); string.addAttributes(regular); StyleConstants.ColorConstants.setForeground(string, Color.blue); reserved = new SimpleAttributeSet(); reserved.addAttributes(regular); StyleConstants.ColorConstants.setForeground(reserved, new Color(0x000080)); //Dark blue StyleConstants.setBold(reserved, true); preprocesor = new SimpleAttributeSet(); preprocesor.addAttributes(regular); StyleConstants.ColorConstants.setForeground(preprocesor, Color.red); //red StyleConstants.setBold(preprocesor, true); variable = new SimpleAttributeSet(); variable.addAttributes(regular); StyleConstants.ColorConstants.setItalic(variable, true); function = new SimpleAttributeSet(); function.addAttributes(regular); StyleConstants.ColorConstants.setItalic(function, true); StyleConstants.ColorConstants.setForeground(function, Color.BLUE.darker()); // blue lineNumber = new SimpleAttributeSet(); lineNumber.addAttributes(regular); StyleConstants.ColorConstants.setBackground(lineNumber, new Color(220, 220, 255)); //light blue highlightedBackground= new SimpleAttributeSet(); StyleConstants.ColorConstants.setBackground(highlightedBackground, new Color(0xff,0xff,0x99)); //Yellow }