setRTextAreaUI(createRTextAreaUI()); enableEvents(AWTEvent.COMPONENT_EVENT_MASK|AWTEvent.KEY_EVENT_MASK); setHighlightCurrentLine(true); setCurrentLineHighlightColor(getDefaultCurrentLineHighlightColor()); setMarginLineEnabled(false); setMarginLineColor(getDefaultMarginLineColor()); setMarginLinePosition(getDefaultMarginLinePosition()); setBackgroundObject(Color.WHITE); setWrapStyleWord(true);// We only support wrapping at word boundaries. setTabSize(5); setForeground(Color.BLACK); setTabsEmulated(false); previousCaretY = currentCaretY = getInsets().top; mouseListener = createMouseListener(); addFocusListener(mouseListener); addCurrentLineHighlightListeners();
MouseMotionListener[] mouseMotionListeners = getMouseMotionListeners(); for (int i=0; i<mouseMotionListeners.length; i++) { if (mouseMotionListeners[i]==mouseListener) { addMouseMotionListener(mouseListener); MouseListener[] mouseListeners = getMouseListeners(); for (int i=0; i<mouseListeners.length; i++) { if (mouseListeners[i]==mouseListener) { addMouseListener(mouseListener);
/** * Rescales the displayed image to be the specified size. * * @param width The new width of the image. * @param height The new height of the image. * @param hint The scaling hint to use. */ @Override protected void rescaleImage(int width, int height, int hint) { bgImage = getRTextAreaBase().createVolatileImage(width, height); if (bgImage!=null) { renderImage(width, height, hint); } }
/** * Sets whether or not the current line is highlighted. This method * fires a property change of type {@link #HIGHLIGHT_CURRENT_LINE_PROPERTY}. * * @param highlight Whether or not to highlight the current line. * @see #getHighlightCurrentLine() * @see #getCurrentLineHighlightColor * @see #setCurrentLineHighlightColor */ public void setHighlightCurrentLine(boolean highlight) { if (highlight!=highlightCurrentLine) { highlightCurrentLine = highlight; firePropertyChange(HIGHLIGHT_CURRENT_LINE_PROPERTY, !highlight, highlight); repaint(); // Repaint entire width of line. } }
/** * Sets whether the current line highlight should have a "fade" effect. * This method fires a property change event of type * <code>CURRENT_LINE_HIGHLIGHT_FADE_PROPERTY</code>. * * @param fade Whether the fade effect should be enabled. * @see #getFadeCurrentLineHighlight */ public void setFadeCurrentLineHighlight(boolean fade) { if (fade!=fadeCurrentLineHighlight) { fadeCurrentLineHighlight = fade; if (getHighlightCurrentLine()) { forceCurrentLineHighlightRepaint(); } firePropertyChange(CURRENT_LINE_HIGHLIGHT_FADE_PROPERTY, !fade, fade); } }
/** * Forces the current line highlight to be repainted. This hack is * necessary for those situations when the view (appearance) changes * but the caret's location hasn't (and thus the current line highlight * coordinates won't get changed). Examples of this are when * word wrap is toggled and when syntax styles are changed in an * <code>RSyntaxTextArea</code>. */ protected void forceCurrentLineHighlightRepaint() { // Check isShowing() to prevent BadLocationException // in constructor if linewrap is set to true. if (isShowing()) { // Changing previousCaretY makes us sure to get a repaint. previousCaretY = -1; // Trick it into checking for the need to repaint by firing // a false caret event. fireCaretUpdate(mouseListener); } }
/** * Sets the background color of this text editor. Note that this is * equivalent to calling <code>setBackgroundObject(bg)</code>.<p> * * NOTE: the opaque property is set to <code>true</code> when the * background is set to a color with 1.0 alpha (by this method). When an * image is used for the background, opaque is set to false. This is * because we perform better when setOpaque is true, but if we use an * image for the background when opaque is true, we get on-screen * garbage when the user scrolls via the arrow keys. Thus we * need setOpaque to be false in that case.<p> * You never have to change the opaque property yourself; it is always done * for you. * * @param bg The color to use as the background color. */ @Override public void setBackground(Color bg) { Object oldBG = getBackgroundObject(); if (oldBG instanceof Color) { // Just change color of strategy. ((ColorBackgroundPainterStrategy)backgroundPainter). setColor(bg); } else { // Was an image painter... backgroundPainter = new ColorBackgroundPainterStrategy(bg); } setOpaque(bg==null || bg.getAlpha()==0xff); firePropertyChange("background", oldBG, bg); repaint(); }
/** * Overridden so we can tell when the text area is resized and update the * current-line highlight, if necessary (i.e., if it is enabled and if * lineWrap is enabled. * * @param e The component event about to be sent to all registered * <code>ComponentListener</code>s. */ @Override protected void processComponentEvent(ComponentEvent e) { // In line wrap mode, resizing the text area means that the caret's // "line" could change - not to a different logical line, but a // different physical one. So, here we force a repaint of the current // line's highlight if necessary. if (e.getID()==ComponentEvent.COMPONENT_RESIZED && getLineWrap() && getHighlightCurrentLine()) { previousCaretY = -1; // So we are sure to repaint. fireCaretUpdate(mouseListener); } super.processComponentEvent(e); }
/** * Sets whether the edges of selections are rounded in this text area. * This method fires a property change of type * {@link #ROUNDED_SELECTION_PROPERTY}. * * @param rounded Whether selection edges should be rounded. * @see #getRoundedSelectionEdges() */ public void setRoundedSelectionEdges(boolean rounded) { if (roundedSelectionEdges!=rounded) { roundedSelectionEdges = rounded; Caret c = getCaret(); if (c instanceof ConfigurableCaret) { ((ConfigurableCaret)c).setRoundedSelectionEdges(rounded); if (c.getDot()!=c.getMark()) { // e.g., there's is a selection repaint(); } } firePropertyChange(ROUNDED_SELECTION_PROPERTY, !rounded, rounded); } }
/** * Sets the color to use to highlight the current line. Note that if * highlighting the current line is turned off, you will not be able to * see this highlight. This method fires a property change of type * {@link #CURRENT_LINE_HIGHLIGHT_COLOR_PROPERTY}. * * @param color The color to use to highlight the current line. * @throws NullPointerException if <code>color</code> is <code>null</code>. * @see #getHighlightCurrentLine() * @see #setHighlightCurrentLine(boolean) * @see #getCurrentLineHighlightColor */ public void setCurrentLineHighlightColor(Color color) { if (color==null) { throw new NullPointerException(); } if (!color.equals(currentLineColor)) { Color old = currentLineColor; currentLineColor = color; firePropertyChange(CURRENT_LINE_HIGHLIGHT_COLOR_PROPERTY, old, color); } }
/** * Notifies all listeners that a caret change has occurred. * * @param e The caret event. */ @Override protected void fireCaretUpdate(CaretEvent e) { // Decide whether we need to repaint the current line background. possiblyUpdateCurrentLineHighlightLocation(); // Now, if there is a highlighted region of text, allow them to cut // and copy. if (e!=null && e.getDot()!=e.getMark()) {// && !cutAction.isEnabled()) { cutAction.setEnabled(true); copyAction.setEnabled(true); } // Otherwise, if there is no highlighted region, don't let them cut // or copy. The condition here should speed things up, because this // way, we will only enable the actions the first time the selection // becomes nothing. else if (cutAction.isEnabled()) { cutAction.setEnabled(false); copyAction.setEnabled(false); } super.fireCaretUpdate(e); }
Object oldBG = getBackgroundObject(); if (oldBG instanceof Image) { // Just change image being displayed. ((ImageBackgroundPainterStrategy)backgroundPainter). backgroundPainter = strategy; setOpaque(false); firePropertyChange(BACKGROUND_IMAGE_PROPERTY, oldBG, image); repaint();
/** * Sets whether the current line highlight should have a "fade" effect. * This method fires a property change event of type * <code>CURRENT_LINE_HIGHLIGHT_FADE_PROPERTY</code>. * * @param fade Whether the fade effect should be enabled. * @see #getFadeCurrentLineHighlight */ public void setFadeCurrentLineHighlight(boolean fade) { if (fade!=fadeCurrentLineHighlight) { fadeCurrentLineHighlight = fade; if (getHighlightCurrentLine()) forceCurrentLineHighlightRepaint(); firePropertyChange(CURRENT_LINE_HIGHLIGHT_FADE_PROPERTY, !fade, fade); } }
/** * Overridden so we can tell when the text area is resized and update the * current-line highlight, if necessary (i.e., if it is enabled and if * lineWrap is enabled. * * @param e The component event about to be sent to all registered * <code>ComponentListener</code>s. */ @Override protected void processComponentEvent(ComponentEvent e) { // In line wrap mode, resizing the text area means that the caret's // "line" could change - not to a different logical line, but a // different physical one. So, here we force a repaint of the current // line's highlight if necessary. if (e.getID()==ComponentEvent.COMPONENT_RESIZED && getLineWrap() && getHighlightCurrentLine()) { previousCaretY = -1; // So we are sure to repaint. fireCaretUpdate(mouseListener); } super.processComponentEvent(e); }
/** * Sets whether the edges of selections are rounded in this text area. * This method fires a property change of type * {@link #ROUNDED_SELECTION_PROPERTY}. * * @param rounded Whether selection edges should be rounded. * @see #getRoundedSelectionEdges() */ public void setRoundedSelectionEdges(boolean rounded) { if (roundedSelectionEdges!=rounded) { roundedSelectionEdges = rounded; ConfigurableCaret cc = (ConfigurableCaret)getCaret(); cc.setRoundedSelectionEdges(rounded); if (cc.getDot()!=cc.getMark()) { // ie, if there is a selection repaint(); } firePropertyChange(ROUNDED_SELECTION_PROPERTY, !rounded, rounded); } }
/** * Sets whether or not the current line is highlighted. This method * fires a property change of type {@link #HIGHLIGHT_CURRENT_LINE_PROPERTY}. * * @param highlight Whether or not to highlight the current line. * @see #getHighlightCurrentLine() * @see #getCurrentLineHighlightColor * @see #setCurrentLineHighlightColor */ public void setHighlightCurrentLine(boolean highlight) { if (highlight!=highlightCurrentLine) { highlightCurrentLine = highlight; firePropertyChange(HIGHLIGHT_CURRENT_LINE_PROPERTY, !highlight, highlight); repaint(); // Repaint entire width of line. } }
/** * Forces the current line highlight to be repainted. This hack is * necessary for those situations when the view (appearance) changes * but the caret's location hasn't (and thus the current line highlight * coordinates won't get changed). Examples of this are when * word wrap is toggled and when syntax styles are changed in an * <code>RSyntaxTextArea</code>. */ protected void forceCurrentLineHighlightRepaint() { // Check isShowing() to prevent BadLocationException // in constructor if linewrap is set to true. if (isShowing()) { // Changing previousCaretY makes us sure to get a repaint. previousCaretY = -1; // Trick it into checking for the need to repaint by firing // a false caret event. fireCaretUpdate(mouseListener); } }
/** * Sets the color to use to highlight the current line. Note that if * highlighting the current line is turned off, you will not be able to * see this highlight. This method fires a property change of type * {@link #CURRENT_LINE_HIGHLIGHT_COLOR_PROPERTY}. * * @param color The color to use to highlight the current line. * @throws NullPointerException if <code>color</code> is <code>null</code>. * @see #getHighlightCurrentLine() * @see #setHighlightCurrentLine(boolean) * @see #getCurrentLineHighlightColor */ public void setCurrentLineHighlightColor(Color color) { if (color==null) throw new NullPointerException(); if (!color.equals(currentLineColor)) { Color old = currentLineColor; currentLineColor = color; firePropertyChange(CURRENT_LINE_HIGHLIGHT_COLOR_PROPERTY, old, color); } }
/** * Notifies all listeners that a caret change has occurred. * * @param e The caret event. */ protected void fireCaretUpdate(CaretEvent e) { // Decide whether we need to repaint the current line background. possiblyUpdateCurrentLineHighlightLocation(); // Now, if there is a highlighted region of text, allow them to cut // and copy. if (e!=null && e.getDot()!=e.getMark()) {// && !cutAction.isEnabled()) { cutAction.setEnabled(true); copyAction.setEnabled(true); } // Otherwise, if there is no highlighted region, don't let them cut // or copy. The condition here should speed things up, because this // way, we will only enable the actions the first time the selection // becomes nothing. else if (cutAction.isEnabled()==true) { cutAction.setEnabled(false); copyAction.setEnabled(false); } super.fireCaretUpdate(e); }
setRTextAreaUI(createRTextAreaUI()); enableEvents(AWTEvent.COMPONENT_EVENT_MASK|AWTEvent.KEY_EVENT_MASK); setHighlightCurrentLine(true); setCurrentLineHighlightColor(getDefaultCurrentLineHighlightColor()); setMarginLineEnabled(false); setMarginLineColor(getDefaultMarginLineColor()); setMarginLinePosition(getDefaultMarginLinePosition()); setBackgroundObject(Color.WHITE); setWrapStyleWord(true);// We only support wrapping at word boundaries. setTabSize(5); setForeground(Color.BLACK); setTabsEmulated(false); previousCaretY = currentCaretY = getInsets().top; mouseListener = createMouseListener(); addFocusListener(mouseListener); addCurrentLineHighlightListeners();