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.editor; 016 017 import nextapp.echo2.app.ApplicationInstance; 018 import nextapp.echo2.app.event.ActionListener; 019 import ch.uzh.ifi.attempto.acewiki.Wiki; 020 import ch.uzh.ifi.attempto.acewiki.core.ontology.Individual; 021 import ch.uzh.ifi.attempto.acewiki.core.ontology.OntologyElement; 022 import ch.uzh.ifi.attempto.ape.FunctionWords; 023 import ch.uzh.ifi.attempto.echocomp.CheckBox; 024 import ch.uzh.ifi.attempto.echocomp.TextField; 025 import ch.uzh.ifi.attempto.echocomp.WindowPane; 026 027 /** 028 * This class represents a form to create or modify proper names. 029 * 030 * @author Tobias Kuhn 031 */ 032 public class ProperNameForm extends FormPane { 033 034 private static final long serialVersionUID = 7860047859937196093L; 035 036 private TextField nameField = new TextField(this); 037 private CheckBox nameDefArtCheckBox = new CheckBox(); 038 private TextField abbrevField = new TextField(this); 039 private CheckBox abbrevDefArtCheckBox = new CheckBox(); 040 041 private Individual ind; 042 043 /** 044 * Creates a new proper name form. 045 * 046 * @param ind The individual that is represented by the proper name. 047 * @param window The host window of the form. 048 * @param wiki The wiki instance. 049 * @param actionListener The actionlistener. 050 */ 051 public ProperNameForm(Individual ind, WindowPane window, Wiki wiki, ActionListener actionListener) { 052 super(window, wiki, actionListener); 053 this.ind = ind; 054 055 setIconRow("individual", 056 "Every proper name represents a certain individual. " + 057 "The country \"Switzerland\", the person \"Bob Dylan\", the river \"Nile\", " + 058 "and the organization \"United Nations\" are typical examples. " + 059 "Some proper names are used with \"the\" (\"the Nile\", \"the United Nations\") " + 060 "and others are not (\"Switzerland\", \"Bob Dylan\"). " + 061 "Proper names can have an abbreviation that has the same meaning as the longer proper name." 062 ); 063 addRow("proper name", nameField, "examples: Switzerland, Bob Dylan, Nile, United Nations", true); 064 addRow("... used with \"the\"", nameDefArtCheckBox, "examples: the Nile, the United Nations", false); 065 addRow("abbreviation", abbrevField, "examples: CH, UN", false); 066 addRow("... used with \"the\"", abbrevDefArtCheckBox, "example: the UN", false); 067 068 nameField.setText(ind.getPrettyWord(1)); 069 nameDefArtCheckBox.setSelected(ind.hasDefiniteArticle(0)); 070 abbrevField.setText(ind.getAbbreviation()); 071 abbrevDefArtCheckBox.setSelected(ind.hasDefiniteArticle(2)); 072 073 ApplicationInstance.getActive().setFocusedComponent(nameField); 074 } 075 076 /** 077 * Creates a new creator window for proper names. 078 * 079 * @param wiki The wiki instance. 080 * @param actionListener The actionlistener. 081 * @return The new creator window. 082 */ 083 public static WordEditorWindow createCreatorWindow(Wiki wiki, ActionListener actionListener) { 084 WordEditorWindow creatorWindow = WordEditorWindow.createCreatorWindow(); 085 creatorWindow.addTab(new ProperNameForm(new Individual(), creatorWindow, wiki, actionListener)); 086 return creatorWindow; 087 } 088 089 /** 090 * Creates a new editor window for proper names. 091 * 092 * @param ind The individual that is represented by the proper name that should be edited. 093 * @param wiki The wiki instance. 094 * @return The new editor window. 095 */ 096 public static WordEditorWindow createEditorWindow(Individual ind, Wiki wiki) { 097 WordEditorWindow editorWindow = WordEditorWindow.createEditorWindow(); 098 editorWindow.addTab(new ProperNameForm(ind, editorWindow, wiki, wiki)); 099 return editorWindow; 100 } 101 102 protected void save() { 103 Wiki wiki = getWiki(); 104 String name = normalize(nameField.getText()); 105 String abbrev = normalize(abbrevField.getText()); 106 boolean nameDefArt = nameDefArtCheckBox.isSelected(); 107 boolean abbrevDefArt = abbrevDefArtCheckBox.isSelected(); 108 109 if (name.toLowerCase().startsWith("the_")) { 110 name = name.substring(4); 111 nameDefArt = true; 112 } 113 if (abbrev.toLowerCase().startsWith("the_")) { 114 abbrev = abbrev.substring(4); 115 abbrevDefArt = true; 116 } 117 String nameP = name.replace("_", " "); 118 String abbrevP = abbrev.replace("_", " "); 119 120 if (name.equals("")) { 121 wiki.log("edit", "error: no word defined"); 122 showErrorMessage("No proper name defined: Please specify a name."); 123 return; 124 } 125 if (!isValidString(name) || !isValidString(abbrev)) { 126 wiki.log("edit", "error: word contains invalid character"); 127 showErrorMessage("Invalid character: Only a-z, A-Z, 0-9, -, and spaces are allowed, " + 128 "and the first character must be one of a-z A-Z."); 129 return; 130 } 131 if (FunctionWords.isFunctionWord(name)) { 132 wiki.log("edit", "error: word is predefined"); 133 showErrorMessage("'" + nameP + "' is a predefined word and cannot be used here."); 134 return; 135 } 136 if (FunctionWords.isFunctionWord(abbrev)) { 137 wiki.log("edit", "error: word is predefined"); 138 showErrorMessage("'" + abbrevP + "' is a predefined word and cannot be used here."); 139 return; 140 } 141 if (abbrev.length() >= name.length()) { 142 wiki.log("edit", "error: abbreviation is not shorter than the full proper name"); 143 showErrorMessage("The abbreviation has to be shorter than the full proper name."); 144 return; 145 } 146 OntologyElement oe = wiki.getOntology().get(name); 147 if (oe != null && oe != ind) { 148 wiki.log("edit", "error: word is already used"); 149 showErrorMessage("The word '" + nameP + "' is already used. Please use a different one."); 150 return; 151 } 152 oe = wiki.getOntology().get(abbrev); 153 if (oe != null && oe != ind) { 154 wiki.log("edit", "error: word is already used"); 155 showErrorMessage("The word '" + abbrevP + "' is already used. Please use a different one."); 156 return; 157 } 158 String word = name; 159 if (nameDefArt) { 160 word = "the " + name; 161 } 162 String abbrevWord = abbrev; 163 if (abbrev.equals("")) { 164 abbrev = null; 165 abbrevWord = null; 166 } else { 167 if (abbrevDefArt) { 168 abbrevWord = "the " + abbrev; 169 } 170 } 171 ind.setWords(word, name, abbrevWord, abbrev); 172 wiki.log("edit", "proper name: " + word + " / " + abbrevWord); 173 if (ind.getOntology() == null) { 174 ind.registerAt(getWiki().getOntology()); 175 } 176 finished(ind); 177 } 178 179 public String toString() { 180 return "Proper Name"; 181 } 182 183 }