// This file is part of AceWiki.
// Copyright 2008-2013, AceWiki developers.
//
// AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
// even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
// not, see http://www.gnu.org/licenses/.
package ch.uzh.ifi.attempto.acewiki.gui;
import java.util.ArrayList;
import java.util.List;
import nextapp.echo.app.Column;
import nextapp.echo.app.Extent;
import nextapp.echo.app.Font;
import nextapp.echo.app.Insets;
import nextapp.echo.app.Row;
import nextapp.echo.app.event.ActionEvent;
import nextapp.echo.app.event.ActionListener;
import ch.uzh.ifi.attempto.acewiki.Task;
import ch.uzh.ifi.attempto.acewiki.Wiki;
import ch.uzh.ifi.attempto.acewiki.core.CachingReasoner;
import ch.uzh.ifi.attempto.acewiki.core.Concept;
import ch.uzh.ifi.attempto.acewiki.core.Individual;
import ch.uzh.ifi.attempto.acewiki.core.LanguageUtils;
import ch.uzh.ifi.attempto.acewiki.core.Sentence;
import ch.uzh.ifi.attempto.acewiki.core.StatementFactory;
import ch.uzh.ifi.attempto.echocomp.SolidLabel;
import ch.uzh.ifi.attempto.echocomp.VSpace;
/**
* This class represents a page that shows to which concepts a certain individual belongs.
* Such concept memberships are called "assignments" in AceWiki.
*
* @author Tobias Kuhn
*/
public class AssignmentsPage extends WikiPage implements ActionListener {
private static final long serialVersionUID = -6955789540998283993L;
private static final int pageSize = 50;
private Individual individual;
private Column assignmentsColumn = new Column();
private int chosenPage = 0;
private Title title;
/**
* Creates a new assignments page.
*
* @param individual The individual in the assignments
* @param wiki The wiki that contains the new page
*/
public AssignmentsPage(Individual individual, Wiki wiki) {
super(wiki);
this.individual = individual;
title = new Title("", "", "", this);
add(title);
addHorizontalLine();
add(assignmentsColumn);
}
protected void doUpdate() {
setTabRow(TabRow.getArticleTabRow(individual, TabRow.TAB_ASSIGNMENTS, getWiki()));
title.setText(getHeading(individual));
title.setPostTitle("- " + getWiki().getGUIText("acewiki_page_assignments"));
title.setTooltip(individual.getType());
assignmentsColumn.removeAll();
final Column waitComp = new Column();
waitComp.setInsets(new Insets(10, 0, 0, 0));
waitComp.add(new RecalcIcon(getWiki().getGUIText("acewiki_list_updating")));
CachingReasoner cr = getWiki().getOntology().getReasoner();
if (cr.areCachedConceptsUpToDate(individual)) {
assignmentsColumn.add(new VSpace(18));
assignmentsColumn.add(new AssignmentsComponent(true));
} else {
assignmentsColumn.add(new VSpace(4));
assignmentsColumn.add(waitComp);
assignmentsColumn.add(new AssignmentsComponent(true));
getWiki().enqueueWeakAsyncTask(new Task() {
private AssignmentsComponent delayedComp;
public void run() {
delayedComp = new AssignmentsComponent(false);
}
public void updateGUI() {
assignmentsColumn.removeAll();
assignmentsColumn.add(new VSpace(18));
assignmentsColumn.add(delayedComp);
}
});
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == title) {
getWiki().showEditorWindow(individual);
}
}
public boolean equals(Object obj) {
if (obj instanceof AssignmentsPage) {
return individual.equals(((AssignmentsPage) obj).individual);
}
return false;
}
public boolean isExpired() {
return !getWiki().getOntology().contains(individual);
}
public String toString() {
return "-ASS- " + individual.getWord();
}
private class AssignmentsComponent extends Column implements ActionListener {
private static final long serialVersionUID = -441448088305771435L;
private Column sentencesColumn = new Column();
private IndexBar indexBar;
private List<Sentence> sentences;
public AssignmentsComponent(boolean cached) {
indexBar = new IndexBar(0, this);
add(indexBar);
sentencesColumn.setInsets(new Insets(10, 2, 5, 20));
sentencesColumn.setCellSpacing(new Extent(2));
add(sentencesColumn);
CachingReasoner cr = getWiki().getOntology().getReasoner();
List<Concept> concepts;
if (cached) {
concepts = cr.getCachedConcepts(individual);
} else {
concepts = cr.getConcepts(individual);
}
if (concepts != null) {
sentences = new ArrayList<Sentence>();
LanguageUtils.sortOntologyElements(concepts);
for (Concept c : concepts) {
StatementFactory sf = getWiki().getOntology().getStatementFactory();
sentences.add(sf.createAssignmentSentence(individual, c));
}
if (sentences.size() == 0) {
indexBar.setVisible(false);
sentencesColumn.add(new SolidLabel(getWiki().getGUIText("acewiki_list_empty"), Font.ITALIC, 10));
} else {
int i = ((sentences.size()-1) / pageSize) + 1;
if (chosenPage > i) chosenPage = 0;
indexBar.setNumbers(i);
indexBar.setActiveButton(chosenPage);
updatePage();
}
} else {
indexBar.setVisible(false);
sentencesColumn.add(new SolidLabel("...", Font.ITALIC, 10));
}
}
private void updatePage() {
sentencesColumn.removeAll();
indexBar.setVisible(sentences.size() > pageSize);
int max = sentences.size();
if (max > (chosenPage + 1) * pageSize) max = (chosenPage + 1) * pageSize;
for (int i = chosenPage * pageSize; i < max; i++) {
Row r = new Row();
r.add(new SentenceComponent(sentences.get(i), AssignmentsPage.this));
sentencesColumn.add(r);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == indexBar) {
chosenPage = Integer.parseInt(e.getActionCommand()) - 1;
log("page", "pressed: page " + (chosenPage+1));
updatePage();
}
}
}
}