Hallo zusammen
Habe eine kleines Programm geschrieben dass Dateien per Drag&Drop von X nach Y kopiert. Die ProgressBar zeigt den aktuellen Stand an, läuft alles bestens.
Folgendes Problem steht vor mir...
So lange der upload/kopiervorgang läuft ist das Fenster nicht benutzbar, sprich ich kann den 'abbrechen'-Button nicht betätigen. Weiss jemand eine Lösung?
Hier noch der Code:
[HIGHLIGHT="Java"]import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.iutputStreamWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.Socket;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import com.swtdesigner.SWTResourceManager;
public class DropBox {
public static void main(String[] args) {
int style = SWT.SHELL_TRIM | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER;
final Display display = new Display();
final Shell shell = new Shell(display, SWT.CLOSE);
shell.setText("REVIS DropBox");
shell.setLayout(new FillLayout());
// TableItem item = new TableItem(table, SWT.NONE);
// item.setText("Drop a file");
shell.setSize(276, 199);
// final Table table = new Table(shell, SWT.BORDER);
// DropTarget dropTarget = new DropTarget(table, DND.DROP_COPY
// | DND.DROP_DEFAULT);
final Group group = new Group(shell, style);
group.setBounds(10, 40, 170, 50);
group.setText("Dateien hinzufügen");
final Label label = new Label(group, style);
label.setFont(SWTResourceManager.getFont("", 8, SWT.BOLD));
label.setToolTipText("Dateien in diesem Bereich reinziehen!");
label.setBounds(10, 22, 248, 105);
final Button button = new Button(group, SWT.PUSH);
button.setText("abbrechen");
//button.setEnabled(false);
button.setBounds(10, 133, 70, 25);
final ProgressBar progressBar = new ProgressBar(group, SWT.HORIZONTAL);
progressBar.setBounds(86, 133, 172, 25);
progressBar.setMinimum(0);
progressBar.setMaximum(100);
shell.open();
progressBar.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
/**
String string = (progressBar.getSelection() * 1.0
/ (progressBar.getMaximum() - progressBar.getMinimum()) * 100)
+ "%";Point point = progressBar.getSize();
FontMetrics fontMetrics = e.gc.getFontMetrics();
int width = fontMetrics.getAverageCharWidth() * string.length();
int height = fontMetrics.getHeight();
e.gc.setForeground(group.getDisplay().getSystemColor(
SWT.COLOR_WHITE));
e.gc.drawString(string, (point.x - width) / 2,
(point.y - height) / 2, true);
*/
}
});
DragSource dragSource = new DragSource(label, DND.DROP_COPY);
dragSource.setTransfer(new Transfer[] { FileTransfer.getInstance() });
dragSource.addDragListener(new DragSourceAdapter() {
public void dragSetData(DragSourceEvent event) {
if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
File file = new File("temp");
event.data = new String[] { file.getAbsolutePath() };
}
}
});
DropTarget dropTarget = new DropTarget(label, DND.DROP_COPY
| DND.DROP_DEFAULT);
dropTarget.setTransfer(new Transfer[] { FileTransfer.getInstance() });
dropTarget.addDropListener(new DropTargetAdapter() {
FileTransfer fileTransfer = FileTransfer.getInstance();
public void dragEnter(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT)
event.detail = DND.DROP_COPY;
}
public void dragOperationChanged(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT)
event.detail = DND.DROP_COPY;
}
public void dragOver(DropTargetEvent event) {
System.out.println(label);
event.detail = DND.DROP_NONE;
int index = 0;
while (index < event.dataTypes.length) {
if (fileTransfer.isSupportedType(event.dataTypes[index]))
break;
index++;
}
if (index < event.dataTypes.length) {
event.currentDataType = event.dataTypes[index];
event.detail = DND.DROP_COPY;
return;
}
}
public void drop(DropTargetEvent event) {
System.out.println(event);
System.out.println(event.currentDataType);
if (fileTransfer.isSupportedType(event.currentDataType)) {
label.setText("Uploadvorgang...");
button.setEnabled(true);
final String[] files = (String[]) event.data;
for (int i = 0; i < files.length; i++) {
progressBar.setSelection(100 / files.length * i);
try {
if (files != null && files.length > 0) {
label.setText(label.getText() + "\n" + files);
}
String hostname = "hostname.de";
int port = 80;
InetAddress addr = InetAddress.getByName(hostname);
Socket socket = new Socket(addr, port);
// Send header
String path = "pfad/zum/uploadScript.php";
// File To Upload
File theFile = new File(files);
System.out.println(files);
System.out.println(addr.getHostAddress());
System.out.println("Dateigrösse: " + (int) theFile.length());
DataInputStream fis = new DataInputStream(
new BufferedInputStream(
new FileInputStream(theFile)));
byte[] theData = new byte[(int) theFile.length()];
fis.readFully(theData);
fis.close();
DataOutputStream raw = new DataOutputStream(
socket.getOutputStream());
Writer wr = new OutputStreamWriter(raw);
String command = "--dill\r\n"
+ "Content-Disposition: form-data; name=\"uploadfile\"; filename=\""
+ theFile.getName() + "\"\r\n"
+ "Content-Type: text/xml\r\n"
+ "\r\n";
String trail = "\r\n--dill--\r\n";
String header = "POST "
+ path
+ " HTTP/1.0\r\n"
+ "Accept: */*\r\n"
+ "Referer: dev.xxx.xx\r\n"
+ "Accept-Language: de\r\n"
+ "Content-Type: multipart/form-data; boundary=dill\r\n"
+ "User_Agent: \r\n"
+ "Host: dev.xxx.xx\r\n"
+ "Content-Length: "
+ ((int) theFile.length()
+ command.length() + trail
.length()) + "\r\n"
// + "Connection: Keep-Alive\r\n"
+ "Pragma: no-cache\r\n" + "\r\n";
wr.write(header);
wr.write(command);
wr.flush();
raw.write(theData);
raw.flush();
wr.write("\r\n--dill--\r\n");
wr.flush();
BufferedReader rd = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
raw.close();
socket.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
};
button.setEnabled(false);
progressBar.setSelection(100);
label.setText("Upload erfolgreich!");
}
}
});
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}[/HIGHLIGHT]
Habe eine kleines Programm geschrieben dass Dateien per Drag&Drop von X nach Y kopiert. Die ProgressBar zeigt den aktuellen Stand an, läuft alles bestens.
Folgendes Problem steht vor mir...
So lange der upload/kopiervorgang läuft ist das Fenster nicht benutzbar, sprich ich kann den 'abbrechen'-Button nicht betätigen. Weiss jemand eine Lösung?
Hier noch der Code:
[HIGHLIGHT="Java"]import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.iutputStreamWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.Socket;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import com.swtdesigner.SWTResourceManager;
public class DropBox {
public static void main(String[] args) {
int style = SWT.SHELL_TRIM | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER;
final Display display = new Display();
final Shell shell = new Shell(display, SWT.CLOSE);
shell.setText("REVIS DropBox");
shell.setLayout(new FillLayout());
// TableItem item = new TableItem(table, SWT.NONE);
// item.setText("Drop a file");
shell.setSize(276, 199);
// final Table table = new Table(shell, SWT.BORDER);
// DropTarget dropTarget = new DropTarget(table, DND.DROP_COPY
// | DND.DROP_DEFAULT);
final Group group = new Group(shell, style);
group.setBounds(10, 40, 170, 50);
group.setText("Dateien hinzufügen");
final Label label = new Label(group, style);
label.setFont(SWTResourceManager.getFont("", 8, SWT.BOLD));
label.setToolTipText("Dateien in diesem Bereich reinziehen!");
label.setBounds(10, 22, 248, 105);
final Button button = new Button(group, SWT.PUSH);
button.setText("abbrechen");
//button.setEnabled(false);
button.setBounds(10, 133, 70, 25);
final ProgressBar progressBar = new ProgressBar(group, SWT.HORIZONTAL);
progressBar.setBounds(86, 133, 172, 25);
progressBar.setMinimum(0);
progressBar.setMaximum(100);
shell.open();
progressBar.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
/**
String string = (progressBar.getSelection() * 1.0
/ (progressBar.getMaximum() - progressBar.getMinimum()) * 100)
+ "%";Point point = progressBar.getSize();
FontMetrics fontMetrics = e.gc.getFontMetrics();
int width = fontMetrics.getAverageCharWidth() * string.length();
int height = fontMetrics.getHeight();
e.gc.setForeground(group.getDisplay().getSystemColor(
SWT.COLOR_WHITE));
e.gc.drawString(string, (point.x - width) / 2,
(point.y - height) / 2, true);
*/
}
});
DragSource dragSource = new DragSource(label, DND.DROP_COPY);
dragSource.setTransfer(new Transfer[] { FileTransfer.getInstance() });
dragSource.addDragListener(new DragSourceAdapter() {
public void dragSetData(DragSourceEvent event) {
if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
File file = new File("temp");
event.data = new String[] { file.getAbsolutePath() };
}
}
});
DropTarget dropTarget = new DropTarget(label, DND.DROP_COPY
| DND.DROP_DEFAULT);
dropTarget.setTransfer(new Transfer[] { FileTransfer.getInstance() });
dropTarget.addDropListener(new DropTargetAdapter() {
FileTransfer fileTransfer = FileTransfer.getInstance();
public void dragEnter(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT)
event.detail = DND.DROP_COPY;
}
public void dragOperationChanged(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT)
event.detail = DND.DROP_COPY;
}
public void dragOver(DropTargetEvent event) {
System.out.println(label);
event.detail = DND.DROP_NONE;
int index = 0;
while (index < event.dataTypes.length) {
if (fileTransfer.isSupportedType(event.dataTypes[index]))
break;
index++;
}
if (index < event.dataTypes.length) {
event.currentDataType = event.dataTypes[index];
event.detail = DND.DROP_COPY;
return;
}
}
public void drop(DropTargetEvent event) {
System.out.println(event);
System.out.println(event.currentDataType);
if (fileTransfer.isSupportedType(event.currentDataType)) {
label.setText("Uploadvorgang...");
button.setEnabled(true);
final String[] files = (String[]) event.data;
for (int i = 0; i < files.length; i++) {
progressBar.setSelection(100 / files.length * i);
try {
if (files != null && files.length > 0) {
label.setText(label.getText() + "\n" + files);
}
String hostname = "hostname.de";
int port = 80;
InetAddress addr = InetAddress.getByName(hostname);
Socket socket = new Socket(addr, port);
// Send header
String path = "pfad/zum/uploadScript.php";
// File To Upload
File theFile = new File(files);
System.out.println(files);
System.out.println(addr.getHostAddress());
System.out.println("Dateigrösse: " + (int) theFile.length());
DataInputStream fis = new DataInputStream(
new BufferedInputStream(
new FileInputStream(theFile)));
byte[] theData = new byte[(int) theFile.length()];
fis.readFully(theData);
fis.close();
DataOutputStream raw = new DataOutputStream(
socket.getOutputStream());
Writer wr = new OutputStreamWriter(raw);
String command = "--dill\r\n"
+ "Content-Disposition: form-data; name=\"uploadfile\"; filename=\""
+ theFile.getName() + "\"\r\n"
+ "Content-Type: text/xml\r\n"
+ "\r\n";
String trail = "\r\n--dill--\r\n";
String header = "POST "
+ path
+ " HTTP/1.0\r\n"
+ "Accept: */*\r\n"
+ "Referer: dev.xxx.xx\r\n"
+ "Accept-Language: de\r\n"
+ "Content-Type: multipart/form-data; boundary=dill\r\n"
+ "User_Agent: \r\n"
+ "Host: dev.xxx.xx\r\n"
+ "Content-Length: "
+ ((int) theFile.length()
+ command.length() + trail
.length()) + "\r\n"
// + "Connection: Keep-Alive\r\n"
+ "Pragma: no-cache\r\n" + "\r\n";
wr.write(header);
wr.write(command);
wr.flush();
raw.write(theData);
raw.flush();
wr.write("\r\n--dill--\r\n");
wr.flush();
BufferedReader rd = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
raw.close();
socket.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
};
button.setEnabled(false);
progressBar.setSelection(100);
label.setText("Upload erfolgreich!");
}
}
});
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}[/HIGHLIGHT]