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