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 package edu.uchsc.ccp.knowtator; 030 031 import java.awt.Color; 032 import java.util.Collection; 033 import java.util.HashMap; 034 import java.util.List; 035 import java.util.Map; 036 037 import javax.swing.Icon; 038 039 import org.annotation.gui.SwatchIcon; 040 041 import edu.stanford.smi.protege.event.FrameAdapter; 042 import edu.stanford.smi.protege.event.FrameEvent; 043 import edu.stanford.smi.protege.model.Cls; 044 import edu.stanford.smi.protege.model.Frame; 045 import edu.stanford.smi.protege.model.Instance; 046 import edu.stanford.smi.protege.model.KnowledgeBase; 047 import edu.stanford.smi.protege.model.SimpleInstance; 048 import edu.stanford.smi.protege.model.Slot; 049 import edu.stanford.smi.protege.util.CollectionUtilities; 050 051 public class DisplayColors { 052 public static final Color DEFAULT_ANNOTATION_COLOR = new Color(128, 128, 128); 053 054 public static final Icon DEFAULT_ICON = new SwatchIcon(10, 10, DEFAULT_ANNOTATION_COLOR); 055 056 KnowtatorManager manager; 057 058 KnowtatorProjectUtil kpu; 059 060 KnowledgeBase kb; 061 062 MentionUtil mentionUtil; 063 064 AnnotationUtil annotationUtil; 065 066 Map<Frame, Color> clsColorMap; 067 068 Map<Color, Icon> colorIconMap; 069 070 static Map<Color, Color> backgroundColors = new HashMap<Color, Color>(); 071 072 /** Creates a new instance of DisplayColors */ 073 public DisplayColors(KnowtatorManager manager) { 074 this.manager = manager; 075 this.kpu = manager.getKnowtatorProjectUtil(); 076 this.kb = manager.getKnowledgeBase(); 077 this.mentionUtil = manager.getMentionUtil(); 078 this.annotationUtil = manager.getAnnotationUtil(); 079 080 clsColorMap = new HashMap<Frame, Color>(); 081 colorIconMap = new HashMap<Color, Icon>(); 082 083 List<SimpleInstance> colorAssignments = manager.getColorAssignments(); 084 for (SimpleInstance colorAssignment : colorAssignments) { 085 addAssignment(colorAssignment); 086 } 087 initListener(); 088 } 089 090 private void initListener() { 091 SimpleInstance configuration = ProjectSettings.getActiveConfiguration(kb.getProject()); 092 configuration.addFrameListener(new FrameAdapter() { 093 public void ownSlotValueChanged(FrameEvent frameEvent) { 094 updateColorAssignments(frameEvent.getSlot()); 095 } 096 097 public void ownSlotAdded(FrameEvent frameEvent) { 098 updateColorAssignments(frameEvent.getSlot()); 099 } 100 101 public void ownSlotRemoved(FrameEvent frameEvent) { 102 updateColorAssignments(frameEvent.getSlot()); 103 } 104 105 private void updateColorAssignments(Slot slot) { 106 if (slot.equals(kpu.getColorAssignmentsSlot())) { 107 clsColorMap.clear(); 108 colorIconMap.clear(); 109 110 List<SimpleInstance> colorAssignments = manager.getColorAssignments(); 111 for (SimpleInstance colorAssignment : colorAssignments) { 112 addAssignment(colorAssignment); 113 } 114 } 115 } 116 }); 117 118 } 119 120 private boolean isColorAssignmentInstance(SimpleInstance colorAssignmentInstance) { 121 return colorAssignmentInstance.getDirectType().equals(kpu.colorAssignmentCls); 122 } 123 124 public void addAssignment(SimpleInstance colorAssignmentInstance) { 125 if (isColorAssignmentInstance(colorAssignmentInstance)) { 126 try { 127 Cls colorClass = (Cls) colorAssignmentInstance.getOwnSlotValue(kpu.colorClassSlot); 128 Instance displayColorInstance = (Instance) colorAssignmentInstance 129 .getOwnSlotValue(kpu.displayColorSlot); 130 int blue = (Integer) displayColorInstance.getOwnSlotValue(kpu.displayColorBSlot); 131 int green = (Integer) displayColorInstance.getOwnSlotValue(kpu.displayColorGSlot); 132 int red = (Integer) displayColorInstance.getOwnSlotValue(kpu.displayColorRSlot); 133 Color clsColor = new Color(red, green, blue); 134 clsColorMap.put(colorClass, clsColor); 135 if (!colorIconMap.containsKey(clsColor)) 136 colorIconMap.put(clsColor, new SwatchIcon(10, 10, clsColor)); 137 } catch (NullPointerException npe) { 138 } 139 } 140 } 141 142 public void addAssignment(Cls assignedCls, SimpleInstance colorInstance) { 143 SimpleInstance colorAssignment = kb.createSimpleInstance(null, null, CollectionUtilities 144 .createCollection(kpu.colorAssignmentCls), true); 145 colorAssignment.setOwnSlotValue(kpu.colorClassSlot, assignedCls); 146 colorAssignment.setOwnSlotValue(kpu.displayColorSlot, colorInstance); 147 addAssignment(colorAssignment); 148 } 149 150 public Icon getIcon(Color color) { 151 if (colorIconMap.containsKey(color)) { 152 return colorIconMap.get(color); 153 } else { 154 return DEFAULT_ICON; 155 } 156 } 157 158 private Color _getColor(Cls cls) { 159 Collection<Cls> superClses = (Collection<Cls>) cls.getDirectSuperclasses(); 160 if (superClses == null) 161 return DEFAULT_ANNOTATION_COLOR; 162 for (Cls superCls : superClses) { 163 if (clsColorMap.containsKey(superCls)) { 164 return clsColorMap.get(superCls); 165 } 166 } 167 for (Cls superCls : superClses) { 168 return _getColor(superCls); 169 } 170 return DEFAULT_ANNOTATION_COLOR; 171 } 172 173 public Color getColor(Frame frame) { 174 try { 175 if (frame == null) 176 return DEFAULT_ANNOTATION_COLOR; 177 178 if (clsColorMap.containsKey(frame)) { 179 return clsColorMap.get(frame); 180 } else if (frame instanceof Cls) { 181 Color clsColor = _getColor((Cls) frame); 182 clsColorMap.put(frame, clsColor); 183 return clsColor; 184 } else if (frame instanceof SimpleInstance) { 185 SimpleInstance instance = (SimpleInstance) frame; 186 Color frameColor = null; 187 if (annotationUtil.isAnnotation(instance)) { 188 SimpleInstance annotatedMention = annotationUtil.getMention(instance); 189 frameColor = getColor(annotatedMention); 190 } else if (mentionUtil.isClassMention(instance) || mentionUtil.isInstanceMention(instance)) { 191 Cls mentionCls = mentionUtil.getMentionCls(instance); 192 frameColor = getColor(mentionCls); 193 } else { 194 frameColor = getColor(instance.getDirectType()); 195 } 196 clsColorMap.put(frame, frameColor); 197 return frameColor; 198 } 199 } catch (NullPointerException npe) { 200 } 201 202 return DEFAULT_ANNOTATION_COLOR; 203 } 204 205 public static Color getBackgroundColor(Color color) { 206 if (backgroundColors.containsKey(color)) 207 return backgroundColors.get(color); 208 int red = color.getRed(); 209 int green = color.getGreen(); 210 int blue = color.getBlue(); 211 red = red + 4 * ((255 - red) / 5); 212 green = green + 4 * ((255 - green) / 5); 213 blue = blue + 4 * ((255 - blue) / 5); 214 Color backgroundColor = new Color(red, green, blue); 215 backgroundColors.put(color, backgroundColor); 216 return backgroundColor; 217 } 218 } 219 220 /** 221 *TODO need to check for existing color assignment for a given class first and 222 * update it if possible rather than always creating a new one. 223 */ 224 225 // public void askForColorAssignment(Component component) 226 // { 227 // Cls rootCls = ProjectSettings.getRootCls(kb.getProject()); 228 // Cls cls = DisplayUtilities.pickCls(component, 229 // kb, 230 // CollectionUtilities.createCollection(rootCls), 231 // "Choose class to assign a color to"); 232 // 233 // if(cls != null) 234 // { 235 // Instance colorInstance = DisplayUtilities.pickInstance(component, 236 // CollectionUtilities.createCollection(kpu.displayColorCls), 237 // "Choose color for "+cls.getBrowserText()); 238 // if(colorInstance != null) 239 // { 240 // addAssignment(cls, (SimpleInstance) colorInstance); 241 // JOptionPane.showMessageDialog(component, 242 // "Color assignments may not take full effect until project is re-opened."+ 243 // " Color assignments may be deleted under the \"Instances\" tab.","", 244 // JOptionPane.INFORMATION_MESSAGE); 245 // } 246 // } 247 // 248 // }