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.echocomp;
016    
017    import nextapp.echo2.app.Alignment;
018    import nextapp.echo2.app.Column;
019    import nextapp.echo2.app.Extent;
020    import nextapp.echo2.app.Font;
021    import nextapp.echo2.app.Insets;
022    import nextapp.echo2.app.Row;
023    import nextapp.echo2.app.event.ActionEvent;
024    import nextapp.echo2.app.event.ActionListener;
025    
026    /**
027     * This class represents a window that contains a text area plus "OK" and "Cancel" buttons.
028     * 
029     * @author Tobias Kuhn
030     */
031    public class TextAreaWindow extends WindowPane implements ActionListener {
032            
033            private static final long serialVersionUID = -8910244587508187438L;
034            
035            private ActionListener actionListener;
036            
037            private TextArea textArea;
038            
039            /**
040             * Creates a new text area window with the given title, text, and action-listener.
041             * 
042             * @param title The title of the window.
043             * @param text The initial text in the text area.
044             * @param actionListener The action-listener or null.
045             */
046            public TextAreaWindow(String title, String text, ActionListener actionListener) {
047                    this.actionListener = actionListener;
048                    
049                    setModal(true);
050                    setTitle(title);
051                    setTitleFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
052                    setResizable(false);
053                    setTitleBackground(Style.windowTitleBackground);
054                    setStyleName("Default");
055                    
056                    Column mainColumn = new Column();
057                    mainColumn.setInsets(new Insets(10, 10));
058                    mainColumn.setCellSpacing(new Extent(10));
059                    
060                    textArea = new TextArea();
061                    textArea.setFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
062                    textArea.setText(text);
063                    mainColumn.add(textArea);
064                    
065                    Row buttonBar = new Row();
066                    buttonBar.setAlignment(new Alignment(Alignment.RIGHT, Alignment.CENTER));
067                    buttonBar.setCellSpacing(new Extent(5));
068                    GeneralButton okButton = new GeneralButton("OK", 70, this);
069                    buttonBar.add(okButton);
070                    GeneralButton cancelButton = new GeneralButton("Cancel", 70, this);
071                    buttonBar.add(cancelButton);
072                    mainColumn.add(buttonBar);
073                    
074                    add(mainColumn);
075                    
076                    setSize(450, 300);
077            }
078            
079            /**
080             * Returns the text of the text area.
081             * 
082             * @return The text of the text area.
083             */
084            public String getText() {
085                    return textArea.getText();
086            }
087            
088            /**
089             * Sets the size of the text area window. The size of the text area is set accordingly.
090             * 
091             * @param width The width of the window in pixels.
092             * @param height The height of the window in pixels.
093             */
094            public void setSize(int width, int height) {
095                    if (width < 200) width = 200;
096                    if (height < 120) height = 120;
097                    setWidth(new Extent(width));
098                    setHeight(new Extent(height));
099                    textArea.setWidth(new Extent(width - 45));
100                    textArea.setHeight(new Extent(height - 105));
101            }
102            
103            public void actionPerformed(ActionEvent e) {
104                    if (actionListener != null) {
105                            actionListener.actionPerformed(new ActionEvent(this, e.getActionCommand()));
106                    }
107                    setVisible(false);
108                    dispose();
109            }
110    
111    }