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.Collection;
032    
033    import javax.swing.JOptionPane;
034    
035    import edu.stanford.smi.protege.model.Cls;
036    import edu.stanford.smi.protege.model.Facet;
037    import edu.stanford.smi.protege.model.SimpleInstance;
038    import edu.stanford.smi.protege.model.Slot;
039    import edu.stanford.smi.protege.widget.AbstractSlotWidget;
040    import edu.stanford.smi.protege.widget.ClsWidget;
041    import edu.stanford.smi.protege.widget.SlotWidget;
042    import edu.stanford.smi.protege.widget.TextFieldWidget;
043    
044    /**
045     * This class will hopefully replace SlotMentionValueWidget if I can ever get it
046     * to work the way I want it to. The code here is intended to address feature
047     * request [ 1720295 ] simple slots should dynamically load widget from class
048     * for
049     * http://sourceforge.net/tracker/index.php?func=detail&aid=1720295&group_id=
050     * 128424&atid=714371
051     * 
052     * @author Philip
053     * 
054     */
055    public class SimpleSlotMentionValueWidget extends TextFieldWidget {
056    
057            static final long serialVersionUID = 0;
058    
059            KnowtatorManager manager;
060    
061            MentionUtil mentionUtil;
062    
063            AnnotationUtil annotationUtil;
064    
065            KnowtatorProjectUtil kpu;
066    
067            public void initialize() {
068                    super.initialize();
069                    manager = (KnowtatorManager) getKnowledgeBase().getClientInformation(Knowtator.KNOWTATOR_MANAGER);
070                    mentionUtil = manager.getMentionUtil();
071                    annotationUtil = manager.getAnnotationUtil();
072                    kpu = manager.getKnowtatorProjectUtil();
073                    setPreferredColumns(2);
074                    setPreferredRows(2);
075            }
076    
077            public static boolean isSuitable(Cls cls, Slot slot, Facet facet) {
078                    return true;
079            }
080    
081            private void showTypeMissingMessage(SimpleInstance mention) {
082                    if (mentionUtil.isClassMention(mention))
083                            JOptionPane.showMessageDialog(this, "There is no class assigned to this annotation.\n"
084                                            + "You may not add a value to this slot until a \n" + "class is assigned.", "No class assigned",
085                                            JOptionPane.WARNING_MESSAGE);
086                    else if (mentionUtil.isInstanceMention(mention))
087                            JOptionPane.showMessageDialog(this, "There is no instance assigned to this annotation.\n"
088                                            + "You may not add a value to this slot until an \n" + "instance is assigned.",
089                                            "No instance assigned", JOptionPane.WARNING_MESSAGE);
090    
091            }
092    
093            private void showSlotMissingMessage() {
094                    JOptionPane.showMessageDialog(this, "There is not a slot specified for this slot value.\n"
095                                    + "This is most likely a result of deleting a slot \n"
096                                    + "from the annotation schema after this annotation \n"
097                                    + "was created.  Please remove this slot value, select\n"
098                                    + "another annotation, and re-select this annotation.", "Slot missing", JOptionPane.WARNING_MESSAGE);
099            }
100    
101            public void setValues(Collection values) {
102                    super.setValues(values);
103    
104                    SimpleInstance slotMention = (SimpleInstance) getInstance();
105                    Slot mentionSlot = mentionUtil.getSlotMentionSlot(slotMention);
106                    if (mentionSlot == null) {
107                            showSlotMissingMessage();
108                            return;
109                    }
110                    SimpleInstance mention = mentionUtil.getMentionedBy(slotMention);
111                    Cls mentionCls = mentionUtil.getMentionCls(mention);
112                    if (mentionCls == null) {
113                            showTypeMissingMessage(mention);
114                            return;
115                    }
116    
117                    ClsWidget clsWidget = getProject().getDesignTimeClsWidget(mentionCls);
118                    SlotWidget slotWidget = clsWidget.getSlotWidget(mentionSlot);
119                    AbstractSlotWidget widget = (AbstractSlotWidget) slotWidget;
120                    widget.setSlot(kpu.getSlotMentionSlot());
121                    widget.setInstance(getInstance());
122                    removeAll();
123                    add(widget);
124            }
125    }