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.event.ActionEvent;
018    import nextapp.echo.app.event.ActionListener;
019    import ch.uzh.ifi.attempto.acewiki.core.Article;
020    import ch.uzh.ifi.attempto.acewiki.core.Comment;
021    import ch.uzh.ifi.attempto.acewiki.core.Statement;
022    import ch.uzh.ifi.attempto.echocomp.TextAreaWindow;
023    
024    /**
025     * This class manages the comment editor. It creates the editor window and handles its
026     * responses.
027     * 
028     * @author Tobias Kuhn
029     */
030    public class CommentEditorHandler implements ActionListener {
031            
032            private static final long serialVersionUID = 1156092885844135235L;
033            
034            private TextAreaWindow textAreaWindow;
035            private Statement statement;
036            private ArticlePage page;
037            private boolean edit;
038            
039            /**
040             * Creates a new comment editor handler, either to create a new comment or to edit an existing
041             * comment.
042             * 
043             * @param statement The statement in front of which the new comment should be added (in the
044             *   case of edit=false) or the comment that should be edited (in the case of edit=true).
045             * @param page The host page of the comment.
046             * @param edit true if an existing comment should be edited, or false if a new comment should
047             *   be created.
048             */
049            private CommentEditorHandler(Statement statement, ArticlePage page, boolean edit) {
050                    this.statement = statement;
051                    this.page = page;
052                    this.edit = edit;
053                    
054                    textAreaWindow = new TextAreaWindow("Comment Editor", this);
055                    textAreaWindow.setSize(600, 350);
056                    if (edit) {
057                            textAreaWindow.setText(((Comment) statement).getText());
058                    }
059            }
060    
061            /**
062             * Generates a new comment editor window for the creation of a new comment.
063             * 
064             * @param followingStatement The statement in front of which the new sentences should be added,
065             *   or null if the sentences should be added to the end of the article.
066             * @param page The host page into which the sentence should be added.
067             * @return A new preditor window.
068             */
069            public static TextAreaWindow generateCreationWindow(Statement followingStatement,
070                            ArticlePage page) {
071                    CommentEditorHandler h = new CommentEditorHandler(followingStatement, page, false);
072                    return h.getWindow();
073            }
074    
075            /**
076             * Generates a new comment editor window for editing an existing comment.
077             * 
078             * @param comment The comment that should be edited.
079             * @param page The host page which contains the sentence to be edited.
080             * @return A new preditor window.
081             */
082            public static TextAreaWindow generateEditWindow(Comment comment, ArticlePage page) {
083                    CommentEditorHandler h = new CommentEditorHandler(comment, page, true);
084                    return h.getWindow();
085            }
086            
087            private TextAreaWindow getWindow() {
088                    return textAreaWindow;
089            }
090            
091            public void actionPerformed(ActionEvent e) {
092                    if (e.getActionCommand().equals("OK")) {
093                            Article article = page.getArticle();
094                            Comment comment = article.getOntology().getStatementFactory()
095                                    .createComment(textAreaWindow.getText(), article);
096                            if (edit) {
097                                    article.edit(statement, comment);
098                            } else {
099                                    article.add(statement, comment);
100                            }
101                            page.update();
102                            page.getWiki().removeWindow(textAreaWindow);
103                    } else if (e.getActionCommand().equals("Cancel")) {
104                            page.getWiki().removeWindow(textAreaWindow);
105                    }
106            }
107    
108    }