001    // This file is part of AceWiki.
002    // Copyright 2008-2012, AceWiki developers.
003    // 
004    // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
005    // Lesser General Public License as published by the Free Software Foundation, either version 3 of
006    // the License, or (at your option) any later version.
007    // 
008    // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009    // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010    // Lesser General Public License for more details.
011    // 
012    // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
013    // not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.acewiki.gui;
016    
017    import nextapp.echo.app.Column;
018    import nextapp.echo.app.Row;
019    import nextapp.echo.app.event.ActionEvent;
020    import nextapp.echo.app.event.ActionListener;
021    import ch.uzh.ifi.attempto.acewiki.Task;
022    import ch.uzh.ifi.attempto.acewiki.Wiki;
023    import ch.uzh.ifi.attempto.acewiki.core.InconsistencyException;
024    import ch.uzh.ifi.attempto.acewiki.core.OntologyElement;
025    import ch.uzh.ifi.attempto.acewiki.core.Question;
026    import ch.uzh.ifi.attempto.acewiki.core.Sentence;
027    import ch.uzh.ifi.attempto.echocomp.HSpace;
028    import ch.uzh.ifi.attempto.echocomp.MessageWindow;
029    
030    /**
031     * This class represents a sentence component consisting of a drop down menu and the sentence text.
032     * 
033     * @author Tobias Kuhn
034     */
035    public class SentenceComponent extends Column implements ActionListener {
036    
037            private static final long serialVersionUID = -540135972060005725L;
038            
039            private Sentence sentence;
040            private Wiki wiki;
041            private WikiPage hostPage;
042            
043            private Row sentenceRow = new Row();
044            private StatementMenu dropDown;
045            private RecalcIcon recalcIcon;
046            
047            /**
048             * Creates a new sentence component. The host page is the page that contains the text row
049             * (which is not necessarily the owner page of the sentence).
050             * 
051             * @param sentence The sentence to be shown.
052             * @param hostPage The host page of the text row.
053             */
054            public SentenceComponent(Sentence sentence, WikiPage hostPage) {
055                    this.sentence = sentence;
056                    this.hostPage = hostPage;
057                    this.wiki = hostPage.getWiki();
058                    this.recalcIcon = new RecalcIcon("The answer to this question is being updated.");
059                    update();
060            }
061            
062            private void update() {
063                    if (sentence.isImmutable()) {
064                            dropDown = new StatementMenu(StatementMenu.INFERRED_TYPE, wiki, this);
065                    } else if (sentence instanceof Question) {
066                            dropDown = new StatementMenu(StatementMenu.QUESTION_TYPE, wiki, this);
067                    } else if (sentence.isReasonable()) {
068                            dropDown = new StatementMenu(StatementMenu.REASONING_TYPE, wiki, this);
069                    } else {
070                            dropDown = new StatementMenu(StatementMenu.NOREASONING_TYPE, wiki, this);
071                    }
072                    
073                    if (!wiki.isReadOnly() && !sentence.isImmutable()) {
074                            dropDown.addMenuEntry("Edit...", "Edit this sentence");
075                            if (sentence.isReasonable()) {
076                                    if (sentence.isIntegrated()) {
077                                            dropDown.addMenuEntry("Retract", "Retract this sentence from the knowledge base");
078                                    } else {
079                                            dropDown.addMenuEntry("Reassert", "Reassert this sentence in the knowledge base");
080                                    }
081                            }
082                            dropDown.addMenuEntry("Delete", "Delete this sentence from the article");
083                    }
084                    
085                    dropDown.addMenuEntry("Show Details", "Show the details of this sentence");
086                    
087                    if (!wiki.isReadOnly() && hostPage instanceof ArticlePage) {
088                            dropDown.addMenuSeparator();
089                            dropDown.addMenuEntry("Add Sentence...", "Add a new sentence here");
090                            dropDown.addMenuEntry("Add Comment...", "Add a new comment here");
091                    }
092                    
093                    
094                    boolean isRed = !sentence.isIntegrated() && !sentence.isImmutable() && !(sentence instanceof Question);
095                    
096                    removeAll();
097                    sentenceRow.removeAll();
098                    sentenceRow.add(dropDown);
099                    sentenceRow.add(new HSpace(5));
100                    sentenceRow.add(new TextRow(sentence.getTextElements(wiki.getLanguage()), wiki, isRed));
101                    sentenceRow.add(new HSpace(5));
102                    sentenceRow.add(recalcIcon);
103                    recalcIcon.setVisible(false);
104                    sentenceRow.add(new HSpace(5));
105                    add(sentenceRow);
106                    
107                    // Question Answering:
108                    if (sentence instanceof Question && hostPage instanceof ArticlePage) {
109                            add(new AnswerList(wiki, (Question) sentence, recalcIcon));
110                    }
111            }
112    
113            public void actionPerformed(ActionEvent e) {
114                    if (e.getActionCommand().equals("Edit...")) {
115                            log("dropdown: edit sentence:");
116                            if (!wiki.isEditable()) {
117                                    wiki.showLoginWindow();
118                            } else {
119                                    OntologyElement el = sentence.getArticle().getOntologyElement();
120                                    ArticlePage page = ArticlePage.create(el, wiki);
121                                    wiki.showPage(page);
122                                    wiki.showWindow(SentenceEditorHandler.generateEditWindow(sentence, page));
123                            }
124                    } else if (e.getActionCommand().equals("Add Sentence...")) {
125                            log("dropdown: add sentence");
126                            if (!wiki.isEditable()) {
127                                    wiki.showLoginWindow();
128                            } else {
129                                    wiki.showWindow(SentenceEditorHandler.generateCreationWindow(
130                                                    sentence,
131                                                    (ArticlePage) hostPage
132                                            ));
133                            }
134                    } else if (e.getActionCommand().equals("Add Comment...")) {
135                            log("dropdown: add comment");
136                            if (!wiki.isEditable()) {
137                                    wiki.showLoginWindow();
138                            } else {
139                                    wiki.showWindow(CommentEditorHandler.generateCreationWindow(
140                                                    sentence,
141                                                    (ArticlePage) hostPage
142                                            ));
143                            }
144                    } else if (e.getActionCommand().equals("Delete")) {
145                            log("dropdown: delete sentence:");
146                            if (!wiki.isEditable()) {
147                                    wiki.showLoginWindow();
148                            } else {
149                                    wiki.showWindow(new MessageWindow(
150                                                    "Delete",
151                                                    "Do you really want to delete this sentence?",
152                                                    null,
153                                                    this,
154                                                    "Yes",
155                                                    "No"
156                                            ));
157                            }
158                    } else if (e.getActionCommand().equals("Reassert")) {
159                            log("dropdown: reassert:");
160                            if (!wiki.isEditable()) {
161                                    wiki.showLoginWindow();
162                            } else {
163                                    try {
164                                            wiki.getOntology().reassert(sentence);
165                                    } catch (InconsistencyException ex) {
166                                            wiki.showWindow(new MessageWindow(
167                                                            "Conflict",
168                                                            "The sentence is in conflict with the current knowledge. For that " +
169                                                                    "reason, it cannot be added to the knowledge base.",
170                                                            "OK"
171                                                    ));
172                                    }
173                                    if (sentence.isIntegrated()) {
174                                            update();
175                                            hostPage.update();
176                                    }
177                            }
178                    } else if (e.getActionCommand().equals("Retract")) {
179                            log("dropdown: retract:");
180                            if (!wiki.isEditable()) {
181                                    wiki.showLoginWindow();
182                            } else {
183                                    wiki.getOntology().retract(sentence);
184                                    update();
185                                    hostPage.update();
186                            }
187                    } else if (e.getActionCommand().equals("Show Details")) {
188                            log("dropdown: details sentence:");
189                            wiki.showPage(new SentencePage(wiki, sentence));
190                    } else if (e.getSource() instanceof MessageWindow && e.getActionCommand().equals("Yes")) {
191                            log("dropdown: delete confirmed:");
192                            
193                            wiki.enqueueStrongAsyncTask(
194                                    "Updating",
195                                    "The sentence is being removed from the knowledge base...",
196                                    new Task() {
197                                            public void run() {
198                                                    sentence.getArticle().remove(sentence);
199                                            }
200                                            public void updateGUI() {
201                                                    wiki.update();
202                                                    wiki.refresh();
203                                            }
204                                    }
205                            );
206                    }
207            }
208            
209            private void log(String text) {
210                    if (text.endsWith(":")) {
211                            text += " " + sentence.getText(wiki.getEngine().getLanguages()[0]);
212                    }
213                    wiki.log("page", text);
214            }
215    
216    }