jkfabstractmainapplication.java

来自「主要是对串口驱动的的一些控制源码!!! 在下载javacomm20-win32」· Java 代码 · 共 560 行 · 第 1/2 页

JAVA
560
字号
package de.fhm.jkf.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.util.HashMap;

import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.MenuElement;
import javax.swing.SwingConstants;

import de.fhm.jkf.resource.cl.JKFClient;
import de.fhm.jkf.resource.cl.JKFClientLogger;
import de.fhm.jkf.utils.cl.JKFUtilities;

/**
 * <br><br><center><table border="1" width="80%"><hr>
 * <strong><a href="http://jkf.sourceforge.net">The JKF Project</a></strong>
 * <p>
 * Copyright (C) 2002 by Theodor Willax
 * <p>
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * <p>
 * This library 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
 * Lesser General Public License for more details.
 * <p>
 * You should have received a copy of the <a href="http://www.gnu.org/copyleft/lesser.html">
 * GNU Lesser General Public License</a> along with this library; if not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA  02111-1307  USA
 * <hr></table></center>
 * 
 * @author Marten Wulff
 * @version $Revision: 1.9 $
 */
public abstract class JKFAbstractMainApplication
	extends JFrame
	implements JKFMainApplication {

	// the name of the MainApplication
	private String name = null;

	private JMenuBar menubar = null;
	private JToolBar toolbar = null;

	// the Id of the currently active SubApplication	
	private ApplicationId activeSubApplication = null;

	// stores all registered SubApplications
	private HashMap subApplicationInfos = new HashMap();

	/**
	 * Maps views to application id's for error handling.
	 */
	private HashMap viewIdMappings = new HashMap();

	// menu for changing SubApplications
	private JMenu changeSubApplicationsMenu = null;

	// the Menu of the currently active SubApplication
	private JMenu currentSubApplicationMenu = null;

	private JFrame welcomeFrame = null;

	public JKFAbstractMainApplication(String name, String welcomeText) {
		super(name);

		this.name = name;

		welcomeFrame = new WelcomeFrame(welcomeText);

		initMe();
		ErrorList.setMainApplication(this);
	}

	void showSubApplicationForError(Component c) {
		while (!(c instanceof JKFView)) {
			c = c.getParent();
			if (c == null) {
				if (JKFClientLogger.isDebugEnabled()) {
					JKFClientLogger.debug(
						"Error getting JKFView for Component " + c.toString());
				}
				return;
			}
		}

		ApplicationId id = (ApplicationId) this.viewIdMappings.get(c);
		((SubApplicationInfo) this.subApplicationInfos.get(id))
			.getAction()
			.actionPerformed(new ActionEvent(this, -1, "FROMSOURCE"));
	}

	/**
	 * Initializes the communication component of the JKF
	 * framework, adds the toolbar and menubar.
	 */
	private void initMe() {
		JKFClient.configure();

		this.getContentPane().setLayout(new BorderLayout());

		toolbar = new JToolBar();
		this.getContentPane().add(toolbar, BorderLayout.NORTH);

		initMenuBar();
		super.setJMenuBar(menubar);

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	/**
	 * Initializes the menu bar used by sub applications.
	 */
	private void initMenuBar() {
		menubar = new JMenuBar();
		this.changeSubApplicationsMenu = new JMenu("Sub Application");

		menubar.add(changeSubApplicationsMenu);
	}

	/**
	 * Method for registering a <code>JKFAbstractSubApplication</code> to this 
	 * <code>JKFAbstractMainApplication</code>. When adding a <code>JKFAbstractSubApplication</code>
	 * automatically a <code>ChangeSubApplicationAction</code> is generated, 
	 * and a new <code>JMenuItem</code> is instantiated and added to the 
	 * menu that holds all registered SubApplications.
	 * 
	 * @param subApplication 	the <code>JKFSubApplication</code> to add
	 */
	public void addSubApplication(JKFSubApplication subApplication) {

		buildViewApplicationIdMappings(
			subApplication.getView(),
			subApplication.getApplicationId());
		createSubApplicationInfo(subApplication);

		// if this SubApplication is the first or maybe only SubApplication
		// switch to this SubApplication
		if (subApplicationInfos.size() == 1) {
			SubApplicationInfo info =
				(SubApplicationInfo) subApplicationInfos.get(
					subApplication.getApplicationId());
			this.switchApplication(
				(ChangeSubApplicationAction) info.getAction());
		}
	}

	private void createSubApplicationInfo(JKFSubApplication subApplication) {
		// create a new ChangeSubApplicationAction and a new MenuItem, the MenuItem
		// is added to our Menu for changing the SubApplications
		ChangeSubApplicationAction a =
			new ChangeSubApplicationAction(
				subApplication.getApplicationId(),
				this,
				subApplication.getApplicationName());
		this.changeSubApplicationsMenu.add(new JMenuItem(a));

		// build the menu for the new SubApplication
		JMenu menu = buildMenuForSubApplication(subApplication);

		Component buttons[] = buildToolBarForSubApplication(subApplication);
		JKFSubApplicationPanel panel =
			new JKFSubApplicationPanel(subApplication.getView());
		//		panel.add(subApplication.getView());
		SubApplicationInfo info =
			new SubApplicationInfo(subApplication, menu, buttons, a, panel);
		this.subApplicationInfos.put(subApplication.getApplicationId(), info);
		// repaint
		changeSubApplicationsMenu.validate();
		changeSubApplicationsMenu.repaint();
	}

	private void buildViewApplicationIdMappings(
		Container container,
		ApplicationId id) {

		if (container instanceof JKFView) {
			this.viewIdMappings.put(container, id);
		}

		Component c[] = container.getComponents();

		for (int i = 0; i < c.length; i++) {
			if (c[i] instanceof Container) {
				buildViewApplicationIdMappings((Container) c[i], id);
			}
		}
	}

	/**
	 * Disposes the welcome frame and shows the main application.
	 */
	public void initEnd() {
		welcomeFrame.dispose();
		this.setVisible(true);
	}

	/**
	 * Method for switching between different <code>JKFSubApplications</code>.
	 * The old SubApplication is removed from this container and the
	 * new one is added. The actions that are linked with the SubApplications
	 * are enabled (old SubApplications) and disabled (new SubApplication).
	 * The MenuBar is also updated.
	 * 
	 * @param action	the <code>ChangeSubApplicationAction</code> that
	 * 					keeps all necessary data to change the SubApplications
	 */
	void switchApplication(ChangeSubApplicationAction action) {

		JKFSubApplication oldSub = null;
		JKFSubApplication newSub = null;

		// do nothing if the current active application is identical
		// with the new subapplication
		if (activeSubApplication != null
			&& activeSubApplication.equals(action.getSubApplicationId())) {
			return;
		}

		String actionCommand = (String) action.getValue("COMMAND");
		if (actionCommand != null && !actionCommand.equals("FROMSOURCE")) {
			ErrorList.instance().removeSelection();
		}

		// remove the active sub application
		oldSub = removeActiveSubApplication();

		// show the new sub application
		newSub = showSubApplication(action);

		// change the menus
		changeMenus(oldSub, newSub);

		// change the toolbar
		changeToolBars(oldSub, newSub);

		validate();
		repaint();
	}

	/**
	 * Removes the active sub application. If no application
	 * is active, nothing is done.
	 * 
	 * @return the removed sub application, if none is removed
	 * <code>null</code> is returned.
	 */
	private JKFSubApplication removeActiveSubApplication() {
		JKFSubApplication oldSub = null;
		if (this.activeSubApplication != null) {
			SubApplicationInfo info =
				(SubApplicationInfo) subApplicationInfos.get(
					activeSubApplication);
			oldSub = info.getSubApp();
			try {
				oldSub.deactivate();
			} catch (JKFDeactivationException dae) {
				dae.printStackTrace();
			}
			this.getContentPane().remove(info.getSubPanel());
			if (info.getAction() != null) {
				info.getAction().setEnabled(true);
			}
		}
		return oldSub;
	}

⌨️ 快捷键说明

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