001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-08-24 22:44:14 +0000 (Mon, 24 Aug 2015) $' 
007 * '$Revision: 33630 $'
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.kepler.gui;
031
032import java.awt.Component;
033import java.awt.event.ActionEvent;
034import java.awt.event.ActionListener;
035import java.awt.event.MouseListener;
036import java.lang.ref.WeakReference;
037import java.util.Vector;
038
039import javax.swing.tree.TreeModel;
040import javax.swing.tree.TreePath;
041
042import org.apache.commons.logging.Log;
043import org.apache.commons.logging.LogFactory;
044
045import ptolemy.actor.gui.Configuration;
046import ptolemy.vergil.tree.PTree;
047
048/**
049 * A Wrapper class that adds right click support and custom Kepler Ontology
050 * icons to a PTree
051 * 
052 * @author Chad Berkley
053 * @since February 17, 2005
054 * @version $Id: AnnotatedPTree.java 33630 2015-08-24 22:44:14Z crawl $
055 * @since Kepler 1.0
056 */
057public class AnnotatedPTree extends PTree {
058        private static final Log log = LogFactory.getLog(AnnotatedPTree.class
059                        .getName());
060        private static final boolean isDebugging = log.isDebugEnabled();
061
062        public static final String ALTERNATE_LIBRARY_POPUP_ATTRIBUTE_NAME = "_alternateGetPopupAction";
063
064        private Vector<WeakReference<ActionListener>> listeners;
065        private Component parent;
066        Configuration config = null;
067        private boolean showRootIcon = true;
068        public boolean isSearch = false;
069
070        private MouseListener mouseListener = null;
071
072        /**
073         * Constructor
074         * 
075         * @param model
076         *            the model to build the tree out of
077         */
078        public AnnotatedPTree(TreeModel model, Component parent) {
079                super(model);
080                setParentComponent(parent);
081        }
082
083        public AnnotatedPTree(TreeModel model, Component parent,
084                        boolean showRootIcon) {
085                super(model);
086                setParentComponent(parent);
087                setShowRootIcon(showRootIcon);
088        }
089
090        public void setShowRootIcon(boolean showRootIcon) {
091                this.showRootIcon = showRootIcon;
092        }
093
094        /**
095         * To use a different Mouse listener set it here before calling
096         * initAnnotatedPTree();
097         * 
098         * @param listener
099         */
100        public void setMouseListener(MouseListener listener) {
101                this.mouseListener = listener;
102                this.addMouseListener(mouseListener);
103        }
104
105        public Component getParentComponent() {
106                return this.parent;
107        }
108
109        public void setParentComponent(Component parent) {
110                this.parent = parent;
111        }
112
113        public void initAnotatedPTree() {
114                if (isDebugging)
115                        log.debug("initAnotatedPTree()");
116                if (mouseListener == null) {
117                        MouseListener popupListener = new AnnotatedPTreePopupListener(this);
118                        setMouseListener(popupListener);
119                }
120                listeners = new Vector<WeakReference<ActionListener>>();
121                this.setShowsRootHandles(true);
122
123                // On Windows the row height is too short
124                String osName = System.getProperty("os.name");
125                if (osName.startsWith("Win")) {
126                        int rowHeight = this.getRowHeight();
127                        this.setRowHeight(rowHeight + 2);
128                }
129
130                OntologyTreeCellRenderer otcr = new OntologyTreeCellRenderer(
131                                showRootIcon);
132                this.setCellRenderer(otcr);
133        }
134
135        /**
136         * add a listener for events
137         */
138        public void addListener(ActionListener listener) {
139                listeners.addElement(new WeakReference(listener));
140        }
141
142        /**
143         * notify this tree that the mouse is dragging an object over the x,y
144         * position
145         */
146        public void notifyDragOver(int x, int y) {
147                TreePath location = this.getClosestPathForLocation(x, y);
148                this.clearSelection();
149                this.setSelectionPath(location);
150        }
151
152        /**
153         * notify all listeners of events
154         */
155        private void notifyListeners(ActionEvent event) {
156                for (int i = 0; i < listeners.size(); i++) {
157                        ActionListener listener = (ActionListener) listeners.elementAt(i);
158                        listener.actionPerformed(event);
159                }
160        }
161
162        /**
163         * Listener used to changes from the NewFolderFrame
164         */
165        private class ActionHandler implements ActionListener {
166                /**
167                 * Description of the Method
168                 * 
169                 * @param event
170                 *            Description of Parameter
171                 */
172                public void actionPerformed(ActionEvent event) {
173                        String command = event.getActionCommand();
174                        if (command.equals("new_folder_created")
175                                        || command.equals("new_actor_created")) {
176                                // notify the TabbedLibraryPane that a changeRequest needs to be
177                                // filed
178                                notifyListeners(event);
179                        }
180                }
181        }
182}