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.Extent;
019    import nextapp.echo.app.Font;
020    import nextapp.echo.app.Insets;
021    import nextapp.echo.app.Row;
022    import nextapp.echo.app.event.ActionEvent;
023    import nextapp.echo.app.event.ActionListener;
024    import ch.uzh.ifi.attempto.acewiki.Wiki;
025    import ch.uzh.ifi.attempto.acewiki.core.Article;
026    import ch.uzh.ifi.attempto.acewiki.core.Comment;
027    import ch.uzh.ifi.attempto.acewiki.core.Concept;
028    import ch.uzh.ifi.attempto.acewiki.core.DummyOntologyElement;
029    import ch.uzh.ifi.attempto.acewiki.core.Individual;
030    import ch.uzh.ifi.attempto.acewiki.core.OntologyElement;
031    import ch.uzh.ifi.attempto.acewiki.core.Relation;
032    import ch.uzh.ifi.attempto.acewiki.core.Sentence;
033    import ch.uzh.ifi.attempto.acewiki.core.Statement;
034    import ch.uzh.ifi.attempto.echocomp.SolidLabel;
035    
036    /**
037     * This class stands for a wiki page that represents an ontology element and shows the
038     * article of this ontology element.
039     * 
040     * @author Tobias Kuhn
041     */
042    public abstract class ArticlePage extends WikiPage implements ActionListener {
043            
044            private static final long serialVersionUID = -297830105047433502L;
045            
046            private Column textColumn = new Column();
047            private StatementMenu dropDown = new StatementMenu(StatementMenu.EMPTY_TYPE, getWiki(), this);
048            private Title title;
049            
050            /**
051             * Creates a new article page.
052             * 
053             * @param wiki The wiki instance.
054             * @param ontologyElement The ontology element whose article should be shown.
055             */
056            protected ArticlePage(Wiki wiki, OntologyElement ontologyElement) {
057                    super(wiki);
058                    
059                    if (!(ontologyElement instanceof DummyOntologyElement)) {
060                            addSelectedTab("Article");
061                            addTab("References", this);
062                            title = new Title(getHeading(ontologyElement), ontologyElement.getType(), this);
063                    } else {
064                            title = new Title("", true, null, null);
065                    }
066                    
067                    add(title);
068                    addHorizontalLine();
069                    
070                    dropDown.addMenuEntry("Add Sentence...", "Add a new sentence here");
071                    dropDown.addMenuEntry("Add Comment...", "Add a new comment here");
072                    
073                    textColumn.setInsets(new Insets(10, 20, 0, 50));
074                    textColumn.setCellSpacing(new Extent(2));
075                    add(textColumn);
076            }
077            
078            /**
079             * Creates an article page for the given ontology element.
080             * 
081             * @param oe The ontology element for which an article page should be created.
082             * @param wiki The wiki instance.
083             * @return The new article page.
084             */
085            public static ArticlePage create(OntologyElement oe, Wiki wiki) {
086                    if (oe instanceof Individual) {
087                            return new IndividualPage((Individual) oe, wiki);
088                    } else if (oe instanceof Concept) {
089                            return new ConceptPage((Concept) oe, wiki);
090                    } else if (oe instanceof Relation) {
091                            return new RelationPage((Relation) oe, wiki);
092                    } else if (oe instanceof DummyOntologyElement) {
093                            return new StartPage(wiki);
094                    }
095                    return null;
096            }
097            
098            /**
099             * Returns the ontology element of this article page.
100             * 
101             * @return The ontology element.
102             */
103            public abstract OntologyElement getOntologyElement();
104            
105            /**
106             * Returns the article object.
107             * 
108             * @return The article.
109             */
110            public Article getArticle() {
111                    return getOntologyElement().getArticle();
112            }
113            
114            protected void doUpdate() {
115                    textColumn.removeAll();
116                    
117                    for (Statement s : getArticle().getStatements()) {
118                            if (s instanceof Sentence) {
119                                    textColumn.add(new SentenceComponent((Sentence) s, this));
120                            } else if (s instanceof Comment) {
121                                    textColumn.add(new CommentComponent((Comment) s, this));
122                            }
123                    }
124                    
125                    if (getArticle().getStatements().size() == 0) {
126                            textColumn.add(new SolidLabel("(article is empty)", Font.ITALIC, 10));
127                    }
128                    
129                    if (!getWiki().isReadOnly()) {
130                            Row addButtonRow = new Row();
131                            addButtonRow.add(dropDown);
132                            textColumn.add(addButtonRow);
133                    }
134            }
135    
136            public boolean equals(Object obj) {
137                    if (obj instanceof ArticlePage) {
138                            return getOntologyElement() == ((ArticlePage) obj).getOntologyElement();
139                    }
140                    return false;
141            }
142            
143            public String toString() {
144                    return getOntologyElement().getWord();
145            }
146            
147            public boolean isExpired() {
148                    return !getWiki().getOntology().contains(getOntologyElement());
149            }
150            
151            /**
152             * Returns the title object of this page.
153             * 
154             * @return The title.
155             */
156            protected Title getTitle() {
157                    return title;
158            }
159    
160            public void actionPerformed(ActionEvent e) {
161                    if (e.getActionCommand().equals("Add Sentence...")) {
162                            getWiki().log("page", "dropdown: add sentence");
163                            if (!getWiki().isEditable()) {
164                                    getWiki().showLoginWindow();
165                            } else {
166                                    getWiki().showWindow(SentenceEditorHandler.generateCreationWindow(null, this));
167                            }
168                    } else if (e.getActionCommand().equals("Add Comment...")) {
169                            getWiki().log("page", "dropdown: add comment");
170                            if (!getWiki().isEditable()) {
171                                    getWiki().showLoginWindow();
172                            } else {
173                                    getWiki().showWindow(CommentEditorHandler.generateCreationWindow(null, this));
174                            }
175                    } else if ("References".equals(e.getActionCommand())) {
176                            log("page", "pressed: references");
177                            getWiki().showPage(new ReferencesPage(this));
178                    } else if (e.getSource() == title) {
179                            getWiki().showEditorWindow(getOntologyElement());
180                    }
181            }
182    
183    }