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 java.util.ArrayList;
018    import java.util.List;
019    
020    import nextapp.echo.app.Color;
021    import nextapp.echo.app.Column;
022    import nextapp.echo.app.Extent;
023    import nextapp.echo.app.Font;
024    import nextapp.echo.app.Insets;
025    import nextapp.echo.app.Row;
026    import nextapp.echo.app.event.ActionEvent;
027    import nextapp.echo.app.event.ActionListener;
028    import ch.uzh.ifi.attempto.acewiki.Wiki;
029    import ch.uzh.ifi.attempto.acewiki.core.Article;
030    import ch.uzh.ifi.attempto.acewiki.core.LanguageUtils;
031    import ch.uzh.ifi.attempto.acewiki.core.OntologyElement;
032    import ch.uzh.ifi.attempto.acewiki.core.Sentence;
033    import ch.uzh.ifi.attempto.echocomp.HSpace;
034    import ch.uzh.ifi.attempto.echocomp.SolidLabel;
035    import ch.uzh.ifi.attempto.echocomp.VSpace;
036    
037    /**
038     * This class represents a page that shows all references for a certain ontology element.
039     * 
040     * @author Tobias Kuhn
041     */
042    public class ReferencesPage extends WikiPage implements ActionListener {
043    
044            private static final long serialVersionUID = 1025665226113017153L;
045    
046            private static final int pageSize = 50;
047            
048            private ArticlePage page;
049            private Column referenceColumn = new Column();
050            private IndexBar indexBar;
051            private List<Sentence> sentences;
052            private int chosenPage = 0;
053            private Title title;
054            
055            /**
056             * Creates a new references page.
057             * 
058             * @param page The main page that contains the article.
059             */
060            public ReferencesPage(ArticlePage page) {
061                    super(page.getWiki());
062                    this.page = page;
063                    
064                    addTab("Article", this);
065                    addSelectedTab("References");
066                    if (page instanceof ConceptPage) {
067                            addTab("Individuals", this);
068                            addTab("Hierarchy", this);
069                    }
070                    if (page instanceof IndividualPage) {
071                            addTab("Assignments", this);
072                    }
073                    
074                    OntologyElement oe = page.getOntologyElement();
075                    title = new Title(getHeading(oe), "- References", oe.getType(), this);
076                    add(title);
077                    addHorizontalLine();
078                    add(new VSpace(18));
079                    
080                    indexBar = new IndexBar("Page:", 0, this);
081                    add(indexBar);
082    
083                    referenceColumn.setInsets(new Insets(10, 2, 5, 20));
084                    referenceColumn.setCellSpacing(new Extent(2));
085                    add(referenceColumn);
086            }
087            
088            protected void doUpdate() {
089                    title.setText(getHeading(page.getOntologyElement()));
090                    referenceColumn.removeAll();
091                    List<OntologyElement> ontologyElements = getWiki().getOntologyElements();
092                    sentences = new ArrayList<Sentence>();
093                    LanguageUtils.sortOntologyElements(ontologyElements);
094                    for (OntologyElement oe : ontologyElements) {
095                            if (oe == page.getOntologyElement()) continue;
096                            for (Sentence s : oe.getArticle().getSentences()) {
097                                    if (s.contains(page.getOntologyElement())) {
098                                            sentences.add(s);
099                                    }
100                            }
101                    }
102                    if (sentences.size() == 0) {
103                            indexBar.setVisible(false);
104                            String h = LanguageUtils.getPrettyPrinted(page.getOntologyElement().getHeadwords()[0]);
105                            referenceColumn.add(new SolidLabel(
106                                            "(no other article refers to '" + h + "')",
107                                            Font.ITALIC,
108                                            10
109                                    ));
110                    } else {
111                            int i = ((sentences.size()-1) / pageSize) + 1;
112                            if (chosenPage > i) chosenPage = 0;
113                            indexBar.setNumbers(i);
114                            indexBar.setActiveButton(chosenPage);
115                            updatePage();
116                    }
117            }
118            
119            private void updatePage() {
120                    referenceColumn.removeAll();
121                    
122                    indexBar.setVisible(sentences.size() > pageSize);
123                    
124                    int max = sentences.size();
125                    if (max > (chosenPage + 1) * pageSize) max = (chosenPage + 1) * pageSize;
126                    
127                    Article a = null;
128                    for (int i = chosenPage * pageSize; i < max; i++) {
129                            Sentence s = sentences.get(i);
130                            if (a != s.getArticle()) {
131                                    a = s.getArticle();
132                                    Row r = new Row();
133                                    Column c = new Column();
134                                    c.add(new WikiLink(a.getOntologyElement(), getWiki()));
135                                    Row line = new Row();
136                                    line.setBackground(Color.DARKGRAY);
137                                    line.setInsets(new Insets(0, 1, 0, 0));
138                                    c.add(line);
139                                    r.add(c);
140                                    if (i > 0 && sentences.get(i-1).getArticle() == a) {
141                                            r.add(new HSpace());
142                                            r.add(new SolidLabel("(continued)", Font.ITALIC, 10));
143                                    }
144                                    referenceColumn.add(new VSpace());
145                                    referenceColumn.add(r);
146                            }
147                            Row r = new Row();
148                            r.add(new SentenceComponent(s, this));
149                            referenceColumn.add(r);
150                    }
151            }
152    
153            public void actionPerformed(ActionEvent e) {
154                    Wiki wiki = getWiki();
155                    if ("Article".equals(e.getActionCommand())) {
156                            log("page", "pressed: article");
157                            wiki.showPage(page);
158                    } else if ("Individuals".equals(e.getActionCommand())) {
159                            log("page", "pressed: individuals");
160                            wiki.showPage(new IndividualsPage((ConceptPage) page));
161                    } else if ("Hierarchy".equals(e.getActionCommand())) {
162                            log("page", "pressed: hierarchy");
163                            wiki.showPage(new HierarchyPage((ConceptPage) page));
164                    } else if ("Assignments".equals(e.getActionCommand())) {
165                            log("page", "pressed: assignments");
166                            wiki.showPage(new AssignmentsPage((IndividualPage) page));
167                    } else if (e.getSource() == indexBar) {
168                            chosenPage = Integer.parseInt(e.getActionCommand()) - 1;
169                            log("page", "pressed: page " + (chosenPage+1));
170                            updatePage();
171                    } else if (e.getSource() == title) {
172                            wiki.showEditorWindow(page.getOntologyElement());
173                    }
174            }
175    
176            public boolean equals(Object obj) {
177                    if (obj instanceof ReferencesPage) {
178                            return page.equals(((ReferencesPage) obj).page);
179                    }
180                    return false;
181            }
182            
183            public boolean isExpired() {
184                    return page.isExpired();
185            }
186            
187            public String toString() {
188                    return "-REF- " + page.getOntologyElement().getWord();
189            }
190    
191    }