From: <doc...@us...> - 2007-05-30 22:57:57
|
Revision: 75 http://openpcl.svn.sourceforge.net/openpcl/?rev=75&view=rev Author: documentsystems Date: 2007-05-30 15:57:59 -0700 (Wed, 30 May 2007) Log Message: ----------- Howard Hoagland. Added a right click on tree nodes popup menu that has "View Selected, Print All, Print Selected, Print Remaining. Implemented the View Selected from the right click menu choice to work with multiselected nodes, and with left clicking tree nodes to show pages. Made PosTree implement new interface IPluginHooksPosTree method buildTreePopupMenu that subclass plugins override to add more menu items. Modified Paths: -------------- openpcl/src/com/openpcl/viewer/tree/PosTree.java Added Paths: ----------- openpcl/src/com/openpcl/viewer/api/IPluginHooksPosTree.java Added: openpcl/src/com/openpcl/viewer/api/IPluginHooksPosTree.java =================================================================== --- openpcl/src/com/openpcl/viewer/api/IPluginHooksPosTree.java (rev 0) +++ openpcl/src/com/openpcl/viewer/api/IPluginHooksPosTree.java 2007-05-30 22:57:59 UTC (rev 75) @@ -0,0 +1,19 @@ +package com.openpcl.viewer.api; + +import javax.swing.JPopupMenu; + +/** + * The methods in this interface are designed to be overridden by a subclass of PosTree + * that is a plugin that adds additional features. + * + * Therefore, it is mandatory to not delete or rename these methods or else you'll break the plugin support + * that adds additional features. + * + * @author DocMagic, Document Systems Inc, HowardH. 5/29/07 + */ +public interface IPluginHooksPosTree { + /** + * Build the JPopupMenu to show when the user right clicks on tree nodes.<br> + * The items on this popup menu are in the String array mTreePopupMenuItems. */ + public void buildTreePopupMenu(JPopupMenu pJPopupMenu); +} Modified: openpcl/src/com/openpcl/viewer/tree/PosTree.java =================================================================== --- openpcl/src/com/openpcl/viewer/tree/PosTree.java 2007-05-26 00:33:13 UTC (rev 74) +++ openpcl/src/com/openpcl/viewer/tree/PosTree.java 2007-05-30 22:57:59 UTC (rev 75) @@ -3,12 +3,18 @@ import java.awt.Color; import java.awt.Component; import java.awt.Rectangle; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import java.security.InvalidParameterException; import java.util.NoSuchElementException; import javax.swing.BorderFactory; import javax.swing.Icon; import javax.swing.JLabel; +import javax.swing.JMenuItem; +import javax.swing.JPopupMenu; import javax.swing.JTree; import javax.swing.UIManager; import javax.swing.event.TreeSelectionEvent; @@ -23,16 +29,22 @@ import com.openpcl.pclrenderimage.util.PriDebug; import com.openpcl.viewer.OpenPCLViewer; +import com.openpcl.viewer.api.IPluginHooksPosTree; import com.openpcl.viewer.panels.PosView; import com.openpcl.viewer.util.PosReadImageIcon; -public class PosTree { +public class PosTree implements IPluginHooksPosTree { private PosView mPosView = null; private JTree mPagesJTree = null; private TreePath mLastSelectedTreePath = null; private PosTreeNode mClickedNode = null; + protected TreePath mRightClickedTreePath = null; + protected PosTreeNode mRightClickedNode = null; private DefaultMutableTreeNode mTreeNodeRoot = null; private String mRootNodeName = null; + private static String[] sTreePopupMenuItems = new String[] { + "View Selected", "Print All", "Print Selected", "Print Remaining"}; + private JPopupMenu mTreeJPopupMenu = null; protected static final String sPCL_ICON = "Eye.gif"; protected static final String sPAGE_ICON = "Page.gif"; @@ -71,6 +83,9 @@ mPagesJTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); mPagesJTree.setShowsRootHandles(false); mPagesJTree.setExpandsSelectedPaths(true); + + // Add the right click mouse listener + mPagesJTree.addMouseListener(new RightclickTreeMouseListener()); } /** Get the JTree that is on the screen */ @@ -115,7 +130,7 @@ System.gc(); } - public TreeModel getTreeModel() { + public TreeModel getTreeModel() { if (mPagesJTree == null) { return null; } else { return mPagesJTree.getModel(); } @@ -315,15 +330,21 @@ if (mPagesJTree == null) { return; } - mLastSelectedTreePath = e.getPath(); + if (mRightClickedTreePath == null) { + // Handle left click on tree node + mLastSelectedTreePath = e.getPath(); + } else { + // Handle right click on tree node, then on the popup menu, pick "View Selected". Ignore muti selected nodes. + mLastSelectedTreePath = mRightClickedTreePath; + } + Object tSelectedNode = mLastSelectedTreePath.getLastPathComponent(); DefaultMutableTreeNode tFirstLeafNode = (DefaultMutableTreeNode) tSelectedNode; while (!tFirstLeafNode.isLeaf()) { try { - // If not a Leaf node, then this tree is not showing a normal PCL file because it has multi level tree nodes. - // In this case, this might be a DSI Blocument tree node that might be a Document or Borrower node, - // so keep looking indented to the right to find the first leaf page to view + // If not a Leaf node, then this tree has multi level tree nodes, so keep looking indented to the right + // to find the first leaf page to view tFirstLeafNode = (DefaultMutableTreeNode)tFirstLeafNode.getFirstChild(); } catch (NoSuchElementException nse) { break; // break out of the while, with it set to the last node assigned @@ -388,6 +409,100 @@ } /** + * Build the JPopupMenu to show when the user right clicks on tree nodes.<br> + * The items on this popup menu are in the String array sTreePopupMenuItems.<br> + * Subclass plugins override this method so do not delete or rename this method. */ + public void buildTreePopupMenu(JPopupMenu pJPopupMenu) { + JMenuItem tJMenuItem = new JMenuItem(sTreePopupMenuItems[0]); // View Selected + tJMenuItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent pE) { + // The user clicked "View Selected" on the popup menu. + // The below line causes the right clicked on tree node to be rendered and viewed. + // The mRightClickedTreePath was saved when the JPopupMenu was put on the screen in showTreeNodeMenuPopup() + if (mRightClickedTreePath != null) { mPagesJTree.setSelectionPath(mRightClickedTreePath); } + // After setting the view to the right clicked tree node, set mRightClickedTreePath to null + mRightClickedTreePath = null; + mRightClickedNode = null; + }}); + pJPopupMenu.add(tJMenuItem); + pJPopupMenu.addSeparator(); + + tJMenuItem = new JMenuItem(sTreePopupMenuItems[1]); // Print All + tJMenuItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent pE) { + // The user clicked "Print All" on the popup menu. + System.out.println("To do: Handle Print All"); + }}); + pJPopupMenu.add(tJMenuItem); + + tJMenuItem = new JMenuItem(sTreePopupMenuItems[2]); // Print Selected + tJMenuItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent pE) { + // The user clicked "Print Selected" on the popup menu. + System.out.println("To do: Handle Print Selected"); + }}); + pJPopupMenu.add(tJMenuItem); + + tJMenuItem = new JMenuItem(sTreePopupMenuItems[3]); // Print Remaining + tJMenuItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent pE) { + // The user clicked "Print Remaining" on the popup menu. + System.out.println("To do: Handle Print Remaining"); + }}); + pJPopupMenu.add(tJMenuItem); + } + + /** MouseAdapter class for when the user right clicks on a node in the tree.<br> + * This checks for mouse event for popup menu in both mouseReleased() and mousePressed() because:<br> + * <br> + * As stated at http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html + * "The exact gesture that should bring up a popup menu varies by look and feel. In Microsoft Windows, the user + * by convention brings up a popup menu by releasing the right mouse button while the cursor is over a component + * that is popup-enabled."<br> + * <br> + * In that case, the mouseReleased() method will be handling the event. But on other platforms + * and/or look and feel settings, the popup menu should show upon mousePressed() and not upon mouseReleased(). + */ + protected class RightclickTreeMouseListener extends MouseAdapter { + public void mouseReleased(MouseEvent pE) { + if ( pE.isPopupTrigger() ) { + showTreeNodeMenuPopup(pE.getX(), pE.getY()); + } + } + + public void mousePressed(MouseEvent pE) { + if ( pE.isPopupTrigger() ) { + showTreeNodeMenuPopup(pE.getX(), pE.getY()); + } + } + + private void showTreeNodeMenuPopup(int pMouseX, int pMouseY) { + // Get the TreePath at the right clicked mouse (x,y) position in the JTree coordinate space + mRightClickedTreePath = getPagesJTree().getPathForLocation(pMouseX, pMouseY); + if (mRightClickedTreePath == null) { return; } + + // Get the node at that TreePath + DefaultMutableTreeNode node = (DefaultMutableTreeNode)mRightClickedTreePath.getLastPathComponent(); + + // Save the PosTreeNode that was right clicked on, because all the menu items on the right click menu + // work with the right clicked node + mRightClickedNode = (PosTreeNode)node.getUserObject(); + + // Must make a new JPopupMenu each time the user right clicks in case the user changed the Look and Feel, + // so that the JPopupMenu's L&F is the same. + // TODO is track when the user changes the L&F and only make a new JPopupMenu if the user changed the L&F + mTreeJPopupMenu = new JPopupMenu(); + // Right click on tree nodes brings up the pop up menu. Pass in the JPopupMenu because it's passed to subclass plugins + // that can then add more JMenuItems with their action listeners + buildTreePopupMenu(mTreeJPopupMenu); + mTreeJPopupMenu.pack(); + + // Show the right click menu + mTreeJPopupMenu.show(mPagesJTree, pMouseX, pMouseY); + } + } + + /** * Tree cell renderer class specifies the icons to use for the different node types: * 1. Pcl page * 2. Blockument doc with pages under it (icon for open and another icon for closed) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |