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

📄 menupane.java

📁 一个简易的java画图软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package app.pane;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.util.StringTokenizer;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;

import net.NetMessage;

import draw.Draw;
import draw.InnerFrame;
import draw.PaintCanvas;
import draw.View;
import draw.figuare.AbstractFiguare;
import draw.figuare.TextFiguare;

import app.Application;
import app.action.Actions;
import app.action.PaintAction;

import skin.Display;

/**
 * 生成菜单的类。<br/> 主要生成主窗口的菜单条和画布的弹出菜单
 * 
 * @author Thihy
 * 
 */
public class MenuPane {
	protected JMenuBar menubar;
	protected Display display;
	protected Application app;

	public MenuPane(Application app) {
		display = new Display();
		this.app = app;
		menubar = createMenuBar();
	}

	/**
	 * 获取生成的菜单条
	 * 
	 * @return 生成的菜单条
	 */
	public JMenuBar getMenuBar() {

		return menubar;
	}

	/**
	 * 创建菜单条
	 * 
	 * @return
	 */
	private JMenuBar createMenuBar() {
		JMenuBar mb = new JMenuBar();

		String mbList = getValue("", "main");
		StringTokenizer st = new StringTokenizer(mbList);
		while (st.hasMoreTokens()) {
			String str = st.nextToken();
			mb.add(createMenu(str));
		}
		return mb;
	}

	private JMenu createMenu(String menuName) {
		String menuTitle = getValue(menuName, menuName);
		JMenu menu = new JMenu(menuTitle);

		String menm = getValue(menuName, menuName + "Mnem");
		if (menm != null && menm.length() > 0) {
			char menmChar = menm.charAt(0);
			if (menuTitle.indexOf(menmChar) < 0) {
				menu.setText(menuTitle + "(" + menmChar + ")");
			}
			menu.setMnemonic(menmChar);
		}
		StringTokenizer st = new StringTokenizer(getValue(menuName, menuName
				+ "Menu"));
		while (st.hasMoreTokens()) {
			String str = st.nextToken();
			if (str.equals("-")) {
				menu.addSeparator();
			} else {
				JMenuItem mi = createMenuItem(menuName, str);
				if (mi != null)
					menu.add(mi);
			}
		}

		return menu;
	}

	private JMenuItem createMenuItem(String menuName, String miName) {
		String miTitle = getValue(menuName, miName);
		JMenuItem mi = new JMenuItem(miTitle);
		String menm = getValue(menuName, miName + "Mnem");
		if (menm != null && menm.length() > 0) {
			char menmChar = menm.charAt(0);
			if (miTitle.indexOf(menmChar) < 0) {
				mi.setText(miTitle + "(" + menmChar + ")");
			}
			mi.setMnemonic(menmChar);
		}
		String acc = getValue(menuName, miName + "Acc");
		if (acc != null) {
			mi.setAccelerator(getStroke(acc));
		}
		PaintAction action = Actions.getAction(miName + "Action");
		if (action != null) {
			action.setApplication(app);
			mi.addActionListener(action);
			action.addPropertyChangeListener(createActionChangeListener(mi));
			mi.setEnabled(action.isEnabled());
		} else {
			mi.setEnabled(false);
		}

		return mi;
	}

	private PropertyChangeListener createActionChangeListener(JMenuItem b) {
		return new ActionChangedListener(b);
	}

	/**
	 * 内部类,用于当Action禁用时,菜单项会紧随着被禁用
	 * 
	 * @author Thihy
	 * 
	 */
	class ActionChangedListener implements PropertyChangeListener {
		JMenuItem menuItem;

		ActionChangedListener(JMenuItem mi) {
			super();
			this.menuItem = mi;
		}

		public void propertyChange(PropertyChangeEvent e) {
			String propertyName = e.getPropertyName();
			if (e.getPropertyName().equals(Action.NAME)) {
				String text = (String) e.getNewValue();
				menuItem.setText(text);
			} else if (propertyName.equals("enabled")) {
				Boolean enabledState = (Boolean) e.getNewValue();
				menuItem.setEnabled(enabledState.booleanValue());
			}
		}
	}

	private KeyStroke getStroke(String acc) {
		if (acc == null)
			return null;
		String ks = "";

		StringTokenizer st = new StringTokenizer(acc, "+");
		int i = st.countTokens();
		while (i > 0) {
			String str = st.nextToken();
			switch (i) {
			case 3: // Ctrl,Alt,Shift
			case 2:
				if (str.equalsIgnoreCase("Ctrl")) {
					ks += " control ";
				} else if (str.equalsIgnoreCase("Alt")) {
					ks += " alt ";
				} else if (str.equalsIgnoreCase("Shift")) {
					ks += " shift ";
				}
				break;
			case 1: // 字符
				if (str.length() != 1)
					return null;
				else {
					ks += str;
				}
				break;
			default:

			}
			--i;
		}
		return KeyStroke.getKeyStroke(ks);
	}

	protected String getValue(String sect, String key) {
		String str = display.getMenuValue(sect + "." + key);
		return str;
	}

	public JPanel creatPanel(final Draw draw) {
		final int BUTTON_SIZE = 14;
		final JPanel opPane = new JPanel();
		;
		opPane.setLayout(new FlowLayout(FlowLayout.TRAILING));
		JButton minimize = new JButton(new ImageIcon(getClass().getResource(
				"images/minimize.png")));
		minimize.setToolTipText("最小化");
		minimize.setMargin(new Insets(0, 0, 0, 0));
		minimize.setContentAreaFilled(false);
		minimize.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
		minimize.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				InnerFrame i = (InnerFrame) draw.getCurrentView().getViewPort();
				try {
					i.setMaximum(false);
					i.setIcon(true);
				} catch (PropertyVetoException ex) {
					ex.printStackTrace();
				}
				i.showNorthPanel();
				opPane.setVisible(false);
			}

		});
		opPane.add(minimize);

		JButton restore = new JButton(new ImageIcon(getClass().getResource(
				"images/restore.png")));
		restore.setToolTipText("还原");
		restore.setMargin(new Insets(0, 0, 0, 0));
		restore.setContentAreaFilled(false);
		restore.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
		restore.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				draw.restoreAllViews();
			}

		});
		opPane.add(restore);

		JButton close = new JButton(new ImageIcon(getClass().getResource(
				"images/close.png")));
		close.setToolTipText("关闭");
		close.setMargin(new Insets(0, 0, 0, 0));
		close.setContentAreaFilled(false);
		close.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
		close.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				draw.closeCurrentView();
			}

		});
		opPane.add(close);
		menubar.add(opPane);
		opPane.setVisible(false);
		return opPane;
	}

	/**
	 * 生成弹出采用,供画布使用 <br/> 对指定的图形进行操作,如选择,旋转等
	 * 
	 * @param gbc
	 *            画布
	 * @param fig
	 *            图形
	 */
	public static JPopupMenu creatCanvasPopmenu(final PaintCanvas gbc,
			final AbstractFiguare fig) {
		if (gbc == null || fig == null)
			return null;

		JPopupMenu popmenu = new JPopupMenu();

		// ----------------------- 选择 ------------------------
		JMenuItem selectMi = new JMenuItem("选择");
		popmenu.add(selectMi);

		Action selectAction = new AbstractAction() {

			@Override
			public void actionPerformed(ActionEvent e) {
				gbc.setCurrentShape(fig);
			}
		};
		selectMi.addActionListener(selectAction);

		// ------------------------- 删除 ------------------------
		JMenuItem delMi = new JMenuItem("删除");
		popmenu.add(delMi);

		Action delAction = new AbstractAction() {

			@Override
			public void actionPerformed(ActionEvent e) {
				gbc.delFiguare(fig);
			}
		};
		delMi.addActionListener(delAction);

		// ------------------------- 旋转------------------------
		JMenu rotateM = new JMenu("旋转");
		rotateM.setEnabled(fig.getID() != AbstractFiguare.STRAIGHT_LINE);
		if (fig.getID() != AbstractFiguare.STRAIGHT_LINE) {
			popmenu.addSeparator();
			popmenu.add(rotateM);
		}

		rotate: {
			JMenuItem rotate90 = new JMenuItem("顺时针旋转90度");
			JMenuItem rotate270 = new JMenuItem("逆时针旋转90度");
			JMenuItem rotateRedo = new JMenuItem("重置旋转");

			rotateM.add(rotate90);
			rotateM.add(rotate270);
			rotateM.add(rotateRedo);

			Action rotate90Action = new AbstractAction() {

				@Override
				public void actionPerformed(ActionEvent e) {
					fig.rotate(90);
					gbc.updateNetFiguare(NetMessage.MES_CHANGE_FIGUARE, fig);
					gbc.putClientProperty(View.CANVAS_CHANGED_PROPERTY,
							fig.serialId + ":向右旋转90度");
					gbc.repaint();
				}

			};

			Action rotate270Action = new AbstractAction() {

⌨️ 快捷键说明

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