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

📄 gpbarfactory.java

📁 用JGraph编的软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * @(#)GPBarFactory.java	1.2 30.01.2003
 *
 * Copyright (C) 2003 sven.luzar
 *
 * 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 (at your option) 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.jgraph.pad;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;

import org.jgraph.GPGraphpad;
import org.jgraph.pad.actions.AbstractActionDefault;
import org.jgraph.pad.resources.ImageLoader;
import org.jgraph.pad.resources.Translator;
import org.jgraph.pad.resources.TranslatorConstants;
import org.jgraph.utils.Utilities;
import org.jgraph.utils.gui.LocaleChangeAdapter;

/** The bar factory creates the menubars
 *  and the toolbars.
 *
 *  For Framework users:
 *  You can insert you own bar entries by register each entry
 *  at the static method <tt>addBarEntry</tt>.<br>
 *  <br>
 *  Example:
 *  <pre>
 * 	GPBarFactory.addBarEntry(new GPBarEntry("File", 15, "FileCopy"));
 *  </pre>
 *
 *
 * @see GPBarEntry
 * @author sven.luzar
 * @version 1.0
 *
 */
public class GPBarFactory implements TranslatorConstants {

	/** Main key for the menu bar
	 */
	public static final String MENUBAR = "Menubar";

	/** Main key for the toolbars
	 */
	public static final String TOOLBARS = "Toolbars";

	/** Main key for the graph popup menu
	 */
	public static final String GRAPH_POPUP = "GraphPopup";

	/** Main key for the library popup menu
	 */
	public static final String LIBRARY_POPUP = "LibraryPopup";

	/** Vector with Bar entries
	 */
	protected static Hashtable barEntries = new Hashtable();

	/** a reference to the joint graphpad
	 */
	protected GPGraphpad graphpad;

	/** creates an instance and memorizes the gaphpad
	 */
	public GPBarFactory(GPGraphpad graphpad) {
		this.graphpad = graphpad;
	}

	/**
	 * This is the hook through which all menu items are
	 * created.  It registers the result with the menuitem
	 * hashtable so that it can be fetched with getMenuItem().
	 */
	protected Component[] createMenuItem(String cmd) {
		if (cmd == null)
			return new Component[] {
		};

		String subMenu = Translator.getString(cmd + SUFFIX_MENU);
		if (subMenu != null) {
			String[] itemKeys = tokenize(cmd + SUFFIX_MENU, subMenu);
			return new Component[] { createMenu(cmd, itemKeys)};
		} else {
			Action a = getAction(cmd);

			if (a == null)
				return new Component[] {
			};

			if (a instanceof AbstractActionDefault) {
				return ((AbstractActionDefault) a).getMenuComponents();
			} else {
				JMenuItem item = new JMenuItem();
				item.setAction(a);
				fillMenuButton(item, cmd, "");
				return new Component[] { item };
			}
		}
	}

	/**
	 * Create the menubar for the app.  By default this pulls the
	 * definition of the menu from the associated resource file.
	 */
	public JMenuBar createMenubar() {
		JMenuBar mb = new JMenuBar();

		String[] menuKeys = tokenize(MENUBAR, Translator.getString(MENUBAR));
		for (int i = 0; i < menuKeys.length; i++) {
			String itemKey = Translator.getString(menuKeys[i] + SUFFIX_MENU);
			if (itemKey == null) {
				System.err.println(
					"Can't find MenuKey: '"
						+ menuKeys[i]
						+ "'. I'm ignoring the MenuKey!");
				continue;
			}
			String[] itemKeys = tokenize(menuKeys[i], itemKey);
			JMenu m = createMenu(menuKeys[i], itemKeys);
			if (m != null)
				mb.add(m);
		}
		return mb;
	}

	/** creates the popup menu for the graph
	 */
	public JPopupMenu createGraphPopupMenu() {
		return createPopupMenu(GRAPH_POPUP);
	}

	/** creates the popup menu for the library
	 */
	public JPopupMenu createLibraryPopupMenu() {
		return createPopupMenu(LIBRARY_POPUP);
	}

	/** creates a popup menu for the specified key.
	 */
	protected JPopupMenu createPopupMenu(String key) {
		JPopupMenu pop = new JPopupMenu();
		String[] itemKeys = tokenize(key, Translator.getString(key));
		for (int i = 0; i < itemKeys.length; i++) {
			if (itemKeys[i].equals("-")) {
				pop.addSeparator();
			} else {
				Component[] mi = createMenuItem(itemKeys[i]);
				for (int j = 0; j < mi.length; j++) {
					pop.add(mi[j]);
				}
			}
		}

		LocaleChangeAdapter.updateContainer(pop);

		return pop;
	}

	/** creates a menu for the specified key
	 */
	protected JMenu createMenu(String key) {

		return createMenu(key, tokenize(key, Translator.getString(key)));
	}

	/**
	 * Create a menu for the app.  By default this pulls the
	 * definition of the menu from the associated resource file.
	 */
	protected JMenu createMenu(String key, String[] itemKeys) {
		JMenu menu = new JMenu();
		menu.setName(key);
		for (int i = 0; i < itemKeys.length; i++) {
			if (itemKeys[i].equals("-")) {
				menu.addSeparator();
			} else {
				Component[] mi = createMenuItem(itemKeys[i]);
				for (int j = 0; j < mi.length; j++) {
					if (mi[j] != null)
						menu.add(mi[j]);
				}
			}
		}

		ImageIcon icon =
			ImageLoader.getImageIcon(Translator.getString(key + SUFFIX_IMAGE));
		if (icon != null) {
			menu.setHorizontalTextPosition(JButton.RIGHT);
			menu.setIcon(icon);
		}

		// set mnemonic for the JMenus
		String mnemonic = Translator.getString(key + SUFFIX_MNEMONIC);
		if (mnemonic != null && mnemonic.length() > 0)
			menu.setMnemonic(mnemonic.toCharArray()[0]);

		return menu;
	}

	/** creates a panel with the toolbars into.
	 *  For each toolbar a panel was created.
	 *  The inner panel is the return value.
	 *  The outside panel is the parameter.
	 *
	 *  @param toolBarMainPanel The outside panel.
	 *  @return The inner panel
	 */
	public JPanel createToolBars(JPanel toolBarMainPanel) {
		String toolBarsKey = Translator.getString(TOOLBARS);
		if (toolBarsKey == null) {
			System.err.println(
				"Can't find Key: 'toolbars'. I'm ignoring the MenuKey!");
			return toolBarMainPanel;
		}

		String[] toolBars = tokenize(TOOLBARS, toolBarsKey);
		//, JTabbedPane.SCROLL_TAB_LAYOUT); // JDK 1.3
		JPanel innerPanel = toolBarMainPanel;
		for (int i = 0; i < toolBars.length; i++) {
			String label = Translator.getString(toolBars[i] + SUFFIX_LABEL);
			innerPanel.add(
				createToolbar(toolBars[i], label),
				BorderLayout.NORTH);
			JPanel oldInnerPanel = innerPanel;
			innerPanel = new JPanel(new BorderLayout());
			oldInnerPanel.add(innerPanel, BorderLayout.CENTER);
		}
		return innerPanel;
	}

	/**
	 * Create the toolbar.  By default this reads the
	 * resource file for the definition of the toolbar.
	 */
	protected Component createToolbar(String key, String label) {
		JToolBar toolbar = new JToolBar(label);
		//toolbar.putClientProperty("JToolBar.isRollover", Boolean.TRUE);
		//toolbar.setFloatable(false);
		String toolKey = Translator.getString(key);
		if (toolKey == null) {
			System.err.println(
				"Can't find ToolBarKey: '"
					+ toolKey
					+ "'. I'm ignoring the MenuKey!");

⌨️ 快捷键说明

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