All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.becauseQA.window.ui.jmenu.JPopupMenuUtils Maven / Gradle / Ivy

package com.github.becauseQA.window.ui.jmenu;

import java.awt.Rectangle;

/*-
 * false
 * commons-window
 * sectionDelimiter
 * Copyright (C) 2016 Alter Hu
 * sectionDelimiter
 * Licensed 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.
 * #L%
 */

/*-
 * #%L
 * commons-window
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2016 Alter Hu
 * %%
 * Licensed 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.
 * #L%
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.plaf.TabbedPaneUI;

import org.apache.log4j.Logger;

import com.github.becauseQA.lang.ClipboardUtils;
import com.github.becauseQA.window.ui.jframe.JStatusBar;
import com.github.becauseQA.window.ui.jtable.RowDataModel;

public class JPopupMenuUtils {

	private static Logger log = Logger.getLogger(JPopupMenuUtils.class);

	public static void tablePopupMenu(JTable table, RowDataModel rowTableModel) {
		JPopupMenu popupMenu = new JPopupMenu();
		JMenuItem mntmViewDetail = new JMenuItem("View Detail");
		mntmViewDetail.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// view the detail
			}
		});
		popupMenu.add(mntmViewDetail);

		JSeparator separatorCopy = new JSeparator();
		popupMenu.add(separatorCopy);

		JMenuItem mntmCut = new JMenuItem("Cut");
		mntmCut.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				// add the cut operation
				int row = table.getSelectedRow();
				if (row > 0)
					rowTableModel.removeRow(row);

			}
		});
		popupMenu.add(mntmCut);

		JMenuItem mntmCopy = new JMenuItem("Copy");
		mntmCopy.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				// add the copy operation
				int selectedRow = table.getSelectedRow();
				if (selectedRow > 0) {
					List object = (List) rowTableModel.getRowsDataAsList(selectedRow).get(0);
					String rowdata = object.toString();
					ClipboardUtils.setClipboardContents(rowdata);
					log.info("Copied row " + selectedRow + " data: " + rowdata + " in the clipboard.");
				}
			}
		});
		popupMenu.add(mntmCopy);

		JSeparator separatorExport = new JSeparator();
		popupMenu.add(separatorExport);

		JMenu mnExport = new JMenu("Export");
		popupMenu.add(mnExport);

		JMenuItem mntmTxt = new JMenuItem("Txt");
		mntmTxt.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

			}
		});
		mnExport.add(mntmTxt);

		JMenuItem mntmClipborad = new JMenuItem("Clipborad");
		mntmClipborad.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int selectedRow = table.getSelectedRow();
				if (selectedRow > 0) {
					List object = (List) rowTableModel.getRowsDataAsList(selectedRow).get(0);
					String rowdata = object.toString();
					ClipboardUtils.setClipboardContents(rowdata);
					log.info("Copied row " + selectedRow + " data: " + rowdata + " in the clipboard.");
				}
			}
		});
		mnExport.add(mntmClipborad);

		JMenuItem mntmExcel = new JMenuItem("Excel");
		mntmExcel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

			}
		});
		mnExport.add(mntmExcel);

		JSeparator separatorDelete = new JSeparator();
		popupMenu.add(separatorDelete);

		JMenuItem mntmDelete = new JMenuItem("Delete");
		mntmDelete.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int[] selectedRow = table.getSelectedRows();
				rowTableModel.removeRow(selectedRow);

			}
		});
		popupMenu.add(mntmDelete);

		// add the popup menu
		table.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				popupMenu.show(e.getComponent(), e.getX(), e.getY());
			}
		});

	}

	public static JPopupMenu TabbedPanePopupMenu(JTabbedPane tabbedPane, JStatusBar statusBar, AbstractAction saveAction,
			AbstractAction saveAllAction, AbstractAction pasteAction, AbstractAction closeAction,
			AbstractAction closeAllAction) {
		JPopupMenu tabMenu = new JPopupMenu();

		JMenuItem saveMenuItem = new JMenuItem(saveAction);
		saveMenuItem.setToolTipText(null);
		new JMenuItemStatusBarChangeHandler(saveMenuItem, (String) saveAction.getValue(Action.LONG_DESCRIPTION),
				statusBar);
		tabMenu.add(saveMenuItem);

		JMenuItem saveAllMenuItem = new JMenuItem(saveAllAction);
		saveAllMenuItem.setToolTipText(null);
		new JMenuItemStatusBarChangeHandler(saveAllMenuItem, (String) saveAllAction.getValue(Action.LONG_DESCRIPTION),
				statusBar);
		tabMenu.add(saveAllMenuItem);

		tabMenu.addSeparator();

		JMenuItem pasteMenuItem = new JMenuItem(pasteAction);
		pasteMenuItem.setToolTipText(null);
		new JMenuItemStatusBarChangeHandler(pasteMenuItem, (String) pasteAction.getValue(Action.LONG_DESCRIPTION),
				statusBar);
		tabMenu.add(pasteMenuItem);

		tabMenu.addSeparator();

		JMenuItem closeMenuItem = new JMenuItem(closeAction);
		closeMenuItem.setToolTipText(null);
		new JMenuItemStatusBarChangeHandler(closeMenuItem, (String) closeAction.getValue(Action.LONG_DESCRIPTION),
				statusBar);
		tabMenu.add(closeMenuItem);

		JMenuItem closeallMenuItem = new JMenuItem(closeAllAction);
		closeallMenuItem.setToolTipText(null);
		new JMenuItemStatusBarChangeHandler(closeallMenuItem, (String) closeAllAction.getValue(Action.LONG_DESCRIPTION),
				statusBar);
		tabMenu.add(closeallMenuItem);

		tabbedPane.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				if (e.isPopupTrigger()) {
					showpopupMenu(e);
				}
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				if (e.isPopupTrigger()) {
					showpopupMenu(e);
				}
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				if (e.getButton() == MouseEvent.BUTTON2) {

				}
			}

			public void showpopupMenu(MouseEvent e) {

				int tabCount = tabbedPane.getTabCount();
				TabbedPaneUI tpu = tabbedPane.getUI();

				for (int i = 0; i < tabCount; i++) {
					Rectangle rect = tpu.getTabBounds(tabbedPane, i);

					int x = e.getX();
					int y = e.getY();

					if (x < rect.x || x > rect.x + rect.width || y < rect.y || y > rect.y + rect.height) {
						continue;
					}

					tabMenu.show(e.getComponent(), e.getX(), e.getY());
					break;
				}

			}
		});

		return tabMenu;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy