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.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 to which concepts a certain individual belongs.
043     * Such concept memberships are called "assignments" in AceWiki.
044     * 
045     * @author Tobias Kuhn
046     */
047    public class AssignmentsPage extends WikiPage implements ActionListener {
048    
049            private static final long serialVersionUID = -6955789540998283993L;
050    
051            private static final int pageSize = 20;
052            
053            private IndividualPage page;
054            private Column assignmentsColumn = new Column();
055            private int chosenPage = 0;
056            
057            /**
058             * Creates a new assignments page.
059             * 
060             * @param page The main page that contains the article.
061             */
062            public AssignmentsPage(IndividualPage page) {
063                    super(page.getWiki(), new Title(page.getOntologyElement().getHeadword(), "- Assignments"));
064                    this.page = page;
065                    
066                    addTab("Article", this);
067                    addTab("Proper Name", this);
068                    addTab("References", this);
069                    addSelectedTab("Assignments");
070    
071                    add(new VSpace(18));
072                    add(assignmentsColumn);
073            }
074            
075            protected void doUpdate() {
076                    getTitle().setText(page.getOntologyElement().getHeadword());
077                    assignmentsColumn.removeAll();
078                    
079                    final Column waitComp = new Column();
080                    waitComp.setInsets(new Insets(10, 0, 0, 0));
081                    waitComp.add(new Label(new ResourceImageReference("ch/uzh/ifi/attempto/acewiki/gui/img/wait.gif")));
082                    
083                    if (((Individual) page.getOntologyElement()).areConceptsCached()) {
084                            assignmentsColumn.add(new AssignmentsComponent(true));
085                    } else {
086                            assignmentsColumn.add(waitComp);
087                            assignmentsColumn.add(
088                                    new DelayedComponent(new AssignmentsComponent(true), true) {
089                                    
090                                            private static final long serialVersionUID = 5909558908521223531L;
091            
092                                            public Component initComponent() {
093                                                    return new AssignmentsComponent(false);
094                                            }
095                                            
096                                            public void finalizeAction() {
097                                                    assignmentsColumn.remove(waitComp);
098                                            }
099                                    
100                                    }
101                            );
102                    }
103            }
104    
105            public void actionPerformed(ActionEvent e) {
106                    if ("Article".equals(e.getActionCommand())) {
107                            log("page", "pressed: article");
108                            getWiki().showPage(page);
109                    } else if ("Proper Name".equals(e.getActionCommand())) {
110                            log("page", "pressed: word");
111                            getWiki().showPage(new WordPage(page));
112                    } else if ("References".equals(e.getActionCommand())) {
113                            log("page", "pressed: references");
114                            getWiki().showPage(new ReferencesPage(page));
115                    }
116            }
117    
118            public boolean equals(Object obj) {
119                    if (obj instanceof AssignmentsPage) {
120                            return page.equals(((AssignmentsPage) obj).page);
121                    }
122                    return false;
123            }
124            
125            public boolean isExpired() {
126                    return page.isExpired();
127            }
128            
129            public String toString() {
130                    return "-ASS- " + page.getOntologyElement().getWord();
131            }
132            
133            
134            private class AssignmentsComponent extends Column implements ActionListener {
135                    
136                    private static final long serialVersionUID = -441448088305771435L;
137                    
138                    private Column sentencesColumn = new Column();
139                    private IndexBar indexBar;
140                    private ArrayList<Sentence> sentences;
141                    
142                    public AssignmentsComponent(boolean cached) {
143                            indexBar = new IndexBar("Page:", 0, this);
144                            add(indexBar);
145                            
146                            sentencesColumn.setInsets(new Insets(10, 2, 5, 20));
147                            sentencesColumn.setCellSpacing(new Extent(2));
148                            add(sentencesColumn);
149                            
150                            Individual ind = (Individual) page.getOntologyElement();
151                            List<Concept> concepts;
152                            if (cached) {
153                                    concepts = ind.getCachedConcepts();
154                            } else {
155                                    concepts = ind.getConcepts();
156                            }
157                            if (concepts != null) {
158                                    sentences = new ArrayList<Sentence>();
159                                    Collections.sort(concepts);
160                                    for (Concept c : concepts) {
161                                            sentences.add(new Sentence(ind.getWord(2) + " is a " + c.getWord() + ".", ind.getOntology()));
162                                    }
163                                    if (sentences.size() == 0) {
164                                            indexBar.setVisible(false);
165                                            sentencesColumn.add(new SolidLabel("(no assignment 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                            } else {
174                                    indexBar.setVisible(false);
175                                    sentencesColumn.add(new SolidLabel("...", Font.ITALIC, 10));
176                            }
177                    }
178                    
179                    private void updatePage() {
180                            sentencesColumn.removeAll();
181                            
182                            indexBar.setVisible(sentences.size() > pageSize);
183                            
184                            int max = sentences.size();
185                            if (max > (chosenPage + 1) * pageSize) max = (chosenPage + 1) * pageSize;
186                            
187                            for (int i = chosenPage * pageSize; i < max; i++) {
188                                    Row r = new Row();
189                                    r.add(new TextRow(sentences.get(i), AssignmentsPage.this));
190                                    sentencesColumn.add(r);
191                            }
192                    }
193    
194                    public void actionPerformed(ActionEvent e) {
195                            if (e.getSource() == indexBar) {
196                                    chosenPage = Integer.parseInt(e.getActionCommand()) - 1;
197                                    log("page", "pressed: page " + (chosenPage+1));
198                                    updatePage();
199                            }
200                    }
201                    
202            }
203    
204    }