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

📄 iconsview.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 07/14/2004
 *
 * IconsView.java - The "Icons 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,1
 * 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.*;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import java.io.File;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import javax.swing.plaf.DesktopPaneUI;
import javax.swing.plaf.InternalFrameUI;
import javax.swing.plaf.basic.*;
import javax.swing.text.Segment;
import javax.swing.text.Utilities;


/**
 * An icons view for a file chooser similar to the "Icons" view found in
 * Microsoft Windows file choosers.<p>
 * Note that this class is NOT efficient when compared to other file chooser
 * view such as <code>ListView</code> and <code>DetailsView</code> because it
 * does not paint its "files" via a renderer; rather, all files are represented
 * by separate <code>Component</code>s.  This ends up taking a huge amount of
 * memory when viewing many files.
 *
 * @author Robert Futrell
 * @version 0.1
 */
class IconsView extends IconDesktopPane implements RTextFileChooserView {

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

	private static final int SPACING			= 10;	// Spacing between icons.
	private static final int DEFAULT_ICON_WIDTH	= 64;
	private static final int DEFAULT_ROW_SIZE	= 5;		// Default # of icons per row.
	private RTextFileChooser chooser;

	private MouseListener mouseListener;


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


	/**
	 * Constructor.
	 *
	 * @param chooser The owning file chooser.
	 */
	public IconsView(RTextFileChooser chooser) {

		super();
		this.chooser = chooser;

		// Since JDesktopPanes don't receive input events (or process them),
		// we must explicitly add our delete key functionality to its action
		// and input maps.
		InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
		ActionMap actionMap = getActionMap();
		inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "OnDelete");
		actionMap.put("OnDelete", new AbstractAction() {
			public void actionPerformed(ActionEvent e) {
				IconsView.this.chooser.handleDelete();
			}
		});

		setBackground(UIManager.getColor("List.background"));

		// Add any listeners.
		mouseListener = new MouseListener(chooser);
		addMouseListener(mouseListener);
		addComponentListener(new ComponentAdapter() {
						public void componentShown(ComponentEvent e) {
							refresh();// This makes icons organized neatly on first appearance.
						}
						});

	}


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


	/**
	 * Clears all files displayed by this view.
	 */
	public void clearDisplayedFiles() {

		// Selected files are kept in a separate array so we clear them separately.
		clearSelection();

		Component[] components = getComponents();
		int componentCount = components.length;
		for (int i=0; i<componentCount; i++) {
			if (components[i] instanceof IconInternalFrame) {
				remove(components[i]);
			}
		}
		if (componentCount>0)
			repaint();
	}


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


	/**
	 * Makes sure there are no selected files in this view.
	 */
	public void clearSelection() {
		JInternalFrame[] frames = getSelectedFrames();
		if (frames==null)
			return;
		int count = frames.length;
		for (int i=0; i<count; i++) {
			try {
				frames[i].setSelected(false);
			} catch (PropertyVetoException pve) {}
		}	
	}


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


	/**
	 * Makes sure the specified file is visible in the view.
	 *
	 * @param file The file that is to be visible.
	 */
	public void ensureFileIsVisible(File file) {
		System.err.println("Not implemented!");
	}


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


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


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


	/**
	 * 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???  This may very-well be view-dependent).
	 */
	public File getFileAtPoint(Point p) {
		Component c = getComponentAt(p);
		if (c!=null && c instanceof IconInternalFrame)
			return ((IconInternalFrame)c).getFile();
		return null;
	}


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


	/**
	 * 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() {
		IconInternalFrame frame = (IconInternalFrame)getSelectedFrame();
		if (frame!=null)
			return frame.getFile();
		return null;
	}


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


	/**
	 * Returns all selected files in this view.
	 *
	 * @return An array of all selected files.
	 */
	public File[] getSelectedFiles() {
		JInternalFrame[] frames = getSelectedFrames();
		File[] files = null;
		if (frames!=null) {
			int num = frames.length;
			files = new File[num];
			for (int i=0; i<num; i++)
				files[i] = ((IconInternalFrame)frames[i]).getFile();
			return files;
		}
		return new File[0]; // Necessary for standards...
	}



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


	/**
	 * 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.
	 */
	// FIXME: JDesktopPanes evidently don't receive key (input?) events, so
	// we'll need to find another way to be alerted of Delete keypresses.
	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);
		}
	}


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


	/**
	 * Organizes the icons nicely in the visible screen space.
	 */
	public void refresh() {

		// Resize so that our width is that of the viewport in the scrollpane.
		Dimension size = getVisibleRect().getSize();
		int defaultWidth = (SPACING+DEFAULT_ICON_WIDTH)*DEFAULT_ROW_SIZE; // Remember there's spacing at the front too.
		int width = size.width<defaultWidth ? defaultWidth : size.width;

		int x = SPACING;
		int y = SPACING;

		JInternalFrame[] frames = getAllFrames();
		int num = frames==null ? 0 : frames.length;
		for (int i=0; i<num; i++) {
			frames[i].setLocation(x,y);
			x += DEFAULT_ICON_WIDTH + SPACING;
			if (x+DEFAULT_ICON_WIDTH>width) {
				x = SPACING;
				y += frames[i].getHeight() + SPACING;
			}
		}

		size.height = y+DEFAULT_ICON_WIDTH;
		setPreferredSize(size);
		setSize(size);			// Causes the scroll pane to revalidate.

	}


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


	/**
	 * 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() {
		clearDisplayedFiles(); // Just in case.
		removeMouseListener(mouseListener);
	}


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


	/**
	 * 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) {
		clearSelection();
		Component c = getComponentAt(p);
		if (c!=null && (c instanceof IconInternalFrame)) { // could return "this".
			IconInternalFrame frame = (IconInternalFrame)c;
			addSelectedFrame(frame);
			try {
				frame.setSelected(true);
			} catch (PropertyVetoException pve) {}
		}
	}


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


	/**
	 * Sets the files displayed by this view.
	 *
	 * @param files A vector containing the files to display.  These files
	 *              are not necessarily sorted by file name.
	 */
	public void setDisplayedFiles(Vector files) {

		// Clears view and also clears "selected files" vector.
		clearDisplayedFiles();

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

			File file = (File)files.get(i);

			// Create the internal frame.
			// Set the image and text color according to the file type.
			FileTypeInfo info = chooser.getFileTypeInfoFor(file);
			Color fg = (chooser.getShowHiddenFiles() && file.isHidden()) ?
							chooser.getHiddenFileColor() :
							info.labelTextColor;
			IconInternalFrame frame = new IconInternalFrame(file, 
									info.icon, fg);

			add(frame);
			try {
				frame.setSelected(false);
			} catch (PropertyVetoException pve) {}

		}

		refresh();

	}


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


	/**
	 * 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) {
		// This method doesn't do anything; rather, the view actually polls
		// the file chooser when a new file is selected, to find out whether
		// or not the user can multi-select.
	}


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


	/**
	 * 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) {

		clearSelection();

		int componentCount = getComponentCount();
		int numToSelect = files.length;

		for (int i=0; i<numToSelect; i++) {
			for (int j=0; j<componentCount; j++) {
				IconInternalFrame frame = (IconInternalFrame)getComponent(j);
				if (frame.getFile().equals(files[i])) {
					addSelectedFrame(frame);
					break; // Go to the next iteration in the "i" loop.
				}
			}
		}

	}


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


⌨️ 快捷键说明

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