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