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

📄 popupmanager.java

📁 java 调用windows的api
💻 JAVA
字号:
package org.jawin.browser.popup;

import javax.swing.JPopupMenu;
import java.util.HashMap;
import org.jawin.browser.data.BaseData;
import org.jawin.browser.tree.*;
import org.jawin.browser.config.ConfigManager;

/**
 * Manages tree popup menus according to base data class implementation.
 *
 * This class listens for selection changes to the tree and can
 * provide the currently selected node to any requesting popup.
 *
 * <p>Title: Jawin Code Generation GUI</p>
 * <p>Description: GUI for exploring type libraries and generating Java code</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Open Source Incentive</p>
 *
 * @author Josh Passenger
 * @version 1.0
 */

public class PopupManager implements TypeDataTreeSelectionListener
{

	private static PopupManager instance = null;

	private HashMap popupActionConfigMap = new HashMap();

	private BaseData selectedBaseData = null;
	private JPopupMenu popupMenu = new JPopupMenu();

	/**
	 * Private constructor to support singleton
	 */
	private PopupManager()
	{
	}

	public static synchronized void initialize()
	{
		instance = new PopupManager();
		instance.loadConfiguration();
	}

	public static PopupManager getInstance()
	{
		if (instance == null)
		{
			throw new IllegalStateException("PopupManager.getInstance() PopupManager was not initialized");
		}

		return instance;
	}

	/**
	 * Should a popup menu be shown for this class of base data
	 * when its node is right clicked in the tree?
	 *
	 * @return true if a popup should be shown, false if it should not
	 */
	private boolean shouldPopupShow()
	{
		if ((selectedBaseData != null) && popupActionConfigMap.containsKey(selectedBaseData.getClass().getName()))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * Builds a popup menu suitable for this baseDataClass if one is
	 * currently selected and shows it in the requested location
	 *
	 * @param tree the tree that requested the popup
	 * @param x the x location to show the popup
	 * @param y the y location to show the popup
	 */
	public void showMenu(TypeDataTree tree, int x, int y)
	{
		/**
		 * This will return false if there is no base data currently selected in
		 * the tree or there is no configuration loaded for this base data class
		 */
		if (!shouldPopupShow())
		{
			return;
		}

		popupMenu.removeAll();

		PopupActionConfig popupActionConfig = getPopupActionConfig(selectedBaseData);

		int count = 0;

		for (int i = 0; i < popupActionConfig.getActionCount(); i++)
		{
			PopupAction action = popupActionConfig.getPopupActionAt(i);

			if (action.shouldDisplay(selectedBaseData))
			{
				PopupActionMenuItem menuItem = new PopupActionMenuItem(action);
				popupMenu.add(menuItem);
				count++;
			}
		}

		/**
		 * If menus were added show the popup, else just return
		 */
		if (count > 0)
		{
			popupMenu.show(tree, x, y);
		}
	}

	private PopupActionConfig getPopupActionConfig(String baseDataClass)
	{
		return (PopupActionConfig) popupActionConfigMap.get(baseDataClass);
	}

	private PopupActionConfig getPopupActionConfig(BaseData baseData)
	{
		return getPopupActionConfig(baseData.getClass().getName());
	}

	/**
	 * Fired when the tree selection changes
	 *
	 * @param selectedNode the newly selected BaseData node
	 */
	public void selectionChanged(BaseData selectedNode)
	{
		selectedBaseData = selectedNode;
	}

	public BaseData getSelectedBaseData()
	{
		return selectedBaseData;
	}

	/**
	 * Loads the configuration from the XML popups configuration file
	 */
	private void loadConfiguration()
	{
		PopupActionConfigParser parser = new PopupActionConfigParser();
		parser.parsePopupActionConfig(ConfigManager.getInstance().getString("popup.config", "config/popups.xml"));
	}

	/**
	 * Clear the laoded configuration
	 */
	public void clearConfig()
	{
		popupActionConfigMap.clear();
	}

	public void addConfig(PopupActionConfig newConfig)
	{
		popupActionConfigMap.put(newConfig.getBaseDataClass(), newConfig);
	}
}

⌨️ 快捷键说明

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