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.util.ArrayList;
032 import java.util.Collections;
033 import java.util.Comparator;
034 import java.util.Iterator;
035
036 import edu.stanford.smi.protege.model.Cls;
037 import edu.stanford.smi.protege.model.SimpleInstance;
038 import edu.stanford.smi.protege.model.Slot;
039 import edu.stanford.smi.protege.widget.ClsWidget;
040 import edu.stanford.smi.protege.widget.SlotWidget;
041 import edu.stanford.smi.protegex.widget.contains.ContainsWidget;
042
043 /**
044 * This class is a simple extension of the ContainsWidget that simply overrides
045 * the setValues method so that the slot mentions of a class mention or instance
046 * mention will always be in a predictable order - rather than a random order(!)
047 * The order of the slots is currently alphabetical by the slot name. Preference
048 * is given to the slot's form label as it appears in the mentioned classes slot
049 * widget as configured in the Forms tab.
050 *
051 * @author Philip V. Ogren
052 */
053 public class SlotMentionWidget extends ContainsWidget {
054
055 static final long serialVersionUID = 0;
056
057 MentionUtil mentionUtil;
058
059 SlotMentionComparator slotMentionComparator;
060
061 public void initialize() {
062 KnowtatorManager manager = (KnowtatorManager) getKnowledgeBase().getClientInformation(
063 Knowtator.KNOWTATOR_MANAGER);
064 mentionUtil = manager.getMentionUtil();
065 slotMentionComparator = new SlotMentionComparator();
066 super.initialize();
067 }
068
069 public void setValues(java.util.Collection values) {
070 ArrayList<SimpleInstance> sortedValues = new ArrayList<SimpleInstance>();
071 Iterator valuesIterator = values.iterator();
072 while (valuesIterator.hasNext()) {
073 sortedValues.add((SimpleInstance) valuesIterator.next());
074 }
075
076 Collections.sort(sortedValues, slotMentionComparator);
077
078 super.setValues(sortedValues);
079 }
080
081 public class SlotMentionComparator implements Comparator<SimpleInstance> {
082
083 public int compare(SimpleInstance mention1, SimpleInstance mention2) {
084 if (mentionUtil.isSlotMention(mention1) && mentionUtil.isSlotMention(mention2)) {
085 Slot slot1 = mentionUtil.getSlotMentionSlot(mention1);
086 Slot slot2 = mentionUtil.getSlotMentionSlot(mention2);
087
088 if (slot1 == null && slot2 == null)
089 return 0;
090 if (slot1 == null)
091 return 1;
092 if (slot2 == null)
093 return -1;
094
095 String mentionLabel1 = null;
096 String mentionLabel2 = null;
097
098 try {
099 SimpleInstance mentionedByMention = mentionUtil.getMentionedBy(mention1);
100 Cls mentionCls = mentionUtil.getMentionCls(mentionedByMention);
101 ClsWidget clsWidget = getProject().getDesignTimeClsWidget(mentionCls);
102 SlotWidget slotWidget = clsWidget.getSlotWidget(slot1);
103 mentionLabel1 = slotWidget.getLabel();
104 } catch (Exception e) {
105 }
106
107 if (mentionLabel1 == null)
108 mentionLabel1 = slot1.getBrowserText();
109
110 try {
111 SimpleInstance mentionedByMention = mentionUtil.getMentionedBy(mention2);
112 Cls mentionCls = mentionUtil.getMentionCls(mentionedByMention);
113 ClsWidget clsWidget = getProject().getDesignTimeClsWidget(mentionCls);
114 SlotWidget slotWidget = clsWidget.getSlotWidget(slot2);
115 mentionLabel2 = slotWidget.getLabel();
116 } catch (Exception e) {
117 }
118
119 if (mentionLabel2 == null)
120 mentionLabel2 = slot2.getBrowserText();
121
122 return mentionLabel1.compareTo(mentionLabel2);
123 }
124 return 0;
125 }
126 }
127
128 }