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

📄 xpfilechooserui.java

📁 Swing Windows XP 外观和感觉 BeanSoft 修改版, 2003年 原始的作者: XP 外观和感觉 by Stefan Krause - http://www.stefan
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		centerPanel.add(getAccessoryPanel(), BorderLayout.AFTER_LINE_ENDS);
		JComponent accessory = fc.getAccessory();
		if (accessory != null) {
			getAccessoryPanel().add(accessory);
		}
		fc.add(centerPanel, BorderLayout.CENTER);

		// ********************************** //
		// **** Construct the bottom panel ** //
		// ********************************** //
		JPanel bottomPanel = getBottomPanel();
		bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS));
		fc.add(bottomPanel, BorderLayout.SOUTH);

		// FileName label and textfield
		JPanel fileNamePanel = new JPanel();
		fileNamePanel.setLayout(new BoxLayout(fileNamePanel, BoxLayout.LINE_AXIS));
		bottomPanel.add(fileNamePanel);
		bottomPanel.add(Box.createRigidArea(vstrut5));

		AlignedLabel fileNameLabel = new AlignedLabel(fileNameLabelText);
		fileNameLabel.setDisplayedMnemonic(fileNameLabelMnemonic);
		fileNamePanel.add(fileNameLabel);

		fileNameTextField = new JTextField() {
			public Dimension getMaximumSize() {
				return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height);
			}
		};
		fileNamePanel.add(fileNameTextField);
		fileNameLabel.setLabelFor(fileNameTextField);
		fileNameTextField.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent e) {
				if (!getFileChooser().isMultiSelectionEnabled()) {
					listSelectionModel.clearSelection();
				}
			}
		});
		if (fc.isMultiSelectionEnabled()) {
			setFileName(fileNameString(fc.getSelectedFiles()));
		} else {
			setFileName(fileNameString(fc.getSelectedFile()));
		}

		// Filetype label and combobox
		JPanel filesOfTypePanel = new JPanel();
		filesOfTypePanel.setLayout(new BoxLayout(filesOfTypePanel, BoxLayout.LINE_AXIS));
		bottomPanel.add(filesOfTypePanel);

		AlignedLabel filesOfTypeLabel = new AlignedLabel(filesOfTypeLabelText);
		filesOfTypeLabel.setDisplayedMnemonic(filesOfTypeLabelMnemonic);
		filesOfTypePanel.add(filesOfTypeLabel);

		filterComboBoxModel = createFilterComboBoxModel();
		fc.addPropertyChangeListener(filterComboBoxModel);
		filterComboBox = new JComboBox(filterComboBoxModel);
		filesOfTypeLabel.setLabelFor(filterComboBox);
		filterComboBox.setRenderer(createFilterComboBoxRenderer());
		filesOfTypePanel.add(filterComboBox);

		// buttons
		getButtonPanel().setLayout(new ButtonAreaLayout());

		approveButton = new JButton(getApproveButtonText(fc));
		// Note: Metal does not use mnemonics for approve and cancel
		approveButton.addActionListener(getApproveSelectionAction());
		approveButton.setToolTipText(getApproveButtonToolTipText(fc));
		getButtonPanel().add(approveButton);

		cancelButton = new JButton(cancelButtonText);
		cancelButton.setToolTipText(cancelButtonToolTipText);
		cancelButton.addActionListener(getCancelSelectionAction());
		getButtonPanel().add(cancelButton);

		if (fc.getControlButtonsAreShown()) {
			addControlButtons();
		}

		groupLabels(new AlignedLabel[] { fileNameLabel, filesOfTypeLabel });
	}

	protected JPanel getButtonPanel() {
		if (buttonPanel == null) {
			buttonPanel = new JPanel();
		}
		return buttonPanel;
	}

	protected JPanel getBottomPanel() {
		if (bottomPanel == null) {
			bottomPanel = new JPanel();
		}
		return bottomPanel;
	}

	protected void installStrings(JFileChooser fc) {
		super.installStrings(fc);

		Locale l = fc.getLocale();

		lookInLabelMnemonic = UIManager.getInt("FileChooser.lookInLabelMnemonic");
		lookInLabelText = UIManager.getString("FileChooser.lookInLabelText", l);
		saveInLabelText = UIManager.getString("FileChooser.saveInLabelText", l);

		fileNameLabelMnemonic = UIManager.getInt("FileChooser.fileNameLabelMnemonic");
		fileNameLabelText = UIManager.getString("FileChooser.fileNameLabelText", l);

		filesOfTypeLabelMnemonic = UIManager.getInt("FileChooser.filesOfTypeLabelMnemonic");
		filesOfTypeLabelText = UIManager.getString("FileChooser.filesOfTypeLabelText", l);

		upFolderToolTipText = UIManager.getString("FileChooser.upFolderToolTipText", l);
		upFolderAccessibleName = UIManager.getString("FileChooser.upFolderAccessibleName", l);

		homeFolderToolTipText = UIManager.getString("FileChooser.homeFolderToolTipText", l);
		homeFolderAccessibleName = UIManager.getString("FileChooser.homeFolderAccessibleName", l);

		newFolderToolTipText = UIManager.getString("FileChooser.newFolderToolTipText", l);
		newFolderAccessibleName = UIManager.getString("FileChooser.newFolderAccessibleName", l);

		listViewButtonToolTipText = UIManager.getString("FileChooser.listViewButtonToolTipText", l);
		listViewButtonAccessibleName = UIManager.getString("FileChooser.listViewButtonAccessibleName", l);

		detailsViewButtonToolTipText = UIManager.getString("FileChooser.detailsViewButtonToolTipText", l);
		detailsViewButtonAccessibleName = UIManager.getString("FileChooser.detailsViewButtonAccessibleName", l);

		fileNameHeaderText = UIManager.getString("FileChooser.fileNameHeaderText", l);
		fileSizeHeaderText = UIManager.getString("FileChooser.fileSizeHeaderText", l);
		fileTypeHeaderText = UIManager.getString("FileChooser.fileTypeHeaderText", l);
		fileDateHeaderText = UIManager.getString("FileChooser.fileDateHeaderText", l);
		fileAttrHeaderText = UIManager.getString("FileChooser.fileAttrHeaderText", l);
	}

	protected void installListeners(JFileChooser fc) {
		super.installListeners(fc);
		ActionMap actionMap = getActionMap();
		SwingUtilities.replaceUIActionMap(fc, actionMap);
	}

	protected ActionMap getActionMap() {
		return createActionMap();
	}

	protected ActionMap createActionMap() {
		AbstractAction escAction = new AbstractAction() {
			public void actionPerformed(ActionEvent e) {
				if (editFile != null) {
					cancelEdit();
				} else {
					getFileChooser().cancelSelection();
				}
			}
			public boolean isEnabled() {
				return getFileChooser().isEnabled();
			}
		};
		ActionMap map = new ActionMapUIResource();
		map.put("approveSelection", getApproveSelectionAction());
		map.put("cancelSelection", escAction);
		map.put("Go Up", getChangeToParentDirectoryAction());
		return map;
	}

	protected JPanel createList(JFileChooser fc) {
		JPanel p = new JPanel(new BorderLayout());
		final JFileChooser fileChooser = fc;
		list = new JList() {
			public int getNextMatch(String prefix, int startIndex, Position.Bias bias) {
				ListModel model = getModel();
				int max = model.getSize();
				if (prefix == null || startIndex < 0 || startIndex >= max) {
					throw new IllegalArgumentException();
				}
				// start search from the next element before/after the selected element
				boolean backwards = (bias == Position.Bias.Backward);
				for (int i = startIndex; backwards ? i >= 0 : i < max; i += (backwards ? -1 : 1)) {
					String filename = fileChooser.getName((File) model.getElementAt(i));
					if (filename.regionMatches(true, 0, prefix, 0, prefix.length())) {
						return i;
					}
				}
				return -1;
			}
		};
		list.setCellRenderer(new FileRenderer());
		list.setLayoutOrientation(JList.VERTICAL_WRAP);
		list.setVisibleRowCount(-1);

		if (fc.isMultiSelectionEnabled()) {
			list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		} else {
			list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		}
		list.setModel(getModel());
		list.addListSelectionListener(createListSelectionListener(fc));
		list.addMouseListener(createDoubleClickListener(fc, list));
		list.addMouseListener(createSingleClickListener(fc, list));
		getModel().addListDataListener(new ListDataListener() {
			public void contentsChanged(ListDataEvent e) {
				// Update the selection after JList has been updated
				new DelayedSelectionUpdater();
			}
			public void intervalAdded(ListDataEvent e) {
				new DelayedSelectionUpdater();
			}
			public void intervalRemoved(ListDataEvent e) {
			}
		});

		JScrollPane scrollpane = new JScrollPane(list);
		p.add(scrollpane, BorderLayout.CENTER);
		return p;
	}

	class DetailsTableModel extends AbstractTableModel implements ListDataListener {
		String[] columnNames = { fileNameHeaderText, fileSizeHeaderText, fileTypeHeaderText, fileDateHeaderText, fileAttrHeaderText };
		JFileChooser chooser;
		ListModel listModel;

		DetailsTableModel(JFileChooser fc) {
			this.chooser = fc;
			listModel = getModel();
			listModel.addListDataListener(this);
		}

		public int getRowCount() {
			return listModel.getSize();
		}

		public int getColumnCount() {
			return COLUMN_COLCOUNT;
		}

		public String getColumnName(int column) {
			return columnNames[column];
		}

		public Class getColumnClass(int column) {
			switch (column) {
				case COLUMN_FILENAME :
					return File.class;
				case COLUMN_FILEDATE :
					return Date.class;
				default :
					return super.getColumnClass(column);
			}
		}

		public Object getValueAt(int row, int col) {
			// Note: It is very important to avoid getting info on drives, as
			// this will trigger "No disk in A:" and similar dialogs.
			//
			// Use (f.exists() && !chooser.getFileSystemView().isFileSystemRoot(f)) to
			// determine if it is safe to call methods directly on f.

			File f = (File) listModel.getElementAt(row);
			switch (col) {
				case COLUMN_FILENAME :
					return f;

				case COLUMN_FILESIZE :
					if (!f.exists() || f.isDirectory()) {
						return null;
					}
					long len = f.length() / 1024L;
					if (len < 1024L) {
						return ((len == 0L) ? 1L : len) + " KB";
					} else {
						len /= 1024L;
						if (len < 1024L) {
							return len + " MB";
						} else {
							len /= 1024L;
							return len + " GB";
						}
					}

				case COLUMN_FILETYPE :
					if (!f.exists()) {
						return null;
					}
					return chooser.getFileSystemView().getSystemTypeDescription(f);

				case COLUMN_FILEDATE :
					if (!f.exists() || chooser.getFileSystemView().isFileSystemRoot(f)) {
						return null;
					}
					long time = f.lastModified();
					return (time == 0L) ? null : new Date(time);

				case COLUMN_FILEATTR :
					if (!f.exists() || chooser.getFileSystemView().isFileSystemRoot(f)) {
						return null;
					}
					String attributes = "";
					if (!f.canWrite()) {
						attributes += "R";
					}
					if (f.isHidden()) {
						attributes += "H";
					}
					return attributes;
			}
			return null;
		}

		public void setValueAt(Object value, int row, int col) {
			if (col == COLUMN_FILENAME) {
				JFileChooser chooser = getFileChooser();
				File f = (File) getValueAt(row, col);
				if (f != null) {
					String oldDisplayName = chooser.getName(f);
					String oldFileName = f.getName();
					String newDisplayName = ((String) value).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 (!f2.exists() && XPFileChooserUI.this.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
						}
					}
				}
			}
		}

		public boolean isCellEditable(int row, int column) {
			return (column == COLUMN_FILENAME);
		}

		public void contentsChanged(ListDataEvent e) {
			fireTableDataChanged();
		}
		public void intervalAdded(ListDataEvent e) {
			fireTableDataChanged();
		}
		public void intervalRemoved(ListDataEvent e) {
			fireTableDataChanged();
		}
	}

	class DetailsTableCellRenderer extends DefaultTableCellRenderer {
		JFileChooser chooser;
		DateFormat df;

		DetailsTableCellRenderer(JFileChooser chooser) {
			this.chooser = chooser;
			df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, chooser.getLocale());
		}

		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

			if (column == COLUMN_FILESIZE || column == COLUMN_FILEATTR) {
				setHorizontalAlignment(SwingConstants.TRAILING);
			} else {
				setHorizontalAlignment(SwingConstants.LEADING);
			}

			return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		}

		public void setValue(Object value) {
			setIcon(null);
			if (value instanceof File) {
				File file = (File) value;
				String fileName = chooser.getName(file);
				setText(fileName);
				Icon icon = chooser.getIcon(file);
				setIcon(icon);
			} else if (value instanceof Date) {
				setText((value == null) ? "" : df.format((Date) value));
			} else {
				super.setValue(value);
			}
		}
	}

	protected JPanel createDetailsView(JFileChooser fc) {
		final JFileChooser chooser = fc;
		JPanel p = new JPanel(new BorderLayout());

⌨️ 快捷键说明

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