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()); } }
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()); } }
public int getRow(int pos,JTextArea textPane1) { int rn=(pos==0) ? 1:0; try { int offs=pos; while(offs>0) { offs=Utilities.getRowStart(textPane1, offs)-1; rn++; } } catch(BadLocationException evt){ evt.printStackTrace();} return rn; }
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(); } } }
/** * Get the cursor position for the popup menu * * @param jText * current JTextComponent * @return the current position * @throws BadLocationException * should never occur */ protected int getCursorPosition( JTextComponent jText ) throws BadLocationException { Caret caret = jText.getCaret(); int offs; Point p = jText.getMousePosition(); if( p != null ) { // use position from mouse click and not from editor cursor position offs = jText.viewToModel( p ); // calculate rectangle of line int startPos = Utilities.getRowStart( jText, offs ); int endPos = Utilities.getRowEnd( jText, offs ); Rectangle bounds = jText.modelToView( startPos ).union( jText.modelToView( endPos ) ); if( !bounds.contains( p ) ){ return -1; // mouse is outside of text } } else { offs = Math.min( caret.getDot(), caret.getMark() ); } Document doc = jText.getDocument(); if( offs > 0 && (offs >= doc.getLength() || Character.isWhitespace( doc.getText( offs, 1 ).charAt( 0 ) )) ) { // if the next character is a white space then use the word on the left site offs--; } return offs; }
public void action() { JTextComponent c = getTextComponent(); if(c != null) { try { int offs = c.getCaretPosition(); int begOffs = Utilities.getRowStart(c, offs); if(select) { c.moveCaretPosition(begOffs); } else { c.setCaretPosition(begOffs); } } catch(Exception e) { UIManager.getLookAndFeel().provideErrorFeedback(c); } } }
public void action() { JTextComponent c = getTextComponent(); if(c != null) { try { int offs = c.getCaretPosition(); int endOffs = Utilities.getRowEnd(c, offs); if(select) { c.moveCaretPosition(endOffs); } else { c.setCaretPosition(endOffs); } } catch(Exception e) { UIManager.getLookAndFeel().provideErrorFeedback(c); } } }
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", ""); TextEditor.this.replaceSelection(text); TextEditor.this.select(start, start + text.length()); } } catch (Exception e) { e.printStackTrace(); } }
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); TextEditor.this.replaceSelection(toReplace); TextEditor.this.select(start, start + toReplace.length()); } else { int pos = TextEditor.this.getCaretPosition(); doc.insertString(pos, text, null); } } catch (Exception e) { e.printStackTrace(); } }
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); } }
public void caretUpdate(CaretEvent evt) { JTextComponent comp = (JTextComponent)evt.getSource(); if (comp != null && highlight != null) { comp.getHighlighter().removeHighlight(highlight); highlight = null; } int pos = comp.getCaretPosition(); Element elem = Utilities.getParagraphElement(comp, pos); int start = elem.getStartOffset(); int end = elem.getEndOffset(); try { highlight = comp.getHighlighter().addHighlight(start, end, painter); } catch (BadLocationException ex) { ex.printStackTrace(); } }
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; }
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; }
@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(); } }
@Override public void actionPerformed(ActionEvent e) { JTextComponent jtc = getTextComponent(e); if (jtc != null) { try { int offs = jtc.getCaretPosition(); jtc.setSelectionEnd(offs); offs = Utilities.getPreviousWord(jtc, offs); jtc.setSelectionStart(offs); String selectedText = jtc.getSelectedText(); if (selectedText != null) { KillRing.getInstance().add(selectedText); } jtc.cut(); } catch (BadLocationException ble) { jtc.getToolkit().beep(); } } }
@Override public void actionPerformed(ActionEvent e) { JTextComponent jtc = getTextComponent(e); if (jtc != null) { try { int start = jtc.getCaretPosition(); int end = Utilities.getRowEnd(jtc, start); if ((start == end) && jtc.isEditable()) { Document doc = jtc.getDocument(); doc.remove(end, 1); } else { jtc.setSelectionStart(start); jtc.setSelectionEnd(end); String selectedText = jtc.getSelectedText(); if (selectedText != null) { KillRing.getInstance().add(selectedText); } jtc.cut(); // jtc.replaceSelection(""); } } catch (BadLocationException ble) { jtc.getToolkit().beep(); } } }
/** * 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; }
public void actionPerformed(ActionEvent e) { JTextComponent jtc = getTextComponent(e); if (jtc != null) { try { int start = jtc.getCaretPosition(); int end = Utilities.getRowEnd(jtc, start); if (start == end && jtc.isEditable()) { Document doc = jtc.getDocument(); doc.remove(end, 1); } else { jtc.setSelectionStart(start); jtc.setSelectionEnd(end); KillRing.getInstance().add(jtc.getSelectedText()); jtc.cut(); // jtc.replaceSelection(""); } } catch (BadLocationException ble) { jtc.getToolkit().beep(); } } }
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()); }
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); }
/** * 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(); int offset = 0; 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; offset += length; tokens = tokens.next; } return x; }
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); } }
private int drawColoredText(Graphics g, int x, int y, TabExpander ex, Document doc, int p0, int p1, Element elem) throws BadLocationException { final Segment s = new Segment(); doc.getText(p0, p1 - p0, s); g.setColor(getColor(elem)); final Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); return Utilities.drawTabbedText(s, x, y, g, ex, p0); }
public int getColumn(int pos,JTextArea textPane1) { try { return pos-Utilities.getRowStart(textPane1, pos)+1; } catch (BadLocationException evt) {evt.printStackTrace(); } return -1; }
@Override public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { int offs = textArea.getCaretPosition(); int endOffs = 0; try { if (textArea.getLineWrap()) { // Must check per character, since one logical line may be // many physical lines. // FIXME: Replace Utilities call with custom version to // cut down on all of the modelToViews, as each call causes // a getTokenList => expensive! endOffs = Utilities.getRowEnd(textArea, offs); } else { Element root = textArea.getDocument().getDefaultRootElement(); int line = root.getElementIndex(offs); endOffs = root.getElement(line).getEndOffset() - 1; } if (select) { textArea.moveCaretPosition(endOffs); } else { textArea.setCaretPosition(endOffs); } } catch (Exception ex) { UIManager.getLookAndFeel().provideErrorFeedback(textArea); } }
@Override public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { int offs = textArea.getCaretPosition(); int oldOffs = offs; Element curPara = Utilities.getParagraphElement(textArea, offs); try { offs = getNextWord(textArea, offs); if(offs >= curPara.getEndOffset() && oldOffs != curPara.getEndOffset() - 1) { // we should first move to the end of current paragraph // http://bugs.sun.com/view_bug.do?bug_id=4278839 offs = curPara.getEndOffset() - 1; } } catch (BadLocationException ble) { int end = textArea.getDocument().getLength(); if (offs != end) { if(oldOffs != curPara.getEndOffset() - 1) { offs = curPara.getEndOffset() - 1; } else { offs = end; } } } if (select) { textArea.moveCaretPosition(offs); } else { textArea.setCaretPosition(offs); } }
@Override public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { int offs = textArea.getCaretPosition(); boolean failed = false; try { Element curPara = Utilities.getParagraphElement(textArea, offs); offs = getPreviousWord(textArea, offs); if(offs < curPara.getStartOffset()) { offs = Utilities.getParagraphElement(textArea, offs). getEndOffset() - 1; } } catch (BadLocationException bl) { if (offs != 0) { offs = 0; } else { failed = true; } } if (!failed) { if (select) { textArea.moveCaretPosition(offs); } else { textArea.setCaretPosition(offs); } } else { UIManager.getLookAndFeel().provideErrorFeedback(textArea); } }
public static int getRow(int pos, JTextPane editor) { int rn = (pos == 0) ? 1 : 0; try { int offs = pos; while (offs > 0) { offs = Utilities.getRowStart(editor, offs) - 1; rn++; } } catch (BadLocationException e) { } return rn; }
public static int getColumn(int pos, JTextPane editor) { try { return pos - Utilities.getRowStart(editor, pos) + 1; } catch (BadLocationException e) { } return -1; }
public static int getPos(int line, int column, JTextPane editor) { // return editor.getDocument().getDefaultRootElement().getElement(line).getStartOffset() + column; try { int off=0; int rn = 0; while( rn<line-1) { off=Utilities.getRowEnd(editor, off)+1; rn++; } return off + column-1; } catch (BadLocationException e) { } return 0; }
private int getCurrentCaretPosition() { int rowNum=0; try { int caretPos = PrincipalWindow.interpreter.getCaretPosition(); rowNum = (caretPos == 0) ? 1 : 0; for (int offset = caretPos; offset > 0;) { offset = Utilities.getRowStart(PrincipalWindow.interpreter, offset) - 1; rowNum++; } } catch (BadLocationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return rowNum; }
/** * 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; }
protected int getWordStart(JTextComponent c, int pos) { try { return Utilities.getWordStart(c, pos); } catch(Exception e) { } return -1; }
protected int getWordEnd(JTextComponent c, int pos) { try { return Utilities.getWordEnd(c, pos); } catch(Exception e) { } return -1; }
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; }