summaryrefslogtreecommitdiff
path: root/pdf-over-gui/src/main/java/at/asit/pdfover/gui/components/DataSourceSelectComposite.java
diff options
context:
space:
mode:
Diffstat (limited to 'pdf-over-gui/src/main/java/at/asit/pdfover/gui/components/DataSourceSelectComposite.java')
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/components/DataSourceSelectComposite.java213
1 files changed, 178 insertions, 35 deletions
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/components/DataSourceSelectComposite.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/components/DataSourceSelectComposite.java
index e6f3e1ba..d5719917 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/components/DataSourceSelectComposite.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/components/DataSourceSelectComposite.java
@@ -16,9 +16,12 @@
package at.asit.pdfover.gui.components;
// Imports
+import java.io.File;
+
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -30,6 +33,9 @@ import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.SWT;
+import org.eclipse.swt.dnd.*;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
import at.asit.pdfover.gui.workflow.Workflow;
@@ -37,7 +43,40 @@ import at.asit.pdfover.gui.workflow.Workflow;
*
*
*/
-public class DataSourceSelectComposite extends Composite implements StateComposite {
+public class DataSourceSelectComposite extends Composite implements
+ StateComposite {
+
+ /**
+ *
+ */
+ private final class FileBrowseDialog implements SelectionListener {
+ /**
+ *
+ */
+ public FileBrowseDialog() {
+ // Nothing to do here
+ }
+
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ FileDialog dialog = new FileDialog(DataSourceSelectComposite.this.getShell(), SWT.OPEN);
+ dialog.setFilterExtensions(new String[] {"*.pdf"});
+ dialog.setFilterNames(new String[] {"PDF Dateien"});
+ String fileName = dialog.open();
+ File file = null;
+ if (fileName != null) {
+ file = new File(fileName);
+ if(file.exists()) {
+ DataSourceSelectComposite.this.setSelected(file);
+ }
+ }
+ }
+
+ @Override
+ public void widgetDefaultSelected(SelectionEvent e) {
+ // Nothing to do here
+ }
+ }
/**
* SFL4J Logger instance
@@ -46,22 +85,46 @@ public class DataSourceSelectComposite extends Composite implements StateComposi
.getLogger(DataSourceSelectComposite.class);
private Workflow workflow;
-
+
+ /**
+ * Set this value through the setter method!!
+ */
+ private File selected = null;
+
+ /**
+ * Sets the selected file and calls update to the workflow
+ * @param selected
+ */
+ protected void setSelected(File selected) {
+ this.selected = selected;
+ workflow.update();
+ }
+
+ /**
+ * Gets the selected file
+ *
+ * @return the selected file
+ */
+ public File getSelected() {
+ return this.selected;
+ }
+
/**
* Create the composite.
+ *
* @param parent
* @param style
- * @param flow
+ * @param flow
*/
public DataSourceSelectComposite(Composite parent, int style, Workflow flow) {
super(parent, style);
-
+
this.workflow = flow;
-
+
this.setLayout(new FormLayout());
-
+
Color back = new Color(Display.getCurrent(), 77, 190, 250);
-
+
this.drop_area = new Composite(this, SWT.RESIZE | SWT.BORDER_DASH);
FormData fd_drop_area = new FormData();
fd_drop_area.left = new FormAttachment(0, 0);
@@ -71,8 +134,89 @@ public class DataSourceSelectComposite extends Composite implements StateComposi
this.drop_area.setLayoutData(fd_drop_area);
this.drop_area.setLayout(new FormLayout());
this.drop_area.setBackground(back);
-
- final Label lbl_drag = new Label(this.drop_area, SWT.NONE | SWT.RESIZE );
+
+ DropTarget dnd_target = new DropTarget(this.drop_area, DND.DROP_DEFAULT
+ | DND.DROP_COPY);
+ final FileTransfer fileTransfer = FileTransfer.getInstance();
+ Transfer[] types = new Transfer[] { fileTransfer };
+ dnd_target.setTransfer(types);
+
+ dnd_target.addDropListener(new DropTargetListener() {
+
+ @Override
+ public void dropAccept(DropTargetEvent event) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void drop(DropTargetEvent event) {
+ if (fileTransfer.isSupportedType(event.currentDataType)){
+ String[] files = (String[])event.data;
+ if(files.length > 0) {
+ // Only taking first file ...
+ File file = new File(files[0]);
+ if(!file.exists())
+ {
+ log.error("File: " + files[0] + " doesnot exists!");
+ return;
+ }
+ DataSourceSelectComposite.this.setSelected(file);
+ }
+ }
+ }
+
+ @Override
+ public void dragOver(DropTargetEvent event) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void dragOperationChanged(DropTargetEvent event) {
+ if (event.detail == DND.DROP_DEFAULT) {
+ if ((event.operations & DND.DROP_COPY) != 0) {
+ event.detail = DND.DROP_COPY;
+ } else {
+ event.detail = DND.DROP_NONE;
+ }
+ }
+ }
+
+ @Override
+ public void dragLeave(DropTargetEvent event) {
+ // No need to do anything here...
+ }
+
+ @Override
+ public void dragEnter(DropTargetEvent event) {
+ if (event.detail == DND.DROP_DEFAULT) {
+ if ((event.operations & DND.DROP_COPY) != 0) {
+ event.detail = DND.DROP_COPY;
+ } else {
+ event.detail = DND.DROP_NONE;
+ }
+ }
+ // Only drop one item!
+ if(event.dataTypes.length > 1) {
+ event.detail = DND.DROP_NONE;
+ return;
+ }
+ // will accept text but prefer to have files dropped
+ for (int i = 0; i < event.dataTypes.length; i++) {
+ if (fileTransfer.isSupportedType(event.dataTypes[i])) {
+ event.currentDataType = event.dataTypes[i];
+ // files should only be copied
+ if (event.detail != DND.DROP_COPY) {
+ event.detail = DND.DROP_NONE;
+ }
+ break;
+ }
+ }
+ }
+ });
+
+ final Label lbl_drag = new Label(this.drop_area, SWT.NONE | SWT.RESIZE);
FormData fd_lbl_drag = new FormData();
fd_lbl_drag.left = new FormAttachment(5, 5);
fd_lbl_drag.right = new FormAttachment(100, -5);
@@ -81,13 +225,12 @@ public class DataSourceSelectComposite extends Composite implements StateComposi
lbl_drag.setLayoutData(fd_lbl_drag);
FontData[] fD = lbl_drag.getFont().getFontData();
fD[0].setHeight(18);
- lbl_drag.setFont( new Font(Display.getCurrent(),fD[0]));
+ lbl_drag.setFont(new Font(Display.getCurrent(), fD[0]));
lbl_drag.setText("Drag and Drop");
lbl_drag.setAlignment(SWT.CENTER);
lbl_drag.setBackground(back);
-
-
- Button btn_open = new Button(this.drop_area, SWT.NATIVE | SWT.RESIZE );
+
+ Button btn_open = new Button(this.drop_area, SWT.NATIVE | SWT.RESIZE);
btn_open.setText("Choose file ...");
Point size = btn_open.computeSize(SWT.DEFAULT, SWT.DEFAULT);
FormData fd_btn_open = new FormData();
@@ -97,28 +240,25 @@ public class DataSourceSelectComposite extends Composite implements StateComposi
fd_btn_open.bottom = new FormAttachment(100, -5);
btn_open.setLayoutData(fd_btn_open);
btn_open.setBackground(back);
-
+ btn_open.addSelectionListener(new FileBrowseDialog());
this.drop_area.pack();
-
- /*Button btn = new Button(this, SWT.NATIVE);
- btn.setBounds(50, 20, 100, 50);
- btn.setText("Click Me");
- btn.addSelectionListener(new SelectionListener() {
-
- @Override
- public void widgetSelected(SelectionEvent arg0) {
- DataSourceSelectComposite.this.setPress(true);
- DataSourceSelectComposite.this.workflow.update();
- }
-
- @Override
- public void widgetDefaultSelected(SelectionEvent arg0) {
- // TODO Auto-generated method stub
-
- }
- });*/
+
+ /*
+ * Button btn = new Button(this, SWT.NATIVE); btn.setBounds(50, 20, 100,
+ * 50); btn.setText("Click Me"); btn.addSelectionListener(new
+ * SelectionListener() {
+ *
+ * @Override public void widgetSelected(SelectionEvent arg0) {
+ * DataSourceSelectComposite.this.setPress(true);
+ * DataSourceSelectComposite.this.workflow.update(); }
+ *
+ * @Override public void widgetDefaultSelected(SelectionEvent arg0) { //
+ * TODO Auto-generated method stub
+ *
+ * } });
+ */
}
-
+
private boolean press = false;
private Composite drop_area;
@@ -136,13 +276,16 @@ public class DataSourceSelectComposite extends Composite implements StateComposi
}
/**
- * @param press the press to set
+ * @param press
+ * the press to set
*/
public void setPress(boolean press) {
this.press = press;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see at.asit.pdfover.gui.components.StateComposite#doLayout()
*/
@Override