001/* A top-level dialog window for displaying dependency results.
002
003   Copyright (c) 2012-2014 The Regents of the University of California.
004   All rights reserved.
005   Permission is hereby granted, without written agreement and without
006   license or royalty fees, to use, copy, modify, and distribute this
007   software and its documentation for any purpose, provided that the above
008   copyright notice and the following two paragraphs appear in all copies
009   of this software.
010
011   IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012   FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013   ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014   THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015   SUCH DAMAGE.
016
017   THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020   PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021   CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022   ENHANCEMENTS, OR MODIFICATIONS.
023
024   PT_COPYRIGHT_VERSION_2
025   COPYRIGHTENDKEY
026 */
027package ptolemy.vergil.basic;
028
029import java.awt.Frame;
030import java.net.URL;
031import java.util.Set;
032import java.util.SortedSet;
033import java.util.TreeSet;
034
035import javax.swing.table.DefaultTableCellRenderer;
036
037import ptolemy.actor.Actor;
038import ptolemy.actor.AtomicActor;
039import ptolemy.actor.gui.Configuration;
040import ptolemy.actor.gui.DialogTableau;
041import ptolemy.actor.util.ActorDependencies;
042import ptolemy.kernel.Entity;
043import ptolemy.kernel.util.KernelException;
044import ptolemy.kernel.util.NamedObj;
045import ptolemy.util.MessageHandler;
046
047///////////////////////////////////////////////////////////////////
048//// DependencyResultsDialog
049
050/**
051   A non-modal dialog that displays the actor dependency analysis results as a
052   list.
053
054   @author Christopher Brooks, Based on SearchResultsDialog by Edward A. Lee
055   @version $Id$
056   @since Ptolemy II 10.0
057   @Pt.ProposedRating Yellow (cxh)
058   @Pt.AcceptedRating Red (cxh)
059 */
060@SuppressWarnings("serial")
061public class DependencyResultsDialog extends SearchResultsDialog {
062
063    /** Construct a dialog for search results.
064     *  @param tableau The DialogTableau.
065     *  @param owner The frame that, per the user, is generating the dialog.
066     *  @param target The object on which the search is to be done.
067     *  @param configuration The configuration to use to open the help screen
068     *   (or null if help is not supported).
069     */
070    public DependencyResultsDialog(DialogTableau tableau, Frame owner,
071            Entity target, Configuration configuration) {
072        super("Dependency analysis for " + target.getName(), tableau, owner,
073                target, configuration);
074        _resultsTable.setDefaultRenderer(NamedObj.class,
075                new DependencyResultsNamedObjRenderer());
076    }
077
078    ///////////////////////////////////////////////////////////////////
079    ////                         protected methods                 ////
080
081    /** Initialize the query dialog.
082     *  Derived classes may change the layout of the query dialog.
083     */
084    @Override
085    protected void _initializeQuery() {
086        _query.setColumns(2);
087        _query.addCheckBox("prerequisites", "Prerequisites", true);
088        _query.addCheckBox("dependents", "Dependents", true);
089    }
090
091    /** Perform a search and update the results table.
092     */
093    @Override
094    protected void _search() {
095        boolean prerequisites = _query.getBooleanValue("prerequisites");
096        boolean dependents = _query.getBooleanValue("dependents");
097        try {
098            Set<NamedObj> results = _findDependencies((Actor) _target,
099                    prerequisites, dependents);
100            _resultsTableModel.setContents(results);
101            if (results.size() == 0) {
102                MessageHandler.message("No prerequisites and/or dependents.");
103            }
104        } catch (KernelException ex) {
105            MessageHandler.error("Failed to get prequisites or dependents for "
106                    + _target.getFullName() + ".", ex);
107        }
108    }
109
110    /** Return a list of objects in the model that match the
111     *  specified search.
112     *  @param actor The actor to be searched.
113     *  @param prerequisites True to search for prerequisites.
114     *  @param dependents True to search for dependents.
115     *  @return The Set of objects in the model that match the specified search.
116     *  @exception KernelException If thrown while preinitializing() or wrapping up.
117     */
118    protected Set<NamedObj> _findDependencies(Actor actor,
119            boolean prerequisites, boolean dependents) throws KernelException {
120
121        // FIXME: we could add a field to the search box for specifying the filter
122        // class.  However, how would we specify that searching Publishers should
123        // have Subscribers as the field?
124        Class clazz = AtomicActor.class;
125        SortedSet<NamedObj> result = new TreeSet<NamedObj>(
126                new NamedObjComparator());
127        if (prerequisites) {
128            BasicGraphFrame.report(_owner,
129                    "Generating prerequisite information.");
130            System.out.println("_findDependencies: " + actor);
131            result.addAll(ActorDependencies.prerequisites(actor, clazz));
132            BasicGraphFrame.report(_owner, "");
133        }
134        if (dependents) {
135            BasicGraphFrame.report(_owner,
136                    "Generating dependency information.");
137            result.addAll(ActorDependencies.dependents(actor, clazz));
138            BasicGraphFrame.report(_owner, "");
139        }
140        return result;
141    }
142
143    /** Return a URL that points to the help page.
144     *  @return A URL that points to the help page
145     */
146    @Override
147    protected URL _getHelpURL() {
148        URL helpURL = getClass().getClassLoader().getResource(
149                "ptolemy/vergil/basic/doc/DependencyResultsDialog.htm");
150        return helpURL;
151    }
152
153    ///////////////////////////////////////////////////////////////////
154    ////                         inner classes                     ////
155
156    /** Default renderer for results table. */
157    private static class DependencyResultsNamedObjRenderer
158            extends DefaultTableCellRenderer {
159        // FindBugs indicates that this should be a static class.
160
161        @Override
162        public void setValue(Object value) {
163            String fullName = ((NamedObj) value).getFullName();
164            String strippedName = fullName.substring(fullName.indexOf(".", 1));
165            setText(strippedName);
166        }
167    }
168}