001    // This file is part of AceWiki.
002    // Copyright 2008-2012, AceWiki developers.
003    // 
004    // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
005    // Lesser General Public License as published by the Free Software Foundation, either version 3 of
006    // the License, or (at your option) any later version.
007    // 
008    // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009    // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010    // Lesser General Public License for more details.
011    // 
012    // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
013    // not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.acewiki.gui;
016    
017    import java.util.ArrayList;
018    import java.util.List;
019    
020    import nextapp.echo.app.Column;
021    import nextapp.echo.app.Extent;
022    import nextapp.echo.app.Font;
023    import nextapp.echo.app.Insets;
024    import nextapp.echo.app.Row;
025    import nextapp.echo.app.event.ActionEvent;
026    import nextapp.echo.app.event.ActionListener;
027    import ch.uzh.ifi.attempto.acewiki.Task;
028    import ch.uzh.ifi.attempto.acewiki.core.CachingReasoner;
029    import ch.uzh.ifi.attempto.acewiki.core.Concept;
030    import ch.uzh.ifi.attempto.acewiki.core.Individual;
031    import ch.uzh.ifi.attempto.acewiki.core.LanguageUtils;
032    import ch.uzh.ifi.attempto.acewiki.core.OntologyElement;
033    import ch.uzh.ifi.attempto.acewiki.core.Sentence;
034    import ch.uzh.ifi.attempto.acewiki.core.StatementFactory;
035    import ch.uzh.ifi.attempto.echocomp.SolidLabel;
036    import ch.uzh.ifi.attempto.echocomp.VSpace;
037    
038    /**
039     * This class represents a page that shows to which concepts a certain individual belongs.
040     * Such concept memberships are called "assignments" in AceWiki.
041     * 
042     * @author Tobias Kuhn
043     */
044    public class AssignmentsPage extends WikiPage implements ActionListener {
045    
046            private static final long serialVersionUID = -6955789540998283993L;
047    
048            private static final int pageSize = 50;
049            
050            private IndividualPage page;
051            private Column assignmentsColumn = new Column();
052            private int chosenPage = 0;
053            private Title title;
054            
055            /**
056             * Creates a new assignments page.
057             * 
058             * @param page The main page that contains the article.
059             */
060            public AssignmentsPage(IndividualPage page) {
061                    super(page.getWiki());
062                    this.page = page;
063                    
064                    addTab("Article", this);
065                    addTab("References", this);
066                    addSelectedTab("Assignments");
067                    
068                    OntologyElement oe = page.getOntologyElement();
069                    title = new Title(getHeading(oe), "- Assignments", oe.getType(), this);
070                    add(title);
071                    addHorizontalLine();
072                    add(assignmentsColumn);
073            }
074            
075            protected void doUpdate() {
076                    title.setText(getHeading(page.getOntologyElement()));
077                    assignmentsColumn.removeAll();
078                    
079                    final Column waitComp = new Column();
080                    waitComp.setInsets(new Insets(10, 0, 0, 0));
081                    waitComp.add(new RecalcIcon("This list is being updated."));
082                    
083                    CachingReasoner cr = getWiki().getOntology().getReasoner();
084                    
085                    if (cr.areCachedConceptsUpToDate((Individual) page.getOntologyElement())) {
086                            assignmentsColumn.add(new VSpace(18));
087                            assignmentsColumn.add(new AssignmentsComponent(true));
088                    } else {
089                            assignmentsColumn.add(new VSpace(4));
090                            assignmentsColumn.add(waitComp);
091                            assignmentsColumn.add(new AssignmentsComponent(true));
092                            page.getWiki().enqueueWeakAsyncTask(new Task() {
093                                    
094                                    private AssignmentsComponent delayedComp;
095                                    
096                                    public void run() {
097                                            delayedComp = new AssignmentsComponent(false);
098                                    }
099                                    
100                                    public void updateGUI() {
101                                            assignmentsColumn.removeAll();
102                                            assignmentsColumn.add(new VSpace(18));
103                                            assignmentsColumn.add(delayedComp);
104                                    }
105                                    
106                            });
107                    }
108            }
109    
110            public void actionPerformed(ActionEvent e) {
111                    if ("Article".equals(e.getActionCommand())) {
112                            log("page", "pressed: article");
113                            getWiki().showPage(page);
114                    } else if ("References".equals(e.getActionCommand())) {
115                            log("page", "pressed: references");
116                            getWiki().showPage(new ReferencesPage(page));
117                    } else if (e.getSource() == title) {
118                            getWiki().showEditorWindow(page.getOntologyElement());
119                    }
120            }
121    
122            public boolean equals(Object obj) {
123                    if (obj instanceof AssignmentsPage) {
124                            return page.equals(((AssignmentsPage) obj).page);
125                    }
126                    return false;
127            }
128            
129            public boolean isExpired() {
130                    return page.isExpired();
131            }
132            
133            public String toString() {
134                    return "-ASS- " + page.getOntologyElement().getWord();
135            }
136            
137            
138            private class AssignmentsComponent extends Column implements ActionListener {
139                    
140                    private static final long serialVersionUID = -441448088305771435L;
141                    
142                    private Column sentencesColumn = new Column();
143                    private IndexBar indexBar;
144                    private List<Sentence> sentences;
145                    
146                    public AssignmentsComponent(boolean cached) {
147                            indexBar = new IndexBar("Page:", 0, this);
148                            add(indexBar);
149                            
150                            sentencesColumn.setInsets(new Insets(10, 2, 5, 20));
151                            sentencesColumn.setCellSpacing(new Extent(2));
152                            add(sentencesColumn);
153    
154                            CachingReasoner cr = getWiki().getOntology().getReasoner();
155                            Individual ind = (Individual) page.getOntologyElement();
156                            List<Concept> concepts;
157                            if (cached) {
158                                    concepts = cr.getCachedConcepts(ind);
159                            } else {
160                                    concepts = cr.getConcepts(ind);
161                            }
162                            if (concepts != null) {
163                                    sentences = new ArrayList<Sentence>();
164                                    LanguageUtils.sortOntologyElements(concepts);
165                                    for (Concept c : concepts) {
166                                            StatementFactory sf = getWiki().getOntology().getStatementFactory();
167                                            sentences.add(sf.createAssignmentSentence(ind, c));
168                                    }
169                                    if (sentences.size() == 0) {
170                                            indexBar.setVisible(false);
171                                            sentencesColumn.add(new SolidLabel("(no assignment found)", Font.ITALIC, 10));
172                                    } else {
173                                            int i = ((sentences.size()-1) / pageSize) + 1;
174                                            if (chosenPage > i) chosenPage = 0;
175                                            indexBar.setNumbers(i);
176                                            indexBar.setActiveButton(chosenPage);
177                                            updatePage();
178                                    }
179                            } else {
180                                    indexBar.setVisible(false);
181                                    sentencesColumn.add(new SolidLabel("...", Font.ITALIC, 10));
182                            }
183                    }
184                    
185                    private void updatePage() {
186                            sentencesColumn.removeAll();
187                            
188                            indexBar.setVisible(sentences.size() > pageSize);
189                            
190                            int max = sentences.size();
191                            if (max > (chosenPage + 1) * pageSize) max = (chosenPage + 1) * pageSize;
192                            
193                            for (int i = chosenPage * pageSize; i < max; i++) {
194                                    Row r = new Row();
195                                    r.add(new SentenceComponent(sentences.get(i), AssignmentsPage.this));
196                                    sentencesColumn.add(r);
197                            }
198                    }
199    
200                    public void actionPerformed(ActionEvent e) {
201                            if (e.getSource() == indexBar) {
202                                    chosenPage = Integer.parseInt(e.getActionCommand()) - 1;
203                                    log("page", "pressed: page " + (chosenPage+1));
204                                    updatePage();
205                            }
206                    }
207                    
208            }
209    
210    }