jkfabstractmainapplication.java

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

JAVA
560
字号
	/**
	 * Shows a new sub application. The action parameter contains
	 * the information about the wanted new sub application (ID).
	 * 
	 * @return the new sub application, if the action is <code>null</code>
	 * <code>null</code> is returned.
	 */
	private JKFSubApplication showSubApplication(ChangeSubApplicationAction action) {
		JKFSubApplication newSub = null;
		if (action != null) {
			SubApplicationInfo info =
				(SubApplicationInfo) subApplicationInfos.get(
					action.getSubApplicationId());
			newSub = info.getSubApp();
			this.activeSubApplication = newSub.getApplicationId();
			action.setEnabled(false);
			try {
				newSub.activate();
			} catch (JKFActivationException ae) {
				ae.printStackTrace();
			}
			this.getContentPane().add(info.getSubPanel(), BorderLayout.CENTER);
		}
		return newSub;
	}

	/**
	 * Method for building a JMenu, that contains all Menus from the 
	 * SubApplications MenuBar.
	 * 
	 * @param subApplication 	the SubApplication for which the Menu is build
	 */
	private JMenu buildMenuForSubApplication(JKFSubApplication subApplication) {
		MenuElement[] subMenus = subApplication.getMenuBarElements();
		JMenu main = new JMenu(subApplication.getApplicationName());

		for (int i = 0; i < subMenus.length; i++) {
			main.add(subMenus[i].getComponent());
		}
		return main;
	}

	private Component[] buildToolBarForSubApplication(JKFSubApplication subApplication) {
		return subApplication.getToolBar().getComponents();
	}

	/**
	 * Removes a <code>JKFAbstractSubApplication</code>. The <code>JMenuItem</code> 
	 * that is used to switch to this SubApplication is removed also.
	 * 
	 * @param subApplication 	the <code>JKFAbstractSubApplication</code> to remove
	 */
	public void removeSubApplication(JKFSubApplication subApplication) {

		// check if the SubApplication to remove is active 
		if (subApplication.getApplicationId().equals(activeSubApplication)) {
			switchApplication(null);
			activeSubApplication = null;
		}

		// remove the subApplication from the map
		subApplicationInfos.remove(subApplication.getApplicationId());

		// remove the MenuItem
		JMenuItem item = null;
		ChangeSubApplicationAction action = null;
		if (changeSubApplicationsMenu != null) {
			for (int i = 0, n = changeSubApplicationsMenu.getItemCount();
				i < n;
				i++) {
				item = changeSubApplicationsMenu.getItem(i);
				action = (ChangeSubApplicationAction) item.getAction();
				if (subApplication
					.getApplicationId()
					.equals(action.getSubApplicationId())) {
					changeSubApplicationsMenu.remove(item);
					break;
				}
			}
		}
	}

	public void addMenu(JMenu menu) {
		menubar.add(menu);
	}

	/**
	 * Overwritten from super class <code>JFrame</code>.
	 * 
	 * @param bar	the <code>JMenuBar</code> for this Frame
	 * 
	 * @see javax.swing.JFrame#setJMenuBar(JMenuBar)
	 */
	public void setJMenuBar(JMenuBar bar) {
		this.menubar = bar;

		// always add the JMenu for switching between different
		// JKFSubApplications to the MenuBar
		menubar.add(this.changeSubApplicationsMenu);

		super.setJMenuBar(menubar);

		invalidate();
		repaint();
	}

	public void setToolBar(JToolBar bar) {
		this.getContentPane().remove(toolbar);

		this.toolbar = bar;
		this.getContentPane().add(toolbar, BorderLayout.NORTH);
	}

	public void addToolBarAction(Action action) {
		toolbar.add(action);
	}

	/**
	 * Switches the menu from the one of the old
	 * sub application to one of the new sub application.
	 */
	private void changeMenus(
		JKFSubApplication oldApp,
		JKFSubApplication newApp) {

		if (oldApp != null) {
			JMenu oldActive =
				((SubApplicationInfo) subApplicationInfos
					.get(oldApp.getApplicationId()))
					.getSubMenu();
			menubar.remove(oldActive);
		}

		if (newApp != null) {
			JMenu newActive =
				((SubApplicationInfo) subApplicationInfos
					.get(newApp.getApplicationId()))
					.getSubMenu();
			if(newActive.getItemCount() > 0) {		
				menubar.add(newActive);
			}
		}

		menubar.invalidate();
		menubar.repaint();
	}

	/**
	 * Switches the toolbar from the one of the old
	 * sub application to the one of the new sub application.
	 */
	private void changeToolBars(
		JKFSubApplication oldApp,
		JKFSubApplication newApp) {

		if (oldApp != null) {
			Component[] oldComps =
				((SubApplicationInfo) subApplicationInfos
					.get(oldApp.getApplicationId()))
					.getSubToolBar();
			for (int i = 0; i < oldComps.length; i++) {
				this.toolbar.remove(oldComps[i]);
			}
		}

		if (newApp != null) {
			Component[] newComps =
				((SubApplicationInfo) subApplicationInfos
					.get(newApp.getApplicationId()))
					.getSubToolBar();
			for (int i = 0; i < newComps.length; i++) {
				toolbar.add(newComps[i]);
			}
		}

		toolbar.invalidate();
		toolbar.repaint();

		invalidate();
		repaint();
	}

	private class SubApplicationInfo {
		private JKFSubApplication subApp;
		private JMenu subMenu;
		private Component subToolBar[];
		private ChangeSubApplicationAction action;
		private JKFSubApplicationPanel subPanel;

		public SubApplicationInfo(
			JKFSubApplication subApp,
			JMenu subMenu,
			Component subToolBar[],
			ChangeSubApplicationAction action,
			JKFSubApplicationPanel subPanel) {
			this.subApp = subApp;
			this.subMenu = subMenu;
			this.subToolBar = subToolBar;
			this.action = action;
			this.subPanel = subPanel;
		}

		/**
		 * Returns the action.
		 * @return Action
		 */
		public ChangeSubApplicationAction getAction() {
			return action;
		}

		/**
		 * Returns the subApp.
		 * @return JKFSubApplication
		 */
		public JKFSubApplication getSubApp() {
			return subApp;
		}

		/**
		 * Returns the subMenu.
		 * @return JMenu
		 */
		public JMenu getSubMenu() {
			return subMenu;
		}

		/**
		 * Returns the subToolBar.
		 * @return Component[]
		 */
		public Component[] getSubToolBar() {
			return subToolBar;
		}
		/**
		 * Returns the subPanel.
		 * @return JKFSubApplicationPanel
		 */
		public JKFSubApplicationPanel getSubPanel() {
			return subPanel;
		}

		/**
		 * Sets the subPanel.
		 * @param subPanel The subPanel to set
		 */
		public void setSubPanel(JKFSubApplicationPanel subPanel) {
			this.subPanel = subPanel;
		}

	}

	/**
	 * This frame is shown during application startup.
	 */
	private class WelcomeFrame extends JFrame {
		private String welcomeText = null;

		public WelcomeFrame(String welcomeText) {
			super("JKF");

			this.welcomeText = welcomeText;
			init();
		}

		private void init() {
			Container c = this.getContentPane();
			c.setLayout(new BorderLayout());
			JLabel label = new JLabel(welcomeText, SwingConstants.CENTER);
			c.add(label, BorderLayout.CENTER);

			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

			this.setSize(300, 150);
			JKFUtilities.centerComponent(null, this);
			this.setVisible(true);
		}
	}

}

⌨️ 快捷键说明

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