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

📄 replacedialog.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		return replaceButton.getText();
	}


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


	/**
	 * Returns the text on the "Replace All" button.
	 *
	 * @return The text on the Replace All button.
	 * @see #setReplaceAllButtonText
	 */
	public final String getReplaceAllButtonText() {
		return replaceAllButton.getText();
	}


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


	/**
	 * Returns the <code>java.lang.String</code> to replace with.
	 *
	 * @return The <code>java.lang.String</code> the user wants to replace the
	 *         text to find with.
	 */
	public String getReplaceString() {
		return (String)replaceWithComboBox.getSelectedItem();
	}


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


	/**
	 * Returns the label on the "Replace with" text field.
	 *
	 * @return The text on the "Replace with" text field.
	 * @see #setReplaceWithLabelText
	 */
	public final String getReplaceWithLabelText() {
		return replaceFieldLabel.getText();
	}


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


	/**
	 * Removes an <code>ActionListener</code> from this dialog.
	 *
	 * @param l The listener to remove
	 * @see #addActionListener
	 */
	public void removeActionListener(ActionListener l) {
		findNextButton.removeActionListener(l);
		replaceButton.removeActionListener(l);
		replaceAllButton.removeActionListener(l);
	}


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


	/**
	 * Sets the text on the "Find" button.
	 *
	 * @param text The text for the Find button.
	 * @see #getFindButtonText
	 */
	public final void setFindButtonText(String text) {
		findNextButton.setText(text);
	}


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


	/**
	 * Sets the text on the "Replace" button.
	 *
	 * @param text The text for the Replace button.
	 * @see #getReplaceButtonText
	 */
	public final void setReplaceButtonText(String text) {
		replaceButton.setText(text);
	}


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


	/**
	 * Sets the text on the "Replace All" button.
	 *
	 * @param text The text for the Replace All button.
	 * @see #getReplaceAllButtonText
	 */
	public final void setReplaceAllButtonText(String text) {
		replaceAllButton.setText(text);
	}


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


	/**
	 * Sets the label on the "Find what" text field.
	 *
	 * @param text The text for the "Find what" text field's label.
	 * @see #getFindWhatLabelText
	 */
	public final void setFindWhatLabelText(String text) {
		findFieldLabel.setText(text);
	}


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


	/**
	 * Sets the label on the "Replace with" text field.
	 *
	 * @param text The text for the "Replace with" text field's label.
	 * @see #getReplaceWithLabelText
	 */
	public final void setReplaceWithLabelText(String text) {
		replaceFieldLabel.setText(text);
	}


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


	/**
	 * Sets the <code>java.lang.String</code> to replace with
	 *
	 * @param newReplaceString The <code>java.lang.String</code> to put into
	 *                         the replace field.
	 */
	public void setReplaceString(String newReplaceString) {
		replaceWithComboBox.addItem(newReplaceString);
		replaceWithComboBox.setSelectedIndex(0);
	}


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


	/**
	 * Overrides <code>JDialog</code>'s <code>setVisible</code> method; decides
	 * whether or not buttons are enabled.
	 *
	 * @param visible Whether or not the dialog should be visible.
	 */
	public void setVisible(boolean visible) {

		if (visible==true) {

			String selectedItem = (String)findTextComboBox.getSelectedItem();
			if (selectedItem==null) {
				findNextButton.setEnabled(false);
				replaceButton.setEnabled(false);
				replaceAllButton.setEnabled(false);
			}
			else {
				findNextButton.setEnabled(true);
				replaceButton.setEnabled(true);
				replaceAllButton.setEnabled(true);
			}

			// Call JDialog's setVisible().
			super.setVisible(true);

			// Make the "Find" text field active.
			JTextField textField = (JTextField)findTextComboBox.getEditor().
											getEditorComponent();
			textField.requestFocusInWindow();
			textField.selectAll();

		}

		else
			super.setVisible(false);

	}


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


	/**
	 * Called whenever the user changes the Look and Feel, etc.
	 * This is overridden so we can reinstate the listeners that are evidently
	 * lost on the JTextField portion of our combo box.
	*/
	public void updateUI() {

		// Create listeners for the combo boxes.
		ReplaceFocusAdapter replaceFocusAdapter = new ReplaceFocusAdapter();
		ReplaceKeyListener replaceKeyListener = new ReplaceKeyListener();
		ReplaceDocumentListener replaceDocumentListener = new ReplaceDocumentListener();

		// Fix the Find What combo box's listeners.
		JTextField textField = (JTextField)findTextComboBox.getEditor().getEditorComponent();
		textField.addFocusListener(replaceFocusAdapter);
		textField.addKeyListener(replaceKeyListener);
		textField.getDocument().addDocumentListener(replaceDocumentListener);

		// Fix the Replace With combo box's listeners.
		textField = (JTextField)replaceWithComboBox.getEditor().getEditorComponent();
		textField.addFocusListener(replaceFocusAdapter);
		textField.addKeyListener(replaceKeyListener);
		textField.getDocument().addDocumentListener(replaceDocumentListener);

	}


/*****************************************************************************/
/**************************** PRIVATE INNER CLASSES **************************/
/*****************************************************************************/


	/**
	 * Listens for changes in the text field (find search field).
	 */
	private class ReplaceDocumentListener implements DocumentListener {

		// This function is called whenever text is inserted into the document AFTER the fact.
		public void insertUpdate(DocumentEvent e) {
			JTextField findWhatTextField = (JTextField)findTextComboBox.getEditor().getEditorComponent();
			if (e.getDocument().equals(findWhatTextField.getDocument())) {
				findNextButton.setEnabled(true);
				replaceButton.setEnabled(true);
				replaceAllButton.setEnabled(true);
			}
		}

		// This function is called whenever text is removed from the document AFTER the fact.
		public void removeUpdate(DocumentEvent e) {
			JTextField findWhatTextField = (JTextField)findTextComboBox.getEditor().getEditorComponent();
			if (e.getDocument().equals(findWhatTextField.getDocument()) && findWhatTextField.getDocument().getLength()==0) {
				findNextButton.setEnabled(false);
				replaceButton.setEnabled(false);
				replaceAllButton.setEnabled(false);
			}
		}

		// This function is called whenever a property of the text is changed AFTER the fact.
		// NOTE: This isn't done here, as the combo box's text field  isn't a rich text edit.
		public void changedUpdate(DocumentEvent e) {
		}

	}


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


	/**
	 * Listens for the text fields gaining focus.
	 */
	private class ReplaceFocusAdapter extends FocusAdapter {

		public void focusGained(FocusEvent e) {

			JTextField textField = (JTextField)e.getSource();
			textField.selectAll();

			if (textField==(JTextField)findTextComboBox.getEditor().getEditorComponent())
				lastSearchString = (String)findTextComboBox.getSelectedItem();	// Remember what it originally was, in case they tabbed out.

			else		// if (textField==(JTextField)replaceWithComboBox.getEditor().getEditorComponent()).
				lastReplaceString = (String)replaceWithComboBox.getSelectedItem();	// Remember what it originally was, in case they tabbed out.

		}

	}


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


	/**
	 * Listens for key presses in the replace dialog.
	 */
	private class ReplaceKeyListener implements KeyListener {

		// Listens for the user pressing a key down.
		public void keyPressed(KeyEvent e) {
		}

		// Listens for a user releasing a key.
		public void keyReleased(KeyEvent e) {

			// This is an ugly hack to get around JComboBox's
			// insistance on eating the first Enter keypress
			// it receives when it has focus.
			if (e.getKeyCode()==KeyEvent.VK_ENTER) {
				if (e.getSource()==findTextComboBox.getEditor().getEditorComponent()) {
					String replaceString = (String)replaceWithComboBox.getSelectedItem();
					lastReplaceString = replaceString;	// Just in case it changed too.
					String searchString = (String)findTextComboBox.getSelectedItem();
					if (searchString != lastSearchString) {
						findNextButton.doClick();
						lastSearchString = searchString;
						((JTextField)findTextComboBox.getEditor().getEditorComponent()).selectAll();
					}
				}
				else { // if (e.getSource()==replaceWithComboBox.getEditor().getEditorComponent()) {
					String searchString = (String)findTextComboBox.getSelectedItem();
					lastSearchString = searchString;	// Just in case it changed too.
					String replaceString = (String)replaceWithComboBox.getSelectedItem();
					if (replaceString != lastReplaceString) {
						findNextButton.doClick();
						lastReplaceString = replaceString;
						((JTextField)replaceWithComboBox.getEditor().getEditorComponent()).selectAll();
					}
				}
			}

			else if (e.getKeyCode()==KeyEvent.VK_ESCAPE)
				setVisible(false);

		}

		// Listens for a key being typed.
		public void keyTyped(KeyEvent e) {
		}

	}


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

}

⌨️ 快捷键说明

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