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

📄 filechooserui.java

📁 用于java swing的皮肤软件
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	 * @param c  a <code>JFileChooser</code>
	 * @return   a <code>Dimension</code> specifying the preferred
	 *           width and height of the file chooser
	 */
	public Dimension getPreferredSize(JComponent c)
	{
		int prefWidth= PREF_SIZE.width;
		Dimension d= c.getLayout().preferredLayoutSize(c);
		if (d != null)
		{
			return new Dimension(
				d.width < prefWidth ? prefWidth : d.width,
				d.height < PREF_SIZE.height ? PREF_SIZE.height : d.height);
		}
		else
		{
			return new Dimension(prefWidth, PREF_SIZE.height);
		}
	}

	/**
	 * Returns the minimum size of the <code>JFileChooser</code>.
	 *
	 * @param c  a <code>JFileChooser</code>
	 * @return   a <code>Dimension</code> specifying the minimum
	 *           width and height of the file chooser
	 */
	public Dimension getMinimumSize(JComponent c)
	{
		return MIN_SIZE;
	}

	/**
	 * Returns the maximum size of the <code>JFileChooser</code>.
	 *
	 * @param c  a <code>JFileChooser</code>
	 * @return   a <code>Dimension</code> specifying the maximum 
	 *           width and height of the file chooser
	 */
	public Dimension getMaximumSize(JComponent c)
	{
		return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
	}

	void setFileSelected()
	{
		if (getFileChooser().isMultiSelectionEnabled() && !isDirectorySelected())
		{
			File[] files= getFileChooser().getSelectedFiles();
			// Should be selected
			Object[] selectedObjects= list.getSelectedValues();
			// Are actually selected

			// Remove files that shouldn't be selected
			for (int j= 0; j < selectedObjects.length; j++)
			{
				boolean found= false;
				for (int i= 0; i < files.length; i++)
				{
					if (files[i].equals(selectedObjects[j]))
					{
						found= true;
						break;
					}
				}
				if (!found)
				{
					int index= getModel().indexOf(selectedObjects[j]);
					if (index >= 0)
					{
						listSelectionModel.removeSelectionInterval(index, index);
					}
				}
			}
			// Add files that should be selected
			for (int i= 0; i < files.length; i++)
			{
				boolean found= false;
				for (int j= 0; j < selectedObjects.length; j++)
				{
					if (files[i].equals(selectedObjects[j]))
					{
						found= true;
						break;
					}
				}
				if (!found)
				{
					int index= getModel().indexOf(files[i]);
					if (index >= 0)
					{
						listSelectionModel.addSelectionInterval(index, index);
					}
				}
			}
		}
		else
		{
			JFileChooser chooser= getFileChooser();
			File f= null;
			if (isDirectorySelected())
			{
				f= getDirectory();
			}
			else
			{
				f= chooser.getSelectedFile();
			}
			int i;
			if (f != null && (i= getModel().indexOf(f)) >= 0)
			{
				listSelectionModel.setSelectionInterval(i, i);
				ensureIndexIsVisible(i);
			}
			else
			{
				listSelectionModel.clearSelection();
			}
		}
	}


	/**	Returns the icon for the specified file.
	 * 
	 * 	@param	f				The file for which to return the icon
	 * 	@param	fileIcon		The default file icon
	 * 	@param	fsv			The file system view from which we get
	 * 								the system-specific file icons
	 * 
	 * 	@return					The icon for the specified file, or the
	 * 								default icon, if the file system view did not
	 * 								provide a suitable icon.
	 */
	public static Icon getIcon(File f, Icon fileIcon, FileSystemView fsv)
	{
		Icon icon= (Icon)iconCache.get(f);
		if (icon != null)
		{
			return icon;
		}
		
		icon= fileIcon;
		if(f!=null)
		{
			if(f.isDirectory() && !fsv.isDrive(f) && !fsv.isFileSystemRoot(f) && !fsv.isComputerNode(f)
					&& !fsv.isFloppyDrive(f) && !fsv.isRoot(f))
				icon=UIManager.getIcon("FileChooser.folderIcon");
			else
				icon=fsv.getSystemIcon(f);
		}
		
		iconCache.put(f, icon);
		
		return icon;
	}


	private String fileNameString(File file)
	{
		if (file == null)
		{
			return null;
		}
		else
		{
			JFileChooser fc= getFileChooser();
			if (fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled())
			{
				return file.getPath();
			}
			else
			{
				return file.getName();
			}
		}
	}

	private String fileNameString(File[] files)
	{
		StringBuffer buf= new StringBuffer();
		for (int i= 0; files != null && i < files.length; i++)
		{
			if (i > 0)
			{
				buf.append(" ");
			}
			if (files.length > 1)
			{
				buf.append("\"");
			}
			buf.append(fileNameString(files[i]));
			if (files.length > 1)
			{
				buf.append("\"");
			}
		}
		return buf.toString();
	}

	/* The following methods are used by the PropertyChange Listener */

	private void doSelectedFileChanged(PropertyChangeEvent e)
	{
		applyEdit();
		File f= (File) e.getNewValue();
		JFileChooser fc= getFileChooser();
		if (f != null
			&& ((fc.isFileSelectionEnabled() && !f.isDirectory())
				|| (f.isDirectory() && fc.isDirectorySelectionEnabled())))
		{

			setFileName(fileNameString(f));
		}
		else
		{
			setFileName(null);
		}
		setFileSelected();
	}

	private void doSelectedFilesChanged(PropertyChangeEvent e)
	{
		applyEdit();
		File[] files= (File[]) e.getNewValue();
		JFileChooser fc= getFileChooser();
		if (files != null
			&& files.length > 0
			&& (files.length > 1
				|| fc.isDirectorySelectionEnabled()
				|| !files[0].isDirectory()))
		{
			setFileName(fileNameString(files));
		}
		else
		{
			setFileName(null);
		}
		setFileSelected();
	}

	private void doDirectoryChanged(PropertyChangeEvent e)
	{
		JFileChooser fc= getFileChooser();
		FileSystemView fsv= fc.getFileSystemView();

		applyEdit();
		resetEditIndex();
		clearIconCache();
		listSelectionModel.clearSelection();
		ensureIndexIsVisible(0);
		File currentDirectory= fc.getCurrentDirectory();
		if (currentDirectory != null)
		{
			directoryComboBoxModel.addItem(currentDirectory);
			// Currently can not create folder in the Desktop folder on Windows
			// (ShellFolder limitation)
			getNewFolderAction().setEnabled(
				fsv.isFileSystem(currentDirectory) && currentDirectory.canWrite());
			getChangeToParentDirectoryAction().setEnabled(
				!fsv.isRoot(currentDirectory));

			if (fc.isDirectorySelectionEnabled()
				&& !fc.isFileSelectionEnabled()
				&& fsv.isFileSystem(currentDirectory))
			{

				setFileName(currentDirectory.getPath());
			}
			else
			{
				setFileName(null);
			}
		}
	}

	private void doFilterChanged(PropertyChangeEvent e)
	{
		applyEdit();
		resetEditIndex();
		clearIconCache();
		listSelectionModel.clearSelection();
	}

	private void doFileSelectionModeChanged(PropertyChangeEvent e)
	{
		applyEdit();
		resetEditIndex();
		clearIconCache();
		listSelectionModel.clearSelection();

		JFileChooser fc= getFileChooser();
		File currentDirectory= fc.getCurrentDirectory();
		if (currentDirectory != null
			&& fc.isDirectorySelectionEnabled()
			&& !fc.isFileSelectionEnabled()
			&& fc.getFileSystemView().isFileSystem(currentDirectory))
		{

			setFileName(currentDirectory.getPath());
		}
		else
		{
			setFileName(null);
		}
	}

	private void doMultiSelectionChanged(PropertyChangeEvent e)
	{
		if (getFileChooser().isMultiSelectionEnabled())
		{
			listSelectionModel.setSelectionMode(
				ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		}
		else
		{
			listSelectionModel.setSelectionMode(
				ListSelectionModel.SINGLE_SELECTION);
			listSelectionModel.clearSelection();
			getFileChooser().setSelectedFiles(null);
		}
	}

	private void doAccessoryChanged(PropertyChangeEvent e)
	{
		if (getAccessoryPanel() != null)
		{
			if (e.getOldValue() != null)
			{
				getAccessoryPanel().remove((JComponent) e.getOldValue());
			}
			JComponent accessory= (JComponent) e.getNewValue();
			if (accessory != null)
			{
				getAccessoryPanel().add(accessory, BorderLayout.CENTER);
			}
		}
	}

	private void doApproveButtonTextChanged(PropertyChangeEvent e)
	{
		JFileChooser chooser= getFileChooser();
		approveButton.setText(getApproveButtonText(chooser));
		approveButton.setToolTipText(getApproveButtonToolTipText(chooser));
	}

	private void doDialogTypeChanged(PropertyChangeEvent e)
	{
		JFileChooser chooser= getFileChooser();
		approveButton.setText(getApproveButtonText(chooser));
		approveButton.setToolTipText(getApproveButtonToolTipText(chooser));
		if (chooser.getDialogType() == JFileChooser.SAVE_DIALOG)
		{
			lookInLabel.setText(saveInLabelText);
		}
		else
		{
			lookInLabel.setText(lookInLabelText);
		}
	}

	private void doApproveButtonMnemonicChanged(PropertyChangeEvent e)
	{
		// Note: Metal does not use mnemonics for approve and cancel
	}

	private void doControlButtonsChanged(PropertyChangeEvent e)
	{
		if (getFileChooser().getControlButtonsAreShown())
		{
			addControlButtons();
		}
		else
		{
			removeControlButtons();
		}
	}

	/*
	 * Listen for filechooser property changes, such as
	 * the selected file changing, or the type of the dialog changing.
	 */
	public PropertyChangeListener createPropertyChangeListener(JFileChooser fc)
	{
		return new PropertyChangeListener()
		{
			public void propertyChange(PropertyChangeEvent e)
			{
				String s= e.getPropertyName();
				if (s.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY))
				{
					doSelectedFileChanged(e);
				}
				else if (s.equals(JFileChooser.SELECTED_FILES_CHANGED_PROPERTY))
				{
					doSelectedFilesChanged(e);
				}
				else if (s.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY))
				{
					doDirectoryChanged(e);
				}
				else if (s.equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY))
				{
					doFilterChanged(e);
				}
				else if (
					s.equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY))
				{
					doFileSelectionModeChanged(e);
				}
				else if (
					s.equals(JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY))
				{
					doMultiSelectionChanged(e);
				}
				else if (s.equals(JFileChooser.ACCESSORY_CHANGED_PROPERTY))
				{
					doAccessoryChanged(e);
				}
				else if (
					s.equals(JFileChooser.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY)
						|| s.equals(
							JFileChooser
								.APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY))
				{
					doApproveButtonTextChanged(e);
				}
				else if (s.equals(JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY))
				{
					doDialogTypeChanged(e);
				}
				else if (
					s.equals(JFileChooser.APPROVE_BUTTON_MNEMONIC_CHANGED_PROPERTY))
				{
					doApproveButtonMnemonicChanged(e);
				}
				else if (
					s.equals(
						JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY))
				{
					doControlButtonsChanged(e);
				}
				else if (s.equals(JFileChooser.CANCEL_SELECTION))
				{
					applyEdit();
				}
				else if (s.equals("componentOrientation"))
				{
					ComponentOrientation o= (ComponentOrientation) e.getNewValue();
					JFileChooser cc= (JFileChooser) e.getSource();
					if (o != (ComponentOrientation) e.getOldValue())
					{
						cc.applyComponentOrientation(o);
					}
					if (detailsTable != null)
					{
						detailsTable.setComponentOrientation(o);
						detailsTable.getParent().getParent().setComponentOrientation(
							o);
					}
				}
				else if (s.equals("ancestor"))
				{
					if (e.getOldValue() == null && e.getNewValue() != null)
					{
						// Ancestor was added, set initial focus
						fileNameTextField.selectAll();
						fileNameTextField.requestFocus();
					}
				}
			}
		};
	}

	protected void removeControlButtons()
	{
		getBottomPanel().remove(getButtonPanel());
	}

	protected void addControlButtons()
	{
		getBottomPanel().add(getButtonPanel());
	}

	private void ensureIndexIsVisible(int i)
	{
		if (i >= 0)
		{
			list.ensureIndexIsVisible(i);
			if (detailsTable != null)
			{
				detailsTable.scrollRectToVisible(
					detailsTable.getCellRect(i, COLUMN_FILENAME, true));
			}
		}
	}


	/**	Ensures that the specified file in the specified JFileChooser
	 * 	is visible in the file list.
	 */
	public void ensureFileIsVisible(JFileChooser fc, File f)
	{
		ensureIndexIsVisible(getModel().indexOf(f));
	}


	/**	Updates the display for the current directory, by rescanning it
	 * 	and updating the files contained in the directory.
	 */
	public void rescanCurrentDirectory(JFileChooser fc)
	{
		getModel().validateFileCache();
	}


⌨️ 快捷键说明

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