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

📄 menufactory.java

📁 测试工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.apache.jmeter.gui.util;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.MenuElement;

import org.apache.jmeter.control.Controller;
import org.apache.jmeter.gui.GuiPackage;
import org.apache.jmeter.gui.JMeterGUIComponent;
import org.apache.jmeter.gui.action.ActionNames;
import org.apache.jmeter.gui.action.ActionRouter;
import org.apache.jmeter.gui.action.KeyStrokes;
import org.apache.jmeter.gui.tree.JMeterTreeNode;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.testbeans.TestBean;
import org.apache.jmeter.testbeans.gui.TestBeanGUI;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.testelement.WorkBench;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.visualizers.Printable;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.reflect.ClassFinder;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;

public final class MenuFactory {
	private static final Logger log = LoggingManager.getLoggerForClass();

	/*
	 *  Predefined strings for makeMenu().
	 *  These are used as menu categories in the menuMap Hashmap, 
	 *  and also for resource lookup in messages.properties
	*/
	public final static String TIMERS = "menu_timer"; //$NON-NLS-1$

	public final static String CONTROLLERS = "menu_logic_controller"; //$NON-NLS-1$

	public final static String SAMPLERS = "menu_generative_controller"; //$NON-NLS-1$

	public final static String CONFIG_ELEMENTS = "menu_config_element"; //$NON-NLS-1$

	public final static String POST_PROCESSORS = "menu_post_processors"; //$NON-NLS-1$

	public final static String PRE_PROCESSORS = "menu_pre_processors"; //$NON-NLS-1$

	public final static String ASSERTIONS = "menu_assertions"; //$NON-NLS-1$

	public final static String NON_TEST_ELEMENTS = "menu_non_test_elements"; //$NON-NLS-1$

	public final static String LISTENERS = "menu_listener"; //$NON-NLS-1$

	private static Map menuMap = new HashMap();

	private static Set elementsToSkip = new HashSet();

	// MENU_ADD_xxx - controls which items are in the ADD menu
	// MENU_PARENT_xxx - controls which items are in the Insert Parent menu
	private static final String[] MENU_ADD_CONTROLLER = new String[] {
        MenuFactory.CONTROLLERS, 
		MenuFactory.CONFIG_ELEMENTS, 
        MenuFactory.TIMERS, 
        MenuFactory.PRE_PROCESSORS,
        MenuFactory.SAMPLERS,
		MenuFactory.ASSERTIONS, 
		MenuFactory.POST_PROCESSORS,
        MenuFactory.LISTENERS, 
		};

	private static final String[] MENU_PARENT_CONTROLLER = new String[] { 
        MenuFactory.CONTROLLERS };

	private static final String[] MENU_ADD_SAMPLER = new String[] { 
        MenuFactory.CONFIG_ELEMENTS,
		MenuFactory.TIMERS, 
        MenuFactory.PRE_PROCESSORS,
		MenuFactory.ASSERTIONS, 
		MenuFactory.POST_PROCESSORS,
		MenuFactory.LISTENERS, 
		};

	private static final String[] MENU_PARENT_SAMPLER = new String[] { 
        MenuFactory.CONTROLLERS };

	private static List timers, controllers, samplers, configElements, 
        assertions, listeners, nonTestElements,
		postProcessors, preProcessors;

	static {
		try {
			String[] classesToSkip = 
                JOrphanUtils.split(JMeterUtils.getPropDefault("not_in_menu", ""), ","); //$NON-NLS-1$
			for (int i = 0; i < classesToSkip.length; i++) {
				elementsToSkip.add(classesToSkip[i].trim());
			}

			initializeMenus();
		} catch (Throwable e) {
			log.error("", e);
		}
	}

	/**
	 * Private constructor to prevent instantiation.
	 */
	private MenuFactory() {
	}

	public static void addEditMenu(JPopupMenu menu, boolean removable) {
		addSeparator(menu);
		if (removable) {
			menu.add(makeMenuItem(JMeterUtils.getResString("cut"), //$NON-NLS-1$
                "Cut", ActionNames.CUT, //$NON-NLS-1$
                KeyStrokes.CUT));
        }
		menu.add(makeMenuItem(JMeterUtils.getResString("copy"),  //$NON-NLS-1$
                "Copy", ActionNames.COPY, //$NON-NLS-1$
                KeyStrokes.COPY));
		menu.add(makeMenuItem(JMeterUtils.getResString("paste"), //$NON-NLS-1$
                "Paste", ActionNames.PASTE, //$NON-NLS-1$
                KeyStrokes.PASTE));
		menu.add(makeMenuItem(JMeterUtils.getResString("reset_gui"), //$NON-NLS-1$
                "Reset", ActionNames.RESET_GUI //$NON-NLS-1$
                ));
		if (removable) {
			menu.add(makeMenuItem(JMeterUtils.getResString("remove"), //$NON-NLS-1$
                    "Remove", ActionNames.REMOVE, //$NON-NLS-1$
                    KeyStrokes.REMOVE));
		}
	}

	public static void addPasteResetMenu(JPopupMenu menu) {
		addSeparator(menu);
		menu.add(makeMenuItem(JMeterUtils.getResString("paste"), //$NON-NLS-1$
                "Paste", ActionNames.PASTE, //$NON-NLS-1$
                KeyStrokes.PASTE));
		menu.add(makeMenuItem(JMeterUtils.getResString("reset_gui"), //$NON-NLS-1$
                "Reset", ActionNames.RESET_GUI //$NON-NLS-1$
                ));
	}

	public static void addFileMenu(JPopupMenu menu) {
		addSeparator(menu);
		menu.add(makeMenuItem(JMeterUtils.getResString("open"),// $NON-NLS-1$
                "Open", ActionNames.OPEN));// $NON-NLS-1$
        menu.add(makeMenuItem(JMeterUtils.getResString("menu_merge"),// $NON-NLS-1$
                "Merge", ActionNames.MERGE));// $NON-NLS-1$
        menu.add(makeMenuItem(JMeterUtils.getResString("save_as"),// $NON-NLS-1$
                "Save As", ActionNames.SAVE_AS));// $NON-NLS-1$

		addSeparator(menu);
        JMenuItem savePicture = makeMenuItem(JMeterUtils.getResString("save_as_image"),// $NON-NLS-1$
                "Save Image", ActionNames.SAVE_GRAPHICS,// $NON-NLS-1$
				KeyStrokes.SAVE_GRAPHICS);
		menu.add(savePicture);
		if (!(GuiPackage.getInstance().getCurrentGui() instanceof Printable)) {
			savePicture.setEnabled(false);
		}
        
        JMenuItem savePictureAll = makeMenuItem(JMeterUtils.getResString("save_as_image_all"),// $NON-NLS-1$
                "Save Image All", ActionNames.SAVE_GRAPHICS_ALL,// $NON-NLS-1$
                KeyStrokes.SAVE_GRAPHICS_ALL);
        menu.add(savePictureAll);

        addSeparator(menu);
        
		JMenuItem disabled = makeMenuItem(JMeterUtils.getResString("disable"),// $NON-NLS-1$
                "Disable", ActionNames.DISABLE);// $NON-NLS-1$
		JMenuItem enabled = makeMenuItem(JMeterUtils.getResString("enable"),// $NON-NLS-1$
                "Enable", ActionNames.ENABLE);// $NON-NLS-1$
		boolean isEnabled = GuiPackage.getInstance().getTreeListener().getCurrentNode().isEnabled();
		if (isEnabled) {
			disabled.setEnabled(true);
			enabled.setEnabled(false);
		} else {
			disabled.setEnabled(false);
			enabled.setEnabled(true);
		}
		menu.add(enabled);
		menu.add(disabled);
		addSeparator(menu);
		menu.add(makeMenuItem(JMeterUtils.getResString("help"), // $NON-NLS-1$
                "Help", ActionNames.HELP));// $NON-NLS-1$
	}

	public static JMenu makeMenus(String[] categories, String label, String actionCommand) {
		JMenu addMenu = new JMenu(label);
		for (int i = 0; i < categories.length; i++) {
			addMenu.add(makeMenu(categories[i], actionCommand));
		}
		return addMenu;
	}

	public static JPopupMenu getDefaultControllerMenu() {
		JPopupMenu pop = new JPopupMenu();
		pop.add(MenuFactory.makeMenus(MENU_ADD_CONTROLLER, 
				JMeterUtils.getResString("add"),// $NON-NLS-1$
				ActionNames.ADD));
		pop.add(makeMenus(MENU_PARENT_CONTROLLER, 
				JMeterUtils.getResString("insert_parent"),// $NON-NLS-1$
				ActionNames.ADD_PARENT));
		MenuFactory.addEditMenu(pop, true);
		MenuFactory.addFileMenu(pop);
		return pop;
	}

	public static JPopupMenu getDefaultSamplerMenu() {
		JPopupMenu pop = new JPopupMenu();
		pop.add(MenuFactory.makeMenus(MENU_ADD_SAMPLER, 
				JMeterUtils.getResString("add"),// $NON-NLS-1$
                ActionNames.ADD));
		pop.add(makeMenus(MENU_PARENT_SAMPLER, 
				JMeterUtils.getResString("insert_parent"),// $NON-NLS-1$
                ActionNames.ADD_PARENT));
		MenuFactory.addEditMenu(pop, true);
		MenuFactory.addFileMenu(pop);
		return pop;
	}

	public static JPopupMenu getDefaultConfigElementMenu() {
		JPopupMenu pop = new JPopupMenu();
		MenuFactory.addEditMenu(pop, true);
		MenuFactory.addFileMenu(pop);
		return pop;
	}

	public static JPopupMenu getDefaultVisualizerMenu() {
		JPopupMenu pop = new JPopupMenu();
		MenuFactory.addEditMenu(pop, true);
		MenuFactory.addFileMenu(pop);
		return pop;
	}

	public static JPopupMenu getDefaultTimerMenu() {
		JPopupMenu pop = new JPopupMenu();
		MenuFactory.addEditMenu(pop, true);
		MenuFactory.addFileMenu(pop);
		return pop;
	}

	public static JPopupMenu getDefaultAssertionMenu() {
		JPopupMenu pop = new JPopupMenu();
		MenuFactory.addEditMenu(pop, true);
		MenuFactory.addFileMenu(pop);
		return pop;
	}

	public static JPopupMenu getDefaultExtractorMenu() {
		JPopupMenu pop = new JPopupMenu();
		MenuFactory.addEditMenu(pop, true);
		MenuFactory.addFileMenu(pop);
		return pop;
	}

	/**
	 * Create a menu from a menu category.
	 * 
	 * @param category - predefined string (used as key for menuMap HashMap and messages.properties lookup)
	 * @param actionCommand - predefined string, e.g. ActionNames.ADD
	 *     @see org.apache.jmeter.gui.action.ActionNames
	 * @return the menu
	 */
	public static JMenu makeMenu(String category, String actionCommand) {
		return makeMenu((Collection) menuMap.get(category), actionCommand, JMeterUtils.getResString(category));

⌨️ 快捷键说明

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