Java Code Examples for javax.swing.text.Utilities

The following examples show how to use javax.swing.text.Utilities. These examples are extracted from open source projects. 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 Project: netbeans   Source File: UnitTestUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void prepareTest (String[] stringLayers, Lookup lkp) 
            throws IOException, SAXException, PropertyVetoException {
    URL[] layers = new URL[stringLayers.length];
    
    for (int cntr = 0; cntr < layers.length; cntr++) {
        layers[cntr] = Utilities.class.getResource(stringLayers[cntr]);
    }
    
    XMLFileSystem system = new XMLFileSystem();
    system.setXmlUrls(layers);
    
    Repository repository = new Repository(system);
    
    if (lkp == null) {
        UnitTestUtils.setLookup(new Object[] { repository }, UnitTestUtils.class.getClassLoader());
    } else {
        UnitTestUtils.setLookup(new Object[] { repository }, lkp, UnitTestUtils.class.getClassLoader());
    }
}
 
Example 2
Source Project: netbeans   Source File: UnitTestUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void prepareTest (String[] stringLayers, Lookup lkp) 
            throws IOException, SAXException, PropertyVetoException {
    URL[] layers = new URL[stringLayers.length];
    
    for (int cntr = 0; cntr < layers.length; cntr++) {
        layers[cntr] = Utilities.class.getResource(stringLayers[cntr]);
    }
    
    XMLFileSystem system = new XMLFileSystem();
    system.setXmlUrls(layers);
    
    Repository repository = new Repository(system);
    
    if (lkp == null) {
        DEFAULT_LOOKUP.setLookup(new Object[] { repository }, UnitTestUtils.class.getClassLoader());
    } else {
        DEFAULT_LOOKUP.setLookup(new Object[] { repository }, lkp, UnitTestUtils.class.getClassLoader());
    }
}
 
Example 3
Source Project: openjdk-jdk9   Source File: bug8134721.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void testNPE() {

        Graphics g = null;
        try {
            String test = "\ttest\ttest2";
            BufferedImage buffImage = new BufferedImage(
                    100, 100, BufferedImage.TYPE_INT_RGB);
            g = buffImage.createGraphics();
            Segment segment = new Segment(test.toCharArray(), 0, test.length());
            Utilities.drawTabbedText(segment, 0, 0, g, null, 0);
        } finally {
            if (g != null) {
                g.dispose();
            }
        }
    }
 
Example 4
Source Project: visualvm   Source File: SyntaxStyle.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Draw text.  This can directly call the Utilities.drawTabbedText.
 * Sub-classes can override this method to provide any other decorations.
 * @param  segment - the source of the text
 * @param  x - the X origin >= 0
 * @param  y - the Y origin >= 0
 * @param  graphics - the graphics context
 * @param e - how to expand the tabs. If this value is null, tabs will be 
 * expanded as a space character.
 * @param startOffset - starting offset of the text in the document >= 0 
 * @return
 */
public int drawText(Segment segment, int x, int y,
        Graphics graphics, TabExpander e, int startOffset) {
    graphics.setFont(graphics.getFont().deriveFont(getFontStyle()));
    FontMetrics fontMetrics = graphics.getFontMetrics();
    int a = fontMetrics.getAscent();
    int h = a + fontMetrics.getDescent();
    int w = Utilities.getTabbedTextWidth(segment, fontMetrics, 0, e, startOffset);
    int rX = x - 1;
    int rY = y - a;
    int rW = w + 2;
    int rH = h;
    if ((getFontStyle() & 0x10) != 0) {
        graphics.setColor(Color.decode("#EEEEEE"));
        graphics.fillRect(rX, rY, rW, rH);
    }
    graphics.setColor(getColor());
    x = Utilities.drawTabbedText(segment, x, y, graphics, e, startOffset);
    if ((getFontStyle() & 0x8) != 0) {
        graphics.setColor(Color.RED);
        graphics.drawRect(rX, rY, rW, rH);
    }
    return x;
}
 
Example 5
Source Project: groovy   Source File: TextEditor.java    License: Apache License 2.0 6 votes vote down vote up
public void actionPerformed(ActionEvent ae) {
    try {
        if (multiLineTab && TextEditor.this.getSelectedText() != null) {
            int end = Utilities.getRowEnd(TextEditor.this, getSelectionEnd());
            TextEditor.this.setSelectionEnd(end);

            Element el = Utilities.getParagraphElement(TextEditor.this, getSelectionStart());
            int start = el.getStartOffset();
            TextEditor.this.setSelectionStart(start);

            // remove text and reselect the text
            String text = tabsAsSpaces ?
                    TAB_BACK_PATTERN.matcher(getSelectedText()).replaceAll("") :
                    getSelectedText().replaceAll("^\t", "");

            int stop = start + text.length();
            setRenderRange(start, stop);
            TextEditor.this.replaceSelection(text);
            TextEditor.this.select(start, stop);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 6 votes vote down vote up
@SuppressWarnings("PMD.AvoidLiteralsInIfCondition")
@Override public void mousePressed(MouseEvent e) {
  super.mousePressed(e);
  int clickCount = e.getClickCount();
  if (SwingUtilities.isLeftMouseButton(e) && !e.isConsumed()) {
    if (clickCount == 2) {
      selectingMode = SelectingMode.WORD;
      p0 = Math.min(getDot(), getMark());
      p1 = Math.max(getDot(), getMark());
    } else if (clickCount >= 3) {
      selectingMode = SelectingMode.ROW;
      JTextComponent target = getComponent();
      int offs = target.getCaretPosition();
      try {
        p0 = Utilities.getRowStart(target, offs);
        p1 = Utilities.getRowEnd(target, offs);
        setDot(p0);
        moveDot(p1);
      } catch (BadLocationException ex) {
        UIManager.getLookAndFeel().provideErrorFeedback(target);
      }
    }
  } else {
    selectingMode = SelectingMode.CHAR;
  }
}
 
Example 7
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 6 votes vote down vote up
private void continuouslySelectWords(MouseEvent e) {
  Position.Bias[] biasRet = new Position.Bias[1];
  JTextComponent c = getComponent();
  int pos = getCaretPositionByLocation(c, e.getPoint(), biasRet);
  try {
    if (p0 < pos && pos < p1) {
      setDot(p0);
      moveDot(p1, biasRet[0]);
    } else if (p1 < pos) {
      setDot(p0);
      moveDot(Utilities.getWordEnd(c, pos), biasRet[0]);
    } else if (p0 > pos) {
      setDot(p1);
      moveDot(Utilities.getWordStart(c, pos), biasRet[0]);
    }
  } catch (BadLocationException ex) {
    UIManager.getLookAndFeel().provideErrorFeedback(c);
  }
}
 
Example 8
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 6 votes vote down vote up
private void continuouslySelectRows(MouseEvent e) {
  Position.Bias[] biasRet = new Position.Bias[1];
  JTextComponent c = getComponent();
  int pos = getCaretPositionByLocation(c, e.getPoint(), biasRet);
  try {
    if (p0 < pos && pos < p1) {
      setDot(p0);
      moveDot(p1, biasRet[0]);
    } else if (p1 < pos) {
      setDot(p0);
      moveDot(Utilities.getRowEnd(c, pos), biasRet[0]);
    } else if (p0 > pos) {
      setDot(p1);
      moveDot(Utilities.getRowStart(c, pos), biasRet[0]);
    }
  } catch (BadLocationException ex) {
    UIManager.getLookAndFeel().provideErrorFeedback(c);
  }
}
 
Example 9
Source Project: jpexs-decompiler   Source File: UniTools.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static int getTabbedTextOffset(Segment segment, FontMetrics metrics, int tabBase,int x,TabExpander e, int startOffset){
    List<Segment> segments=new ArrayList<Segment>();
    List<Boolean> unis=new ArrayList<Boolean>();
    
    Font origFont=metrics.getFont();
    getSegments(origFont, segment, segments, unis);
    Graphics g=new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).getGraphics();
    Font uniFont = defaultUniFont.deriveFont(origFont.getStyle(),origFont.getSize2D());       
    int ofs=0;
    int totalto = 0;
    for(int i=0;i<segments.size();i++){
        Segment seg=segments.get(i);
        FontMetrics fm=unis.get(i)?g.getFontMetrics(uniFont):metrics;
        int to = Utilities.getTabbedTextOffset(seg, fm, tabBase+ofs,x, e, startOffset);
        totalto += to;
        ofs+=fm.stringWidth(seg.toString());
        if(to<seg.length()){
            break;
        }            
    }        
    return totalto;
}
 
Example 10
Source Project: jpexs-decompiler   Source File: UniTools.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static int drawTabbedText(Segment segment, int x, int y, Graphics g, TabExpander e, int startOffset){
    
    List<Segment> segments=new ArrayList<Segment>();
    List<Boolean> unis=new ArrayList<Boolean>();
    getSegments(g.getFont(), segment, segments, unis);
    Font origFont=g.getFont();
    Font uniFont = defaultUniFont.deriveFont(origFont.getStyle(),origFont.getSize2D());
    int ret=x;
    int pos=0;
    for(int i=0;i<segments.size();i++){
        Segment seg=segments.get(i);
        if(unis.get(i)){
            g.setFont(uniFont);
        }else{
            g.setFont(origFont);
        }
        ret = Utilities.drawTabbedText(seg, ret, y, g, e, startOffset+pos);   
        pos += seg.length();
    }
    g.setFont(origFont);
    return ret;         
}
 
Example 11
@Override
public void mouseClicked(MouseEvent me) {
    if (me.getClickCount() < 2) return;

    int pos = output.viewToModel(me.getPoint());

    try {
        int rowStart = Utilities.getRowStart(output, pos);
        int rowEnd = Utilities.getRowEnd(output, pos);
        String line = output.getDocument().getText(rowStart, rowEnd - rowStart);
        if (opListener.getCmdHistory().contains(line)) {
            output.select(rowStart, rowEnd);
            cliGuiCtx.getCommandLine().getCmdText().setText(line);
            systemClipboard.setContents(new StringSelection(line), this);
        }
    } catch (BadLocationException e) {
        e.printStackTrace();
    }

}
 
Example 12
Source Project: nextreports-designer   Source File: SyntaxStyle.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Draw text. This can directly call the Utilities.drawTabbedText.
 * Sub-classes can override this method to provide any other decorations.
 * 
 * @param  segment - the source of the text
 * @param  x - the X origin >= 0
 * @param  y - the Y origin >= 0
 * @param  graphics - the graphics context
 * @param e - how to expand the tabs. If this value is null, tabs will be 
 * expanded as a space character.
 * @param startOffset - starting offset of the text in the document >= 0 
 * @return
 */
public int drawText(Segment segment, int x, int y,
        Graphics graphics, TabExpander e, int startOffset) {
    graphics.setFont(graphics.getFont().deriveFont(getFontStyle()));
    FontMetrics fontMetrics = graphics.getFontMetrics();
    int a = fontMetrics.getAscent();
    int h = a + fontMetrics.getDescent();
    int w = Utilities.getTabbedTextWidth(segment, fontMetrics, 0, e, startOffset);
    int rX = x - 1;
    int rY = y - a;
    int rW = w + 2;
    int rH = h;
    if ((getFontStyle() & 0x10) != 0) {
        graphics.setColor(Color.decode("#EEEEEE"));
        graphics.fillRect(rX, rY, rW, rH);
    }
    graphics.setColor(getColor());
    x = Utilities.drawTabbedText(segment, x, y, graphics, e, startOffset);
    if ((getFontStyle() & 0x8) != 0) {
        graphics.setColor(Color.RED);
        graphics.drawRect(rX, rY, rW, rH);
    }
    
    return x;
}
 
Example 13
Source Project: netbeans   Source File: UnitUtilities.java    License: Apache License 2.0 5 votes vote down vote up
public static void prepareTest(String[] additionalLayers, Object[] additionalLookupContent) throws IOException, SAXException, PropertyVetoException {
    URL[] layers = new URL[additionalLayers.length + 1];
    
    layers[0] = Utilities.class.getResource("/org/netbeans/modules/editor/errorstripe/resources/layer.xml");
    
    for (int cntr = 0; cntr < additionalLayers.length; cntr++) {
        layers[cntr + 1] = Utilities.class.getResource(additionalLayers[cntr]);
    }
    
    for (int cntr = 0; cntr < layers.length; cntr++) {
        if (layers[cntr] == null) {
            throw new IllegalStateException("layers[" + cntr + "]=null");
        }
    }
    
    XMLFileSystem system = new XMLFileSystem();
    system.setXmlUrls(layers);
    
    Repository repository = new Repository(system);
    Object[] lookupContent = new Object[additionalLookupContent.length + 1];
    
    System.arraycopy(additionalLookupContent, 0, lookupContent, 1, additionalLookupContent.length);
    
    lookupContent[0] = repository;
    
    DEFAULT_LOOKUP.setLookup(lookupContent, UnitUtilities.class.getClassLoader());
}
 
Example 14
Source Project: netbeans   Source File: OutputView.java    License: Apache License 2.0 5 votes vote down vote up
private int drawText(Graphics g,
                     int x, int y,
                     int startOffset, int endOffset,
                     boolean error,
                     boolean selected,
                     DocElement docElem) throws BadLocationException {
    Segment s = EventQueue.isDispatchThread() ? SEGMENT : new Segment(); 
    s.array = docElem.getChars();
    s.offset = startOffset - docElem.offset;
    s.count = endOffset - startOffset;

    g.setColor(getColor(error, selected));

    return Utilities.drawTabbedText(s, x, y, g, this, startOffset);
}
 
Example 15
/**
 * Paints the specified line onto the graphics context. Note that this method munges the offset
 * and count values of the segment.
 * 
 * @param line
 *            The line segment
 * @param tokens
 *            The token list for the line
 * @param styles
 *            The syntax style list
 * @param expander
 *            The tab expander used to determine tab stops. May be null
 * @param gfx
 *            The graphics context
 * @param x
 *            The x co-ordinate
 * @param y
 *            The y co-ordinate
 * @return The x co-ordinate, plus the width of the painted string
 */
public static int paintSyntaxLine(Segment line, Token tokens, SyntaxStyle[] styles, TabExpander expander, Graphics gfx,
		int x, int y) {
	Font defaultFont = gfx.getFont();
	Color defaultColor = gfx.getColor();

	for (;;) {
		byte id = tokens.id;
		if (id == Token.END) {
			break;
		}

		int length = tokens.length;
		if (id == Token.NULL) {
			if (!defaultColor.equals(gfx.getColor())) {
				gfx.setColor(defaultColor);
			}
			if (!defaultFont.equals(gfx.getFont())) {
				gfx.setFont(defaultFont);
			}
		} else {
			styles[id].setGraphicsFlags(gfx, defaultFont);
		}

		line.count = length;
		x = Utilities.drawTabbedText(line, x, y, gfx, expander, 0);
		line.offset += length;

		tokens = tokens.next;
	}

	return x;
}
 
Example 16
protected void paintPlainLine(Graphics gfx, int line, Font defaultFont, Color defaultColor, int x, int y) {
	paintHighlight(gfx, line, y);
	textArea.getLineText(line, currentLine);

	gfx.setFont(defaultFont);
	gfx.setColor(defaultColor);

	y += fm.getHeight();
	x = Utilities.drawTabbedText(currentLine, x, y, gfx, this, 0);

	if (eolMarkers) {
		gfx.setColor(eolMarkerColor);
		gfx.drawString(".", x, y);
	}
}
 
Example 17
Source Project: netbeans-mmd-plugin   Source File: NoteEditor.java    License: Apache License 2.0 5 votes vote down vote up
private static int getRow(final int pos, final JTextComponent editor) {
  int rn = (pos == 0) ? 1 : 0;
  try {
    int offs = pos;
    while (offs > 0) {
      offs = Utilities.getRowStart(editor, offs) - 1;
      rn++;
    }
  } catch (BadLocationException e) {
    LOGGER.error("Bad location", e); //NOI18N
  }
  return rn;
}
 
Example 18
Source Project: netbeans-mmd-plugin   Source File: NoteEditor.java    License: Apache License 2.0 5 votes vote down vote up
private static int getColumn(final int pos, final JTextComponent editor) {
  try {
    return pos - Utilities.getRowStart(editor, pos) + 1;
  } catch (BadLocationException e) {
    LOGGER.error("Bad location", e); //NOI18N
  }
  return -1;
}
 
Example 19
Source Project: groovy   Source File: TextEditor.java    License: Apache License 2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent ae) {
    try {
        Document doc = TextEditor.this.getDocument();
        String text = tabsAsSpaces ? TABBED_SPACES : "\t";
        if (multiLineTab && getSelectedText() != null) {
            int end = Utilities.getRowEnd(TextEditor.this, getSelectionEnd());
            TextEditor.this.setSelectionEnd(end);

            Element el = Utilities.getParagraphElement(TextEditor.this, getSelectionStart());
            int start = el.getStartOffset();
            TextEditor.this.setSelectionStart(start);

            String toReplace = TextEditor.this.getSelectedText();
            toReplace = LINE_START.matcher(toReplace).replaceAll(text);

            int stop = start + toReplace.length();
            setRenderRange(start, stop);
            TextEditor.this.replaceSelection(toReplace);
            TextEditor.this.select(start, stop);
        } else {
            int pos = TextEditor.this.getCaretPosition();
            doc.insertString(pos, text, null);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 20
Source Project: seaglass   Source File: MacEditorKit.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 */
public void actionPerformed(ActionEvent e) {
    JTextComponent target = getTextComponent(e);
    boolean        beep   = true;

    if ((target != null) && (target.isEditable())) {

        try {

            // select the next word
            int    offs    = target.getCaretPosition();
            int    endOffs;
            String s       = target.getDocument().getText(offs, 1);

            if (Character.isWhitespace(s.charAt(0))) {
                endOffs = Utilities.getNextWord(target, offs);
                endOffs = Utilities.getWordEnd(target, endOffs);
            } else {
                endOffs = Utilities.getWordEnd(target, offs);
            }

            target.moveCaretPosition(endOffs);

            // and then delete it
            target.replaceSelection("");
            beep = false;
        } catch (BadLocationException exc) {
            // nothing to do, because we set beep to true already
        }
    }

    if (beep) {
        provideErrorFeedback(target);
    }
}
 
Example 21
Source Project: seaglass   Source File: MacEditorKit.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 */
public void actionPerformed(ActionEvent e) {
    JTextComponent target = getTextComponent(e);
    boolean        beep   = true;

    if ((target != null) && (target.isEditable())) {
        int     offs   = target.getCaretPosition();
        boolean failed = false;

        try {
            offs = Utilities.getPreviousWord(target, offs);
        } catch (BadLocationException bl) {

            if (offs != 0) {
                offs = 0;
            } else {
                failed = true;
            }
        }

        if (!failed) {
            target.moveCaretPosition(offs);

            // and then delete it
            target.replaceSelection("");
            beep = false;
        }
    }

    if (beep) {
        provideErrorFeedback(target);
    }
}
 
Example 22
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 5 votes vote down vote up
public static int getWordStart(JTextComponent c, int offs) throws BadLocationException {
  Element line = Optional.ofNullable(Utilities.getParagraphElement(c, offs))
      .orElseThrow(() -> new BadLocationException("No word at " + offs, offs));
  Document doc = c.getDocument();
  int lineStart = line.getStartOffset();
  int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
  int offs2 = offs;
  Segment seg = SegmentCache.getSharedSegment();
  doc.getText(lineStart, lineEnd - lineStart, seg);
  if (seg.count > 0) {
    BreakIterator words = BreakIterator.getWordInstance(c.getLocale());
    words.setText(seg);
    int wordPosition = seg.offset + offs - lineStart;
    if (wordPosition >= words.last()) {
      wordPosition = words.last() - 1;
      words.following(wordPosition);
      offs2 = lineStart + words.previous() - seg.offset;
    } else {
      words.following(wordPosition);
      offs2 = lineStart + words.previous() - seg.offset;
      for (int i = offs; i > offs2; i--) {
        char ch = seg.charAt(i - seg.offset);
        if (ch == '_' || ch == '-') {
          offs2 = i + 1;
          break;
        }
      }
    }
  }
  SegmentCache.releaseSharedSegment(seg);
  return offs2;
}
 
Example 23
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 5 votes vote down vote up
public static int getWordEnd(JTextComponent c, int offs) throws BadLocationException {
  Element line = Optional.ofNullable(Utilities.getParagraphElement(c, offs))
      .orElseThrow(() -> new BadLocationException("No word at " + offs, offs));
  Document doc = c.getDocument();
  int lineStart = line.getStartOffset();
  int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
  int offs2 = offs;

  Segment seg = SegmentCache.getSharedSegment();
  doc.getText(lineStart, lineEnd - lineStart, seg);
  if (seg.count > 0) {
    BreakIterator words = BreakIterator.getWordInstance(c.getLocale());
    words.setText(seg);
    int wordPosition = offs - lineStart + seg.offset;
    if (wordPosition >= words.last()) {
      wordPosition = words.last() - 1;
    }
    offs2 = lineStart + words.following(wordPosition) - seg.offset;

    for (int i = offs; i < offs2; i++) {
      char ch = seg.charAt(i - seg.offset);
      if (ch == '_' || ch == '-') {
        offs2 = i;
        break;
      }
    }
  }
  SegmentCache.releaseSharedSegment(seg);
  return offs2;
}
 
Example 24
Source Project: jpexs-decompiler   Source File: UniTools.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static int getTabbedTextWidth(Graphics g,Segment segment,int x,TabExpander e, int startOffset){
    List<Segment> segments=new ArrayList<Segment>();
    List<Boolean> unis=new ArrayList<Boolean>();
    getSegments(g.getFont(), segment, segments, unis);
    Font origFont=g.getFont();
    Font uniFont = defaultUniFont.deriveFont(origFont.getStyle(),origFont.getSize2D());
    int ret=0;
    int pos=0;
    for(int i=0;i<segments.size();i++){
        Segment seg=segments.get(i);
        ret += Utilities.getTabbedTextWidth(seg, g.getFontMetrics(unis.get(i)?uniFont:origFont), 0, e, startOffset+pos);     
        pos += seg.length();
    }
    return ret;       
}
 
Example 25
Source Project: pdfxtk   Source File: MultiLineToolTip.java    License: Apache License 2.0 5 votes vote down vote up
public Dimension getPreferredSize(JComponent c) {
     FontMetrics metrics = c.getFontMetrics(c.getFont());
     String tipText = ((JToolTip)c).getTipText();
     if(tipText == null) tipText = "";
     
     ArrayList linesa = new ArrayList();
     String[]  strs   = Strings.split(tipText, '\n');
     for(int i = 0; i < strs.length; i++) {     
Segment s = new Segment(strs[i].toCharArray(), 0, strs[i].length());

for(int brk = 0; ;) {
  if(Utilities.getTabbedTextWidth(s, metrics, 0, null, 0) < maxAllowedWidth) {
    linesa.add(new String(s.array, s.offset, s.array.length - s.offset));
    break;
  }
  brk = Utilities.getBreakLocation(s, metrics, 0, maxAllowedWidth, null, 0);
  linesa.add(new String(s.array, s.offset, brk));
  s.offset += brk;
  s.count  -= brk;
}
     }
     
     lines = (String[])linesa.toArray(new String[linesa.size()]);
     maxWidth = 0;
     for(int i = 0; i < lines.length; i++)
maxWidth = Math.max(maxWidth, SwingUtilities.computeStringWidth(metrics, lines[i]));
     return new Dimension(maxWidth + 6, metrics.getHeight() * lines.length + 2);
   }
 
Example 26
/**
 * Converts an offset in a line into an x co-ordinate. This is a fast version that should only
 * be used if no changes were made to the text since the last repaint.
 * 
 * @param line
 *            The line
 * @param offset
 *            The offset, from the start of the line
 */
public int _offsetToX(int line, int offset) {
	TokenMarker tokenMarker = getTokenMarker();

	/* Use painter's cached info for speed */
	FontMetrics fm = painter.getFontMetrics();

	getLineText(line, lineSegment);

	int segmentOffset = lineSegment.offset;
	int x = horizontalOffset;

	/* If syntax coloring is disabled, do simple translation */
	if (tokenMarker == null) {
		lineSegment.count = offset;
		return x + Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
	}
	/*
	 * If syntax coloring is enabled, we have to do this because tokens can vary in width
	 */
	else {
		Token tokens;
		if (painter.currentLineIndex == line && painter.currentLineTokens != null) {
			tokens = painter.currentLineTokens;
		} else {
			painter.currentLineIndex = line;
			tokens = painter.currentLineTokens = tokenMarker.markTokens(lineSegment, line);
		}

		Font defaultFont = painter.getFont();
		SyntaxStyle[] styles = painter.getStyles();

		for (;;) {
			byte id = tokens.id;
			if (id == Token.END) {
				return x;
			}

			if (id == Token.NULL) {
				fm = painter.getFontMetrics();
			} else {
				fm = styles[id].getFontMetrics(defaultFont);
			}

			int length = tokens.length;

			if (offset + segmentOffset < lineSegment.offset + length) {
				lineSegment.count = offset - (lineSegment.offset - segmentOffset);
				return x + Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
			} else {
				lineSegment.count = length;
				x += Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
				lineSegment.offset += length;
			}
			tokens = tokens.next;
		}
	}
}
 
Example 27
Source Project: nextreports-designer   Source File: SyntaxView.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected int drawUnselectedText(Graphics graphics, int x, int y, int p0, int p1) {
    Font saveFont = graphics.getFont();
    Color saveColor = graphics.getColor();
    SyntaxDocument doc = (SyntaxDocument) getDocument();
    Segment segment = getLineBuffer();
    try {
        // Colour the parts
        Iterator<Token> tokens = doc.getTokens(p0, p1);
        int start = p0;
        while (tokens.hasNext()) {
            Token t = tokens.next();
            // if there is a gap between the next token start and where we
            // should be starting (spaces not returned in tokens), then draw
            // it in the default type
            if (start < t.start) {
                SyntaxStyles.getInstance().setGraphicsStyle(graphics,
                        TokenType.DEFAULT);
                doc.getText(start, t.start - start, segment);
                x = Utilities.drawTabbedText(segment, x, y, graphics, this, start);
            }
            // t and s are the actual start and length of what we should
            // put on the screen.  assume these are the whole token....
            int l = t.length;
            int s = t.start;
            // ... unless the token starts before p0:
            if (s < p0) {
                // token is before what is requested. adgust the length and s
                l -= (p0 - s);
                s = p0;
            }
            // if token end (s + l is still the token end pos) is greater 
            // than p1, then just put up to p1
            if (s + l > p1) {
                l = p1 - s;
            }
            doc.getText(s, l, segment);
            x = SyntaxStyles.getInstance().drawText(segment, x, y, graphics, this, t);
            start = t.start + t.length;
        }
        // now for any remaining text not tokenized:
        if (start < p1) {
            SyntaxStyles.getInstance().setGraphicsStyle(graphics, TokenType.DEFAULT);
            doc.getText(start, p1 - start, segment);
            x = Utilities.drawTabbedText(segment, x, y, graphics, this, start);
        }
    } catch (BadLocationException e) {
        System.err.println("Requested: " + e.offsetRequested());
        LOG.error(e.getMessage(), e);
    } finally {
        graphics.setFont(saveFont);
        graphics.setColor(saveColor);
    }
    
    return x;
}