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