    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.NounConcept;
    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.TextField;
    024    import ch.uzh.ifi.attempto.echocomp.WindowPane;
    025
    026    /**
    027     * This class represents a form to create or modify nouns.
    028     *
    029     * @author Tobias Kuhn
    030     */
    031    public class NounForm extends FormPane {
    032
    033            private static final long serialVersionUID = 172544159284997517L;
    034
    035            private TextField singularField = new TextField();
    036            private TextField pluralField = new TextField();
    037
    038            private NounConcept concept;
    039            private int wordNumber;
    040
    041            /**
    042             * Creates a new noun form.
    043             *
    044             * @param concept The concept that is represented by the noun.
    045             * @param wordNumber The word form id (only used if called from the sentence editor).
    046             * @param window The host window of the form.
    047             * @param wiki The wiki instance.
    048             * @param actionListener
    049             */
    050            public NounForm(NounConcept concept, int wordNumber, WindowPane window, Wiki wiki, ActionListener actionListener) {
    051                    super(window, wiki, actionListener);
    052                    this.concept = concept;
    053                    this.wordNumber = wordNumber;
    054
    055                    setIconRow("concept",
    056                                    "Every noun represents a certain type of things. " +
    057                                    "For example, the noun \"credit card\" stands for all things that are credit cards. " +
    058                                    "Every noun has a singular form and a plural form."
    059                            );
    060                    addRow("singular", singularField, "examples: woman, credit card, process", true);
    061                    addRow("plural", pluralField, "examples: women, credit cards, processes", true);
    062
    063                    singularField.setText(concept.getPrettyWord(0));
    064                    pluralField.setText(concept.getPrettyWord(1));
    065
    066                    ApplicationInstance.getActive().setFocusedComponent(singularField);
    067            }
    068
    069            /**
    070             * Creates a new creator window for nouns.
    071             *
    072             * @param wordNumber The word form id (only used if called from the sentence editor).
    073             * @param wiki The wiki instance.
    074             * @param actionListener The actionlistener.
    075             * @return The new creator window.
    076             */
    077            public static WordEditorWindow createCreatorWindow(int wordNumber, Wiki wiki, ActionListener actionListener) {
    078                    WordEditorWindow creatorWindow = WordEditorWindow.createCreatorWindow();
    079                    creatorWindow.addTab(new NounForm(new NounConcept(), wordNumber, creatorWindow, wiki, actionListener));
    080                    return creatorWindow;
    081            }
    082
    083            /**
    084             * Creates a new editor window for nouns.
    085             *
    086             * @param concept The concept that is represented by the noun that should be edited.
    087             * @param wiki The wiki instance.
    088             * @return The new editor window.
    089             */
    090            public static WordEditorWindow createEditorWindow(NounConcept concept, Wiki wiki) {
    091                    WordEditorWindow editorWindow = WordEditorWindow.createEditorWindow();
    092                    editorWindow.addTab(new NounForm(concept, 0, editorWindow, wiki, wiki));
    093                    return editorWindow;
    094            }
    095
    096            protected void save() {
    097                    Wiki wiki = getWiki();
    098                    String singular = normalize(singularField.getText());
    099                    String plural = normalize(pluralField.getText());
    100                    String singularP = singular.replace("_", " ");
    101                    String pluralP = plural.replace("_", " ");
    102
    103                    if (singular.equals(plural)) {
    104                            wiki.log("edit", "error: singular and plural form have to be distinct.");
    105                            showErrorMessage("Singular and plural form have to be distinct.");
    106                            return;
    107                    }
    108                    if (singular.equals("")) {
    109                            wiki.log("edit", "error: no word defined");
    110                            showErrorMessage("No singular form defined: Please specify the singular form.");
    111                            return;
    112                    }
    113                    if (!isValidString(singular)) {
    114                            wiki.log("edit", "error: word contains invalid character");
    115                            showErrorMessage("Invalid character: Only a-z, A-Z, 0-9, -, and spaces are allowed, " +
    116                                    "and the first character must be one of a-z A-Z.");
    117                            return;
    118                    }
    119                    if (FunctionWords.isFunctionWord(singular)) {
    120                            wiki.log("edit", "error: word is predefined");
    121                            showErrorMessage("'" + singularP + "' is a predefined word and cannot be used here.");
    122                            return;
    123                    }
    124                    OntologyElement oe = wiki.getOntology().get(singular);
    125                    if (oe != null && oe != concept) {
    126                            wiki.log("edit", "error: word is already used");
    127                            showErrorMessage("The word '" + singularP + "' is already used. Please use a different one.");
    128                            return;
    129                    }
    130                    if (plural.equals("")) {
    131                            wiki.log("edit", "error: no word defined");
    132                            showErrorMessage("No plural form defined: Please specify the plural form.");
    133                            return;
    134                    }
    135                    if (!isValidString(plural)) {
    136                            wiki.log("edit", "error: word contains invalid character");
    137                            showErrorMessage("Invalid character: Only a-z, A-Z, 0-9, -, and spaces are allowed, " +
    138                                    "and the first character must be one of a-z A-Z.");
    139                            return;
    140                    }
    141                    if (FunctionWords.isFunctionWord(plural)) {
    142                            wiki.log("edit", "error: word is predefined");
    143                            showErrorMessage("'" + pluralP + "' is a predefined word and cannot be used here.");
    144                            return;
    145                    }
    146                    oe = wiki.getOntology().get(plural);
    147                    if (oe != null && oe != concept) {
    148                            wiki.log("edit", "error: word is already used");
    149                            showErrorMessage("The word '" + pluralP + "' is already used. Please use a different one.");
    150                            return;
    151                    }
    152                    concept.setWords(singular, plural);
    153                    wiki.log("edit", "noun: " + singular + " / " + plural);
    154                    if (concept.getOntology() == null) {
    155                            concept.registerAt(getWiki().getOntology());
    156                    }
    157                    finished(concept, wordNumber);
    158            }
    159
    160            public String toString() {
    161                    return "Noun";
    162            }
    163
    164    }