001    // This file is part of AceWiki.
002    // Copyright 2008-2012, AceWiki developers.
003    // 
004    // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
005    // Lesser General Public License as published by the Free Software Foundation, either version 3 of
006    // the License, or (at your option) any later version.
007    // 
008    // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009    // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010    // Lesser General Public License for more details.
011    // 
012    // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
013    // not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.acewiki.gui;
016    
017    import java.util.ArrayList;
018    import java.util.Comparator;
019    import java.util.List;
020    
021    import nextapp.echo.app.event.ActionEvent;
022    import nextapp.echo.app.event.ActionListener;
023    import ch.uzh.ifi.attempto.acewiki.Wiki;
024    import ch.uzh.ifi.attempto.acewiki.core.EditorController;
025    import ch.uzh.ifi.attempto.acewiki.core.OntologyElement;
026    import ch.uzh.ifi.attempto.acewiki.core.OntologyTextElement;
027    import ch.uzh.ifi.attempto.base.ConcreteOption;
028    import ch.uzh.ifi.attempto.base.NextTokenOptions;
029    import ch.uzh.ifi.attempto.base.TextElement;
030    import ch.uzh.ifi.attempto.base.TextOperator;
031    import ch.uzh.ifi.attempto.preditor.DefaultMenuItemComparator;
032    import ch.uzh.ifi.attempto.preditor.MenuCreator;
033    import ch.uzh.ifi.attempto.preditor.MenuEntry;
034    import ch.uzh.ifi.attempto.preditor.MenuItem;
035    import ch.uzh.ifi.attempto.preditor.SpecialMenuItem;
036    
037    /**
038     * This is the menu creator class that generates the menu entries for the predictive editor
039     * on the basis of the AceWiki grammar.
040     * 
041     * @author Tobias Kuhn
042     */
043    public class AceWikiMenuCreator implements MenuCreator, ActionListener {
044            
045            private static final long serialVersionUID = -6442603864805781298L;
046            
047            private Wiki wiki;
048            private OntologyElement highlightedElement;
049            private ActionListener actionListener;
050            private DefaultMenuItemComparator comparator = new DefaultMenuItemComparator();
051            
052            /**
053             * Creates a new AceWiki-specific menu creator object.
054             * 
055             * @param wiki The wiki instance.
056             * @param highlightedElement The ontology element that should be highlighted in the editor
057             *     (because it is the current element).
058             * @param actionListener The action-listener.
059             */
060            public AceWikiMenuCreator(Wiki wiki, OntologyElement highlightedElement,
061                            ActionListener actionListener) {
062                    this.wiki = wiki;
063                    this.highlightedElement = highlightedElement;
064                    this.actionListener = actionListener;
065            }
066            
067            private EditorController getEditorController() {
068                    return wiki.getLanguageHandler().getEditorController();
069            }
070            
071            public List<String> getMenuGroupOrdering() {
072                    return getEditorController().getMenuGroups();
073            }
074    
075            public MenuEntry createMenuEntry(ConcreteOption option) {
076                    String menuGroup = getEditorController().getMenuGroup(option.getCategoryName());
077                    TextOperator to = wiki.getLanguageHandler().getTextOperator();
078                    TextElement te = to.createTextElement(option.getWord());
079                    MenuEntry me = new MenuEntry(te, menuGroup);
080                    if (te instanceof OntologyTextElement) {
081                            OntologyTextElement ote = (OntologyTextElement) te;
082                            me.setHighlighted(ote.getOntologyElement() == highlightedElement);
083                    }
084                    return me;
085            }
086    
087            public List<SpecialMenuItem> createSpecialMenuItems(NextTokenOptions options) {
088                    List<SpecialMenuItem> menuItems = new ArrayList<SpecialMenuItem>();
089                    for (String p : getEditorController().getExtensibleCategories()) {
090                            if (!options.containsCategory(p)) continue;
091                            String g = getEditorController().getMenuGroup(p);
092                            menuItems.add(new SpecialMenuItem("new...", g, p, this));
093                    }
094                    return menuItems;
095            }
096            
097            public void actionPerformed(ActionEvent e) {
098                    String p = e.getActionCommand();
099                    String type = getEditorController().getWordType(p);
100                    int wordNumber = getEditorController().getWordNumber(p);
101                    wiki.showCreatorWindow(type, wordNumber, actionListener);
102            }
103    
104            public int getColorShift(String menuBlockName) {
105                    return getEditorController().getColorShift(menuBlockName);
106            }
107    
108            public Comparator<MenuItem> getMenuItemComparator() {
109                    return comparator;
110            }
111    
112    }