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() && sentence.isOWL()) ) {
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                                    OntologyTextElement ote = (OntologyTextElement) e;
117                                    OntologyElement oe = ote.getOntologyElement();
118                                    if (oe instanceof Individual) {
119                                            Individual ind = (Individual) oe;
120                                            int wn = ote.getWordNumber();
121                                            
122                                            if (ind.hasDefiniteArticle(wn)) {
123                                                    SolidLabel l = new SolidLabel(e.getText().substring(0, 3));
124                                                    l.setForeground(color);
125                                                    r.add(l);
126                                                    r.add(new HSpace());
127                                                    r.add(new WikiLink(oe, oe.getPrettyWord(wn + 1), wiki, isRed));
128                                            } else {
129                                                    r.add(new WikiLink(((OntologyTextElement) e), wiki, isRed));
130                                            }
131                                    } else {
132                                            r.add(new WikiLink(((OntologyTextElement) e), wiki, isRed));
133                                    }
134                            } else {
135                                    SolidLabel l = new SolidLabel(e.getText());
136                                    l.setForeground(color);
137                                    r.add(l);
138                            }
139                    }
140                    
141                    removeAll();
142                    sentenceRow.removeAll();
143                    sentenceRow.add(dropDown);
144                    sentenceRow.add(new HSpace(5));
145                    sentenceRow.add(r);
146                    sentenceRow.add(new HSpace(10));
147                    add(sentenceRow);
148                    
149                    if (sentence.isQuestion() && hostPage instanceof ArticlePage) {
150                            
151                            Column answerColumn = new Column();
152                            answerColumn.setInsets(new Insets(20, 0, 0, 0));
153                            add(answerColumn);
154                            
155                            if (sentence.isAnswerCached()) {
156                                    
157                                    Column column = new Column();
158                                    List<Individual> individuals = sentence.getAnswer();
159                                    if (individuals == null) {
160                                            column.add(new SolidLabel("(invalid question)", Font.ITALIC, 10));
161                                    } else if (individuals.size() > 0) {
162                                            Collections.sort(individuals);
163                                            for (Individual ind : individuals) {
164                                                    Row answerRow = new Row();
165                                                    answerRow.add(new ListItem(new WikiLink(ind, wiki)));
166                                                    column.add(answerRow);
167                                            }
168                                    } else {
169                                            column.add(new SolidLabel("(no answer found)", Font.ITALIC, 10));
170                                    }
171                                    column.add(new VSpace(4));
172                                    answerColumn.add(column);
173                                    
174                            } else {
175                                    answerColumn.add(new DelayedComponent(new Label(new ResourceImageReference("ch/uzh/ifi/attempto/acewiki/gui/img/wait.gif"))) {
176                                            
177                                            private static final long serialVersionUID = 7865984138467729544L;
178    
179                                            public Component initComponent() {
180                                                    Column column = new Column();
181                                                    List<Individual> individuals = sentence.getAnswer();
182                                                    if (individuals == null) {
183                                                            column.add(new SolidLabel("(invalid question)", Font.ITALIC, 10));
184                                                    } else if (individuals.size() > 0) {
185                                                            Collections.sort(individuals);
186                                                            for (Individual ind : individuals) {
187                                                                    Row answerRow = new Row();
188                                                                    answerRow.add(new ListItem(new WikiLink(ind, wiki)));
189                                                                    column.add(answerRow);
190                                                            }
191                                                    } else {
192                                                            column.add(new SolidLabel("(no answer found)", Font.ITALIC, 10));
193                                                    }
194                                                    column.add(new VSpace(4));
195                                                    
196                                                    return column;
197                                            }
198                                            
199                                    });
200                            }
201                            
202                    }
203            }
204    
205            public void actionPerformed(ActionEvent e) {
206                    if (e.getActionCommand().equals("Edit...")) {
207                            wiki.log("page", "dropdown: edit sentence: " + sentence.getText());
208                            ArticlePage page = ArticlePage.create(sentence.getOwner(), wiki);
209                            wiki.showPage(page);
210                            page.edit(sentence);
211                    } else if (e.getActionCommand().equals("Add...")) {
212                            wiki.log("page", "dropdown: add sentence");
213                            wiki.showWindow(SentenceEditorHandler.generatePreditorAddWindow(sentence, (ArticlePage) hostPage));
214                    } else if (e.getActionCommand().equals("Delete")) {
215                            wiki.log("page", "dropdown: delete sentence: " + sentence.getText());
216                            wiki.showWindow(new MessageWindow("Delete", "Do you really want to delete this sentence?", null, this, "Yes", "No"));
217                    } else if (e.getActionCommand().equals("Reassert")) {
218                            int success = sentence.reassert();
219                            if (success == 1) {
220                                    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"));
221                            } else if (success == 2) {
222                                    wiki.showWindow(new MessageWindow("Error", "A sentence could not be added to the knowledge base because the knowledge base got too complex.", "OK"));
223                            }
224                            if (sentence.isIntegrated()) {
225                                    update();
226                            }
227                    } else if (e.getActionCommand().equals("Details")) {
228                            wiki.log("page", "dropdown: details sentence: " + sentence.getText());
229                            wiki.showPage(new SentencePage(wiki, sentence));
230                    } else if (e.getActionCommand().equals("Logic")) {
231                            wiki.log("page", "dropdown: logic sentence: " + sentence.getText());
232                            wiki.showPage(new LogicPage(wiki, sentence));
233                    } else if (e.getSource() instanceof MessageWindow && e.getActionCommand().equals("Yes")) {
234                            wiki.log("page", "dropdown: delete confirmed: " + sentence.getText());
235                            
236                            wiki.enqueueTaskShowingWaitWindow(
237                                    "Updating",
238                                    "The sentence is being removed from the knowledge base...",
239                                    new Task() {
240                                            public void run() {
241                                                    sentence.getOwner().remove(sentence);
242                                            }
243                                            public void updateGUI() {
244                                                    wiki.update();
245                                                    wiki.refresh();
246                                            }
247                                    }
248                            );
249                    }
250            }
251    
252    }