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    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                    final 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(true));
087                    } else {
088                            individualsColumn.add(waitComp);
089                            individualsColumn.add(
090                                    new DelayedComponent(new IndividualsComponent(true), true) {
091                                    
092                                            private static final long serialVersionUID = -992569061632136205L;
093            
094                                            public Component initComponent() {
095                                                    return new IndividualsComponent(false);
096                                            }
097                                            
098                                            public void finalizeAction() {
099                                                    individualsColumn.remove(waitComp);
100                                            }
101                                            
102                                    }
103                            );
104                    }
105            }
106    
107            public void actionPerformed(ActionEvent e) {
108                    if ("Article".equals(e.getActionCommand())) {
109                            log("page", "pressed: article");
110                            getWiki().showPage(page);
111                    } else if ("Noun".equals(e.getActionCommand())) {
112                            log("page", "pressed: word");
113                            getWiki().showPage(new WordPage(page));
114                    } else if ("References".equals(e.getActionCommand())) {
115                            log("page", "pressed: references");
116                            getWiki().showPage(new ReferencesPage(page));
117                    } else if ("Hierarchy".equals(e.getActionCommand())) {
118                            log("page", "pressed: hierarchy");
119                            getWiki().showPage(new HierarchyPage(page));
120                    }
121            }
122    
123            public boolean equals(Object obj) {
124                    if (obj instanceof IndividualsPage) {
125                            return page.equals(((IndividualsPage) obj).page);
126                    }
127                    return false;
128            }
129            
130            public boolean isExpired() {
131                    return page.isExpired();
132            }
133            
134            public String toString() {
135                    return "-IND- " + page.getOntologyElement().getWord();
136            }
137            
138            
139            private class IndividualsComponent extends Column implements ActionListener {
140                    
141                    private static final long serialVersionUID = -2897618204616741456L;
142                    
143                    private Column sentencesColumn = new Column();
144                    private IndexBar indexBar;
145                    private ArrayList<Sentence> sentences;
146                    
147                    
148                    public IndividualsComponent(boolean cached) {
149                            indexBar = new IndexBar("Page:", 0, this);
150                            add(indexBar);
151                            
152                            sentencesColumn.setInsets(new Insets(10, 2, 5, 20));
153                            sentencesColumn.setCellSpacing(new Extent(2));
154                            add(sentencesColumn);
155                            
156                            Concept concept = (Concept) page.getOntologyElement();
157                            List<Individual> individuals;
158                            if (cached) {
159                                    individuals = concept.getCachedIndividuals();
160                            } else {
161                                    individuals = concept.getIndividuals();
162                            }
163                            if (individuals != null) {
164                                    sentences = new ArrayList<Sentence>();
165                                    
166                                    Comparator<Individual> comparator = new Comparator<Individual>() {
167                                            public int compare(Individual o1, Individual o2) {
168                                                    return o1.getWord(2).compareTo(o2.getWord(2));
169                                            }
170                                    };
171                                    
172                                    Collections.sort(individuals, comparator);
173                                    for (Individual ind : individuals) {
174                                            sentences.add(new Sentence(ind.getWord(2) + " is a " + concept.getWord() + ".", concept.getOntology()));
175                                    }
176                                    if (sentences.size() == 0) {
177                                            indexBar.setVisible(false);
178                                            sentencesColumn.add(new SolidLabel("(no individual found)", Font.ITALIC, 10));
179                                    } else {
180                                            int i = ((sentences.size()-1) / pageSize) + 1;
181                                            if (chosenPage > i) chosenPage = 0;
182                                            indexBar.setNumbers(i);
183                                            indexBar.setActiveButton(chosenPage);
184                                            updatePage();
185                                    }
186                            } else {
187                                    indexBar.setVisible(false);
188                                    sentencesColumn.add(new SolidLabel("...", Font.ITALIC, 10));
189                            }
190                    }
191                    
192                    private void updatePage() {
193                            sentencesColumn.removeAll();
194                            
195                            indexBar.setVisible(sentences.size() > pageSize);
196                            
197                            int max = sentences.size();
198                            if (max > (chosenPage + 1) * pageSize) max = (chosenPage + 1) * pageSize;
199                            
200                            for (int i = chosenPage * pageSize; i < max; i++) {
201                                    Row r = new Row();
202                                    r.add(new TextRow(sentences.get(i), IndividualsPage.this));
203                                    sentencesColumn.add(r);
204                            }
205                    }
206    
207                    public void actionPerformed(ActionEvent e) {
208                            if (e.getSource() == indexBar) {
209                                    chosenPage = Integer.parseInt(e.getActionCommand()) - 1;
210                                    log("page", "pressed: page " + (chosenPage+1));
211                                    updatePage();
212                            }
213                    }
214                    
215            }
216    
217    }