001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-04-13 23:45:39 +0000 (Mon, 13 Apr 2015) $' 
007 * '$Revision: 33344 $'
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 */
029package org.kepler.gui;
030
031import java.awt.Component;
032import java.net.URL;
033
034import javax.swing.Icon;
035import javax.swing.ImageIcon;
036import javax.swing.JTree;
037import javax.swing.tree.DefaultTreeCellRenderer;
038
039import org.apache.commons.logging.Log;
040import org.apache.commons.logging.LogFactory;
041import org.kepler.moml.DownloadableKAREntityLibrary;
042import org.kepler.moml.FolderEntityLibrary;
043import org.kepler.moml.KAREntityLibrary;
044import org.kepler.moml.KARErrorEntityLibrary;
045import org.kepler.moml.OntologyEntityLibrary;
046import org.kepler.moml.RemoteKARErrorEntityLibrary;
047import org.kepler.moml.RemoteRepositoryEntityLibrary;
048import org.kepler.moml.SearchEntityLibrary;
049import org.kepler.util.StaticResources;
050
051import ptolemy.kernel.util.NamedObj;
052import ptolemy.kernel.util.Settable;
053import ptolemy.moml.EntityLibrary;
054import ptolemy.vergil.tree.PtolemyTreeCellRenderer;
055
056/**
057 * A TreeCellRenderer for Ontologies.
058 * 
059 * @author Daniel Crawl (copied from AnnotatedPtree.java by Chad Berkley)
060 * @since February 17, 2005
061 * @version $Id: OntologyTreeCellRenderer.java 33344 2015-04-13 23:45:39Z crawl $
062 * @since Kepler 1.0
063 */
064public class OntologyTreeCellRenderer extends PtolemyTreeCellRenderer {
065    private static final Log log = LogFactory
066            .getLog(OntologyTreeCellRenderer.class.getName());
067    private static final boolean isDebugging = log.isDebugEnabled();
068
069    private ImageIcon _ontIconClosed, _ontIconOpen, _generalIcon, _searchIcon,
070            _folderIconClosed, _folderIconOpen, _packIconOpen, _packIconClosed,
071            _packIconError, _remoteIcon;
072
073    private final String CAT_IMG = StaticResources.getSettingsString(
074            "ONTOL_CLASS_TREEICON_PATH", "");
075
076    private final String ONT_IMG_OPEN = StaticResources.getSettingsString(
077            "ONTOLOGY_TREEICON_OPEN_PATH", "");
078
079    private final String ONT_IMG_CLOSED = StaticResources.getSettingsString(
080            "ONTOLOGY_TREEICON_CLOSED_PATH", "");
081
082    private final String SEARCH_IMG = StaticResources.getSettingsString(
083            "MAGNIFIER_SILKICON_PATH", "");
084
085    private final String FOL_IMG_OPEN = StaticResources.getSettingsString(
086            "FOLDER_TREEICON_OPEN_PATH", "");
087
088    private final String FOL_IMG_CLOSED = StaticResources.getSettingsString(
089            "FOLDER_TREEICON_OPEN_PATH", "");
090
091    private final String PACK_IMG_OPEN = StaticResources.getSettingsString(
092            "PACKAGE_SILKICON_OPEN_PATH", "");
093    private final String PACK_IMG_CLOSED = StaticResources.getSettingsString(
094            "PACKAGE_SILKICON_CLOSED_PATH", "");
095    private final String PACK_IMG_ERROR = StaticResources.getSettingsString(
096            "PACKAGE_SILKICON_ERROR_PATH", "");
097    private final String REMOTE_IMG = StaticResources.getSettingsString(
098            "REMOTE_TREEICON_PATH", "");
099
100    /**
101     * Constructor - creates a new TreeCellRenderer
102     */
103    public OntologyTreeCellRenderer() {
104        this(true);
105    }
106
107    public OntologyTreeCellRenderer(boolean showIcons) {
108        if (isDebugging)
109            log.debug("OntologyTreeCellRenderer(" + showIcons + ")");
110
111        URL catImgURL = null;
112        // OntologyTreeCellRenderer.class.getResource(CAT_IMG);
113        if (catImgURL != null) {
114            _generalIcon = new ImageIcon(catImgURL);
115        }
116        URL searchImgURL = OntologyTreeCellRenderer.class
117                .getResource(SEARCH_IMG);
118        if (searchImgURL != null) {
119            _searchIcon = new ImageIcon(searchImgURL);
120        }
121        if (showIcons) {
122            URL ontImgURLOpen = OntologyTreeCellRenderer.class
123                    .getResource(ONT_IMG_OPEN);
124            if (ontImgURLOpen != null) {
125                _ontIconOpen = new ImageIcon(ontImgURLOpen);
126            }
127            URL ontImgURLClosed = OntologyTreeCellRenderer.class
128                    .getResource(ONT_IMG_CLOSED);
129            if (ontImgURLClosed != null) {
130                _ontIconClosed = new ImageIcon(ontImgURLClosed);
131            }
132            URL folImgURLOpen = OntologyTreeCellRenderer.class
133                    .getResource(FOL_IMG_OPEN);
134            if (folImgURLOpen != null) {
135                _folderIconOpen = new ImageIcon(folImgURLOpen);
136            }
137            URL folImgURLClosed = OntologyTreeCellRenderer.class
138                    .getResource(FOL_IMG_CLOSED);
139            if (folImgURLClosed != null) {
140                _folderIconClosed = new ImageIcon(folImgURLClosed);
141            }
142            URL packImgURLOpen = OntologyTreeCellRenderer.class
143                    .getResource(PACK_IMG_OPEN);
144            if (packImgURLOpen != null) {
145                _packIconOpen = new ImageIcon(packImgURLOpen);
146            }
147            URL packImgURLClosed = OntologyTreeCellRenderer.class
148                    .getResource(PACK_IMG_CLOSED);
149            if (packImgURLClosed != null) {
150                _packIconClosed = new ImageIcon(packImgURLClosed);
151            }
152            URL packImgURLError = OntologyTreeCellRenderer.class
153                    .getResource(PACK_IMG_ERROR);
154            if (packImgURLError != null) {
155                _packIconError = new ImageIcon(packImgURLError);
156            }
157            URL remoteImgURL = OntologyTreeCellRenderer.class
158                    .getResource(REMOTE_IMG);
159            if (remoteImgURL != null) {
160                _remoteIcon = new ImageIcon(remoteImgURL);
161            }
162        } else {
163            _ontIconOpen = new ImageIcon(catImgURL);
164        }
165
166    }
167
168    @Override
169    public Component getTreeCellRendererComponent(JTree tree, Object value,
170            boolean selected, boolean expanded, boolean leaf, int row,
171            boolean hasFocus) {
172        if (isDebugging) {
173            log.debug("getTreeCellRendererComponent("
174                    + tree.getClass().getName() + ", "
175                    + value.getClass().getName() + ", " + selected + ", "
176                    + expanded + ", " + leaf + ", " + row + ", " + hasFocus
177                    + ")");
178            log.debug(tree.getShowsRootHandles());
179        }
180
181        if (value instanceof ptolemy.moml.EntityLibrary) {
182            EntityLibrary el = (EntityLibrary) value;
183            if (isDebugging) {
184                log.debug(el.getName() + " " + el.getClass().getName());
185            }
186
187            if (el instanceof KAREntityLibrary) {
188                setOpenIcon(_packIconOpen);
189                setClosedIcon(_packIconClosed);
190            } else if (el instanceof FolderEntityLibrary) {
191                setOpenIcon(_folderIconOpen);
192                setClosedIcon(_folderIconClosed);
193            } else if (el instanceof OntologyEntityLibrary) {
194                setOpenIcon(_ontIconOpen);
195                setClosedIcon(_ontIconClosed);
196            } else if (el instanceof SearchEntityLibrary) {
197                setOpenIcon(_searchIcon);
198                setClosedIcon(_searchIcon);
199            } else if (el instanceof KARErrorEntityLibrary || el instanceof RemoteKARErrorEntityLibrary) {
200                setOpenIcon(_packIconError);
201                setClosedIcon(_packIconError);
202            } else if (el instanceof DownloadableKAREntityLibrary) {
203                // Icons for the KAR files - trash cans?
204                setOpenIcon(_packIconOpen);
205                setClosedIcon(_packIconClosed);             
206            } else if (el instanceof RemoteRepositoryEntityLibrary) {
207                // Icons for the Remote repositories - computers
208                setOpenIcon(_remoteIcon);
209                setClosedIcon(_remoteIcon);
210                // TODO: Also make sure that the top-level "Remote Components" is a globe
211            } else {
212                setOpenIcon(_generalIcon);
213                setClosedIcon(_generalIcon);
214            }
215        } else {
216            if (isDebugging)
217                log.debug("set general icon");
218            setOpenIcon(_generalIcon);
219            setClosedIcon(_generalIcon);
220        }
221        Component c = super.getTreeCellRendererComponent(tree, value, selected,
222                expanded, leaf, row, hasFocus);
223        
224        // if the object is a NamedObj, use display name for the label
225        // since the display name is used on the canvas. the component returned
226        // by super.getTreeCellRenderer() uses the display name for settables.
227        if(!(value instanceof Settable) && (value instanceof NamedObj) &&
228                (c instanceof DefaultTreeCellRenderer)) {
229            ((DefaultTreeCellRenderer)c).setText(((NamedObj)value).getDisplayName());
230        }
231        
232        if (isDebugging)
233            log.debug("Component: " + c.getClass().getName());
234        return c;
235    }
236
237    /**
238     * Sets the icon used to represent non-leaf nodes that are expanded.
239     */
240    @Override
241    public void setOpenIcon(Icon newIcon) {
242        if (newIcon != null) {
243            super.setOpenIcon(newIcon);
244        } else {
245            // super.setOpenIcon(UIManager.getIcon("Tree.openIcon"));
246            super.setOpenIcon(null);
247        }
248    }
249
250    /**
251     * Sets the icon used to represent non-leaf nodes that are not expanded.
252     */
253    @Override
254    public void setClosedIcon(Icon newIcon) {
255        if (newIcon != null) {
256            super.setClosedIcon(newIcon);
257        } else {
258            // super.setClosedIcon(UIManager.getIcon("Tree.closeIcon"));
259            super.setClosedIcon(null);
260        }
261    }
262}