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