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 java.util.ArrayList;
018    
019    import nextapp.echo2.app.Alignment;
020    import nextapp.echo2.app.ApplicationInstance;
021    import nextapp.echo2.app.Border;
022    import nextapp.echo2.app.Color;
023    import nextapp.echo2.app.Component;
024    import nextapp.echo2.app.ContentPane;
025    import nextapp.echo2.app.Extent;
026    import nextapp.echo2.app.Font;
027    import nextapp.echo2.app.Insets;
028    import nextapp.echo2.app.event.ChangeEvent;
029    import nextapp.echo2.app.event.ChangeListener;
030    import ch.uzh.ifi.attempto.echocomp.Style;
031    import ch.uzh.ifi.attempto.echocomp.TextField;
032    import ch.uzh.ifi.attempto.echocomp.WindowPane;
033    import echopointng.ButtonEx;
034    import echopointng.TabbedPane;
035    import echopointng.tabbedpane.DefaultTabModel;
036    
037    /**
038     * This class represents the word editor that is used to create or modify words. It can contain
039     * several tabs for several word forms.
040     * 
041     * @author Tobias Kuhn
042     */
043    public class WordEditorWindow extends WindowPane implements ChangeListener {
044            
045            private static final long serialVersionUID = 6805275173727379038L;
046            
047            private TabbedPane tabbedPane = new TabbedPane();
048            private ArrayList<ContentPane> tabs = new ArrayList<ContentPane>();
049            
050            private WordEditorWindow(String title) {
051                    setModal(true);
052                    setTitle(title);
053                    setTitleFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
054                    setWidth(new Extent(753));
055                    setHeight(new Extent(503));
056                    setResizable(false);
057                    setTitleBackground(Style.windowTitleBackground);
058                    setStyleName("Default");
059                    
060                    tabbedPane.setOutsets(new Insets(10));
061                    tabbedPane.setTabPlacement(Alignment.TOP);
062                    tabbedPane.setBorder(new Border(1, Color.BLACK, Border.STYLE_INSET));
063                    tabbedPane.setHeight(new Extent(400));
064                    tabbedPane.getSelectionModel().addChangeListener(this);
065                    
066                    add(tabbedPane);
067            }
068            
069            /**
070             * Creates a new word editor window.
071             * 
072             * @return The new word editor window.
073             */
074            public static WordEditorWindow createEditorWindow() {
075                    return new WordEditorWindow("Word Editor");
076            }
077            
078            /**
079             * Creates a new word creator window.
080             * 
081             * @return The new word creator window.
082             */
083            public static WordEditorWindow createCreatorWindow() {
084                    return new WordEditorWindow("Word Creator");
085            }
086            
087            /**
088             * Adds a new tab to the window. The toString() value is used as the title of the tab.
089             * 
090             * @param pane The pane that should be shown in the new tab.
091             */
092            public void addTab(ContentPane pane) {
093                    DefaultTabModel tabModel = (DefaultTabModel) tabbedPane.getModel();
094                    ButtonEx tab = new ButtonEx(pane.toString(), null);
095                    tab.setStyle(DefaultTabModel.DEFAULT_TOP_ALIGNED_STYLE);
096                    tab.setFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
097                    tab.setInsets(new Insets(5, 2));
098                    tabModel.addTab(tab, pane);
099                    tabs.add(pane);
100                    doFocus();
101            }
102            
103            private void doFocus() {
104                    int i = tabbedPane.getSelectedIndex();
105                    if (i < tabs.size()) {
106                            doFocus(tabs.get(i));
107                    }
108            }
109            
110            private boolean doFocus(Component c) {
111                    if (c instanceof TextField) {
112                            ApplicationInstance.getActive().setFocusedComponent((TextField) c);
113                            return true;
114                    } else {
115                            for (Component child : c.getComponents()) {
116                                    boolean b = doFocus(child);
117                                    if (b) return true;
118                            }
119                    }
120                    return false;
121            }
122            
123            public void stateChanged(ChangeEvent e) {
124                    doFocus();
125            }
126            
127    }