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

📄 filechooserui.java

📁 用于java swing的皮肤软件
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
				int row,
				int column)
			{
				Component comp=
					super.getTableCellEditorComponent(
						table,
						value,
						isSelected,
						row,
						column);
				if (value instanceof File)
				{
					tf.setText(chooser.getName((File) value));
					tf.requestFocus();
					tf.selectAll();
				}
				return comp;
			}
		});

		JList fakeList= new JList(detailsTableModel.listModel)
		{
			JTable table= detailsTable;

			public int locationToIndex(Point location)
			{
				return table.rowAtPoint(location);
			}

			public Rectangle getCellBounds(int index0, int index1)
			{
				Rectangle r0= table.getCellRect(index0, COLUMN_FILENAME, false);
				Rectangle r1= table.getCellRect(index1, COLUMN_FILENAME, false);
				return r0.union(r1);
			}

			public Object getSelectedValue()
			{
				return table.getValueAt(table.getSelectedRow(), COLUMN_FILENAME);
			}

			public Component add(Component comp)
			{
				if (comp instanceof JTextField)
				{
					return table.add(comp);
				}
				else
				{
					return super.add(comp);
				}
			}

			public void repaint()
			{
				if (table != null)
					table.repaint();
			}

			public TransferHandler getTransferHandler()
			{
				if (table != null)
				{
					return table.getTransferHandler();
				}
				else
				{
					return super.getTransferHandler();
				}
			}

			public void setTransferHandler(TransferHandler newHandler)
			{
				if (table != null)
				{
					table.setTransferHandler(newHandler);
				}
				else
				{
					super.setTransferHandler(newHandler);
				}
			}

			public boolean getDragEnabled()
			{
				if (table != null)
				{
					return table.getDragEnabled();
				}
				else
				{
					return super.getDragEnabled();
				}
			}

			public void setDragEnabled(boolean b)
			{
				if (table != null)
				{
					table.setDragEnabled(b);
				}
				else
				{
					super.setDragEnabled(b);
				}
			}
		};

		fakeList.setSelectionModel(listSelectionModel);
		detailsTable.addMouseListener(
			createDoubleClickListener(chooser, fakeList));
		//detailsTable.addMouseListener(createSingleClickListener(chooser, fakeList));

		JScrollPane scrollpane= new JScrollPane(detailsTable);
		scrollpane.setComponentOrientation(chooser.getComponentOrientation());
		LookAndFeel.installColors(
			scrollpane.getViewport(),
			"Table.background",
			"Table.foreground");

		// Adjust width of first column so the table fills the viewport when
		// first displayed (temporary listener).
		scrollpane.addComponentListener(new ComponentAdapter()
		{
			public void componentResized(ComponentEvent e)
			{
				JScrollPane sp= (JScrollPane) e.getComponent();
				fixNameColumnWidth(sp.getViewport().getSize().width);
				sp.removeComponentListener(this);
			}
		});

		p.add(scrollpane, BorderLayout.CENTER);
		return p;
	}

	private void fixNameColumnWidth(int viewWidth)
	{
		TableColumn nameCol=
			detailsTable.getColumnModel().getColumn(COLUMN_FILENAME);
		int tableWidth= detailsTable.getPreferredSize().width;

		if (tableWidth < viewWidth)
		{
			nameCol.setPreferredWidth(
				nameCol.getPreferredWidth() + viewWidth - tableWidth);
		}
	}

	private class DelayedSelectionUpdater implements Runnable
	{
		DelayedSelectionUpdater()
		{
			SwingUtilities.invokeLater(this);
		}

		public void run()
		{
			setFileSelected();
		}
	}

	/**
	 * Creates a selection listener for the list of files and directories.
	 *
	 * @param fc a <code>JFileChooser</code>
	 * @return a <code>ListSelectionListener</code>
	 */
	public ListSelectionListener createListSelectionListener(JFileChooser fc)
	{
		return new SelectionListener()
		{
			public void valueChanged(ListSelectionEvent e)
			{
				if (!e.getValueIsAdjusting())
				{
					JFileChooser chooser= getFileChooser();
					FileSystemView fsv= chooser.getFileSystemView();
					JList list= (JList) e.getSource();

					if (chooser.isMultiSelectionEnabled())
					{
						File[] files= null;
						Object[] objects= list.getSelectedValues();
						if (objects != null)
						{
							if (objects.length == 1
								&& ((File) objects[0]).isDirectory()
								&& chooser.isTraversable(((File) objects[0]))
								&& (chooser.getFileSelectionMode()
									== JFileChooser.FILES_ONLY
									|| !fsv.isFileSystem(((File) objects[0]))))
							{
								setDirectorySelected(true);
								setDirectory(((File) objects[0]));
							}
							else
							{
								files= new File[objects.length];
								int j= 0;
								for (int i= 0; i < objects.length; i++)
								{
									File f= (File) objects[i];
									if ((chooser.isFileSelectionEnabled() && f.isFile())
										|| (chooser.isDirectorySelectionEnabled()
											&& fsv.isFileSystem(f)
											&& f.isDirectory()))
									{
										files[j++]= f;
									}
								}
								if (j == 0)
								{
									files= null;
								}
								else if (j < objects.length)
								{
									File[] tmpFiles= new File[j];
									System.arraycopy(files, 0, tmpFiles, 0, j);
									files= tmpFiles;
								}
								setDirectorySelected(false);
							}
						}
						chooser.setSelectedFiles(files);
					}
					else
					{
						File file= (File) list.getSelectedValue();
						if (file != null
							&& file.isDirectory()
							&& chooser.isTraversable(file)
							&& (chooser.getFileSelectionMode() == JFileChooser.FILES_ONLY
								|| !fsv.isFileSystem(file)))
						{
							setDirectorySelected(true);
							setDirectory(file);
							chooser.setSelectedFile(null);
						}
						else
						{
							setDirectorySelected(false);
							if (file != null)
							{
								chooser.setSelectedFile(file);
							}
						}
					}
				}
			}
		};
	}

	private MouseListener createSingleClickListener(JFileChooser fc, JList list)
	{
		return new SingleClickListener(list);
	}

	int lastIndex= -1;
	boolean editing= false;
	int editX= 20;

	private int getEditIndex()
	{
		return lastIndex;
	}

	private void setEditIndex(int i)
	{
		lastIndex= i;
	}

	private void resetEditIndex()
	{
		lastIndex= -1;
	}

	private void cancelEdit()
	{
		if (editing)
		{
			editing= false;
			list.remove(editCell);
			centerPanel.repaint();
		}
		else if (detailsTable != null && detailsTable.isEditing())
		{
			detailsTable.getCellEditor().cancelCellEditing();
		}
	}

	JTextField editCell= null;

	private void editFileName(int index)
	{
		ensureIndexIsVisible(index);
		if (listViewPanel.isVisible())
		{
			editing= true;
			Rectangle r= list.getCellBounds(index, index);
			if (editCell == null)
			{
				editCell= new JTextField();
				editCell.addActionListener(new EditActionListener());
				editCell.addFocusListener(editorFocusListener);
				editCell.setNextFocusableComponent(list);
			}
			list.add(editCell);
			File f= (File) getModel().getElementAt(index);
			editCell.setText(getFileChooser().getName(f));
			if (list.getComponentOrientation().isLeftToRight())
			{
				editCell.setBounds(editX + r.x, r.y, r.width - editX, r.height);
			}
			else
			{
				editCell.setBounds(r.x, r.y, r.width - editX, r.height);
			}

			editCell.requestFocus();
			editCell.selectAll();
		}
		else if (detailsViewPanel.isVisible())
		{
			detailsTable.editCellAt(index, COLUMN_FILENAME);
		}
	}

	protected class SingleClickListener extends MouseAdapter
	{
		JList list;

		private long lastClick=0L;
		
		
		public SingleClickListener(JList list)
		{
			this.list= list;
		}

		public void mouseClicked(MouseEvent e)
		{
			if (SwingUtilities.isLeftMouseButton(e))
			{
				long clickTime=System.currentTimeMillis();
				
				if (clickTime-lastClick>750 && e.getClickCount() == 1)
				{
					lastClick=clickTime;
					JFileChooser fc= getFileChooser();
					int index= list.locationToIndex(e.getPoint());
					if ((!fc.isMultiSelectionEnabled()
						|| fc.getSelectedFiles().length <= 1)
						&& index >= 0
						&& list.isSelectedIndex(index)
						&& getEditIndex() == index
						&& !editing)
					{

						editFileName(index);
					}
					else
					{
						if (index >= 0)
						{
							setEditIndex(index);
						}
						else
						{
							resetEditIndex();
						}
					}
				}
				else
				{
					// on double click (open or drill down one directory) be
					// sure to clear the edit index
					lastClick=clickTime;
										
					resetEditIndex();
				}
			}
		}
	}

	class EditActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			applyEdit();
		}
	}

	private void applyEdit()
	{
		if (editing)
		{
			JFileChooser chooser= getFileChooser();
			File f= null;
			if (isDirectorySelected())
			{
				f= getDirectory();
			}
			else
			{
				f= chooser.getSelectedFile();
			}
			if (f != null)
			{
				String oldDisplayName= chooser.getName(f);
				String oldFileName= f.getName();
				String newDisplayName= editCell.getText().trim();
				String newFileName;

				if (!newDisplayName.equals(oldDisplayName))
				{
					newFileName= newDisplayName;
					//Check if extension is hidden from user
					int i1= oldFileName.length();
					int i2= oldDisplayName.length();
					if (i1 > i2 && oldFileName.charAt(i2) == '.')
					{
						newFileName= newDisplayName + oldFileName.substring(i2);
					}

					// rename
					FileSystemView fsv= chooser.getFileSystemView();
					File f2= fsv.createFileObject(f.getParentFile(), newFileName);
					if (getModel().renameFile(f, f2))
					{
						if (fsv.isParent(chooser.getCurrentDirectory(), f2))
						{
							if (chooser.isMultiSelectionEnabled())
							{
								chooser.setSelectedFiles(new File[] { f2 });
							}
							else
							{
								chooser.setSelectedFile(f2);
							}
						}
						else
						{
							//Could be because of delay in updating Desktop folder
							//chooser.setSelectedFile(null);
						}
					}
					else
					{
						// PENDING(jeff) - show a dialog indicating failure
					}
				}
			}
		}
		if (detailsTable != null && detailsTable.isEditing())
		{
			detailsTable.getCellEditor().stopCellEditing();
		}
		cancelEdit();
	}

	protected class FileRenderer extends DefaultListCellRenderer
	{	
		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= getFileChooser().getName(file);
			setText(fileName);

			Icon icon= FileChooserUI.getIcon(file, fileIcon, getFileChooser().getFileSystemView());
			setIcon(icon);

			if (isSelected)
			{
				// PENDING(jeff) - grab padding (4) below from defaults table.
				editX= icon.getIconWidth() + 4;
			}

			return this;
		}
	}


	/**	Uninstalls the UI delegeate from the specified component */
	public void uninstallUI(JComponent c)
	{
		// Remove listeners
		c.removePropertyChangeListener(filterComboBoxModel);
		cancelButton.removeActionListener(getCancelSelectionAction());
		approveButton.removeActionListener(getApproveSelectionAction());
		fileNameTextField.removeActionListener(getApproveSelectionAction());

		super.uninstallUI(c);
	}

	/**
	 * Returns the preferred size of the specified
	 * <code>JFileChooser</code>.
	 * The preferred size is at least as large,
	 * in both height and width,
	 * as the preferred size recommended
	 * by the file chooser's layout manager.
	 *

⌨️ 快捷键说明

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