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

📄 topicsfounddialog.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
/*
 * 11/14/2003
 *
 * TopicsFoundDialog.java - Dialog used by HelpDialog.  Allows the user to
 *                          choose exactly one of multiple choices.
 * Copyright (C) 2003 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.fife.help;

import java.util.ArrayList;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.BorderFactory;

import org.fife.RListSelectionModel;
import org.fife.ui.RButton;
import org.fife.ui.RScrollPane;
import org.fife.ui.UIUtilities;


/**
 * Used by an instance of <code>org.fife.help.HelpDialog</code>.
 *
 * @author Robert Futrell
 * @version 1.0
 */
class TopicsFoundDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3318939353584278737L;

	private JList choicesList;			// List of topics found.
	private JButton okButton;			// OK button.
	private JButton cancelButton;		// Cancel button.

	private int selectedIndex = -1;


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


	/**
	 * Creates a new <code>TopicsFoundDialog</code>.
	 *
	 * @param owner The HelpDialog that spawns this TopicsFoundDialog.
	 * @param choices An array of Strings to use as the choices (the "topics
	 *                found").
	 */
	TopicsFoundDialog(JFrame owner, ArrayList choices) {

		// Call parent's constructor and set the dialog's title.
		super(owner, "Topics Found");

		// Create a label for the dialog.
		JLabel instruction = new JLabel("Click a topic, then click Display.");
		JPanel labelPanel = new JPanel();
		labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.Y_AXIS));
		labelPanel.add(instruction);
		labelPanel.add(Box.createHorizontalGlue());
		labelPanel.setBorder(UIUtilities.getEmpty5Border());

		TopicsFoundActionListener tfal = new TopicsFoundActionListener();

		// Create a panel for the buttons.
		okButton = new RButton("Display");
		okButton.setActionCommand("Display");
		okButton.addActionListener(tfal);
		cancelButton = new RButton("Cancel");
		cancelButton.setActionCommand("Cancel");
		cancelButton.addActionListener(tfal);
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		buttonPanel.add(Box.createHorizontalGlue());
		buttonPanel.add(okButton);
		buttonPanel.add(Box.createHorizontalStrut(3));
		buttonPanel.add(cancelButton);
		buttonPanel.setBorder(UIUtilities.getEmpty5Border());

		// Create the list of choices.
		choicesList = new JList(choices.toArray());
		choicesList.addMouseListener(new TopicsFoundMouseListener());
		choicesList.addKeyListener(new TopicsFoundKeyListener());
		choicesList.setSelectionModel(new RListSelectionModel());
		choicesList.setSelectedIndex(0);
		JScrollPane indexScrollPane = new RScrollPane(choicesList);
		indexScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		JPanel choicesPanel = new JPanel();
		choicesPanel.setLayout(new BoxLayout(choicesPanel, BoxLayout.Y_AXIS));
		choicesPanel.add(indexScrollPane);
		choicesPanel.setBorder(UIUtilities.getEmpty5Border());

		// Make it so the use cannot go back to the "Help" dialog.
		setModal(true);

		// Arrange the dialog!
		JPanel contentPanel = new JPanel(new BorderLayout());
		contentPanel.add(labelPanel, BorderLayout.NORTH);
		contentPanel.add(choicesPanel, BorderLayout.CENTER);
		contentPanel.add(buttonPanel, BorderLayout.SOUTH);
		getContentPane().add(contentPanel);
		setLocationRelativeTo(owner);
		pack();

	}


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


	/**
	 * Returns the index the user selected, or <code>-1</code> if they
	 * canceled the dialog.
	 *
	 * @return The index they selected, or <code>-1</code> if they canceled
	 *         the dialog.
	 */
	public int getSelectedIndex() {
		return selectedIndex;
	}


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


	/**
	 * Listens for action events in this dialog.
	 */
	private class TopicsFoundActionListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {

			if (e.getActionCommand().equals("Display")) {
				selectedIndex = choicesList.getSelectedIndex();
				setVisible(false);
			}

			else if (e.getActionCommand().equals("Cancel")) {
				selectedIndex = -1;
				setVisible(false);
			}

		}

	}


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


	/**
	 * Listens for mouse events in this dialog.
	 */
	private class TopicsFoundMouseListener implements MouseListener {

		// Callback for when the user clicks in our topic list.
		public void mouseClicked(MouseEvent e) {
			// If they've double-clicked in choicesList, get their solution and leave.
			if (e.getClickCount()==2) {
				selectedIndex = choicesList.getSelectedIndex();
				setVisible(false);
			}
		}

		public void mouseEntered(MouseEvent e) {
		}

		public void mouseExited(MouseEvent e) {
		}

		public void mousePressed(MouseEvent e) {
		}

		public void mouseReleased(MouseEvent e) {
		}

	}


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


	/**
	 * Listens for key events in this dialog.
	 */
	private class TopicsFoundKeyListener implements KeyListener {

		// Called whenever the user presses a key in choicesList.
		public void keyPressed(KeyEvent e) {

			// If they press Enter, give them the currently selected help.
			if ( e.getKeyCode()==KeyEvent.VK_ENTER) {
				selectedIndex = choicesList.getSelectedIndex();
				setVisible(false);
			}

			// If they press Escape, quit without changing the help (like pressing Cancel).
			else if ( e.getKeyCode()==KeyEvent.VK_ESCAPE) {
				selectedIndex = -1;
				setVisible(false);
			}

		}

		// Called whenever the user releases a key in choicesList.
		public void keyReleased(KeyEvent e) {
		}


		// Called whenever the user presses and releases a key in choicesList.
		public void keyTyped(KeyEvent e) {
		}

	}


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

}

⌨️ 快捷键说明

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