001    // This file is part of the Attempto Java Packages.
002    // Copyright 2008, 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    import java.util.List;
020    
021    import nextapp.echo2.app.Column;
022    import nextapp.echo2.app.Component;
023    import nextapp.echo2.app.Extent;
024    import nextapp.echo2.app.Font;
025    import nextapp.echo2.app.Insets;
026    import nextapp.echo2.app.ResourceImageReference;
027    import nextapp.echo2.app.Row;
028    import nextapp.echo2.app.event.ActionEvent;
029    import nextapp.echo2.app.event.ActionListener;
030    import ch.uzh.ifi.attempto.acewiki.core.ontology.Concept;
031    import ch.uzh.ifi.attempto.acewiki.core.ontology.Individual;
032    import ch.uzh.ifi.attempto.acewiki.core.ontology.Sentence;
033    import ch.uzh.ifi.attempto.acewiki.gui.IndexBar;
034    import ch.uzh.ifi.attempto.acewiki.gui.TextRow;
035    import ch.uzh.ifi.attempto.acewiki.gui.Title;
036    import ch.uzh.ifi.attempto.echocomp.DelayedComponent;
037    import ch.uzh.ifi.attempto.echocomp.Label;
038    import ch.uzh.ifi.attempto.echocomp.SolidLabel;
039    import ch.uzh.ifi.attempto.echocomp.VSpace;
040    
041    /**
042     * This class represents a page that shows all individuals that belong to a certain concept.
043     * 
044     * @author Tobias Kuhn
045     */
046    public class IndividualsPage extends WikiPage implements ActionListener {
047    
048            private static final long serialVersionUID = 4273564259160715684L;
049    
050            private static final int pageSize = 20;
051            
052            private ConceptPage page;
053            private Column individualsColumn = new Column();
054            private int chosenPage = 0;
055            
056            /**
057             * Creates a new individuals page.
058             * 
059             * @param page The main page that contains the article.
060             */
061            public IndividualsPage(ConceptPage page) {
062                    super(page.getWiki(), new Title(page.getOntologyElement().getHeadword(), "- Individuals"));
063                    this.page = page;
064                    
065                    addTab("Article", this);
066                    addTab("Noun", this);
067                    addTab("References", this);
068                    addSelectedTab("Individuals");
069                    addTab("Hierarchy", this);
070                    
071                    add(new VSpace(18));
072                    
073                    add(individualsColumn);
074            }
075            
076            protected void doUpdate() {
077                    getTitle().setText(page.getOntologyElement().getHeadword());
078                    individualsColumn.removeAll();
079                    
080                    Column waitComp = new Column();
081                    waitComp.setInsets(new Insets(10, 0, 0, 0));
082                    waitComp.add(new Label(new ResourceImageReference("ch/uzh/ifi/attempto/acewiki/gui/img/wait.gif")));
083                    
084                    if (((Concept) page.getOntologyElement()).areIndividualsCached()) {
085                            individualsColumn.add(new IndividualsComponent());
086                    } else {
087                            individualsColumn.add(new DelayedComponent(waitComp) {
088                                    
089                                    private static final long serialVersionUID = -992569061632136205L;
090    
091                                    public Component initComponent() {
092                                            return new IndividualsComponent();
093                                    }
094                                    
095                            });
096                    }
097            }
098    
099            public void actionPerformed(ActionEvent e) {
100                    if ("Article".equals(e.getActionCommand())) {
101                            log("page", "pressed: article");
102                            getWiki().showPage(page);
103                    } else if ("Noun".equals(e.getActionCommand())) {
104                            log("page", "pressed: word");
105                            getWiki().showPage(new WordPage(page));
106                    } else if ("References".equals(e.getActionCommand())) {
107                            log("page", "pressed: references");
108                            getWiki().showPage(new ReferencesPage(page));
109                    } else if ("Hierarchy".equals(e.getActionCommand())) {
110                            log("page", "pressed: hierarchy");
111                            getWiki().showPage(new HierarchyPage(page));
112                    }
113            }
114    
115            public boolean equals(Object obj) {
116                    if (obj instanceof IndividualsPage) {
117                            return page.equals(((IndividualsPage) obj).page);
118                    }
119                    return false;
120            }
121            
122            public boolean isExpired() {
123                    return page.isExpired();
124            }
125            
126            public String toString() {
127                    return "-IND- " + page.getOntologyElement().getWord();
128            }
129            
130            
131            private class IndividualsComponent extends Column implements ActionListener {
132                    
133                    private static final long serialVersionUID = -2897618204616741456L;
134                    
135                    private Column sentencesColumn = new Column();
136                    private IndexBar indexBar;
137                    private ArrayList<Sentence> sentences;
138                    
139                    
140                    public IndividualsComponent() {
141                            indexBar = new IndexBar("Page:", 0, this);
142                            add(indexBar);
143                            
144                            sentencesColumn.setInsets(new Insets(10, 2, 5, 20));
145                            sentencesColumn.setCellSpacing(new Extent(2));
146                            add(sentencesColumn);
147                            
148                            Concept concept = (Concept) page.getOntologyElement();
149                            List<Individual> individuals = concept.getIndividuals();
150                            sentences = new ArrayList<Sentence>();
151                            Collections.sort(individuals);
152                            for (Individual ind : individuals) {
153                                    sentences.add(new Sentence(ind.getWord() + " is a " + concept.getWord() + ".", concept.getOntology()));
154                            }
155                            if (sentences.size() == 0) {
156                                    indexBar.setVisible(false);
157                                    sentencesColumn.add(new SolidLabel("(no individual found)", Font.ITALIC, 10));
158                            } else {
159                                    int i = ((sentences.size()-1) / pageSize) + 1;
160                                    if (chosenPage > i) chosenPage = 0;
161                                    indexBar.setNumbers(i);
162                                    indexBar.setActiveButton(chosenPage);
163                                    updatePage();
164                            }
165                    }
166                    
167                    private void updatePage() {
168                            sentencesColumn.removeAll();
169                            
170                            indexBar.setVisible(sentences.size() > pageSize);
171                            
172                            int max = sentences.size();
173                            if (max > (chosenPage + 1) * pageSize) max = (chosenPage + 1) * pageSize;
174                            
175                            for (int i = chosenPage * pageSize; i < max; i++) {
176                                    Row r = new Row();
177                                    r.add(new TextRow(sentences.get(i), IndividualsPage.this));
178                                    sentencesColumn.add(r);
179                            }
180                    }
181    
182                    public void actionPerformed(ActionEvent e) {
183                            if (e.getSource() == indexBar) {
184                                    chosenPage = Integer.parseInt(e.getActionCommand()) - 1;
185                                    log("page", "pressed: page " + (chosenPage+1));
186                                    updatePage();
187                            }
188                    }
189                    
190            }
191    
192    }