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.editor; 016 017 import java.util.ArrayList; 018 019 import nextapp.echo2.app.event.ActionEvent; 020 import nextapp.echo2.app.event.ActionListener; 021 import ch.uzh.ifi.attempto.acewiki.Task; 022 import ch.uzh.ifi.attempto.acewiki.Wiki; 023 import ch.uzh.ifi.attempto.acewiki.core.ontology.OntologyElement; 024 import ch.uzh.ifi.attempto.acewiki.core.ontology.Sentence; 025 import ch.uzh.ifi.attempto.acewiki.gui.page.ArticlePage; 026 import ch.uzh.ifi.attempto.echocomp.MessageWindow; 027 import ch.uzh.ifi.attempto.preditor.PreditorWindow; 028 import ch.uzh.ifi.attempto.preditor.text.TextContainer; 029 import ch.uzh.ifi.attempto.preditor.text.TextElement; 030 031 /** 032 * This class manages the predictive editor. It creates the editor window and handles its 033 * responses. 034 * 035 * @author Tobias Kuhn 036 */ 037 public class SentenceEditorHandler implements ActionListener { 038 039 private static final long serialVersionUID = -2083910385095284075L; 040 041 private PreditorWindow editorWindow; 042 private ArticlePage page; 043 private Wiki wiki; 044 private boolean edit; 045 private Sentence sentence; 046 047 private SentenceEditorHandler(Sentence sentence, ArticlePage page, boolean edit) { 048 this.page = page; 049 this.wiki = page.getWiki(); 050 this.edit = edit; 051 this.sentence = sentence; 052 AceWikiMenuCreator menuCreator = new AceWikiMenuCreator(wiki, page.getOntologyElement(), this); 053 editorWindow = new PreditorWindow("Sentence Editor", wiki.getGrammar(), menuCreator); 054 editorWindow.addActionListener(this); 055 056 if (edit) { 057 String t = sentence.getPrettyText(); 058 // remove the last element (i.e. the period '.' or question mark '?'): 059 editorWindow.addText(t.substring(0, t.length()-1)); 060 } 061 } 062 063 /** 064 * Generates a new preditive editor (short "preditor") window to create a new sentence. 065 * 066 * @param followingSentence The sentence in front of which the new sentences should be added, 067 * or null if the sentences should be added to the end of the article. 068 * @param page The host page into which the sentence should be added. 069 * @return A new preditor window. 070 */ 071 public static PreditorWindow generatePreditorAddWindow(Sentence followingSentence, ArticlePage page) { 072 SentenceEditorHandler h = new SentenceEditorHandler(followingSentence, page, false); 073 return h.getPreditorWindow(); 074 } 075 076 /** 077 * Generates a new preditive editor (short "preditor") window to create a new sentence. 078 * 079 * @param sentence The sentence that should be edited. 080 * @param page The host page which contains the sentence to be edited. 081 * @return A new preditor window. 082 */ 083 public static PreditorWindow generatePreditorEditWindow(Sentence sentence, ArticlePage page) { 084 SentenceEditorHandler h = new SentenceEditorHandler(sentence, page, true); 085 return h.getPreditorWindow(); 086 } 087 088 private PreditorWindow getPreditorWindow() { 089 return editorWindow; 090 } 091 092 public void actionPerformed(ActionEvent e) { 093 if (e.getSource() == editorWindow && e.getActionCommand().equals("OK")) { 094 final TextContainer textContainer = editorWindow.getTextContainer(); 095 096 TextElement finalElement = editorWindow.getPossibleNextToken("."); 097 if (finalElement == null) { 098 finalElement = editorWindow.getPossibleNextToken("?"); 099 } 100 if (finalElement != null) textContainer.addElement(finalElement); 101 102 ArrayList<TextElement> l = textContainer.getTextElements(); 103 if (l.isEmpty() || l.get(l.size() - 1).getText().matches("[.?]")) { 104 Task task = new Task() { 105 106 int success; 107 108 public void run() { 109 OntologyElement el = page.getOntologyElement(); 110 if (edit) { 111 wiki.log("edit", "sentence updated: " + textContainer.getText()); 112 success = el.edit(sentence, Sentence.generateSentences(textContainer, el)); 113 } else { 114 wiki.log("edit", "sentence created: " + textContainer.getText()); 115 success = el.add(sentence, Sentence.generateSentences(textContainer, el)); 116 } 117 } 118 119 public void updateGUI() { 120 page.update(); 121 if (success == 1) { 122 wiki.showWindow( 123 new MessageWindow( 124 "Conflict", 125 "A sentence is in conflict with the current knowledge. For that reason, " + 126 "it cannot be added to the knowledge base.", 127 "OK" 128 ) 129 ); 130 } else if (success == 2) { 131 wiki.showWindow( 132 new MessageWindow( 133 "Error", 134 "A sentence could not be added to the knowledge base because the knowledge " + 135 "base got too complex.", 136 "OK" 137 ) 138 ); 139 } 140 if (page != null) { 141 page.update(); 142 page.getWiki().update(); 143 } 144 } 145 146 }; 147 148 if (edit) { 149 wiki.enqueueTaskShowingWaitWindow("Updating", "The knowledge base is being updated...", task); 150 } else { 151 wiki.enqueueTaskShowingWaitWindow("Updating", "The sentence is being added to the knowledge base...", task); 152 } 153 154 editorWindow.setVisible(false); 155 156 } else { 157 wiki.log("edit", "error: unfinished sentences"); 158 MessageWindow mw = new MessageWindow("Error", "There are unfinished sentences.", editorWindow, "OK"); 159 page.getWiki().showWindow(mw); 160 } 161 } else if (e.getSource() == editorWindow && e.getActionCommand().equals("Cancel")) { 162 editorWindow.setVisible(false); 163 editorWindow.dispose(); 164 } else if (e.getSource() instanceof TextElement) { 165 editorWindow.addTextElement((TextElement) e.getSource()); 166 } 167 } 168 169 }