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.CheckBox;
019    import nextapp.echo2.app.ResourceImageReference;
020    import nextapp.echo2.app.event.ActionListener;
021    import ch.uzh.ifi.attempto.acewiki.Wiki;
022    import ch.uzh.ifi.attempto.acewiki.core.ontology.Individual;
023    import ch.uzh.ifi.attempto.acewiki.core.ontology.OntologyElement;
024    import ch.uzh.ifi.attempto.ape.FunctionWords;
025    import ch.uzh.ifi.attempto.echocomp.TextField;
026    import ch.uzh.ifi.attempto.echocomp.WindowPane;
027    
028    /**
029     * This class represents a form to create or modify proper names.
030     * 
031     * @author Tobias Kuhn
032     */
033    public class ProperNameForm extends FormPane {
034            
035            private static final long serialVersionUID = 7860047859937196093L;
036            
037            private TextField nameField = new TextField();
038            private CheckBox defArticleCheckBox = new CheckBox();
039            
040            private Individual ind;
041            
042            /**
043             * Creates a new proper name form.
044             * 
045             * @param ind The individual that is represented by the proper name.
046             * @param window The host window of the form.
047             * @param wiki The wiki instance.
048             * @param actionListener The actionlistener.
049             */
050            public ProperNameForm(Individual ind, WindowPane window, Wiki wiki, ActionListener actionListener) {
051                    super(window, wiki, actionListener);
052                    this.ind = ind;
053                    
054                    setIconRow("individual",
055                                    "Every proper name represents a certain individual. " +
056                                    "The country \"Switzerland\", the person \"Bob Dylan\", the river \"Nile\", " +
057                                    "and the organization \"United Nations\" are typical examples for such individuals. " +
058                                    "Some proper names require a definite article (\"the Nile\", \"the United Nations\") " +
059                                    "and others do not (\"Switzerland\", \"Bob Dylan\")."
060                            );
061                    addRow("proper name", nameField, "examples: Switzerland, Bob Dylan, Nile, United Nations", true);
062                    addRow("definite article", defArticleCheckBox, "examples: the Nile, the United Nations", false);
063                    
064                    nameField.setText(ind.getPrettyWord(1));
065                    defArticleCheckBox.setSelected(ind.hasDefiniteArticle());
066                    defArticleCheckBox.setStateIcon(new ResourceImageReference("ch/uzh/ifi/attempto/acewiki/gui/img/notchecked.png"));
067                    defArticleCheckBox.setSelectedStateIcon(new ResourceImageReference("ch/uzh/ifi/attempto/acewiki/gui/img/checked.png"));
068                    
069                    ApplicationInstance.getActive().setFocusedComponent(nameField);
070            }
071            
072            /**
073             * Creates a new creator window for proper names.
074             * 
075             * @param wiki The wiki instance.
076             * @param actionListener The actionlistener.
077             * @return The new creator window.
078             */
079            public static WordEditorWindow createCreatorWindow(Wiki wiki, ActionListener actionListener) {
080                    WordEditorWindow creatorWindow = WordEditorWindow.createCreatorWindow();
081                    creatorWindow.addTab(new ProperNameForm(new Individual(), creatorWindow, wiki, actionListener));
082                    return creatorWindow;
083            }
084            
085            /**
086             * Creates a new editor window for proper names.
087             * 
088             * @param ind The individual that is represented by the proper name that should be edited.
089             * @param wiki The wiki instance.
090             * @return The new editor window.
091             */
092            public static WordEditorWindow createEditorWindow(Individual ind, Wiki wiki) {
093                    WordEditorWindow editorWindow = WordEditorWindow.createEditorWindow();
094                    editorWindow.addTab(new ProperNameForm(ind, editorWindow, wiki, wiki));
095                    return editorWindow;
096            }
097    
098            protected void save() {
099                    Wiki wiki = getWiki();
100                    String name = normalize(nameField.getText());
101                    if (name.toLowerCase().startsWith("the_")) {
102                            name = name.substring(4);
103                    }
104                    String nameP = name.replace("_", " ");
105                    
106                    if (name.equals("")) {
107                            wiki.log("edit", "error: no word defined");
108                            showErrorMessage("No proper name defined: Please specify a name.");
109                            return;
110                    }
111                    if (!isValidString(name)) {
112                            wiki.log("edit", "error: word contains invalid character");
113                            showErrorMessage("Invalid character: Only a-z, A-Z, 0-9, -, and spaces are allowed, " +
114                                    "and the first character must be one of a-z A-Z.");
115                            return;
116                    }
117                    if (FunctionWords.isFunctionWord(name)) {
118                            wiki.log("edit", "error: word is predefined");
119                            showErrorMessage("'" + nameP + "' is a predefined word and cannot be used here.");
120                            return;
121                    }
122                    OntologyElement oe = wiki.getOntology().get(name);
123                    if (oe != null && oe != ind) {
124                            wiki.log("edit", "error: word is already used");
125                            showErrorMessage("The word '" + nameP + "' is already used. Please use a different one.");
126                            return;
127                    }
128                    String word = name;
129                    if (defArticleCheckBox.isSelected()) {
130                            word = "the " + name;
131                    }
132                    ind.setWords(word, name);
133                    wiki.log("edit", "proper name: " + word);
134                    if (ind.getOntology() == null) {
135                            ind.registerAt(getWiki().getOntology());
136                    }
137                    finished(ind);
138            }
139            
140            public String toString() {
141                    return "Proper Name";
142            }
143    
144    }