]> git.argeo.org Git - lgpl/argeo-commons.git/blob - GenericUploadControl.java
4fcdd4db4f6beadf68e654ae6faf1cd25f59653a
[lgpl/argeo-commons.git] / GenericUploadControl.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.eclipse.ui.specific;
17
18 import java.io.File;
19 import java.io.IOException;
20
21 import org.apache.commons.io.FileUtils;
22 import org.argeo.ArgeoException;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.ModifyListener;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Event;
30 import org.eclipse.swt.widgets.FileDialog;
31 import org.eclipse.swt.widgets.Listener;
32 import org.eclipse.swt.widgets.Text;
33
34 /**
35 * RCP specific composite that provides a control to upload a file. WARNING: for
36 * the time being we set a GridLayout(2, false) on th eparent control.
37 */
38 public class GenericUploadControl extends Composite {
39 // private final static Log log = LogFactory
40 // .getLog(GenericUploadControl.class);
41
42 private FileDialog dialog;
43 private Text filePath;
44
45 public GenericUploadControl(Composite parent, int style, String browseLabel) {
46 super(parent, style);
47 createControl(this, browseLabel);
48
49 }
50
51 private void createControl(final Composite parent, String browseLabel) {
52 parent.setLayout(new GridLayout(2, false));
53
54 filePath = new Text(parent, SWT.BORDER | SWT.SINGLE);
55 GridData gd = new GridData(GridData.GRAB_HORIZONTAL
56 | GridData.FILL_HORIZONTAL);
57 filePath.setEditable(false);
58 filePath.setLayoutData(gd);
59
60 // Execute button
61 Button execute = new Button(parent, SWT.PUSH);
62 GridData gridData = new GridData();
63 gridData.horizontalAlignment = GridData.BEGINNING;
64 execute.setLayoutData(gridData);
65 execute.setText(browseLabel);
66
67 // Button listener
68 Listener executeListener = new Listener() {
69 public void handleEvent(Event event) {
70 dialog = new FileDialog(parent.getShell());
71 filePath.setText(dialog.open());
72 }
73 };
74 parent.layout();
75 execute.addListener(SWT.Selection, executeListener);
76 }
77
78 public boolean isControlEmpty() {
79 String path = filePath.getText();
80 if (path == null || "".equals(path.trim()))
81 return true;
82 else
83 return false;
84 }
85
86 public byte[] performUpload() {
87 String path = filePath.getText();
88 if (path != null) {
89 try {
90 File file = new File(path);
91 byte[] fileBA = FileUtils.readFileToByteArray(file);
92 return fileBA;
93 } catch (IOException e) {
94 throw new ArgeoException("Unexpected error while "
95 + "reading file at path " + path, e);
96 }
97 }
98 return null;
99 }
100
101 public void addModifyListener(ModifyListener listener) {
102 filePath.addModifyListener(listener);
103 }
104
105 /**
106 * Always returns null in an RCP environment
107 */
108 public String getLastFileUploadedName() {
109 return null;
110 }
111 }