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.Sentence;
032    import ch.uzh.ifi.attempto.acewiki.gui.IndexBar;
033    import ch.uzh.ifi.attempto.acewiki.gui.TextRow;
034    import ch.uzh.ifi.attempto.acewiki.gui.Title;
035    import ch.uzh.ifi.attempto.echocomp.DelayedComponent;
036    import ch.uzh.ifi.attempto.echocomp.Label;
037    import ch.uzh.ifi.attempto.echocomp.SolidLabel;
038    import ch.uzh.ifi.attempto.echocomp.VSpace;
039    
040    /**
041     * This class represents a page that shows the super-concepts and sub-concepts for a given concept.
042     * Such super- and sub-concept relations are called "hierarchy" in AceWiki.
043     * 
044     * @author Tobias Kuhn
045     */
046    public class HierarchyPage extends WikiPage implements ActionListener {
047    
048            private static final long serialVersionUID = 3126817139010810197L;
049    
050            private static final int pageSize = 15;
051            
052            private ConceptPage page;
053    
054            private Column upHierarchyColumn = new Column();
055            private Column downHierarchyColumn = new Column();
056            private int upChosenPage = 0;
057            private int downChosenPage = 0;
058            
059            /**
060             * Creates a new hierarchy page.
061             * 
062             * @param page The main page that contains the article.
063             */
064            public HierarchyPage(ConceptPage page) {
065                    super(page.getWiki(), new Title(page.getOntologyElement().getHeadword(), "- Hierarchy"));
066                    this.page = page;
067                    
068                    addTab("Article", this);
069                    addTab("Noun", this);
070                    addTab("References", this);
071                    addTab("Individuals", this);
072                    addSelectedTab("Hierarchy");
073                    
074                    add(new VSpace(12));
075                    
076                    addHeadline("Upward");
077                    add(new VSpace(5));
078                    add(upHierarchyColumn);
079                    
080                    addHeadline("Downward");
081                    add(new VSpace(5));
082                    add(downHierarchyColumn);
083            }
084            
085            protected void doUpdate() {
086                    getTitle().setText(page.getOntologyElement().getHeadword());
087                    upHierarchyColumn.removeAll();
088                    downHierarchyColumn.removeAll();
089    
090                    Column waitComp1 = new Column();
091                    waitComp1.setInsets(new Insets(10, 0, 0, 0));
092                    waitComp1.add(new Label(new ResourceImageReference("ch/uzh/ifi/attempto/acewiki/gui/img/wait.gif")));
093                    
094                    Concept c = (Concept) page.getOntologyElement();
095                    
096                    if (c.areSuperConceptsCached()) {
097                            upHierarchyColumn.add(new HierarchyComponent(true));
098                    } else {
099                            upHierarchyColumn.add(new DelayedComponent(waitComp1) {
100                                    
101                                    private static final long serialVersionUID = -5068057863490522605L;
102    
103                                    public Component initComponent() {
104                                            return new HierarchyComponent(true);
105                                    }
106                                    
107                            });
108                    }
109    
110                    Column waitComp2 = new Column();
111                    waitComp2.setInsets(new Insets(10, 0, 0, 0));
112                    waitComp2.add(new Label(new ResourceImageReference("ch/uzh/ifi/attempto/acewiki/gui/img/wait.gif")));
113                    
114                    if (c.areSubConceptsCached()) {
115                            downHierarchyColumn.add(new HierarchyComponent(false));
116                    } else {
117                            downHierarchyColumn.add(new DelayedComponent(waitComp2) {
118                                    
119                                    private static final long serialVersionUID = 2782291841490690030L;
120    
121                                    public Component initComponent() {
122                                            return new HierarchyComponent(false);
123                                    }
124                                    
125                            });
126                    }
127            }
128    
129            public void actionPerformed(ActionEvent e) {
130                    if ("Article".equals(e.getActionCommand())) {
131                            log("page", "pressed: article");
132                            getWiki().showPage(page);
133                    } else if ("Noun".equals(e.getActionCommand())) {
134                            log("page", "pressed: word");
135                            getWiki().showPage(new WordPage(page));
136                    } else if ("References".equals(e.getActionCommand())) {
137                            log("page", "pressed: references");
138                            getWiki().showPage(new ReferencesPage(page));
139                    } else if ("Individuals".equals(e.getActionCommand())) {
140                            log("page", "pressed: individuals");
141                            getWiki().showPage(new IndividualsPage(page));
142                    }
143            }
144    
145            public boolean equals(Object obj) {
146                    if (obj instanceof HierarchyPage) {
147                            return page.equals(((HierarchyPage) obj).page);
148                    }
149                    return false;
150            }
151            
152            public boolean isExpired() {
153                    return page.isExpired();
154            }
155            
156            public String toString() {
157                    return "-IND- " + page.getOntologyElement().getWord();
158            }
159    
160            private class HierarchyComponent extends Column implements ActionListener {
161                    
162                    private static final long serialVersionUID = 6461817187189387351L;
163                    
164                    private boolean up;
165                    private Column column = new Column();
166                    private IndexBar indexBar;
167                    private ArrayList<Sentence> sentences;
168                    
169                    
170                    public HierarchyComponent(boolean up) {
171                            this.up = up;
172                            indexBar = new IndexBar("Page:", 0, this);
173                            add(indexBar);
174                            column.setInsets(new Insets(10, 2, 5, 10));
175                            column.setCellSpacing(new Extent(2));
176                            add(column);
177                            
178                            Concept concept = (Concept) page.getOntologyElement();
179                            List<Concept> concepts;
180                            
181                            if (up) {
182                                    concepts = concept.getSuperConcepts();
183                            } else {
184                                    concepts = concept.getSubConcepts();
185                            }
186                            sentences = new ArrayList<Sentence>();
187                            Collections.sort(concepts);
188                            for (Concept c : concepts) {
189                                    if (up) {
190                                            sentences.add(new Sentence("Every " + concept.getWord() + " is a " + c.getWord() + ".", concept.getOntology()));
191                                    } else {
192                                            sentences.add(new Sentence("Every " + c.getWord() + " is a " + concept.getWord() + ".", concept.getOntology()));
193                                    }
194                            }
195                            
196                            updatePage();
197                    }
198                    
199                    private void updatePage() {
200                            column.removeAll();
201                            
202                            String t;
203                            int chosenPage;
204                            if (up) {
205                                    t = "upward";
206                                    chosenPage = upChosenPage;
207                            } else {
208                                    t = "downward";
209                                    chosenPage = downChosenPage;
210                            }
211                            
212                            if (sentences.size() == 0) {
213                                    indexBar.setVisible(false);
214                                    column.add(new SolidLabel("(" + t + " hierarchy is empty)", Font.ITALIC, 10));
215                            } else {
216                                    int i = ((sentences.size()-1) / pageSize) + 1;
217                                    if (chosenPage > i) chosenPage = 0;
218                                    indexBar.setNumbers(i);
219                                    indexBar.setActiveButton(chosenPage);
220                            }
221                            
222                            indexBar.setVisible(sentences.size() > pageSize);
223                            
224                            int max = sentences.size();
225                            if (max > (chosenPage + 1) * pageSize) max = (chosenPage + 1) * pageSize;
226                            for (int i = chosenPage * pageSize; i < max; i++) {
227                                    Row r = new Row();
228                                    r.add(new TextRow(sentences.get(i), HierarchyPage.this));
229                                    column.add(r);
230                            }
231                    }
232                    
233                    public void actionPerformed(ActionEvent e) {
234                            if (e.getSource() == indexBar) {
235                                    if (up) {
236                                            upChosenPage = Integer.parseInt(e.getActionCommand()) - 1;
237                                            log("page", "pressed: page up:" + (upChosenPage+1));
238                                            updatePage();
239                                    } else {
240                                            downChosenPage = Integer.parseInt(e.getActionCommand()) - 1;
241                                            log("page", "pressed: page down:" + (downChosenPage+1));
242                                            updatePage();
243                                    }
244                            }
245                    }
246                    
247            }
248    
249    }