How to close a window in GWT - gwt

I was required to put a "Done" button in a GWT Composite (despite already having the close icon), it should simply close the window upon clicking. Unfortunately, I can't find a .close() method to implement it. How can it be done?
I have a UserDialog class that contains a Composite component, which I named UserComposite. UserDialog extends to CustomDialogBox, which extends to DialogBox class:
public class UserDialog extends CustomDialogBox {
private UserComposite c = new UserComposite();
// more codes here
private FlowPanel getFlowPanel() {
if (p instanceof Panel && c instanceof Composite) {
p.setSize(WIDTH, HEIGHT);
p.add(c);
}
return p;
}
}
and then this is my UserComposite
public class UserComposite extends Composite {
// codes here
#UiHandler("doneButton")
void onDoneButtonClick(ClickEvent event) {
this.removeFromParent();
}
}
I tried removeFromParent() but the UserComposite was only removed from parent which resulted to an empty DialogBox.

You need to hide the dialog, not the composite. One way to do this is to pass a reference to the dialog box to the UserComposite constructor, and then use that reference to call hide() on the dialog. It could be something along these lines:
public class UserDialog extends CustomDialogBox {
private UserComposite c = new UserComposite(this);
...
}
public class UserComposite extends Composite {
private DialogBox parentDialog;
public UserComposite(DialogBox parentDialog) {
this.parentDialog = parentDialog;
}
#UiHandler("doneButton")
void onDoneButtonClick(ClickEvent event) {
parentDialog.hide();
}
}

#Mr. Xymon, By window if you mean instance of PopupPanel or instance of any subclass of PopupPanel, you can use the following :
popupPanel.hide();

Related

GWT 2.6: Setting interface type instance variable from anonymous inner class

I'm working on a GWT 2.6 application with a PresenterClass that drives a view that contains a FlexTable. I've connected drag-drop event handlers in the presenter to the FlexTable. Cells can either be vacant or contain an entry. When a drag operation begins, I determine which type of cell is being dragged, and from within the DragStartHandler I set the draggable variable in the outer class to the type that is being dragged. But when the DragOverHandler is called, the draggable instance field in the outer class is null.
What am I not seeing or understanding? I thought anonymous inner classes had access to outer class fields.
public class PresenterClass {
private class CellId {
int row;
int column;
}
private interface Draggable {
void beginDrag(CellId cellId);
void canDrop(CellId cellId);
void drop(CellId cellId);
}
private class VacantSelectedCells implements Draggable {
.
.
.
}
private final VacantSelectedCells selectedCells = new SelectedCells();
private class DraggableEntry implements Draggable {
.
.
.
}
private Draggable draggable;
private final DragStartHandler dragStartHandler = new DragStartHandler() {
#override
public void onDragStart(DragStartEvent event) {
// Determine what is being dragged
if (selected cells)
draggable = selectedCells;
else
draggable = new DraggableEntry();
}
private final DragOverHandler dragOverHandler = new DragOverHandler() {
#override
public void onDragOver(DragOverEvent event) {
// find cellId for event
if (draggable.canDrop(cellId))
doSomething();
}
}

TableCell focusedProperty listener not called when inheriting from TextFieldTableCell

I want to create a Cell factory that returns a TableCell that behaves exactly like TextFieldTableCell, with the following difference: When it loses focus, it commits the changes.
My code is very simple:
public final class TextFieldCellFactory<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {
#Override
public TableCell<S, T> call(TableColumn<S, T> p) {
class EditingCell extends TextFieldTableCell {
public EditingCell() {
super();
setConverter(new DefaultStringConverter());
focusedProperty().addListener(new ChangeListener() {
#Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
System.out.println("changed!");
System.out.println("getText() = " + getText());
System.out.println("textProperty() = " + textProperty().get());
System.out.println("getItem = " + getItem());
}
});
}
#Override
public void startEdit() {
super.startEdit();
}
#Override
public void cancelEdit() {
super.cancelEdit();
}
}
return new EditingCell();
}
}
As you see I add a change listener in the focusedProperty. The problem is that the change method is not called (nothing is printed).
How can I get the desired behaviour? Thank you.
Basically, you have to register the listener with the textField's (not the cell's) focusedProperty. As the textfield is a private field of super, it's not directly accessible - you have to look it up once after it was added to the cell. That's when an edit was started for the first time:
private TextField myTextField;
#Override
public void startEdit() {
super.startEdit();
if (isEditing() && myTextField == null) {
// most simple case, assuming that there is no graphic other than the field
// TBD: implement the general case: walk the tree and find the field
myTextField = (TextField) getGraphic();
myTextField.focusedProperty().addListener((e, old, nvalue) -> {
if (!nvalue) {
T edited = getConverter().fromString(myTextField.getText());
commitEdit(edited);
}
});
}
}
Some notes:
this is a workaround around an open issue (vote for it!)
since jdk8, it's not entirely functional: won't commit if you click somewhere else inside the table
a recent answer uses a binding approach which might or not be fully functional (didn't test)

Eclipse plugin - is there any editable TreeSelection class

I am looking for a solution to make this tree selection editable in the package explorer view itself.
the idea
for example- if we click rename on any class in package explorer, it will prompt a new window to rename. This functionality is same for any class that implement TreeSelection Class.
But the Solution i am looking for is - when rename is invoked, the rename option is shown at the tree itself (like we have in Windows Explorer view)
any suggestion on how to attain this behavior on eclipse.
You don't need to have some special editable selection, you just want to make the tree editable. For this you use EditingSupport, like this (adapted from http://www.vogella.com/articles/EclipseJFaceTableAdvanced/article.html#jfacetable_editor):
public class NameEditingSupport extends EditingSupport {
private final TreeViewer viewer;
public FirstNameEditingSupport(TreeViewer viewer) {
super(viewer);
this.viewer = viewer;
}
#Override
protected CellEditor getCellEditor(Object element) {
return new TextCellEditor(viewer.getTree());
}
#Override
protected boolean canEdit(Object element) {
return true;
}
#Override
protected Object getValue(Object element) {
// return the name
}
#Override
protected void setValue(Object element, Object value) {
// update the name of your object
viewer.update(element, null);
}
}
// in the code creating the tree
treeViewer.setEditingSupport(new NameEditingSupport(treeViewer));

GWT - How to associate a business object to a widget

I'd like a way to easily tie a widget back to the business object it is rendering. So when the user interacts with a widget I can easily determine the business object holding the data for that widget.
For example, if we imagine a calendar widget that we're going to implement with an AbsolutePanel. For each appt object we'll add a label to the calendar. Then when a user clicks on a label he can update the appt. So I need to know which appt object that label refers to.
For instance, if we look at the following code; if the label for an appointment receives a click, how can I find out to which appt it represented ? The only solution I can see is to create a ApptLabel sub-class for Label which would hold a reference to its appt. This is fine, but the example illustrates a more general need which is to associate widgets with data objects; however this would mean that every object that has a presence in a view needs to subclass a widget. that seems heavy - I expected to find something in the framework e.g. a string property in a widget that I can set to an object key
other approaches I tried; maintaining a map of Map -- this didnt work as the label object I create doesnt appear to be the same (in terms of the Object.equals which I guess is what HashMap uses)
class WidgetCalendar extends Composite {
AbsolutePanel m_panel = new AbsolutePanel();
m_panel.setStylePrimaryName("calendar");
m_panel.setPixelSize(width, height);
public WidgetCalendar(ArrayList<BomAppt> appts) {
initWidget(m_panel);
for (BomAppt a : appts) {
Label l = new Label();
l.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// how do I know my BomAppt in here ?
}
m_panel.add(l, someX, someY);
}
}
}
Ideally I can do something like this
class WidgetCalendar extends Composite {
AbsolutePanel m_panel = new AbsolutePanel();
m_panel.setStylePrimaryName("calendar");
m_panel.setPixelSize(width, height);
public WidgetCalendar(ArrayList<BomAppt> appts) {
initWidget(m_panel);
for (BomAppt a : appts) {
Label l = new Label();
l.setItemData(a.getUniqueId());
l.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
BomAppt a = BomAppt.getApptWithId(e.getItemData())
}
}
m_panel.add(l, someX, someY);
}
}
}
This is the solution where I create a subclass, this seems heavy to me and I'd prefer something simpler
class ApptLabel extends Label {
public ApptLabel(BomAppt a) {
m_a = a;
this.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
m_a.doSomething();
});
}
BomAppt m_a;
}
class WidgetCalendar extends Composite {
AbsolutePanel m_panel = new AbsolutePanel();
m_panel.setStylePrimaryName("calendar");
m_panel.setPixelSize(width, height);
public WidgetCalendar(ArrayList<BomAppt> appts) {
initWidget(m_panel);
for (BomAppt a : appts) {
BomLabel l = new BomLabel();
l.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// how do I know my BomAppt in here ?
}
m_panel.add(l, someX, someY);
}
}
}
For instance, if we look at the following code; if the label for an
appointment receives a click, how can I find out to which appt it
represented ?
By using Composite pattern you can find out which widget was clicked, initially you should create your own custom Appointment widget which is responsible for drawing one appointment. And in you Appointment widget you can have a set of other widgets, in your case, for Label add click handler. Once user clicks that label, you can execute business logic with its data and you can represent data.
public class Appointment extends Composite {
private AppointmentDetails data;
public Appointment(AppointmentDetails data){
draw(data);
}
private void draw(AppointmentDetails data){
Label label = new Label();
label.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// do your business logic with this AppointmentDetails
}
});
}
}
After that you should have one Calendar widget which contains several Appointments.
Keep in your mind: your classes each serve a single, very clearly defined purpose, separated from other classes with other clearly defined purposes.

GWT Widget.addHandler

I am trying to utilize Widget.addHandler(). However, the handler never gets called. Below is my sample code. What do I need to change to fix this?
My Handler Implementation:
public class CustomMouseMoveHandler
extends GwtEvent.Type<MouseMoveHandler>
implements MouseMoveHandler
{
#Override
public void onMouseMove(MouseMoveEvent event) {
System.out.println("----> onMouseMove.");
}
}
My EntryPoint.OnModuleLoad():
ContentPanel cp = new ContentPanel();
cp.setHeaderVisible(false);
cp.setHeight(com.google.gwt.user.client.Window.getClientHeight());
CustomMouseMoveHandler handler = new CustomMouseMoveHandler();
cp.addHandler(handler, handler);
RootPanel.get().add(cp);
/////
Added on 7/1/2011.
The following complete GWT simple code does not work either (with Jason's hint applied). Please help me out. Thanks
package tut.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class GwtHandler implements EntryPoint, MouseMoveHandler {
/**
* This is the entry point method.
*/
public void onModuleLoad() {
TextArea comp = new TextArea();
comp.setSize("200px", "200px");
comp.setText("Testing Text");
comp.addHandler(this, MouseMoveEvent.getType());
RootPanel.get().add(comp);
}
#Override
public void onMouseMove(MouseMoveEvent event) {
com.google.gwt.user.client.Window.alert("onMouseMove");
}
}
GwtEvent.Type is used to dispatch events based on an event specific and unique object (object equality - == - is used to match event types). Passing your CustomMouseMoveHandler as the Type to addHandler indicates an event type other than that used for MouseMoveEvents (Indeed in this case every CustomMouseMoveHandler would be assigned to a different event Type since each object is different).
Instead of extending GwtEvent.Type<MouseMoveHandler> in your handler you need to get the event Type from MouseMoveEvent itself (using the static getType() method).
Don't extend GwtEvent.Type in your CustomMouseMoveHandler:
public class CustomMouseMoveHandler
implements MouseMoveHandler
{
...
}
And to add the handler:
cp.addDomHandler(handler, MouseMoveEvent.getType());
DomEvents have to be registered using addDomHandler, or you have to sinkEvents for their event type. addDomHandler is a shortcut for sinkEvents+addHandler.
Here's how I solved my problem. I wanted to add handlers to a NumberLabel. This is what worked:
final NumberLabel<Long> label = new NumberLabel<Long>();
label.setValue(2000l);
label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
MouseOverHandler handler = new MouseOverHandler() {
public void onMouseOver(MouseOverEvent event) {
System.out.println("mouse over");
}
};
Widget widget = label.asWidget();
widget.addDomHandler(handler, MouseOverEvent.getType());
Treating is as a Widget did the trick.
By the way, System.out.println worked.