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.OfRole;
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 of-constructs.
028     * 
029     * @author Tobias Kuhn
030     */
031    public class NounOfForm extends FormPane {
032            
033            private static final long serialVersionUID = -6544810970331235077L;
034    
035            private TextField nounField = new TextField();
036            
037            private OfRole role;
038            
039            /**
040             * Creates a new form for of-constructs.
041             * 
042             * @param role The role that is represented by the of-construct.
043             * @param window The host window of the form.
044             * @param wiki The wiki instance.
045             * @param actionListener The actionlistener.
046             */
047            public NounOfForm(OfRole role, WindowPane window, Wiki wiki, ActionListener actionListener) {
048                    super(window, wiki, actionListener);
049                    this.role = role;
050    
051                    setIconRow("role",
052                                    "Every of-construct represents a certain relation between things. " +
053                                    "For example, the of-construct \"child of\" relates persons to their parents. " +
054                                    "Every of-construct consists of a noun plus the preposition \"of\", " +
055                                    "for example \"part of\", \"child of\", \"owner of\", etc."
056                            );
057                    addRow("noun", nounField, "examples: part, child, owner", true);
058                    
059                    nounField.setText(role.getPrettyNoun());
060                    
061                    ApplicationInstance.getActive().setFocusedComponent(nounField);
062            }
063            
064            /**
065             * Creates a new creator window for of-constructs.
066             * 
067             * @param wiki The wiki instance.
068             * @param actionListener The actionlistener.
069             * @return The new creator window.
070             */
071            public static WordEditorWindow createCreatorWindow(Wiki wiki, ActionListener actionListener) {
072                    WordEditorWindow creatorWindow = WordEditorWindow.createCreatorWindow();
073                    creatorWindow.addTab(new NounOfForm(new OfRole(), creatorWindow, wiki, actionListener));
074                    return creatorWindow;
075            }
076            
077            /**
078             * Creates a new editor window for of-constructs.
079             * 
080             * @param role The role that is represented by the of-construct that should be edited.
081             * @param wiki The wiki instance.
082             * @return The new editor window.
083             */
084            public static WordEditorWindow createEditorWindow(OfRole role, Wiki wiki) {
085                    WordEditorWindow editorWindow = WordEditorWindow.createEditorWindow();
086                    editorWindow.addTab(new NounOfForm(role, editorWindow, wiki, wiki));
087                    return editorWindow;
088            }
089    
090            protected void save() {
091                    Wiki wiki = getWiki();
092                    String name = normalize(nounField.getText());
093                    if (name.toLowerCase().endsWith("_of")) {
094                            name = name.substring(0, name.length()-3);
095                    }
096                    String nameP = name.replace("_", " ");
097                    
098                    if (name.equals("")) {
099                            wiki.log("edit", "error: no word defined");
100                            showErrorMessage("No noun defined: Please specify the singular form of a noun.");
101                            return;
102                    }
103                    if (!isValidString(name)) {
104                            wiki.log("edit", "error: word contains invalid character");
105                            showErrorMessage("Invalid character: Only a-z, A-Z, 0-9, -, and spaces are allowed, " +
106                                    "and the first character must be one of a-z A-Z.");
107                            return;
108                    }
109                    if (FunctionWords.isFunctionWord(name)) {
110                            wiki.log("edit", "error: word is predefined");
111                            showErrorMessage("'" + nameP + "' is a predefined word and cannot be used here.");
112                            return;
113                    }
114                    OntologyElement oe = wiki.getOntology().get(name + " of");
115                    if (oe != null && oe != role) {
116                            wiki.log("edit", "error: word is already used");
117                            showErrorMessage("The word '" + nameP + "' is already used. Please use a different one.");
118                            return;
119                    }
120                    role.setWords(name);
121                    wiki.log("edit", "of-construct: " + name);
122                    if (role.getOntology() == null) {
123                            role.registerAt(getWiki().getOntology());
124                    }
125                    finished(role);
126            }
127            
128            public String toString() {
129                    return "Of-Construct";
130            }
131    
132    }