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.echocomp;
016    
017    import nextapp.echo.app.Alignment;
018    import nextapp.echo.app.ApplicationInstance;
019    import nextapp.echo.app.Column;
020    import nextapp.echo.app.Extent;
021    import nextapp.echo.app.Font;
022    import nextapp.echo.app.Grid;
023    import nextapp.echo.app.Insets;
024    import nextapp.echo.app.ResourceImageReference;
025    import nextapp.echo.app.Row;
026    import nextapp.echo.app.WindowPane;
027    import nextapp.echo.app.event.ActionEvent;
028    import nextapp.echo.app.event.ActionListener;
029    import nextapp.echo.app.event.WindowPaneEvent;
030    import nextapp.echo.app.event.WindowPaneListener;
031    import nextapp.echo.app.layout.GridLayoutData;
032    
033    /**
034     * This is a convenience class for easy creation of message windows.
035     * 
036     * @author Tobias Kuhn
037     */
038    public class MessageWindow extends WindowPane implements ActionListener {
039            
040            private static final long serialVersionUID = -6999194368016297503L;
041            
042            private ActionListener actionListener;
043    
044            /**
045             * Creates a new message window.
046             * 
047             * @param title The title of the window.
048             * @param image The image to be displayed above the text.
049             * @param message The message text.
050             * @param parent The parent window.
051             * @param actionListener The action-listener.
052             * @param options A list of options each represented by a button in the message window.
053             */
054            public MessageWindow(String title, ResourceImageReference image, String message,
055                            WindowPane parent, ActionListener actionListener, String... options) {
056                    this.actionListener = actionListener;
057                    setTitle(title);
058                    setTitleFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
059                    setModal(true);
060                    setWidth(new Extent(420));
061                    setHeight(new Extent(180));
062                    setResizable(false);
063                    setMovable(true);
064                    setTitleBackground(Style.windowTitleBackground);
065                    setStyleName("Default");
066                    
067                    addWindowPaneListener(new WindowPaneListener() {
068                            
069                            private static final long serialVersionUID = -3897741327122083261L;
070    
071                            public void windowPaneClosing(WindowPaneEvent e) {
072                                    actionPerformed(new ActionEvent(MessageWindow.this, "Close"));
073                            }
074                            
075                    });
076                    
077                    Grid grid = new Grid(1);
078                    grid.setInsets(new Insets(10, 10, 10, 0));
079                    grid.setColumnWidth(0, new Extent(400));
080                    grid.setRowHeight(0, new Extent(90));
081                    
082                    Column iconMessageColumn = new Column();
083                    GridLayoutData layout1 = new GridLayoutData();
084                    layout1.setAlignment(new Alignment(Alignment.LEFT, Alignment.TOP));
085                    iconMessageColumn.setLayoutData(layout1);
086                    
087                    if (image != null) {
088                            iconMessageColumn.add(new Label(image));
089                            iconMessageColumn.add(new VSpace(5));
090                    }
091                    
092                    Label label = new Label(message);
093                    label.setFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
094                    iconMessageColumn.add(label);
095                    grid.add(iconMessageColumn);
096                    
097                    Row buttonBar = new Row();
098                    buttonBar.setCellSpacing(new Extent(10));
099                    buttonBar.setInsets(new Insets(0, 0, 0, 10));
100                    for (String s : options) {
101                            buttonBar.add(new GeneralButton(s, 80, this));
102                    }
103                    if (options.length == 1) {
104                            ApplicationInstance.getActive().setFocusedComponent(buttonBar.getComponent(0));
105                    }
106                    GridLayoutData layout2 = new GridLayoutData();
107                    layout2.setAlignment(new Alignment(Alignment.CENTER, Alignment.BOTTOM));
108                    buttonBar.setLayoutData(layout2);
109                    grid.add(buttonBar);
110                    
111                    add(grid);
112                    
113                    if (parent != null && parent.getPositionX() != null) {
114                            int px = parent.getPositionX().getValue();
115                            int py = parent.getPositionY().getValue();
116                            int pw = parent.getWidth().getValue();
117                            int ph = parent.getHeight().getValue();
118                            setPositionX(new Extent(px + (pw - getWidth().getValue())/2));
119                            setPositionY(new Extent(py + (ph - getHeight().getValue())/2));
120                    }
121            }
122            
123            /**
124             * Creates a new message window.
125             * 
126             * @param title The title of the window.
127             * @param message The message text.
128             * @param parent The parent window.
129             * @param actionListener The action-listener.
130             * @param options A list of options each represented by a button in the message window.
131             */
132            public MessageWindow(String title, String message, WindowPane parent,
133                            ActionListener actionListener, String... options) {
134                    this(title, null, message, parent, actionListener, options);
135            }
136            
137            /**
138             * Creates a new message window.
139             * 
140             * @param title The title of the window.
141             * @param message The message text.
142             * @param parent The parent window.
143             * @param options A list of options each represented by a button in the message window.
144             */
145            public MessageWindow(String title, String message, WindowPane parent, String... options) {
146                    this(title, null, message, parent, null, options);
147            }
148            
149            /**
150             * Creates a new message window.
151             * 
152             * @param title The title of the window.
153             * @param message The message text.
154             * @param options A list of options each represented by a button in the message window.
155             */
156            public MessageWindow(String title, String message, String... options) {
157                    this(title, null, message, null, null, options);
158            }
159            
160            public void actionPerformed(ActionEvent e) {
161                    setVisible(false);
162                    if (actionListener != null) {
163                            actionListener.actionPerformed(new ActionEvent(this, e.getActionCommand()));
164                    }
165            }
166    
167    }