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 java.io.IOException;
018    import java.util.TooManyListenersException;
019    
020    import nextapp.echo2.app.Alignment;
021    import nextapp.echo2.app.Column;
022    import nextapp.echo2.app.Extent;
023    import nextapp.echo2.app.Font;
024    import nextapp.echo2.app.Grid;
025    import nextapp.echo2.app.Insets;
026    import nextapp.echo2.app.Row;
027    import nextapp.echo2.app.event.ActionEvent;
028    import nextapp.echo2.app.event.ActionListener;
029    import nextapp.echo2.app.event.WindowPaneEvent;
030    import nextapp.echo2.app.event.WindowPaneListener;
031    import nextapp.echo2.app.filetransfer.UploadEvent;
032    import nextapp.echo2.app.filetransfer.UploadListener;
033    import nextapp.echo2.app.filetransfer.UploadSelect;
034    import nextapp.echo2.app.layout.GridLayoutData;
035    
036    /**
037     * This is an upload window that allows the user to choose a local file to be uploaded to the server.
038     * 
039     * @author Tobias Kuhn
040     */
041    public class UploadWindow extends WindowPane implements ActionListener, UploadListener {
042    
043            private static final long serialVersionUID = -8594954833738936914L;
044    
045            private ActionListener actionListener;
046            private String fileContent;
047            private Label fileLabel;
048            private GeneralButton openButton;
049            private long maxFileSize = 0;
050    
051            /**
052             * Creates a new upload window.
053             * 
054             * @param title The window title.
055             * @param message The message that is displayed above the upload button.
056             * @param parent The parent window.
057             * @param actionListener An action-listener or null.
058             */
059            public UploadWindow(String title, String message, WindowPane parent, ActionListener actionListener) {
060                    this.actionListener = actionListener;
061                    setTitle(title);
062                    setTitleFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
063                    setModal(true);
064                    setWidth(new Extent(420));
065                    setHeight(new Extent(200));
066                    setResizable(false);
067                    setMovable(true);
068                    setTitleBackground(Style.windowTitleBackground);
069                    setStyleName("Default");
070    
071                    addWindowPaneListener(new WindowPaneListener() {
072    
073                            private static final long serialVersionUID = -3897741327122083261L;
074    
075                            public void windowPaneClosing(WindowPaneEvent e) {
076                                    actionPerformed(new ActionEvent(UploadWindow.this, "Close"));
077                            }
078    
079                    });
080    
081                    Grid grid = new Grid(1);
082                    grid.setInsets(new Insets(10, 10, 10, 0));
083                    grid.setColumnWidth(0, new Extent(400));
084                    grid.setRowHeight(0, new Extent(110));
085    
086                    Column messageColumn = new Column();
087                    GridLayoutData layout1 = new GridLayoutData();
088                    layout1.setAlignment(new Alignment(Alignment.LEFT, Alignment.TOP));
089                    messageColumn.setLayoutData(layout1);
090    
091                    for (String l : message.split("\\n")) {
092                            Label label = new Label(l);
093                            label.setFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
094                            messageColumn.add(label);
095                    }
096    
097                    UploadSelect uploadSelect = new UploadSelect();
098                    try {
099                            uploadSelect.addUploadListener(this);
100                            uploadSelect.setSendButtonDisplayed(false);
101                            uploadSelect.setHeight(new Extent(40));
102                            uploadSelect.setWidth(new Extent(300));
103                            messageColumn.add(uploadSelect);
104                    } catch (TooManyListenersException ex) {
105                            ex.printStackTrace();
106                    }
107    
108                    fileLabel = new Label();
109                    messageColumn.add(fileLabel);
110    
111                    grid.add(messageColumn);
112    
113                    Row buttonBar = new Row();
114                    buttonBar.setCellSpacing(new Extent(10));
115                    buttonBar.setInsets(new Insets(0, 0, 0, 10));
116                    openButton = new GeneralButton("Open", 80, this);
117                    openButton.setEnabled(false);
118                    buttonBar.add(openButton);
119                    buttonBar.add(new GeneralButton("Cancel", 80, this));
120                    GridLayoutData layout2 = new GridLayoutData();
121                    layout2.setAlignment(new Alignment(Alignment.CENTER, Alignment.BOTTOM));
122                    buttonBar.setLayoutData(layout2);
123                    grid.add(buttonBar);
124    
125                    add(grid);
126    
127                    if (parent != null) {
128                            setPositionX(new Extent(parent.getPositionX().getValue() + (parent.getWidth().getValue() - getWidth().getValue())/2));
129                            setPositionY(new Extent(parent.getPositionY().getValue() + (parent.getHeight().getValue() - getHeight().getValue())/2));
130                    }
131            }
132    
133            /**
134             * Sets the maximum file size.
135             * 
136             * @param maxFileSize The maximum file size in bytes. 0 for unlimited size.
137             */
138            public void setMaxFileSize(long maxFileSize) {
139                    this.maxFileSize = maxFileSize;
140            }
141    
142            /**
143             * Returns the content of the uploaded file as a string.
144             * 
145             * @return The content of the uploaded file.
146             */
147            public String getFileContent() {
148                    return fileContent;
149            }
150    
151            public void actionPerformed(ActionEvent e) {
152                    setVisible(false);
153                    if (actionListener == null) return;
154                    if (e.getActionCommand().equals("Open")) {
155                            actionListener.actionPerformed(new ActionEvent(this, "Upload"));
156                    }
157            }
158    
159            public void fileUpload(UploadEvent e) {
160                    if (maxFileSize > 0 && e.getSize() > maxFileSize) {
161                            fileContent = null;
162                            fileLabel.setText("The chosen file is too large (" + e.getSize() + " Bytes).");
163                            openButton.setEnabled(false);
164                            return;
165                    }
166                    try {
167                            byte[] b = new byte[e.getSize()];
168                            e.getInputStream().read(b, 0, e.getSize());
169                            fileContent = new String(b);
170                            String fileName = e.getFileName();
171                            if (fileName.length() > 15) fileName = fileName.substring(0, 15) + "...";
172                            fileLabel.setText("Chosen file: " + fileName + " (" + e.getSize() + " Bytes)");
173                            openButton.setEnabled(true);
174                    } catch (IOException ioe) {
175                            ioe.printStackTrace();
176                    }
177            }
178    
179            public void invalidFileUpload(UploadEvent uploadEvent) {}
180    
181    }