    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 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.Ontology;
    021    import ch.uzh.ifi.attempto.acewiki.core.ontology.OntologyElement;
    022    import ch.uzh.ifi.attempto.acewiki.core.ontology.VerbRole;
    023    import ch.uzh.ifi.attempto.ape.FunctionWords;
    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 verbs.
    029     *
    030     * @author Tobias Kuhn
    031     */
    032    public class VerbForm extends FormPane {
    033
    034            private static final long serialVersionUID = 5050205540719470842L;
    035
    036            private TextField thirdSgField = new TextField();
    037            private TextField infField = new TextField();
    038            private TextField pastPartField = new TextField();
    039
    040            private VerbRole verbRole;
    041            private int wordNumber;
    042
    043            /**
    044             * Creates a new verb form.
    045             *
    046             * @param verbRole The role that is represented by the verb.
    047             * @param wordNumber The word form id (only used if called from the sentence editor).
    048             * @param window The host window of the form.
    049             * @param wiki The wiki instance.
    050             * @param actionListener The actionlistener.
    051             */
    052            public VerbForm(VerbRole verbRole, int wordNumber, WindowPane window, Wiki wiki, ActionListener actionListener) {
    053                    super(window, wiki, actionListener);
    054                    this.verbRole = verbRole;
    055                    this.wordNumber = wordNumber;
    056
    057                    setIconRow("role",
    058                                    "Every verb represents a certain relation between things. " +
    059                                    "For example, the verb \"owns\" relates persons to their possessions. " +
    060                                    "Every verb has a third singular form, a bare infinitive form, and an optional past participle form."
    061                            );
    062                    addRow("third singular", thirdSgField, "examples: owns, applies to, touches", true);
    063                    addRow("bare infinitive", infField, "examples: own, apply to, touch", true);
    064                    addRow("past participle", pastPartField, "examples: owned, applied to, touched", false);
    065
    066                    thirdSgField.setText(verbRole.getPrettyWord(0));
    067                    infField.setText(verbRole.getPrettyWord(1));
    068                    pastPartField.setText(verbRole.getPrettyPastPart());
    069
    070                    ApplicationInstance.getActive().setFocusedComponent(thirdSgField);
    071            }
    072
    073            /**
    074             * Creates a new creator window for verbs.
    075             *
    076             * @param wordNumber The word form id (only used if called from the sentence editor).
    077             * @param wiki The wiki instance.
    078             * @param actionListener The actionlistener.
    079             * @return The new creator window.
    080             */
    081            public static WordEditorWindow createCreatorWindow(int wordNumber, Wiki wiki, ActionListener actionListener) {
    082                    WordEditorWindow creatorWindow = WordEditorWindow.createCreatorWindow();
    083                    creatorWindow.addTab(new VerbForm(new VerbRole(), wordNumber, creatorWindow, wiki, actionListener));
    084                    return creatorWindow;
    085            }
    086
    087            /**
    088             * Creates a new editor window for verbs.
    089             *
    090             * @param role The role that is represented by the verb that should be edited.
    091             * @param wiki The wiki instance.
    092             * @return The new editor window.
    093             */
    094            public static WordEditorWindow createEditorWindow(VerbRole role, Wiki wiki) {
    095                    WordEditorWindow editorWindow = WordEditorWindow.createEditorWindow();
    096                    editorWindow.addTab(new VerbForm(role, 0, editorWindow, wiki, wiki));
    097                    return editorWindow;
    098            }
    099
    100            protected void save() {
    101                    Wiki wiki = getWiki();
    102                    String thirdSg = normalize(thirdSgField.getText());
    103                    String inf = normalize(infField.getText());
    104                    String pastPart = normalize(pastPartField.getText());
    105                    if (pastPart.toLowerCase().endsWith("_by")) {
    106                            pastPart = pastPart.substring(0, pastPart.length()-3);
    107                    }
    108                    String thirdSgP = thirdSg.replace("_", " ");
    109                    String infP = inf.replace("_", " ");
    110                    String pastPartP = pastPart.replace("_", " ");
    111
    112                    Ontology ontology = wiki.getOntology();
    113
    114                    // check whether all necessary fields are filled-in
    115                    if (thirdSg.equals("")) {
    116                            wiki.log("edit", "error: no third singular defined");
    117                            showErrorMessage("No third singular defined: Please define the third singular form.");
    118                            return;
    119                    }
    120                    if (inf.equals("")) {
    121                            wiki.log("edit", "error: no infinitive defined");
    122                            showErrorMessage("No bare infinitive defined: Please define the bare infinitive form.");
    123                            return;
    124                    }
    125                    if (pastPart.equals("") && wordNumber == 2) {
    126                            wiki.log("edit", "error: no past participle defined");
    127                            showErrorMessage("No past participle defined: Please define the past participle form.");
    128                            return;
    129                    }
    130                    if (pastPart.equals("") && !ontology.getReferences(verbRole, 2).isEmpty()) {
    131                            wiki.log("edit", "error: cannot remove past participle");
    132                            showErrorMessage("The past participle form cannot be removed because there are sentences that are using it.");
    133                            return;
    134                    }
    135
    136                    // check whether the words contain only valid characters
    137                    if (!isValidString(thirdSg) || !isValidString(inf) || !isValidString(pastPart)) {
    138                            wiki.log("edit", "error: word contains invalid character");
    139                            showErrorMessage("Invalid character used: Only a-z, A-Z, 0-9, -, and spaces are allowed, " +
    140                                    "and the first character must be one of a-z A-Z.");
    141                            return;
    142                    }
    143
    144                    // check whether a word is a predefined function word
    145                    if (FunctionWords.isFunctionWord(thirdSg)) {
    146                            wiki.log("edit", "error: word is predefined");
    147                            showErrorMessage("'" + thirdSgP + "' is a predefined word and cannot be used here.");
    148                            return;
    149                    }
    150                    if (FunctionWords.isFunctionWord(inf)) {
    151                            wiki.log("edit", "error: word is predefined");
    152                            showErrorMessage("'" + infP + "' is a predefined word and cannot be used here.");
    153                            return;
    154                    }
    155                    if (FunctionWords.isFunctionWord(pastPart)) {
    156                            wiki.log("edit", "error: word is predefined");
    157                            showErrorMessage("'" + pastPartP + "' is a predefined word and cannot be used here.");
    158                            return;
    159                    }
    160
    161                    // check whether all word forms are distinct
    162                    if (thirdSg.equals(inf)) {
    163                            wiki.log("edit", "error: the singular and plural forms have to be distinct.");
    164                            showErrorMessage("The singular and plural forms have to be distinct.");
    165                            return;
    166                    }
    167
    168                    // check whether a word is already defined
    169                    OntologyElement oe1 = ontology.get(thirdSg);
    170                    if (oe1 != null && oe1 != verbRole) {
    171                            wiki.log("edit", "error: word is already used");
    172                            showErrorMessage("The word '" + thirdSgP + "' is already used. Please use a different one.");
    173                            return;
    174                    }
    175                    OntologyElement oe2 = ontology.get(inf);
    176                    if (oe2 != null && oe2 != verbRole) {
    177                            wiki.log("edit", "error: word is already used");
    178                            showErrorMessage("The word '" + infP + "' is already used. Please use a different one.");
    179                            return;
    180                    }
    181                    OntologyElement oe3 = ontology.get(pastPart);
    182                    if (oe3 != null && oe3 != verbRole) {
    183                            wiki.log("edit", "error: word is already used");
    184                            showErrorMessage("The word '" + pastPartP + "' is already used. Please use a different one.");
    185                            return;
    186                    }
    187
    188                    if (pastPart.equals("")) pastPart = null;
    189                    verbRole.setWords(thirdSg, inf, pastPart);
    190
    191                    wiki.log("edit", "verb: " + thirdSg + " / " + inf + " / " + pastPart);
    192                    if (verbRole.getOntology() == null) {
    193                            verbRole.registerAt(getWiki().getOntology());
    194                    }
    195                    finished(verbRole, wordNumber);
    196            }
    197
    198            public String toString() {
    199                    return "Verb";
    200            }
    201
    202    }