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 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                    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());
085                    } else {
086                            assignmentsColumn.add(new DelayedComponent(waitComp) {
087                                    
088                                    private static final long serialVersionUID = 5909558908521223531L;
089    
090                                    public Component initComponent() {
091                                            return new AssignmentsComponent();
092                                    }
093                                    
094                            });
095                    }
096            }
097    
098            public void actionPerformed(ActionEvent e) {
099                    if ("Article".equals(e.getActionCommand())) {
100                            log("page", "pressed: article");
101                            getWiki().showPage(page);
102                    } else if ("Proper Name".equals(e.getActionCommand())) {
103                            log("page", "pressed: word");
104                            getWiki().showPage(new WordPage(page));
105                    } else if ("References".equals(e.getActionCommand())) {
106                            log("page", "pressed: references");
107                            getWiki().showPage(new ReferencesPage(page));
108                    }
109            }
110    
111            public boolean equals(Object obj) {
112                    if (obj instanceof AssignmentsPage) {
113                            return page.equals(((AssignmentsPage) obj).page);
114                    }
115                    return false;
116            }
117            
118            public boolean isExpired() {
119                    return page.isExpired();
120            }
121            
122            public String toString() {
123                    return "-ASS- " + page.getOntologyElement().getWord();
124            }
125            
126            
127            private class AssignmentsComponent extends Column implements ActionListener {
128                    
129                    private static final long serialVersionUID = -441448088305771435L;
130                    
131                    private Column sentencesColumn = new Column();
132                    private IndexBar indexBar;
133                    private ArrayList<Sentence> sentences;
134                    
135                    public AssignmentsComponent() {
136                            indexBar = new IndexBar("Page:", 0, this);
137                            add(indexBar);
138                            
139                            sentencesColumn.setInsets(new Insets(10, 2, 5, 20));
140                            sentencesColumn.setCellSpacing(new Extent(2));
141                            add(sentencesColumn);
142                            
143                            Individual ind = (Individual) page.getOntologyElement();
144                            List<Concept> concepts = ind.getConcepts();
145                            sentences = new ArrayList<Sentence>();
146                            Collections.sort(concepts);
147                            for (Concept c : concepts) {
148                                    sentences.add(new Sentence(ind.getWord() + " is a " + c.getWord() + ".", ind.getOntology()));
149                            }
150                            if (sentences.size() == 0) {
151                                    indexBar.setVisible(false);
152                                    sentencesColumn.add(new SolidLabel("(no assignment found)", Font.ITALIC, 10));
153                            } else {
154                                    int i = ((sentences.size()-1) / pageSize) + 1;
155                                    if (chosenPage > i) chosenPage = 0;
156                                    indexBar.setNumbers(i);
157                                    indexBar.setActiveButton(chosenPage);
158                                    updatePage();
159                            }
160                    }
161                    
162                    private void updatePage() {
163                            sentencesColumn.removeAll();
164                            
165                            indexBar.setVisible(sentences.size() > pageSize);
166                            
167                            int max = sentences.size();
168                            if (max > (chosenPage + 1) * pageSize) max = (chosenPage + 1) * pageSize;
169                            
170                            for (int i = chosenPage * pageSize; i < max; i++) {
171                                    Row r = new Row();
172                                    r.add(new TextRow(sentences.get(i), AssignmentsPage.this));
173                                    sentencesColumn.add(r);
174                            }
175                    }
176    
177                    public void actionPerformed(ActionEvent e) {
178                            if (e.getSource() == indexBar) {
179                                    chosenPage = Integer.parseInt(e.getActionCommand()) - 1;
180                                    log("page", "pressed: page " + (chosenPage+1));
181                                    updatePage();
182                            }
183                    }
184                    
185            }
186    
187    }