001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.ecoinformatics.seek.ecogrid.quicksearch;
031
032import java.awt.event.MouseAdapter;
033import java.awt.event.MouseEvent;
034import java.awt.event.MouseListener;
035
036import javax.swing.JMenuItem;
037import javax.swing.JPopupMenu;
038import javax.swing.tree.TreeModel;
039import javax.swing.tree.TreePath;
040
041import ptolemy.vergil.tree.PTree;
042
043/**
044 * This class extends the PTree class of Ptolemy. It will display the search
045 * result in ResultPanel. This adds two new features - 1) a double click of
046 * resultrecord will get metadata 2) add a right click button to get a new menu
047 * - get metadata. Metadata and
048 * 
049 *@author Jing Tao
050 *@created February 17, 2005
051 */
052public class DataSearchResultTree extends PTree {
053        private JPopupMenu popup;
054        private JMenuItem getMetadataMenuItem;
055        private ResultPanel panel;
056
057        /**
058         * Consturctor of Tree.
059         * 
060         *@param model
061         *            TreeModel
062         *@param panel
063         *            ResultPanel pass this to GetMetadataAction in order to get
064         *            Configuration
065         */
066        public DataSearchResultTree(TreeModel model, ResultPanel panel) {
067                super(model);
068                this.panel = panel;
069                MouseListener popupListener = new PopupListener();
070                this.addMouseListener(popupListener);
071        }
072
073        /*
074         * This class will add a right click menu for get metadata info
075         */
076        /**
077         * Description of the Class
078         * 
079         *@author berkley
080         *@created February 17, 2005
081         */
082        class PopupListener extends MouseAdapter {
083                // on the Mac, popups are triggered on mouse pressed, while
084                // mouseReleased triggers them on the PC; use the trigger flag to
085                // record a trigger, but do not show popup until the
086                // mouse released event
087                boolean trigger = false;
088
089                /**
090                 * Description of the Method
091                 * 
092                 *@param e
093                 *            Description of the Parameter
094                 */
095                public void mousePressed(MouseEvent e) {
096                        // maybeShowPopup(e);
097                        if (e.isPopupTrigger()) {
098                                trigger = true;
099                        }
100                }
101
102                /**
103                 * Description of the Method
104                 * 
105                 *@param e
106                 *            Description of the Parameter
107                 */
108                public void mouseReleased(MouseEvent e) {
109                        maybeShowPopup(e);
110                }
111
112                /**
113                 * Description of the Method
114                 * 
115                 *@param e
116                 *            Description of the Parameter
117                 */
118                private void maybeShowPopup(MouseEvent e) {
119                        if ((e.isPopupTrigger()) || (trigger)) {
120
121                                trigger = false;
122                                TreePath selPath = getPathForLocation(e.getX(), e.getY());
123                                if ((selPath != null)) {
124                                        Object ob = selPath.getLastPathComponent();
125                                        // only show resultrecord(not show menu in resultdetails)
126                                        if (ob != null && ob instanceof ResultRecord) {
127                                                ResultRecord resultItem = (ResultRecord) ob;
128                                                // String namespace =
129                                                // "eml://ecoinformatics.org/eml-2.0.0";
130                                                // resultItem.setNamespace(namespace);
131                                                GetMetadataAction getMetadataAction = new GetMetadataAction(
132                                                                resultItem);
133                                                getMetadataMenuItem = new JMenuItem(getMetadataAction);
134                                                popup = new JPopupMenu();
135                                                popup.add(getMetadataMenuItem);
136
137                                                setSelectionPath(selPath);
138                                                popup.show(e.getComponent(), e.getX(), e.getY());
139                                        }
140                                }
141                        }
142                }
143        }
144
145}