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

📄 rtextfilechooser.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		filterComboBox.setPreferredSize(fieldSize);
		filterComboBox.setMaximumSize(maximumFieldSize);
		filterComboBox.setRenderer(new FileFilterComboRenderer());
		filterLabel.setLabelFor(filterComboBox);
		filterComboBox.addItemListener(itemListener);

		encodingComboBox = new JComboBox();
		encodingComboBox.setPreferredSize(fieldSize);
		encodingComboBox.setMaximumSize(maximumFieldSize);
		encodingLabel.setLabelFor(encodingComboBox);
		encodingComboBox.addItemListener(itemListener);

		bottomPanel.add(fileNameLabel);
		bottomPanel.add(fileNameTextField);
		bottomPanel.add(filterLabel);
		bottomPanel.add(filterComboBox);
		bottomPanel.add(encodingLabel);
		bottomPanel.add(encodingComboBox);

		JPanel temp = new JPanel(new GridLayout(2,1, 0,5));

		acceptButton = new JButton();
		acceptButton.setActionCommand("AcceptButtonPressed");
		acceptButton.addActionListener(this);
		temp.add(acceptButton);

		cancelButton = new JButton();
		cancelButton.setActionCommand("CancelButtonPressed");
		cancelButton.addActionListener(this);
		temp.add(cancelButton);

		JPanel bottomButtonPanel = new JPanel(new BorderLayout());
		bottomButtonPanel.add(temp, BorderLayout.NORTH);

		temp= new JPanel(new BorderLayout());
		temp.add(bottomPanel);
		temp.add(bottomButtonPanel, BorderLayout.EAST);

		add(temp, BorderLayout.SOUTH);
		RUtilities.makeSpringCompactGrid(bottomPanel, 3, 2,	//rows, cols
										0,0,			//initX, initY
										6, 6);		//xPad, yPad

		// The "accept all files" file filter.
		addChoosableFileFilter(new FileFilter() {
				public boolean accept(File f) {
					return true;
				}
				public String getDescription() {
					return UIManager.getString(
								"FileChooser.acceptAllFileFilterText");
				}
			});
		populatefilterComboBox();

		// Localize and get ready to go!
		installStrings();
		guiInitialized = true;

		// We must call this AFTER guiInitialized is set to true, so that
		// the encoding combo displays the correct encoding.
		setEncoding(RTextFileChooser.getDefaultEncoding());

	}


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


	/**
	 * Listens for actions in this file dialog.
	 */
	public void actionPerformed(ActionEvent e) {

		String actionCommand = e.getActionCommand();

		if (actionCommand.equals("AcceptButtonPressed")) {
			approveSelection();
		}

		else if (actionCommand.equals("CancelButtonPressed")) {
			cancelSelection();
		}

		else if (actionCommand.equals("UpOneLevel")) {
			setCurrentDirectory(currentDirectory.getParentFile());
		}

		else if (actionCommand.equals("CreateNewDirectory")) {
			String newDirName = JOptionPane.showInputDialog(this, newFolderPrompt);
			if (newDirName!=null) {
				boolean result = new File(currentDirectory + separator + newDirName).mkdir();
				if (result==false) {
					JOptionPane.showMessageDialog(this, errorNewDirPrompt,
								errorDialogTitle, JOptionPane.ERROR_MESSAGE);
				}
				else
					refreshView();
			}
		}

		else if (actionCommand.equals("PopupOpen")) {
			actionPerformed(new ActionEvent((JComponent)view,
						ActionEvent.ACTION_PERFORMED, "AcceptButtonPressed"));
		}

		else if (actionCommand.equals("PopupRename")) {
			File file = view.getSelectedFile();
			String oldName = file.getName();
			String newName = JOptionPane.showInputDialog(this,
							newNamePrompt + oldName + ":", oldName);
			if (newName!=null && !newName.equals(oldName)) {
				try {
					// If they have a separator char in the name, assume
					// they've typed a full path.  Otherwise, just rename
					// it and place it in the same directory.
					if (newName.indexOf(separator.charAt(0))==-1)
						newName = currentDirectory.getCanonicalPath() + separator + newName;
					File newFile = new File(newName);
					if (!file.renameTo(newFile))
						throw new Exception(renameFailText);
					refreshView();
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(this, renameErrorMessage + e2,
									errorDialogTitle, JOptionPane.ERROR_MESSAGE);
				}
			}
		}

		else if (actionCommand.equals("PopupDelete")) {
			handleDelete();
		}

		else if (actionCommand.equals("PopupRefresh")) {
			refreshView();
		}

		else if (actionCommand.equals("ListButton")) {
			setViewMode(LIST_MODE);
			viewButton.setIcon(listViewIcon);
		}

		else if (actionCommand.equals("DetailsButton")) {
			setViewMode(DETAILS_MODE);
			viewButton.setIcon(detailsViewIcon);
		}

		else if (actionCommand.equals("IconsButton")) {
			setViewMode(ICONS_MODE);
			viewButton.setIcon(iconsViewIcon);
		}

	}


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


	/**
	 * Adds a file filter to the filter combo box.
	 *
	 * @param filter The file filter to add.
	 * @see #removeChoosableFileFilter
	 * @see #getChoosableFileFilters
	 */
	public void addChoosableFileFilter(FileFilter filter) {
		setFileFilterImpl(filter, false); // Takes care of everything for us.
	}


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


	/**
	 * Ensures that the file chooser never starts off too big or too small
	 * (as this is influenced by the file list displayed).  This should ONLY
	 * be called when <code>dialog</code> is created.
	 */
	private void adjustSizeIfNecessary() {

		Dimension size = dialog.getSize();
		Dimension oldSize = new Dimension(size);

		if (size.width>800)
			size.width = 800;
		else if (size.width<300)
			size.width = 300;
		if (size.height>800)
			size.height = 800;
		else if (size.height<300)
			size.height = 300;

		if (size.width!=oldSize.width || size.height!=oldSize.height)
			dialog.setSize(size);

	}


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


	/**
	 * Called when the user clicks the "Approve" button.  You can also call
	 * this programmatically.  Note that this does NOT necessarily mean the
	 * dialog will close; for example, if they have selected a directory
	 * and the file selection mode is <code>FILES_ONLY</code>, then the
	 * file dialog will simply change to that directory.
	 */
	public void approveSelection() {

		selectedFiles = getFilesFromFileNameTextField();
		if (selectedFiles==null) {
			// Some views allow you to click outside of all files
			// (i.e. IconsView).
			return;
		}

		// Make sure the file paths are absolute.
		ensureAbsoluteFilePaths(selectedFiles);

		// Some "corrections:"  If we're in files-only mode and
		// they have selected more than one directory, or one
		// or more files and a directory, then we simply take
		// the last-selected value and use that.
		if (fileSelectionMode==FILES_ONLY) {
			if (containsFilesAndDirectories(selectedFiles)) {
				File[] temp = new File[1];
				temp[0] = selectedFiles[0];
				if (temp[0].isDirectory()) {
					temp[0] = getCanonicalFileFor(temp[0]);
					setCurrentDirectory(temp[0]);
					return;
				}
				selectedFiles = temp;
			}
			else if (containsOnlyDirectories(selectedFiles)) {
				selectedFiles[0] = getCanonicalFileFor(selectedFiles[0]);
				setCurrentDirectory(selectedFiles[0]);
				return;
			}
		}

		iconManager.clearIconCache(); // To keep cache from growing huge.

		retVal = APPROVE_OPTION;
		dialog.setVisible(false);

	}


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


	/**
	 * Called when the user clicks the Cancel button.  You can also call this
	 * programatically.  Any file selections are nixed and the dialog closes.
	 */
	public void cancelSelection() {
		iconManager.clearIconCache(); // To keep cache from growing huge.
		selectedFiles = null;
		retVal = CANCEL_OPTION;
		dialog.setVisible(false);
	}


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


	/**
	 * Removes all values from the extension-to-color map.
	 *
	 * @see #getColorForExtension
	 * @see #setColorForExtension
	 */
	public void clearExtensionColorMap() {
		customColors.clear();
	}


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


	private boolean containsFilesAndDirectories(Object[] files) {
		int num = files.length;
		boolean containsFile = false;
		boolean containsDirectory = false;
		for (int i=0; i<num; i++) {
			if (((File)files[i]).isDirectory())
				containsDirectory = true;
			else if (((File)files[i]).isFile())
				containsFile = true;
			if (containsDirectory && containsFile)
				return true;
		}
		return false;
	}


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


	private boolean containsOnlyDirectories(Object[] files) {
		int num = files.length;
		for (int i=0; i<num; i++) {
			if (!((File)files[i]).isDirectory())
				return false;
		}
		return true;
	}


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


	/**
	 * Creates a dialog for the given parent frame.
	 *
	 * @param parent The frame that is to be the parent of the created dialog.
	 * @return The dialog.
	 */
	protected JDialog createDialog(Frame parent) throws HeadlessException {

		JDialog dialog;
		Frame frame = parent!=null ? parent : JOptionPane.getRootFrame();

		dialog = new JDialog(frame, true);	

		Container contentPane = dialog.getContentPane();
		contentPane.setLayout(new BorderLayout());
		contentPane.add(this, BorderLayout.CENTER);
		JRootPane rootPane = dialog.getRootPane();
		rootPane.setDefaultButton(acceptButton);
 
		if (JDialog.isDefaultLookAndFeelDecorated()) {
			boolean supportsWindowDecorations = 
				UIManager.getLookAndFeel().getSupportsWindowDecorations();
			if (supportsWindowDecorations)
				dialog.getRootPane().setWindowDecorationStyle(JRootPane.FILE_CHOOSER_DIALOG);
		}

		// Make it so if they "x-out" the dialog, they effectively cancel it.
		dialog.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				cancelSelection();//returnValue = CANCEL_OPTION;
			}
		});

		// Make the Escape key hide the dialog.
		InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
		ActionMap actionMap = rootPane.getActionMap();
		inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "OnEscape");
		actionMap.put("OnEscape",new AbstractAction() {
							public void actionPerformed(ActionEvent e) {
								cancelButton.doClick();
							}
						}
					);

		return dialog;

	}


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


	/**
	 * Creates the right-click popup menu.
	 */
	private void createPopupMenu() {

		popupMenu = new JPopupMenu() {
			public void show(Component c, int x, int y) {

				// Enable the file stuff (open, rename, ...) only if there
				// is a file highlighted.
				int count = view.getSelectedFiles().length;
				boolean filesSelected = count>0;
				((JMenuItem)getComponent(0)).setEnabled(filesSelected);
				((JMenuItem)getComponent(1)).setEnabled(count==1);
				((JMenuItem)getComponent(2)).setEnabled(filesSelected);

				// Only enable the "Up one level" item if we can actually
				// go up a level.
				JMenuItem upOneLevel = (JMenuItem)getComponent(4);
				upOneLevel.setEnabled(upOneLevelButton.isEnabled());

				super.show(c, x,y);

			}
		};

		JMenuItem menuItem = new JMenuItem("Open");
		menuItem.setActionCommand("PopupOpen");
		menuItem.addActionListener(this);
		popupMenu.add(menuItem);

		menuItem = new JMenuItem("Rename");
		menuItem.setActionCommand("PopupRename");
		menuItem.addActionListener(this);
		popupMenu.add(menuItem);

		menuItem = new JMenuItem("Delete");
		menuItem.setActionCommand("PopupDelete");
		menuItem.addActionListener(this);
		popupMenu.add(menuItem);

		popupMenu.addSeparator();

		menuItem = new JMenuItem("Up One Level");
		menuItem.setActionCommand("UpOneLevel");
		menuItem.addActionListener(this);
		popupMenu.add(menuItem);

		popupMenu.addSeparator();

		menuItem = new JMenuItem("Refresh");
		menuItem.setActionCommand("PopupRefresh");
		menuItem.addActionListener(this);
		popupMenu.add(menuItem);

	}

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


	synchronized void displayPopupMenu(JComponent view, int x, int y) {
		if (popupMenu==null)
			createPopupMenu();
		popupMenu.show((JComponent)view, x,y);
	}


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


⌨️ 快捷键说明

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