    001    // This file is part of the Attempto Java Packages.
    002    // Copyright 2008, Attempto Group, University of Zurich (see http://attempto.ifi.uzh.ch).
    003    //
    004    // The Attempto Java Packages is free software: you can redistribute it and/or modify it under the
    005    // terms of the GNU Lesser General Public License as published by the Free Software Foundation,
    006    // either version 3 of the License, or (at your option) any later version.
    007    //
    008    // The Attempto Java Packages is distributed in the hope that it will be useful, but WITHOUT ANY
    009    // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    010    // PURPOSE. See the GNU Lesser General Public License for more details.
    011    //
    012    // You should have received a copy of the GNU Lesser General Public License along with the Attempto
    013    // Java Packages. If not, see http://www.gnu.org/licenses/.
    014
    015    package ch.uzh.ifi.attempto.acewiki.gui;
    016
    017    import java.util.Collections;
    018    import java.util.List;
    019
    020    import nextapp.echo2.app.Color;
    021    import nextapp.echo2.app.Column;
    022    import nextapp.echo2.app.Component;
    023    import nextapp.echo2.app.Font;
    024    import nextapp.echo2.app.Insets;
    025    import nextapp.echo2.app.ResourceImageReference;
    026    import nextapp.echo2.app.Row;
    027    import nextapp.echo2.app.event.ActionEvent;
    028    import nextapp.echo2.app.event.ActionListener;
    029    import ch.uzh.ifi.attempto.acewiki.Task;
    030    import ch.uzh.ifi.attempto.acewiki.Wiki;
    031    import ch.uzh.ifi.attempto.acewiki.core.ontology.Individual;
    032    import ch.uzh.ifi.attempto.acewiki.core.ontology.OntologyElement;
    033    import ch.uzh.ifi.attempto.acewiki.core.ontology.Sentence;
    034    import ch.uzh.ifi.attempto.acewiki.core.text.OntologyTextElement;
    035    import ch.uzh.ifi.attempto.acewiki.gui.editor.SentenceEditorHandler;
    036    import ch.uzh.ifi.attempto.acewiki.gui.page.ArticlePage;
    037    import ch.uzh.ifi.attempto.acewiki.gui.page.LogicPage;
    038    import ch.uzh.ifi.attempto.acewiki.gui.page.SentencePage;
    039    import ch.uzh.ifi.attempto.acewiki.gui.page.WikiPage;
    040    import ch.uzh.ifi.attempto.echocomp.DelayedComponent;
    041    import ch.uzh.ifi.attempto.echocomp.HSpace;
    042    import ch.uzh.ifi.attempto.echocomp.Label;
    043    import ch.uzh.ifi.attempto.echocomp.MessageWindow;
    044    import ch.uzh.ifi.attempto.echocomp.SolidLabel;
    045    import ch.uzh.ifi.attempto.echocomp.VSpace;
    046    import ch.uzh.ifi.attempto.preditor.text.TextElement;
    047
    048    /**
    049     * This class represents a text row that consists of a drop down menu and an ACE text.
    050     *
    051     * @author Tobias Kuhn
    052     */
    053    public class TextRow extends Column implements ActionListener {
    054
    055            private static final long serialVersionUID = -540135972060005725L;
    056
    057            private Sentence sentence;
    058            private Wiki wiki;
    059            private WikiPage hostPage;
    060
    061            private Row sentenceRow = new Row();
    062            private DropDownMenu dropDown;
    063
    064            /**
    065             * Creates a new text row. The host page is the page that contains the text row (which is
    066             * not necessarily the owner page of the sentence).
    067             *
    068             * @param sentence The sentence to be shown.
    069             * @param hostPage The host page of the text row.
    070             */
    071            public TextRow(Sentence sentence, WikiPage hostPage) {
    072                    this.sentence = sentence;
    073                    this.hostPage = hostPage;
    074                    this.wiki = hostPage.getWiki();
    075                    update();
    076            }
    077
    078            private void update() {
    079                    if (sentence.isInferred()) {
    080                            dropDown = new DropDownMenu("light-blue", this);
    081                    } else if (sentence.isReasonerParticipant() || sentence.isQuestion()) {
    082                            dropDown = new DropDownMenu("blue", this);
    083                    } else {
    084                            dropDown = new DropDownMenu("red", this);
    085                    }
    086                    if (!sentence.isIntegrated() && !sentence.isInferred()) {
    087                            dropDown.addMenuEntry("Reassert");
    088                            dropDown.addMenuSeparator();
    089                    }
    090                    if (!sentence.isInferred()) {
    091                            dropDown.addMenuEntry("Edit...");
    092                    }
    093                    if (hostPage instanceof ArticlePage) {
    094                            dropDown.addMenuEntry("Add...");
    095                    }
    096                    if (!sentence.isInferred()) {
    097                            dropDown.addMenuEntry("Delete");
    098                    }
    099                    dropDown.addMenuSeparator();
    100                    dropDown.addMenuEntry("Details");
    101                    dropDown.addMenuEntry("Logic");
    102
    103                    Row r = new Row();
    104                    Color color = Color.BLACK;
    105                    boolean isRed = !sentence.isIntegrated() && !sentence.isInferred() && !sentence.isQuestion();
    106                    if (isRed) {
    107                            color = new Color(193, 0, 0);
    108                    }
    109                    for (TextElement e : sentence.getTextElements()) {
    110                            if (!e.getText().matches("[.?]") && r.getComponentCount() > 0) {
    111                                    r.add(new HSpace());
    112                            }
    113                            if (e instanceof OntologyTextElement) {
    114                                    // Proper names with definite articles are handled differently:
    115                                    // The "the" is not a part of the link. Probably, this should be done at a different place...
    116                                    OntologyElement oe = ((OntologyTextElement) e).getOntologyElement();
    117                                    if (oe instanceof Individual && ((Individual) oe).hasDefiniteArticle()) {
    118                                            SolidLabel l = new SolidLabel(e.getText().substring(0, 3));
    119                                            l.setForeground(color);
    120                                            r.add(l);
    121                                            r.add(new HSpace());
    122                                            r.add(new WikiLink(oe, oe.getPrettyWord(1), wiki, isRed));
    123                                    } else {
    124                                            r.add(new WikiLink(((OntologyTextElement) e), wiki, isRed));
    125                                    }
    126                            } else {
    127                                    SolidLabel l = new SolidLabel(e.getText());
    128                                    l.setForeground(color);
    129                                    r.add(l);
    130                            }
    131                    }
    132
    133                    removeAll();
    134                    sentenceRow.removeAll();
    135                    sentenceRow.add(dropDown);
    136                    sentenceRow.add(new HSpace(5));
    137                    sentenceRow.add(r);
    138                    sentenceRow.add(new HSpace(10));
    139                    add(sentenceRow);
    140
    141                    if (sentence.isQuestion() && hostPage instanceof ArticlePage) {
    142
    143                            Column answerColumn = new Column();
    144                            answerColumn.setInsets(new Insets(20, 0, 0, 0));
    145                            add(answerColumn);
    146
    147                            if (sentence.isAnswerCached()) {
    148
    149                                    Column column = new Column();
    150                                    List<Individual> individuals = sentence.getAnswer();
    151                                    if (individuals.size() > 0) {
    152                                            Collections.sort(individuals);
    153                                            for (Individual ind : individuals) {
    154                                                    Row answerRow = new Row();
    155                                                    answerRow.add(new ListItem(new WikiLink(ind, wiki)));
    156                                                    column.add(answerRow);
    157                                            }
    158                                    } else {
    159                                            column.add(new SolidLabel("(no answer found)", Font.ITALIC, 10));
    160                                    }
    161                                    column.add(new VSpace(4));
    162                                    answerColumn.add(column);
    163
    164                            } else {
    165                                    answerColumn.add(new DelayedComponent(new Label(new ResourceImageReference("ch/uzh/ifi/attempto/acewiki/gui/img/wait.gif"))) {
    166
    167                                            private static final long serialVersionUID = 7865984138467729544L;
    168
    169                                            public Component initComponent() {
    170                                                    Column column = new Column();
    171                                                    List<Individual> individuals = sentence.getAnswer();
    172                                                    if (individuals.size() > 0) {
    173                                                            Collections.sort(individuals);
    174                                                            for (Individual ind : individuals) {
    175                                                                    Row answerRow = new Row();
    176                                                                    answerRow.add(new ListItem(new WikiLink(ind, wiki)));
    177                                                                    column.add(answerRow);
    178                                                            }
    179                                                    } else {
    180                                                            column.add(new SolidLabel("(no answer found)", Font.ITALIC, 10));
    181                                                    }
    182                                                    column.add(new VSpace(4));
    183
    184                                                    return column;
    185                                            }
    186
    187                                    });
    188                            }
    189
    190                    }
    191            }
    192
    193            public void actionPerformed(ActionEvent e) {
    194                    if (e.getActionCommand().equals("Edit...")) {
    195                            wiki.log("page", "dropdown: edit sentence: " + sentence.getText());
    196                            ArticlePage page = ArticlePage.create(sentence.getOwner(), wiki);
    197                            wiki.showPage(page);
    198                            page.edit(sentence);
    199                    } else if (e.getActionCommand().equals("Add...")) {
    200                            wiki.log("page", "dropdown: add sentence");
    201                            wiki.showWindow(SentenceEditorHandler.generatePreditorAddWindow(sentence, (ArticlePage) hostPage));
    202                    } else if (e.getActionCommand().equals("Delete")) {
    203                            wiki.log("page", "dropdown: delete sentence: " + sentence.getText());
    204                            wiki.showWindow(new MessageWindow("Delete", "Do you really want to delete this sentence?", null, this, "Yes", "No"));
    205                    } else if (e.getActionCommand().equals("Reassert")) {
    206                            int success = sentence.reassert();
    207                            if (success == 1) {
    208                                    wiki.showWindow(new MessageWindow("Conflict", "A sentence is in conflict with the current knowledge. For that reason, it cannot be added to the knowledge base.", "OK"));
    209                            } else if (success == 2) {
    210                                    wiki.showWindow(new MessageWindow("Error", "A sentence could not be added to the knowledge base because the knowledge base got too complex.", "OK"));
    211                            }
    212                            if (sentence.isIntegrated()) {
    213                                    update();
    214                            }
    215                    } else if (e.getActionCommand().equals("Details")) {
    216                            wiki.log("page", "dropdown: details sentence: " + sentence.getText());
    217                            wiki.showPage(new SentencePage(wiki, sentence));
    218                    } else if (e.getActionCommand().equals("Logic")) {
    219                            wiki.log("page", "dropdown: logic sentence: " + sentence.getText());
    220                            wiki.showPage(new LogicPage(wiki, sentence));
    221                    } else if (e.getSource() instanceof MessageWindow && e.getActionCommand().equals("Yes")) {
    222                            wiki.log("page", "dropdown: delete confirmed: " + sentence.getText());
    223
    224                            wiki.enqueueTaskShowingWaitWindow(
    225                                    "Updating",
    226                                    "The sentence is being removed from the knowledge base...",
    227                                    new Task() {
    228                                            public void run() {
    229                                                    sentence.getOwner().remove(sentence);
    230                                            }
    231                                            public void updateGUI() {
    232                                                    wiki.update();
    233                                                    wiki.refresh();
    234                                            }
    235                                    }
    236                            );
    237                    }
    238            }
    239
    240    }