📄 gutter.java
字号:
/* * Gutter.java * :tabSize=8:indentSize=8:noTabs=false: * :folding=explicit:collapseFolds=1: * * Copyright (C) 1999, 2000 mike dillon * Portions copyright (C) 2001, 2002 Slava Pestov * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package org.gjt.sp.jedit.textarea;//{{{ Importsimport java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import javax.swing.event.*;import org.gjt.sp.jedit.*;//}}}/** * The gutter is the component that displays folding triangles and line * numbers to the left of the text area. The only methods in this class * that should be called by plugins are those for adding and removing * text area extensions. * * @see #addExtension(TextAreaExtension) * @see #addExtension(int,TextAreaExtension) * @see #removeExtension(TextAreaExtension) * @see TextAreaExtension * @see JEditTextArea * * @author Mike Dillon and Slava Pestov * @version $Id: Gutter.java,v 1.29 2003/01/14 02:09:23 spestov Exp $ */public class Gutter extends JComponent implements SwingConstants{ //{{{ Layers /** * The lowest possible layer. * @see #addExtension(int,TextAreaExtension) * @since jEdit 4.0pre4 */ public static final int LOWEST_LAYER = Integer.MIN_VALUE; /** * Default extension layer. This is above the wrap guide but below the * bracket highlight. * @since jEdit 4.0pre4 */ public static final int DEFAULT_LAYER = 0; /** * Highest possible layer. * @since jEdit 4.0pre4 */ public static final int HIGHEST_LAYER = Integer.MAX_VALUE; //}}} //{{{ Gutter constructor public Gutter(View view, JEditTextArea textArea) { this.view = view; this.textArea = textArea; setAutoscrolls(true); setOpaque(true); extensionMgr = new ExtensionManager(); MouseHandler ml = new MouseHandler(); addMouseListener(ml); addMouseMotionListener(ml); addExtension(new MarkerHighlight()); } //}}} //{{{ paintComponent() method public void paintComponent(Graphics _gfx) { Graphics2D gfx = (Graphics2D)_gfx; // fill the background Rectangle clip = gfx.getClipBounds(); gfx.setColor(getBackground()); gfx.fillRect(clip.x, clip.y, clip.width, clip.height); // if buffer is loading, don't paint anything if (!textArea.getBuffer().isLoaded()) return; int lineHeight = textArea.getPainter().getFontMetrics() .getHeight(); int firstLine = clip.y / lineHeight; int lastLine = (clip.y + clip.height - 1) / lineHeight; int y = (clip.y - clip.y % lineHeight); textArea.chunkCache.updateChunksUpTo(lastLine); for (int line = firstLine; line <= lastLine; line++, y += lineHeight) { paintLine(gfx,line,y); } } //}}} //{{{ addExtension() method /** * Adds a text area extension, which can perform custom painting and * tool tip handling. * @param extension The extension * @since jEdit 4.0pre4 */ public void addExtension(TextAreaExtension extension) { extensionMgr.addExtension(DEFAULT_LAYER,extension); repaint(); } //}}} //{{{ addExtension() method /** * Adds a text area extension, which can perform custom painting and * tool tip handling. * @param layer The layer to add the extension to. Note that more than * extension can share the same layer. * @param extension The extension * @since jEdit 4.0pre4 */ public void addExtension(int layer, TextAreaExtension extension) { extensionMgr.addExtension(layer,extension); repaint(); } //}}} //{{{ removeExtension() method /** * Removes a text area extension. It will no longer be asked to * perform custom painting and tool tip handling. * @param extension The extension * @since jEdit 4.0pre4 */ public void removeExtension(TextAreaExtension extension) { extensionMgr.removeExtension(extension); repaint(); } //}}} //{{{ getExtensions() method /** * Returns an array of registered text area extensions. Useful for * debugging purposes. * @since jEdit 4.1pre5 */ public TextAreaExtension[] getExtensions() { return extensionMgr.getExtensions(); } //}}} //{{{ getToolTipText() method /** * Returns the tool tip to display at the specified location. * @param evt The mouse event */ public String getToolTipText(MouseEvent evt) { if(!textArea.getBuffer().isLoaded()) return null; return extensionMgr.getToolTipText(evt.getX(),evt.getY()); } //}}} //{{{ setBorder() method /** * Convenience method for setting a default matte border on the right * with the specified border width and color * @param width The border width (in pixels) * @param color1 The focused border color * @param color2 The unfocused border color * @param color3 The gutter/text area gap color */ public void setBorder(int width, Color color1, Color color2, Color color3) { this.borderWidth = width; focusBorder = new CompoundBorder(new MatteBorder(0,0,0,width,color3), new MatteBorder(0,0,0,width,color1)); noFocusBorder = new CompoundBorder(new MatteBorder(0,0,0,width,color3), new MatteBorder(0,0,0,width,color2)); updateBorder(); } //}}} //{{{ updateBorder() method /** * Sets the border differently if the text area has focus or not. */ public void updateBorder() { // because we are called from the text area's focus handler, // we do an invokeLater() so that the view's focus handler // has a chance to execute and set the edit pane properly SwingUtilities.invokeLater(new Runnable() { public void run() { if(view.getEditPane() == null) return; if(view.getEditPane().getTextArea() == textArea) setBorder(focusBorder); else setBorder(noFocusBorder); } }); } //}}} //{{{ setBorder() method /* * JComponent.setBorder(Border) is overridden here to cache the left * inset of the border (if any) to avoid having to fetch it during every * repaint. */ public void setBorder(Border border) { super.setBorder(border); if (border == null) { collapsedSize.width = 0; collapsedSize.height = 0; } else { Insets insets = border.getBorderInsets(this); collapsedSize.width = FOLD_MARKER_SIZE + insets.right; collapsedSize.height = gutterSize.height = insets.top + insets.bottom; gutterSize.width = FOLD_MARKER_SIZE + insets.right + fm.stringWidth("12345"); } revalidate(); } //}}} //{{{ setFont() method /* * JComponent.setFont(Font) is overridden here to cache the baseline for * the font. This avoids having to get the font metrics during every * repaint. */ public void setFont(Font font) { super.setFont(font); fm = getFontMetrics(font); baseline = fm.getAscent(); Border border = getBorder(); if(border != null) { gutterSize.width = FOLD_MARKER_SIZE + border.getBorderInsets(this).right + fm.stringWidth("12345"); revalidate(); } } //}}} //{{{ Getters and setters //{{{ getHighlightedForeground() method /** * Get the foreground color for highlighted line numbers * @return The highlight color */ public Color getHighlightedForeground() { return intervalHighlight; } //}}} //{{{ setHighlightedForeground() method public void setHighlightedForeground(Color highlight) { intervalHighlight = highlight; } //}}} //{{{ getCurrentLineForeground() method public Color getCurrentLineForeground() { return currentLineHighlight; } //}}} //{{{ setCurrentLineForeground() method public void setCurrentLineForeground(Color highlight) { currentLineHighlight = highlight; } //}}} //{{{ getFoldColor() method public Color getFoldColor() { return foldColor; } //}}} //{{{ setFoldColor() method public void setFoldColor(Color foldColor) { this.foldColor = foldColor; } //}}} //{{{ getPreferredSize() method /* * Component.getPreferredSize() is overridden here to support the * collapsing behavior. */ public Dimension getPreferredSize() { if (expanded) return gutterSize; else return collapsedSize; } //}}} //{{{ getMinimumSize() method public Dimension getMinimumSize() { return getPreferredSize(); } //}}} //{{{ getLineNumberAlignment() method /** * Identifies whether the horizontal alignment of the line numbers. * @return Gutter.RIGHT, Gutter.CENTER, Gutter.LEFT */ public int getLineNumberAlignment() { return alignment; } //}}} //{{{ setLineNumberAlignment() method /** * Sets the horizontal alignment of the line numbers. * @param alignment Gutter.RIGHT, Gutter.CENTER, Gutter.LEFT */ public void setLineNumberAlignment(int alignment) { if (this.alignment == alignment) return; this.alignment = alignment; repaint(); } //}}} //{{{ isExpanded() method /** * Identifies whether the gutter is collapsed or expanded. * @return true if the gutter is expanded, false if it is collapsed */ public boolean isExpanded() { return expanded; } //}}} //{{{ setExpanded() method /** * Sets whether the gutter is collapsed or expanded and force the text * area to update its layout if there is a change. * @param collapsed true if the gutter is expanded, * false if it is collapsed */ public void setExpanded(boolean expanded) { if (this.expanded == expanded) return; this.expanded = expanded; textArea.revalidate(); } //}}} //{{{ toggleExpanded() method /** * Toggles whether the gutter is collapsed or expanded. */ public void toggleExpanded() { setExpanded(!expanded); } //}}} //{{{ getHighlightInterval() method /** * Sets the number of lines between highlighted line numbers. * @return The number of lines between highlighted line numbers or * zero if highlighting is disabled */ public int getHighlightInterval() { return interval; } //}}} //{{{ setHighlightInterval() method /** * Sets the number of lines between highlighted line numbers. Any value * less than or equal to one will result in highlighting being disabled. * @param interval The number of lines between highlighted line numbers */ public void setHighlightInterval(int interval) { if (interval <= 1) interval = 0; this.interval = interval; repaint(); } //}}} //{{{ isCurrentLineHighlightEnabled() method public boolean isCurrentLineHighlightEnabled() { return currentLineHighlightEnabled; } //}}} //{{{ setCurrentLineHighlightEnabled() method public void setCurrentLineHighlightEnabled(boolean enabled) { if (currentLineHighlightEnabled == enabled) return; currentLineHighlightEnabled = enabled; repaint(); } //}}} //{{{ getBracketHighlightColor() method /** * Returns the bracket highlight color. */ public final Color getBracketHighlightColor() { return bracketHighlightColor; } //}}} //{{{ setBracketHighlightColor() method /** * Sets the bracket highlight color. * @param bracketHighlightColor The bracket highlight color * @since jEdit 4.0pre1 */ public final void setBracketHighlightColor(Color bracketHighlightColor) { this.bracketHighlightColor = bracketHighlightColor; repaint(); } //}}} //{{{ isBracketHighlightEnabled() method /** * Returns true if bracket highlighting is enabled, false otherwise.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -