001    /*
002     * The contents of this file are subject to the Mozilla Public
003     * License Version 1.1 (the "License"); you may not use this file
004     * except in compliance with the License. You may obtain a copy of
005     * the License at http://www.mozilla.org/MPL/
006     *
007     * Software distributed under the License is distributed on an "AS
008     * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
009     * implied. See the License for the specific language governing
010     * rights and limitations under the License.
011     *
012     * The Original Code is Knowtator.
013     *
014     * The Initial Developer of the Original Code is University of Colorado.  
015     * Copyright (C) 2005 - 2008.  All Rights Reserved.
016     *
017     * Knowtator was developed by the Center for Computational Pharmacology
018     * (http://compbio.uchcs.edu) at the University of Colorado Health 
019     *  Sciences Center School of Medicine with support from the National 
020     *  Library of Medicine.  
021     *
022     * Current information about Knowtator can be obtained at 
023     * http://knowtator.sourceforge.net/
024     *
025     * Contributor(s):
026     *   Philip V. Ogren <philip@ogren.info> (Original Author)
027     */
028    
029    /**
030     * Changes:
031     * 8/10/2005    pvo   the renderer is changed so that the main text
032     *                    is set to the result returned from BrowserTextUtil.getBrowserText()
033     *
034     */
035    package edu.uchsc.ccp.knowtator.ui;
036    
037    import java.awt.Color;
038    import java.awt.Component;
039    
040    import edu.stanford.smi.protege.model.Cls;
041    import edu.stanford.smi.protege.model.Frame;
042    import edu.stanford.smi.protege.model.Instance;
043    import edu.stanford.smi.protege.model.SimpleInstance;
044    import edu.stanford.smi.protege.ui.FrameRenderer;
045    import edu.stanford.smi.protege.ui.ParentChildNode;
046    import edu.uchsc.ccp.knowtator.BrowserTextUtil;
047    import edu.uchsc.ccp.knowtator.DisplayColors;
048    import edu.uchsc.ccp.knowtator.KnowtatorManager;
049    
050    public class ColorFrameRenderer extends FrameRenderer {
051            static final long serialVersionUID = 0;
052    
053            KnowtatorManager manager;
054    
055            DisplayColors displayColors;
056    
057            BrowserTextUtil browserTextUtil;
058    
059            public ColorFrameRenderer(KnowtatorManager manager) {
060                    this.manager = manager;
061                    this.displayColors = manager.getDisplayColors();
062                    this.browserTextUtil = manager.getBrowserTextUtil();
063            }
064    
065            protected Component setup(Component c, Object value, boolean hasFocus, boolean isSelected) {
066                    super.setup(c, value, hasFocus, isSelected);
067                    Color color = null;
068    
069                    Frame displayedFrame = null;
070                    if (value instanceof Frame)
071                            displayedFrame = (Frame) value;
072    
073                    if (value instanceof ParentChildNode) {
074                            ParentChildNode node = (ParentChildNode) value;
075                            Object userObject = node.getUserObject();
076                            if (userObject instanceof Frame)
077                                    displayedFrame = (Frame) userObject;
078                    }
079    
080                    if (displayedFrame != null) {
081                            color = displayColors.getColor(displayedFrame);
082                            int count = manager.getCurrentAnnotationCountForFrame(displayedFrame);
083                            if (count > 0) {
084                                    if (manager.isConsensusMode()) {
085                                            addText(" (" + manager.getConsolidatedAnnotationCountForFrame(displayedFrame) + "/" + count + ")");
086                                    } else
087                                            addText(" (" + count + ")");
088                            } else if (value instanceof SimpleInstance) {
089                                    SimpleInstance instance = (SimpleInstance) value;
090                                    if (manager.getAnnotationUtil().isAnnotation(instance)) {
091                                            if (instance.getDirectType() != null) {
092                                                    clear();
093                                                    setMainText(browserTextUtil.getBrowserText(instance, 30));
094                                                    addText(getRendererAnnotationDisplayText(instance));
095                                            }
096                                    } else if (manager.getMentionUtil().isMention(instance)) {
097                                            clear();
098                                            setMainText(browserTextUtil.getBrowserText(instance, 30));
099                                    }
100    
101                            }
102                            // needs to be after clear() call
103                            setMainIcon(displayColors.getIcon(color));
104                    }
105                    return this;
106            }
107    
108            protected String getRendererAnnotationDisplayText(SimpleInstance instance) {
109                    if (manager.getAnnotationUtil().isAnnotation(instance)) {
110                            SimpleInstance annotatedMention = manager.getAnnotationUtil().getMention(instance);
111    
112                            if (manager.getMentionUtil().isClassMention(annotatedMention)) {
113                                    Cls mentionCls = manager.getMentionUtil().getMentionCls(instance);
114                                    if (mentionCls != null)
115                                            return " (" + mentionCls.getName() + ")";
116                            } else if (manager.getMentionUtil().isInstanceMention(annotatedMention)) {
117                                    Instance mentionInstance = manager.getMentionUtil().getMentionInstance(instance);
118                                    if (mentionInstance != null)
119                                            return " (" + mentionInstance.getBrowserText() + ")";
120                            }
121                    }
122                    return "";
123            }
124    
125            protected Color getBackgroundColor() {
126                    Object value = getValue();
127                    if (_isSelected) {
128                            if (value instanceof SimpleInstance) {
129                                    Color instanceColor = displayColors.getColor((SimpleInstance) value);
130                                    if (instanceColor != null)
131                                            return DisplayColors.getBackgroundColor(instanceColor);
132                            } else if (value instanceof ParentChildNode) {
133                                    ParentChildNode node = (ParentChildNode) value;
134                                    Object userObject = node.getUserObject();
135                                    if (userObject instanceof Cls) {
136                                            Color clsColor = displayColors.getColor((Cls) userObject);
137                                            if (clsColor != null)
138                                                    return DisplayColors.getBackgroundColor(clsColor);
139                                    }
140                            }
141                    }
142                    return super.getBackgroundColor();
143            }
144    }