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

📄 listview.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
/*
 * 07/14/2004
 *
 * ListView.java - The "List view" for an RTextFileChooser.
 * Copyright (C) 2004 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.rtextfilechooser;

import java.awt.Component;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Vector;
import javax.swing.*;


/**
 * The List View for an <code>RTextFileChooser</code>.  This is similar to the
 * List View found in Microsoft Windows file choosers.
 *
 * @author Robert Futrell
 * @version 0.1
 */
class ListView extends JList implements RTextFileChooserView {

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

	private RTextFileChooser chooser;	// The chooser that owns this list view.

	// Listeners.
	private MouseListener mouseListener;
	private SelectionListener selectionListener;


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


	/**
	 * Constructor.
	 *
	 * @param chooser The file chooser that owns this list view.
	 */
	public ListView(RTextFileChooser chooser) {

		super(new DefaultListModel()); // Ensure we have a DefaultListModel.
		enableEvents(java.awt.AWTEvent.KEY_EVENT_MASK);
		this.chooser = chooser;

		// Just some other stuff to keep things looking nice.
		setCellRenderer(new ListViewCellRenderer());
		setLayoutOrientation(JList.VERTICAL_WRAP);
		addComponentListener(new ComponentAdapter() {
						public void componentResized(ComponentEvent e) {
							setVisibleRowCount(-1);
						}
						});


		// Add any listeners.
		mouseListener = new MouseListener(chooser);
		addMouseListener(mouseListener);
		selectionListener = new SelectionListener(chooser);
		addListSelectionListener(selectionListener);

	}


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


	/**
	 * Clears all files displayed by this view.
	 */
	public void clearDisplayedFiles() {
		setListData(new File[0]); // Must do this so the file list gets erased.
	}


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


	/**
	 * Makes sure the specified file is visible in the view.
	 *
	 * @param file The file that is to be visible.
	 */
	public void ensureFileIsVisible(File file) {

		// This will always be true because we explicitly set the
		// model below.
		DefaultListModel model = (DefaultListModel)getModel();

		int index = model.indexOf(file);
		if (index!=-1)
			ensureIndexIsVisible(index);

	}


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


	/**
	 * Returns the number of files currently being displayed.
	 *
	 * @return The number of files currently being displayed.
	 */
	public int getDisplayedFileCount() {
		return getModel().getSize();
	}


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


	/**
	 * Returns the file at the specified point in the view.
	 *
	 * @param p The point at which to look for a file.
	 * @return The file at that point (or <code>null</code> if there isn't
	 *         one???).
	 */
	public File getFileAtPoint(Point p) {
		int row = locationToIndex(p);
		return (File)getModel().getElementAt(row);
	}


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


	/**
	 * Gets the selected file, for use when a single file is selected.
	 *
	 * @return The selected file, or <code>null</code> if no file is
	 *         selected.
	 */
	public File getSelectedFile() {
		return (File)getSelectedValue();
	}


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


	/**
	 * Returns all selected files in this view.
	 *
	 * @return An array of all selected files.
	 */
	public File[] getSelectedFiles() {

		Object[] objArray = getSelectedValues();
		int length = objArray.length;

		File[] fileArray = new File[length];
		System.arraycopy(objArray,0, fileArray,0, length);

		return fileArray;

	}


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


	/**
	 * Returns the tool tip to display for a given mouse event.
	 *
	 * @param e The mouse event.
	 * @return The tool tip.
	 */
	public String getToolTipText(MouseEvent e) {
		String tip = null;
		Point p = e.getPoint();
		int index = locationToIndex(p);
		if (index==-1)
			return null;
		Rectangle bounds = getCellBounds(index, index);
		if (bounds.contains(p)) {
			File file = (File)getModel().getElementAt(index);
			if (file==null || file.isDirectory())
				return null;
			tip = chooser.getToolTipFor(file);
		}
		return tip;
	}


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


	/**
	 * Process key presses in this list view.  This is overridden so that
	 * pressing the delete key triggers a delete of the selected file(s).
	 *
	 * @param e The key event.
	 */
	protected void processKeyEvent(KeyEvent e) {
		switch (e.getKeyCode()) {
			case KeyEvent.VK_DELETE:
				if (e.getID()==KeyEvent.KEY_PRESSED)
					chooser.handleDelete();
				// Fall through.
			default:
				super.processKeyEvent(e);
		}
	}


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


	/**
	 * Removes all listeners this view has created and added to itself.  This
	 * method is here to get around the fact that <code>finalize</code> is
	 * not going to be called as long as listeners are still registered for
	 * this view, but nobody else knows about these listeners except for the
	 * view.
	 */
	public void removeAllListeners() {
		removeMouseListener(mouseListener);
		removeListSelectionListener(selectionListener);
	}


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


	/**
	 * Selects the file at the specified point in the view.  If no file
	 * exists at that point, the selection should be cleared.
	 *
	 * @param p The point at which a file should be selected.
	 */
	public void selectFileAtPoint(Point p) {
		int row = locationToIndex(p);
		Rectangle bounds = getCellBounds(row, row);
		if (bounds.contains(p)) {
			setSelectedIndex(row);
			ensureIndexIsVisible(row);
		}
		else
			clearSelection();
	}


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


	/**
	 * Sets the files displayed by this view.
	 *
	 * @param files A vector containing the files to display.
	 */
	public void setDisplayedFiles(Vector files) {
		setListData(files);
	}


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


	/**
	 * Sets whether or not this view allows the selection of multiple files.
	 *
	 * @param enabled Whether or not to allow the selection of multiple
	 *                files.
	 */
	public void setMultiSelectionEnabled(boolean enabled) {
		getSelectionModel().setSelectionMode(
				enabled ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION :
						ListSelectionModel.SINGLE_SELECTION);
	}


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


	/**
	 * Selects the specified files in the view.
	 *
	 * @param files The files to select.  If any of the files are not in
	 *              the file chooser's <code>currentDirectory</code>, then
	 *              they are not selected.
	 */
	public void setSelectedFiles(File[] files) {

		int num = files.length;
		if(num>0) {

			ListModel model = getModel();
			int modelSize = model.getSize();

			for (int i=0; i<num; i++) {

				File f1 = files[i];
				if (!f1.exists())
					continue;
				File parentFile = f1.getParentFile();
				if (!parentFile.equals(chooser.currentDirectory))
					continue;

				for (int j=0; j<modelSize; j++) {
					File f2 = (File)model.getElementAt(j);
					if (f1.equals(f2)) {
						addSelectionInterval(j,j);
						break;
					}
				}

			} // End of for (int i=0; i<num; i++).

		} // End of if (num>0).

	}


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


	/**
	 * The renderer for the list view.
	 */
	class ListViewCellRenderer extends DefaultListCellRenderer {

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

		private Rectangle paintTextR = new Rectangle();
		private Rectangle paintIconR = new Rectangle();
		private Rectangle paintViewR = new Rectangle();
		private boolean isAlreadyOpened;

		public ListViewCellRenderer() {
			setOpaque(true);
		}

		public void paintComponent(Graphics g) {

			String text = getText();
			Icon icon = getIcon();
			FontMetrics fm = g.getFontMetrics();

			paintViewR.x = paintViewR.y = 0;
			paintViewR.width = getWidth();
			paintViewR.height = getHeight();

			g.setColor(getBackground());
			g.fillRect(paintViewR.x,paintViewR.y, paintViewR.width,paintViewR.height);

			paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
			paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;

			String clippedText = 
					SwingUtilities.layoutCompoundLabel(this,
												fm,
												text,
												icon,
												getVerticalAlignment(),
												getHorizontalAlignment(),
												getVerticalTextPosition(),
												getHorizontalTextPosition(),
												paintViewR,
												paintIconR,
												paintTextR,
												getIconTextGap());

			if (icon != null)
				icon.paintIcon(this, g, paintIconR.x, paintIconR.y);

			if (text != null) {
				int textX = paintTextR.x;
				int textY = paintTextR.y + fm.getAscent();
				g.setColor(getForeground());
				g.drawString(clippedText, textX,textY);
				if (isAlreadyOpened)
					g.drawLine(textX, textY+2, textX+paintTextR.width, textY+2);
			}

		}

		public void setBounds(int x, int y, int width, int height) { 
			super.setBounds(x, y,
				Math.min(width, this.getPreferredSize().width+4), height); 
		}

		public Component getListCellRendererComponent(JList list, Object value,
											int index, boolean isSelected,
											boolean cellHasFocus) {

			super.getListCellRendererComponent(list, value, index,
										isSelected, cellHasFocus);

			File file = (File)value;
			String fileName = file.getName();

			isAlreadyOpened = chooser.isUnderlinedFile(file);

			setText(fileName);

			// Set the image according to the file type.
			FileTypeInfo info = chooser.getFileTypeInfoFor(file);
			setIcon(info.icon);
			if (!isSelected) {
				if (chooser.getShowHiddenFiles() && file.isHidden())
					setForeground(chooser.getHiddenFileColor());
				else
					setForeground(info.labelTextColor);
			}

			return this;

		}

	}


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

}

⌨️ 快捷键说明

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