⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rscrollpane.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
/*
 * 11/14/2003
 *
 * RScrollPane.java - An extension of JScrollPane that adds right-click popup
 *                    menus on the scrollbars for scrolling.
 * Copyright (C) 2003 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * 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.fife.ui;

import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;
import java.util.ResourceBundle;
import javax.swing.*;


/**
 * An extension of <code>JScrollPane</code> that adds popup menus on its
 * scrollbars, allowing the user to scroll, page over, etc.
 *
 * @author Robert Futrell
 * @version 1.0
 */
public class RScrollPane extends JScrollPane implements ActionListener,
												Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4469810277612581366L;

	private JPopupMenu verticalScrollBarMenu;
	private JPopupMenu horizontalScrollBarMenu;

	private ResourceBundle bundle;

	// Mouse position RELATIVE TO THE SCROLLBAR when the user opens a
	// scrollbar's popup menu.
	private int mouseX, mouseY;	

	private static final String BUNDLE_NAME	= "org.fife.ui.RScrollPane";


/*****************************************************************************/


	/**
	 * Creates an <code>RScrollPane</code> with no view.  Note that if you use
	 * this constructor, you should then call <code>setViewportView</code>.
	 */
	public RScrollPane() {
		initialize();
	}


/*****************************************************************************/


	/**
	 * Creates an <code>RScrollPane</code> with the specified view.
	 *
	 * @param view The component this scrollpane contains.
	 */
	public RScrollPane(Component view) {
		super(view);
		initialize();
	}


/*****************************************************************************/


	/**
	 * Creates an <code>RScrollPane</code> with the specified size and
	 * specified view.
	 *
	 * @param width The preferred width of the scrollpane.
	 * @param height The preferred height of the scrollpane.
	 * @param view The component this scrollpane contains.
	 */
	public RScrollPane(int width, int height, Component view) {
		super(view);
		setPreferredSize(new Dimension(width, height));
		initialize();
	}


/*****************************************************************************/


	/**
	 * Listens for scrollbars' popup menus' actions.
	 *
	 * @param e The action event.
	 */
	public void actionPerformed(ActionEvent e) {

		String actionCommand = e.getActionCommand();

		// Scroll to the position the mouse points to horizontally.
		if (actionCommand.equals("ScrollHereHorizontal")) {

			int width = getHorizontalScrollBar().getWidth();
			float howFarIn = ((float)mouseX) / ((float)width);

			BoundedRangeModel brm = getHorizontalScrollBar().getModel();
			int newPosition = java.lang.Math.round(howFarIn * (brm.getMaximum() - brm.getMinimum())) - brm.getExtent()/2;
			brm.setValue(newPosition);

	       }

		// Scroll to the position the mouse points to vertically.
		else if (actionCommand.equals("ScrollHereVertical")) {

			int height = getVerticalScrollBar().getHeight();
			float howFarIn = ((float)mouseY) / ((float)height);

			BoundedRangeModel brm = getVerticalScrollBar().getModel();
			int newPosition = java.lang.Math.round(howFarIn * (brm.getMaximum() - brm.getMinimum())) - brm.getExtent()/2;
			brm.setValue(newPosition);

		}

		// Scroll to the top.
		else if (actionCommand.equals("Top")) {
			BoundedRangeModel brm = getVerticalScrollBar().getModel();
			brm.setValue(0);
		}

		// Scroll to the bottom.
		else if (actionCommand.equals("Bottom")) {
			BoundedRangeModel brm = getVerticalScrollBar().getModel();
			brm.setValue(brm.getMaximum());
		}

		// Scroll one page up in the document.
		else if (actionCommand.equals("PageUp")) {
			JViewport viewport = getViewport();
			Point p = viewport.getViewPosition();
			int viewportHeight = viewport.getExtentSize().height;
			p.translate(0, -viewportHeight);
			if (p.getY()<0)
				p.setLocation(p.getX(), 0);
			viewport.setViewPosition(p);
	       }

		// Scroll one page down in the document.
		else if (actionCommand.equals("PageDown")) {
			JViewport viewport = getViewport();
			Point p = viewport.getViewPosition();
			int viewportHeight = viewport.getExtentSize().height;
			Component view = viewport.getView();
			double tempY = p.getY() + viewportHeight;
			if (view.getHeight() >= tempY + viewportHeight)
				p.setLocation(p.getX(), tempY);
			else
				p.setLocation(p.getX(), view.getHeight() - viewportHeight);
			viewport.setViewPosition(p);
		}

		// Scroll one unit up in the document.
		else if (actionCommand.equals("ScrollUp")) {
			JViewport viewport = getViewport();
			Point p = viewport.getViewPosition();
			int unitIncrement = getVerticalScrollBar().getUnitIncrement(-1);
			if (p.getY() > unitIncrement) {
				p.translate(0, -unitIncrement);
				viewport.setViewPosition(p);
			}
			else {
				p.setLocation(p.getX(), 0);
				viewport.setViewPosition(p);
			}
		}

		// Scroll one unit down in the document.
		else if (actionCommand.equals("ScrollDown")) {
			JViewport viewport = getViewport();
			Point p = viewport.getViewPosition();
			int unitIncrement = getVerticalScrollBar().getUnitIncrement(1);
			Component view = viewport.getView();
			if (p.getY() < view.getHeight() - viewport.getHeight() - unitIncrement) {
				p.translate(0, unitIncrement);
				viewport.setViewPosition(p);
			}
			else {
				p.setLocation(p.getX(), view.getHeight() - viewport.getHeight());
				viewport.setViewPosition(p);
			}
		}

		// Scroll all the way to the left.
		else if (actionCommand.equals("LeftEdge")) {
			BoundedRangeModel brm = getHorizontalScrollBar().getModel();
			brm.setValue(0);
		}

		// Scroll all the way to the right.
		else if (actionCommand.equals("RightEdge")) {
			BoundedRangeModel brm = getHorizontalScrollBar().getModel();
			brm.setValue(brm.getMaximum());
		}

		// Scroll one page to the left.
		else if (actionCommand.equals("PageLeft")) {
			JViewport viewport = getViewport();
			Point p = viewport.getViewPosition();
			int viewportWidth = viewport.getExtentSize().width;
			p.translate(-viewportWidth, 0);
			if (p.getX()<0)
				p.setLocation(0, p.getY());
			viewport.setViewPosition(p);
		}

		// Scroll one page to the right.
		else if (actionCommand.equals("PageRight")) {
			JViewport viewport = getViewport();
			Point p = viewport.getViewPosition();
			Component view = viewport.getView();
			int viewportWidth = viewport.getExtentSize().width;
			p.translate(viewportWidth, 0);
			if (p.getX() > view.getWidth()-viewportWidth)
				p.setLocation(view.getWidth()-viewportWidth, p.getY());
			viewport.setViewPosition(p);
		}

		// Scroll one element (pixel) to the left.
		else if (actionCommand.equals("ScrollLeft")) {
			JViewport viewport = getViewport();
			Point p = viewport.getViewPosition();
			int unitIncrement = getHorizontalScrollBar().getUnitIncrement(-1);
			if (p.getX() > unitIncrement) {
				p.translate(-unitIncrement, 0);
				viewport.setViewPosition(p);
			}
			else {
				p.setLocation(0, p.getY());
				viewport.setViewPosition(p);
			}
		}

		// Scroll one element (pixel) to the right.
		else if (actionCommand.equals("ScrollRight")) {
			JViewport viewport = getViewport();
			Point p = viewport.getViewPosition();
			int unitIncrement = getHorizontalScrollBar().getUnitIncrement(1);
			Component view = viewport.getView();
			if (p.getX() < view.getWidth() - viewport.getWidth() - unitIncrement) {
				p.translate(unitIncrement, 0);
				viewport.setViewPosition(p);
			}
			else {
				p.setLocation(view.getWidth() - viewport.getWidth(), p.getY());
				viewport.setViewPosition(p);
			}
		}

	}


/*****************************************************************************/


	/**
	 * Adds a menu item to a menu.
	 *
	 * @param key A key into this scroll pane's resource bundle for the menu
	 *        item's value.
	 * @param actionCommand The command sent to the listener (the scroll pane).
	 * @param menu The popup menu to which to add the item.
	 */
	private final void addMenuItem(String key, String actionCommand,
							JPopupMenu menu) {
		JMenuItem item = new JMenuItem(bundle.getString(key));
		item.setActionCommand(actionCommand);
		item.addActionListener(this);
		menu.add(item);
	}


/*****************************************************************************/


	/**
	 * Sets up horizontal scrollbar's popup menu.
	 */
	private void createHorizontalScrollBarMenu() {
		horizontalScrollBarMenu = new JPopupMenu();
		addMenuItem("ScrollHere", "ScrollHereHorizontal",
					horizontalScrollBarMenu);
		horizontalScrollBarMenu.addSeparator();
		addMenuItem("LeftEdge", "LeftEdge", horizontalScrollBarMenu);
		addMenuItem("RightEdge", "RightEdge", horizontalScrollBarMenu);
		horizontalScrollBarMenu.addSeparator();
		addMenuItem("PageLeft", "PageLeft", horizontalScrollBarMenu);
		addMenuItem("PageRight", "PageRight", horizontalScrollBarMenu);
		horizontalScrollBarMenu.addSeparator();
		addMenuItem("ScrollLeft","ScrollLeft",horizontalScrollBarMenu);
		addMenuItem("ScrollRight", "ScrollRight", horizontalScrollBarMenu);
	}


/*****************************************************************************/


	/**
	 * Sets up vertical scrollbar's popup menu.
	 */
	private void createVerticalScrollBarMenu() {
		verticalScrollBarMenu = new JPopupMenu();
		addMenuItem("ScrollHere","ScrollHereVertical",verticalScrollBarMenu);
		verticalScrollBarMenu.addSeparator();
		addMenuItem("Top", "Top", verticalScrollBarMenu);
		addMenuItem("Bottom", "Bottom", verticalScrollBarMenu);
		verticalScrollBarMenu.addSeparator();
		addMenuItem("PageUp", "PageUp", verticalScrollBarMenu);
		addMenuItem("PageDown", "PageDown", verticalScrollBarMenu);
		verticalScrollBarMenu.addSeparator();
		addMenuItem("ScrollUp", "ScrollUp", verticalScrollBarMenu);
		addMenuItem("ScrollDown", "ScrollDown", verticalScrollBarMenu);
	}


/*****************************************************************************/


	private void initialize() {

		// Set miscellaneous properties.
//		setBorder(BorderFactory.createEtchedBorder());

		// Create scrollbars' popup menus.
		MouseListener popupListener = new PopupListener();
		getVerticalScrollBar().addMouseListener(popupListener);
		getHorizontalScrollBar().addMouseListener(popupListener);

		// This is a hack so background picture is updated whenever user
		// uses scrollbars.
//		getViewport().addChangeListener( new ChangeListener() {
//			public void stateChanged(ChangeEvent e) {
//				textArea.repaint();
//			}
//		} );

	}


/*****************************************************************************/


	/**
	 * Loads the resource bundle for the popup menus, if necessary.
	 */
	private synchronized void maybeLoadResourceBundle() {
		if (bundle==null)
			bundle = ResourceBundle.getBundle(BUNDLE_NAME);
	}


/*****************************************************************************/


	/**
	 * Resets the UI property with a value from the current look and feel.
	 * This overrides <code>JComponent</code>'s <code>updateUI</code> method,
	 * so that the popup menus are updated as well.
	 */
	public void updateUI() {
		super.updateUI();
		if (verticalScrollBarMenu!=null)
			SwingUtilities.updateComponentTreeUI(verticalScrollBarMenu);
		if (horizontalScrollBarMenu!=null)
			SwingUtilities.updateComponentTreeUI(horizontalScrollBarMenu);
	}


/*****************************************************************************/
/************************** INNER CLASSES ************************************/
/*****************************************************************************/


	/**
	 * Class to listen for, and respond do, popup menu requests.
	 */
	class PopupListener extends MouseAdapter {

		public void mousePressed(MouseEvent e) {
			maybeShowPopup(e);
		}

		public void mouseReleased(MouseEvent e) {
			maybeShowPopup(e);
		}

		private void maybeShowPopup(MouseEvent e) {

			if (e.isPopupTrigger()) {

				mouseX = e.getX();
				mouseY = e.getY();

				maybeLoadResourceBundle();

				if (e.getComponent().equals(getVerticalScrollBar())) {
					if (verticalScrollBarMenu==null)
						createVerticalScrollBarMenu();
					verticalScrollBarMenu.show(getVerticalScrollBar(),
											mouseX,mouseY);
				}
				else {
					if (horizontalScrollBarMenu==null)
						createHorizontalScrollBarMenu();
					horizontalScrollBarMenu.show(getHorizontalScrollBar(),
											mouseX,mouseY);
				}

			}

		}

	}


/*****************************************************************************/

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -