GWT Drag and Drop File Upload not working - gwt

So I have implemented a very simple drag and drop file upload widget. Basically my widget is a vertical panel with a couple of labels and a button inside. The user can either drag file into vertical panel or click button and browse for file.
My problem is that when I drag a file into the vertical panel it fires the DragLeaveEvent every time I drag the item over the space that the labels or button occupies. I want it to know that the item is in the vertical panel even when it is on top of the label or button. Im sure I am missing something simple. I provide the drag functionality by adding these dom handlers to the vertical panel:
addDomHandler(new DragEnterHandler() {
#Override
public void onDragEnter(DragEnterEvent event) {
System.out.println("drag enter");
highlight(true);
}
}, DragEnterEvent.getType());
addDomHandler(new DragLeaveHandler() {
#Override
public void onDragLeave(DragLeaveEvent event) {
System.out.println("drag leave");
highlight(false);
}
}, DragLeaveEvent.getType());
addDomHandler(new DragOverHandler() {
#Override
public void onDragOver(DragOverEvent event) {
}
}, DragOverEvent.getType());
addDomHandler(new DropHandler() {
#Override
public void onDrop(DropEvent event) {
System.out.println("drop");
// stop default behaviour
event.preventDefault();
event.stopPropagation();
// starts the fetching, reading and callbacks
if (fileUploadHandler != null) {
handleFiles(event.getDataTransfer(), fileUploadHandler);
}
highlight(false);
}
}, DropEvent.getType());

Check that the event target is a child (or grand child) of your panel, or in this case maybe rather whether the event target is exactly your panel's element:
if (verticalPanel.getElement().isOrHasChild(Node.as(event.getNativeEvent().getEventTarget()))) {
// within the panel (possibly on a child)
}
if (verticalPanel.getElement() == Node.as(event.getNativeEvent().getEventTarget())) {
// targetting exactly the panel (e.g. leaving the panel, not one of its children)
}

Through lots of research I have come to the only solution I could find. I set highlight to true in the dragover handler instead of drag enter.
panel.addDomHandler(new DragEnterHandler() {
#Override
public void onDragEnter(DragEnterEvent event) {
}
}, DragEnterEvent.getType());
panel.addDomHandler(new DragLeaveHandler() {
#Override
public void onDragLeave(DragLeaveEvent event) {
highlight(false);
}
}, DragLeaveEvent.getType());
panel.addDomHandler(new DragOverHandler() {
#Override
public void onDragOver(DragOverEvent event) {
highlight(true);
}
}, DragOverEvent.getType());
panel.addDomHandler(new DropHandler() {
#Override
public void onDrop(DropEvent event) {
// stop default behaviour
event.preventDefault();
event.stopPropagation();
// starts the fetching, reading and callbacks
handleFiles(event.getDataTransfer());
highlight(false);
}
}, DropEvent.getType());

I copy pasted your code, but also added a:
RootPanel.get().addHandler(dropHandler, DropEvent.getType());
My drophandler looks like this:
DropHandler dropHandler = new DropHandler() {
#Override
public void onDrop(DropEvent event) {
handleFiles(event.getDataTransfer(), new FileUploadHandler() {
#Override
public TYPE specifyFileType() {
return TYPE.BINARY;
}
#Override
public void handleFileContent(String fileName, String fileContent) {
// do stuff with filename and content
}
#Override
public boolean checkFileName(String fileName) {
return true;
}
});
event.preventDefault();
event.stopPropagation();
}
};
and the file-upload interface:
public interface FileUploadHandler {
static public enum TYPE {
TEXT, BINARY, DATAURL
};
// check the filename and extension and return true if you are happy with
// proceeding
// returnning false will prevent the file from being read
boolean checkFileName(String fileName);
// tell the method to use to read this file
TYPE specifyFileType();
// do your stuff here, eg upload to a server
void handleFileContent(String fileName, String fileContent);
}
and the handle files func: (note you will have to change classpath to the FileUploadHandler-interface)
// native method to make use of the HTML5 file API functionality
private final native void handleFiles(JavaScriptObject dataTransfer, FileUploadHandler fileUploadHandler) /*-{
var files = dataTransfer.files;
var i;
var file;
var reader = new FileReader();
for (i = 0; i < files.length; i++) {
file = files[i];
if (fileUploadHandler.#<classpath_to>.FileUploadHandler::checkFileName(Ljava/lang/String;)(file.name)) {
var type = fileUploadHandler.#<classpath_to>.FileUploadHandler::specifyFileType()();
reader.onload = function(e) {
fileUploadHandler.#<classpath_to>.FileUploadHandler::handleFileContent(Ljava/lang/String;Ljava/lang/String;)(file.name, e.target.result);
}
if (type == "TEXT") {
reader.readAsText(file);
} else if (type == "BINARY") {
reader.readAsBinaryString(file);
} else if (type == "DATAURL") {
reader.readAsDataURL(file);
// not supported
} else if (type == "ARRAYBUFFER") {
reader.readAsArrayBuffer(file);
} else {
}
}
}
}-*/;

Related

Eclipse EditorPart save on partDeactivated

My problem is that I have a custom application, using EditorParts, which are persisted to a database. The user can open several Editors, and switch between them. I need to ask the user to save any unsaved changes in an Editor, before switching to the next Editor (or else close it).
I have created an IPartListener2, and I receive the partDeactivated notification. If isDirty()==true, I bring up a MessageDialog asking to save or not; because I want to call editor.doSave().
My problem is that does not work. I never see the MessageDialog, because another partDeactivated fires. I guess, this is caused by the MessageDialog over the Editor.
I have researched How to listen to lose focus event of a part in Eclipse E4 RCP?, but that did not help me.
thanks to help a e4 beginner
public class DatasetAttachmentEditor {
... // code here
#Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
... // code here
site.getPage().addPartListener(new EditorsPartListener(this));
}
}
public class EditorsPartListener implements IPartListener2 {
private IEditorPart editor;
public EditorsPartListener(IEditorPart editor) {
this.editor = editor;
}
#Override
public void partClosed(IWorkbenchPartReference partRef) {
if (partRef.getPage().getActiveEditor().getClass().getName().equals(editor.getClass().getName())) {
partRef.getPage().removePartListener(this);
}
}
#Override
public void partDeactivated(IWorkbenchPartReference partRef) {
if (!partRef.getClass().getName().equals("org.eclipse.ui.internal.EditorReference")) {
System.out.println("partDeactivated: not a Editor="+partRef.getClass().getName());
return;
}
if (!editor.isDirty()) {
// if the editor is not dirty - do nothing
return;
}
// ask if to save
int choice = EditorPartSaveDialog(partRef.getPage().getActiveEditor());
if(choice == MessageDialog.OK) {
// save the Editor
try {
ProgressMonitorDialog progress = new ProgressMonitorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
progress.setCancelable(false);
progress.run(false, false, new IRunnableWithProgress() {
#Override
public void run(IProgressMonitor monitor) {
// do the save
editor.doSave(monitor);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
else {
// don't save: just close it
partRef.getPage().closeEditor(editor, false);
}
}
#Override
public void partActivated(IWorkbenchPartReference partRef) {
}
#Override
public void partBroughtToTop(IWorkbenchPartReference partRef) {
}
#Override
public void partOpened(IWorkbenchPartReference partRef) {
}
#Override
public void partHidden(IWorkbenchPartReference partRef) {
}
#Override
public void partVisible(IWorkbenchPartReference partRef) {
}
#Override
public void partInputChanged(IWorkbenchPartReference partRef) {
}
/**
* Asks the user to Save changes
* #param editor
* #return MessageDialog.OK to save, MessageDialog.CANCEL otherwise
*/
private int EditorPartSaveDialog(IEditorPart editor) {
// If save confirmation is required ..
String message = NLS.bind("''{0}'' has been modified. Save changes?", LegacyActionTools.escapeMnemonics(editor.getTitle()));
// Show a dialog.
MessageDialog d = new MessageDialog(
Display.getCurrent().getActiveShell(),
"Save Editor", null, message,
MessageDialog.QUESTION,
0,
"Save",// MessageDialog 0x0 (OK)
"Don't Save: close"// MessageDialog 0x1 (CANCEL)
)
return d.open();
}
}
You probably need to run your code after the deactivate event has finished. You can do this using Display.asyncExec.
Something like:
#Override
public void partDeactivated(IWorkbenchPartReference partRef) {
if (!partRef.getClass().getName().equals("org.eclipse.ui.internal.EditorReference")) {
System.out.println("partDeactivated: not a Editor="+partRef.getClass().getName());
return;
}
if (!editor.isDirty()) {
// if the editor is not dirty - do nothing
return;
}
Display.getDefault().asyncExec(() ->
{
// TODO the rest of your deactivate code goes here
});
}
(Above code assumes you are using Java 8 or later)
This is 3.x compatibility mode code, not e4.
I have found a great solution, using the suggestions above and Enumerating all my Eclipse editors?
I am checking all editors first, then all persisted editors - skipping itself and the persisted objects.
Thanks for your comments!
public class ConceptAcronymValidator implements IValidator {
private ConceptInstanceEditor myEditor;
public ConceptAcronymValidator(ConceptInstanceEditor editor) {
super();
this.myEditor = editor;
}
#Override
public IStatus validate(Object value) {
// check all Editors
for (IEditorReference editorRef: PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences()) {
IEditorPart editor = editorRef.getEditor(false);
if (editor != null) {
// don't check our own Editor
if (!editor.equals(myEditor)) {
ConceptInstanceEditor conceptEditor = (ConceptInstanceEditor)editor;
if (conceptEditor.getTxtAcronym().equals(value.toString())) {
return ValidationStatus.error("This Concept is already used by Editor <"+
conceptEditor.getConceptModel().getName().getValue(MultilingualString.EN)+
">");
}
}
}
}
// check all persisted Concepts
List<Concept> concepts = ReferenceServiceFactory.getService().getConcepts();
for (Concept concept: concepts) {
Concept myConcept = (Concept) myEditor.getConceptModel().getInstance();
// check if a new Editor
if (myConcept == null) {
if (concept.getAcronym().equals(value.toString())) {
return ValidationStatus.error("This Concept is already used by <"+
concept.getName().getValue(MultilingualString.EN)+
">");
}
}
else {
// don't check own Instance
if (!concept.equals(myConcept)) {
if (concept.getAcronym().equals(value.toString())) {
return ValidationStatus.error("This Concept is already used by <"+
concept.getName().getValue(MultilingualString.EN)+
">");
}
}
}
}
return Status.OK_STATUS;
}
}

Why am I not getting any events for my anchor element?

For a quite simple element
public static class ToolHtml extends Anchor {
private Long toolId;
public ToolHtml(ToolDetails toolDetails) {
this.toolId = toolDetails.getToolId();
this.setText(toolDetails.getName());
Style style = this.getElement().getStyle();
style.setColor("orange");
this.getElement().setId(DOM.createUniqueId());
}
public Long getToolId() {
return this.toolId;
}
}
I am trying to add some event handlers. I am interested in the DragStartEvent but not even the ClickEvent seems to work and I simply can't figure out why:
public void addOfferItem(ToolHtml toolHtml) {
Set<ToolHtml> toolHtmlSet = this.toolIdToToolHtml.get(toolHtml.getToolId());
if (toolHtmlSet == null) {
toolHtmlSet = new HashSet<>();
this.toolIdToToolHtml.put(toolHtml.getToolId(), toolHtmlSet);
}
toolHtml.getElement().setAttribute("contenteditable", "false");
toolHtml.getElement().setAttribute("draggable", "true");
toolHtml.addBitlessDomHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
LOGGER.fine("Click");
LOGGER.fine("Click");
LOGGER.fine("Click");
}
}, ClickEvent.getType());
toolHtml.addDomHandler((DragStartEvent event) -> {
LOGGER.fine("HANDLER DRAG START");
},
DragStartEvent.getType());
toolHtml.addDomHandler((DragStartEvent event) -> {
LOGGER.fine("DOM DRAG START");
},
DragStartEvent.getType());
toolHtml.addBitlessDomHandler((DragStartEvent event) -> {
LOGGER.fine("DRAG START");
},
DragStartEvent.getType());
toolHtmlSet.add(toolHtml);
// Attach/append to DOM
this.getElement().appendChild(toolHtml.getElement());
}
Does anybody know what the issue is here?
You append an element, not a Widget to which your handlers have been attached. You need to add a widget to a widget for all the functionality to work.
NB: Instead of toolHtml.addBitlessDomHandler you need to use
toolHtml.addClickHandler

CellTable click swallowed

I've an combo box which is composed of a text field and a popup with a CellTable showing the suggestion items. The text field has a change handler that updates the CellTable's selection.
When typing a character and clicking an already selected suggestion, the first click is swallowed. The second click works and triggers the selection via the CellTable.addDomHandler(...).
Any idea why first click is swallowed?
Example code:
private static class SuggestFieldTextAndPopupSandbox extends SimplePanel {
private final TextField mText;
private CellTable<Handle<String>> mTable;
private SingleSelectionModel<Handle<String>> mTableSelection;
private SingleSelectionModel<Handle<String>> mSelection;
private ProvidesKey<Handle<String>> mKeyProvider = new SimpleKeyProvider<Handle<String>>();
private PopupPanel mPopup;
private List<Handle<String>> mData;
public SuggestFieldTextAndPopupSandbox() {
mData = Lists.newArrayList(new Handle<String>("AAA"), new Handle<String>("AAB"), new Handle<String>("ABB"));
mSelection = new SingleSelectionModel<Handle<String>>();
mText = new TextField();
mText.addKeyPressHandler(new KeyPressHandler() {
#Override
public void onKeyPress(KeyPressEvent pEvent) {
mPopup.showRelativeTo(mText);
}
});
mText.addBlurHandler(new BlurHandler() {
#Override
public void onBlur(BlurEvent pEvent) {
mTableSelection.setSelected(startsWith(mText.getValue()), true);
}
});
mText.addChangeHandler(new ChangeHandler() {
#Override
public void onChange(ChangeEvent pEvent) {
mText.setText(mText.getText().toUpperCase());
}
});
mTable = new CellTable<Handle<String>>(0, GWT.<TableResources>create(TableResources.class));
mTable.setTableLayoutFixed(false);
mTableSelection = new SingleSelectionModel<Handle<String>>(mKeyProvider);
mTable.setSelectionModel(mTableSelection);
mTable.addDomHandler(new ClickHandler() {
#Override
public void onClick(final ClickEvent pEvent) {
Scheduler.get().scheduleFinally(new ScheduledCommand() {
#Override
public void execute() {
mSelection.setSelected(mTableSelection.getSelectedObject(), true);
mText.setFocus(true);
mPopup.hide();
}
});
}
}, ClickEvent.getType());
mTable.addColumn(new TextColumn<Handle<String>>() {
#Override
public String getValue(Handle<String> pObject) {
return pObject.get();
}
});
mTable.setRowData(mData);
mPopup = new PopupPanel();
mPopup.setAutoHideEnabled(true);
mPopup.setWidget(mTable);
mPopup.setWidth("200px");
mPopup.setHeight("200px");
VerticalPanel p = new VerticalPanel();
p.add(mText);
setWidget(p);
}
private Handle<String> startsWith(final String pValue) {
final String val = nullToEmpty(pValue).toLowerCase();
int i = 0;
for (Handle<String> item : mData) {
String value = item.get();
if (value != null && value.toLowerCase().startsWith(val)) {
return item;
}
i++;
}
return null;
}
}
I reproduced your issue and here is the problem:
when you click on the suggestions the following is happening:
The text field is loosing focus which causes the corresponding ChangeEvent to be dealt with followed by the BlurEvent.
The click causes the popup to get the focus now which is why it is swallowed.
If you remove the ChangeHandler and the BlurHandler of the text field the issue disappears. But I think I found another solution
Try replacing the DOM handler of the mTable with a selection handler relative to the mTableSelection as follows:
mTableSelection.addSelectionChangeHandler(new Handler(){
#Override
public void onSelectionChange(SelectionChangeEvent event) {
Scheduler.get().scheduleFinally(new ScheduledCommand() {
#Override
public void execute() {
mSelection.setSelected(mTableSelection.getSelectedObject(), true);
mText.setFocus(true);
mPopup.hide();
}
});
}
});
Found a way how to properly solve this.
Skipping the blur handler when user hovers the suggestion list area seemed to fix that issue, at least from the tests that were done didn't see any more issues.
This was necessary because just before the user clicks a suggestion item, the text is blurred and it fires a selection change. This in turn cancels the selection made when user clicks an item.

Give the delete button a confirmation prompt

Is there a way to give the user a prompt window popup when clicking the remove field button?
enabling the remove button :
setCanRemoveRecords(true);
When I click the red remove button, I want a confirmation box ask me if I want to delete it, yes or no. What should I use to bring that up?
Should I be adding something into
#Override
public void removeData(Record group)
{
...
}
Here are the options:
Use addCellClickHandler on ListGrid and perform operation based on cell no
Add addRecordClickHandler on ListGridField itself that is used for delete icon
I prefer last option.
Sample code:
final ListGrid countryGrid = new ListGrid();
...
countryGrid.setWarnOnRemoval(true);
countryGrid.setCanRemoveRecords(true);
ListGridField ls = new ListGridField();
countryGrid.setRemoveFieldProperties(ls);
ls.setHoverCustomizer(new HoverCustomizer() {
#Override
public String hoverHTML(Object value, ListGridRecord record, int rowNum, int colNum) {
// System.out.println(colNum);
return "click here to delete this record";
}
});
ls.addRecordClickHandler(new RecordClickHandler() {
#Override
public void onRecordClick(final RecordClickEvent event) {
SC.confirm("Are you sure?", new BooleanCallback() {
#Override
public void execute(Boolean value) {
if (value == null || !value) {
event.cancel();
}
}
});
}
});
/*countryGrid.addCellClickHandler(new CellClickHandler() {
#Override
public void onCellClick(final CellClickEvent event) {
// column number having delete icon
// System.out.println(event.getColNum());
if (event.getColNum() == 3) {
SC.confirm("Are you sure", new BooleanCallback() {
#Override
public void execute(Boolean value) {
if (value == null || !value) {
event.cancel();
}
}
});
}
}
});*/
You can use the following methods:
ListGrid#setWarnOnRemoval for showing the warning message and
ListGrid#setWarnOnRemovalMessage for setting a customized message.
Refer documentation.

GWT: How to disable the anchor link event when clicked

I want to disable the anchor link event when it clicked one time. I used anchor.setenabled(false) but nothing happend. When I click the same button again the event e is true. I want false at that time.
public void onCellClick(GridPanel grid, int rowIndex, int colindex,EventObject e)
{
if(rowIndex==0 && colindex==2){
tomcatHandler = "Start";
anchorStart.setEnabled(false);
}else if(rowIndex==0 && colindex==3){
tomcatHandler = "Stop";
****anchorStop.setEnabled(false);
anchorStart.setEnabled(false);
anchorRestart.setEnabled(true);****
}else if(rowIndex==0 &&colindex==4){
tomcatHandler = "Restart";
anchorRestart.setEnabled(false);
}
AdminService.Util.getInstance().tomcat(tomcatHandler,new AsyncCallback<String>() {
#Override
public void onSuccess(String result) {
imageChangeEvent(result);
}
#Override
public void onFailure(Throwable caught) {
}
});}
Anchors in GWT have always had a problem with setEnabled() because HTML doesn't support such a property. A quick workaround is to create a new widget that subclasses GWT's Anchor, adding the following override:
#Override
public void onBrowserEvent(Event event) {
switch (DOM.eventGetType(event)) {
case Event.ONDBLCLICK:
case Event.ONFOCUS:
case Event.ONCLICK:
if (!isEnabled()) {
return;
}
break;
}
super.onBrowserEvent(event);
}
This disables the passing of the browser event to GWT's Anchor class (summarily disabling all related handlers) when the link is double clicked, focused or clicked and is in a disabled state.
Source
It doesn't seem to actually disable the anchor, but it does retain the status that has been set with anchor.setEnabled(), so just test that within your handler e.g.
myAnchor.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent evt) {
// write to filter and then call reload
if (((Anchor) evt.getSource()).isEnabled()) {
//do stuff
}
}
});