How to remove elements from GXT HtmlLayoutContainer - gwt

I have the following Widget that uses GXT HtmlLayoutContainer:
public class MyWidget implements IsWidget {
public interface MyWidgetLayout extends XTemplates {
#XTemplate(source = "my/stuff/resources/MyWidget.html")
SafeHtml getTemplate(List<MyData> myData);
}
public static final MyWidgetLayout LAYOUT = GWT.create(MyWidgetLayout .class);
private final List<MyData> myData;
private HtmlLayoutContainer container;
public TaskRegulationsContainer(List<MyData> myData) {
this.myData = myData;
}
#Override
public Widget asWidget() {
if (container == null) {
container = new HtmlLayoutContainer(LAYOUT.getTemplate(regulations, postfix));
if (null != regulations) {
for (MyData d : myData) {
MyOtherWidget w = new MyOtherWidget(d);
container.add(w, new AbstractHtmlLayoutContainer.HtmlData(".my-data-" + myData.getUniqueId()));
}
}
}
return container;
}
public void handleRemovedData(MyData removedData) {
// How can I remove the widget corresponding to removedData
// with the selector my-data-{uniqueId}?
}
}
with the following template:
<style>
</style>
<div>
<span>My Data:</span>
<div>
<tpl for="myData">
<div class="my-data-{uniqueId}"></div>
</tpl>
</div>
</div>
How can I remove individual myData list items from the UI after the template is rendered?

You can iterate over the children of the container, check if the class attribute contains "my-data-" + uniqueId and delete it.
Something like this should work:
public void handleRemovedData(MyData removedData) {
for (int i = 0; i < container.getChildCount(); i++) {
node = pParent.getChild(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node.cast();
if (element.getClassName().contains("my-data-" + removedData.getUniqueId())) {
container.removeChild(element);
}
}
}
}
Hope that helps.

Related

Weex customized native android component Rich Text height issue

I am creating a Richtext(WXTextView.java) view component in Weex by extending WXComponent. As Richtext component is not available in weex android sdk and "v-html" tag is also not supported in weex text component.
When my Richtext element is wrapped inside a div, the element is not visible. I have to manually add height to its parent div to make it visible.
<div class="parent">
<textView
ref="nativeTextView"
:style="{
color: '#ff6600',
fontSize: '40px',
maxLine: 2,
borderWidth: 2,
borderStyle: 'solid',
borderColor: 'green',
}"
text="ABCDEF"
/>
</div>
Giving height to the parent doesn't solve my purpose because text length is dynamic. I want to make this behavior just like default weex text component supporting rich text.
WXTextView.java
public class WXTextView extends WXComponent<TextView> {
private WXVContainer mContainer;
private int mHeight;
public WXTextView(WXSDKInstance instance, WXDomObject dom, WXVContainer parent) {
super(instance, dom, parent);
mContainer = parent;
}
#Override
protected TextView initComponentHostView(#NonNull Context context) {
TextView textView = new TextView(context);
setProperty(WXComponent.PROP_FIXED_SIZE, WXComponent.PROP_FS_WRAP_CONTENT);
textView.setIncludeFontPadding(false);
textView.setTextSize(WXText.sDEFAULT_SIZE);
return textView;
}
#WXComponentProp(name = "text")
public void setText(String text) {
getHostView().setText(Html.fromHtml(text));
updateUI();
}
private void updateUI() {
ViewGroup.LayoutParams params = mContainer.getRealView().getLayoutParams();
params.height = getHeight();
mContainer.getRealView().setLayoutParams(params);
mContainer.getRealView().invalidate();
}
#WXComponentProp(name = "ellipsize")
public void setEllipsize(String positionString) {
try {
int position = Integer.parseInt(positionString);
TextUtils.TruncateAt truncateType;
switch (position) {
case 0:
truncateType = TextUtils.TruncateAt.START;
break;
case 1:
truncateType = TextUtils.TruncateAt.MIDDLE;
break;
default:
truncateType = TextUtils.TruncateAt.END;
break;
}
getHostView().setEllipsize(truncateType);
updateUI();
} catch (Exception exception) {
exception.printStackTrace();
}
}
#WXComponentProp(name = "maxLine")
public void setMaxLine(String lineString) {
try {
int lineCount = Integer.parseInt(lineString);
getHostView().setMaxLines(lineCount);
updateUI();
} catch (Exception exception) {
exception.printStackTrace();
}
}
#JSMethod
public void getElementSpecs(JSCallback callback){
Log.d("nikhil", "android getHeight: " + getHostView().getHeight());
Map<String, Object> data = new HashMap<>();
data.put("width", getHostView().getMeasuredWidth());
data.put("height", getHostView().getMeasuredHeight());
data.put("positionX", getHostView().getX());
data.put("positionY", getHostView().getY());
callback.invoke(data);
}
#WXComponentProp(name = "color")
public void setColor(String color) {
getHostView().setTextColor(Color.parseColor(color));
}
#WXComponentProp(name = "fontSize")
public void setFontSize(String sizeString) {
int lastIndex = sizeString.indexOf("px");
if (lastIndex == -1) {
lastIndex = sizeString.length();
}
sizeString = sizeString.substring(0, lastIndex);
int size = Integer.parseInt(sizeString);
getHostView().setTextSize(size);
updateUI();
}
public int getHeight() {
getHostView().setText(getHostView().getText());
getHostView().setTextSize(TypedValue.COMPLEX_UNIT_PX, getHostView().getTextSize());
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(mContainer.getRealView().getLayoutParams().width,
View.MeasureSpec.AT_MOST);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
getHostView().measure(widthMeasureSpec, heightMeasureSpec);
mHeight = getHostView().getMeasuredHeight();
return mHeight;
}
}
Text is the most complicated component in Weex. In order to achieve similar behavior like weex text, you need extend WXDomObject as text extend WXTextDomObject, and implement your own text measure function.
In fact, I have written a richtext component in weex which will be released soon.

How can I configure the color of the feedback messages in Wicket Sessions?

The Problem
Hello,
I am trying to configure the color of Wickets feedback messages. I am currently maintaining a Wicket GUI (Wicket 7.6.1). It seems that Session.get().warn("Watch out!") prints a green warning box, annotated with the CSS class alert-success. I would like it to change its color to yellow.
What I got so far:
I found that Session.get().getApplication().getResourceSettings() gives me access to some resource settings, including a properties factory. But I don't know how to use it. Also, I have looked for markup files related to my Session but not found any.
Any help would be greatly appreciated!
You can create your custom feedback panel if you want.
CustomFeedBackPanel.html
<wicket:panel>
<div wicket:id="feedbackul">
<wicket:container wicket:id="messages">
<p wicket:id="message"></p>
</wicket:container>
</div>
</wicket:panel>
CustomFeedBackPanel.java
public class CustomFeedbackPanel extends Panel implements IFeedback {
private static final long serialVersionUID = 1L;
private final MessageListView messageListView;
WebMarkupContainer messagesContainer = new WebMarkupContainer("feedbackul") {
private static final long serialVersionUID = 1L;
#Override
protected void onConfigure() {
super.onConfigure();
setVisible(anyMessage());
}
};
public CustomFeedbackPanel(final String id) {
this(id, null);
}
public CustomFeedbackPanel(final String id, IFeedbackMessageFilter filter) {
super(id);
add(messagesContainer);
messageListView = new MessageListView("messages");
messagesContainer.add(messageListView);
if (filter != null) {
setFilter(filter);
}
}
public final boolean anyErrorMessage() {
return anyMessage(FeedbackMessage.ERROR);
}
public final boolean anyMessage() {
return anyMessage(FeedbackMessage.UNDEFINED);
}
public final boolean anyMessage(int level) {
List<FeedbackMessage> msgs = getCurrentMessages();
for (FeedbackMessage msg : msgs) {
if (msg.isLevel(level)) {
return true;
}
}
return false;
}
public final FeedbackMessagesModel getFeedbackMessagesModel() {
return (FeedbackMessagesModel) messageListView.getDefaultModel();
}
public final IFeedbackMessageFilter getFilter() {
return getFeedbackMessagesModel().getFilter();
}
public final CustomFeedbackPanel setFilter(IFeedbackMessageFilter filter) {
getFeedbackMessagesModel().setFilter(filter);
return this;
}
public final Comparator<FeedbackMessage> getSortingComparator() {
return getFeedbackMessagesModel().getSortingComparator();
}
public final CustomFeedbackPanel setSortingComparator(Comparator<FeedbackMessage> sortingComparator) {
getFeedbackMessagesModel().setSortingComparator(sortingComparator);
return this;
}
#Override
public boolean isVersioned() {
return false;
}
public final CustomFeedbackPanel setMaxMessages(int maxMessages) {
messageListView.setViewSize(maxMessages);
return this;
}
protected String getCSSClass(final FeedbackMessage message) {
String css = "feedback";
if (message.getLevel() == FeedbackMessage.ERROR
|| message.getLevel() == FeedbackMessage.FATAL) {
css = "feedback error";
}
if (message.getLevel() == FeedbackMessage.SUCCESS) {
css = "feedback success";
}
if (message.getLevel() == FeedbackMessage.WARNING) {
css = "feedback warn";
}
return css;
}
protected final List<FeedbackMessage> getCurrentMessages() {
final List<FeedbackMessage> messages = messageListView.getModelObject();
return Collections.unmodifiableList(messages);
}
protected FeedbackMessagesModel newFeedbackMessagesModel() {
return new FeedbackMessagesModel(this);
}
protected Component newMessageDisplayComponent(String id, FeedbackMessage message) {
Serializable serializable = message.getMessage();
Label label = new Label(id, (serializable == null) ? "" : serializable.toString());
label.setEscapeModelStrings(CustomFeedbackPanel.this.getEscapeModelStrings());
//label.add(new AttributeModifier("class",getCSSClass(message)));
return label;
}
private final class MessageListView extends ListView<FeedbackMessage> {
private static final long serialVersionUID = 1L;
public MessageListView(final String id) {
super(id);
setDefaultModel(newFeedbackMessagesModel());
}
#Override
protected IModel<FeedbackMessage> getListItemModel(
final IModel<? extends List<FeedbackMessage>> listViewModel, final int index) {
return new AbstractReadOnlyModel<FeedbackMessage>() {
private static final long serialVersionUID = 1L;
#Override
public FeedbackMessage getObject() {
if (index >= listViewModel.getObject().size()) {
return null;
} else {
return listViewModel.getObject().get(index);
}
}
};
}
#Override
protected void populateItem(final ListItem<FeedbackMessage> listItem) {
final FeedbackMessage message = listItem.getModelObject();
message.markRendered();
final Component label = newMessageDisplayComponent("message", message);
final AttributeModifier levelModifier = AttributeModifier.replace("class",
getCSSClass(message));
//label.add(levelModifier);
listItem.add(levelModifier);
listItem.add(label);
messagesContainer.add(levelModifier);
}
}
}
Main thing you should consider getCssClass() method. You can change according to your requirement.I have modified just for your reference.
protected String getCSSClass(final FeedbackMessage message) {
String css = "feedback";
if (message.getLevel() == FeedbackMessage.ERROR
|| message.getLevel() == FeedbackMessage.FATAL) {
css = "alert error";
}
if (message.getLevel() == FeedbackMessage.SUCCESS) {
css = "alert success";
}
if (message.getLevel() == FeedbackMessage.WARNING) {
css = "alert warn";
}
return css;
}
Feedback messages are rendered by FeedbackPanel class. It seems your application uses custom implementation of FeedbackPanel that renders the messages as Bootstrap Alerts.
By default Wicket sets feedbackMessage<LogLevel> (e.g. feedbackMessageWarning) as a CSS class to all messages, so you can style them however you want.
An alternative to not create a custom FeedbackPanel and new HTML/Java files is to use enclosures:
Using Twitter Bootstrap classes:
<wicket:enclosure>
<div class="alert alert-danger alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-ban"></i> Error on form validation!</h4>
<div wicket:id="errorMessages"></div>
</div>
</wicket:enclosure>
In page constructor:
FeedbackCollector collector = new FeedbackCollector(this);
ExactErrorLevelFilter errorFilter = new ExactErrorLevelFilter(FeedbackMessage.ERROR);
add(new FeedbackPanel("errorMessages", errorFilter) {
#Override public boolean isVisible() {
return !collector.collect(errorFilter).isEmpty();
}
});
Since Wicket 6 feedback messages are attached to components, so you can use a FeedbackCollector and a filter to get and display desired messages. The advantages of enclosures is:
you don't need to create new files;
it works similar to fragments/panels;
it's only rendered if desired messages exists;
Hope it helps.

How to get node actions to display in IconView

In my Netbeans Platform project I have a TopComponent with an IconView element, but when I add a NodeAction to the nodes it wont display. Right-clicking the nodes does not pop up a context menu. How do I get the action to display? This is what I have:
public class ItemDeleteAction extends NodeAction
{
⋮
#Override
protected boolean enable(Node[] nodes)
{
for (int i = 0; i < nodes.length; i++) {
Node node = nodes[i];
if (node instanceof ViewerItemNode) return true;
}
return false;
}
⋮
}
public final class JFlavourViewerTopComponent extends TopComponent implements ExplorerManager.Provider
{
public JFlavourViewerTopComponent()
{
⋮
setLayout(new BorderLayout());
add(new IconView(), BorderLayout.CENTER);
ActionMap actionMap = getActionMap();
actionMap.put("delete", new ItemDeleteAction());
associateLookup(ExplorerUtils.createLookup(explorerManager, actionMap));
}
}
public class ViewerItemNode extends AbstractNode
{
private ViewerItem item;
public ViewerItemNode(ViewerItem item, JFlavourProjectBean activeProject)
{
super (Children.LEAF, Lookups.singleton(item));
this.item = item;
setDisplayName (item.item.getLabel());
}
#Override
public Action[] getActions(boolean context)
{
if (!context) {
return new Action[]
{
SystemAction.get( ItemDeleteAction.class )
};
} else {
return super.getActions(context);
}
}
#Override
public Action getPreferredAction()
{
return SystemAction.get(PlayAudioAction.class);
}
}
I think that's all the relevant code.
I do the same thing in another TopComponent in another module and it works fine, but in that module the nodes are displayed as a BeanTreeView instead of an IconView
Try to remove the context condition:
#Override
public Action[] getActions(boolean context)
{
return new Action[] {
SystemAction.get(ItemDeleteAction.class)
};
}

CellList backed with ListDataProvider does not get redrawn on list change

I'm working on a project with GWT 2.1 and mvp4g. In a view, I'm using
a CellList backed with a ListDataProvider. If I pass a List with data to the constructor
when instantiating the ListDataProvider, the CellList shows this data.
The problem is that afterthat, the CellList never gets redrawn
whenever I change the list within the ListDataProvider. I don't know what I am
doing wrong or if I missing something.
Here is the code:
The UIBinder xml file:
<g:DockLayoutPanel unit="PX">
<g:west size="300">
<g:VerticalPanel styleName='{style.leftPanel}' spacing="8">
<g:Label>Expositores</g:Label>
<g:ScrollPanel addStyleNames='{style.exhibitorList}' width="250px" height="600px">
<c:CellList ui:field="exhibitorList" />
</g:ScrollPanel>
<g:Button ui:field="editExhibitorButton" addStyleNames='{style.button}'>Editar</g:Button>
</g:VerticalPanel>
</g:west>
...
The View class:
public class ExhibitorsAdminView extends Composite implements
ExhibitorsAdminPresenter.IExhibitorsAdminView {
interface Binder extends UiBinder<Widget, ExhibitorsAdminView> {}
private static final Binder binder = GWT.create( Binder.class );
private static class ExhibitorCell extends AbstractCell<Exhibitor> {
#Override
public void render(Cell.Context context, Exhibitor exhibitor,
SafeHtmlBuilder sb) {
if (exhibitor != null) {
sb.appendEscaped(exhibitor.getName());
}
}
}
private ListDataProvider<Exhibitor> exhibitorsDataProvider;
private SingleSelectionModel<Exhibitor> exhibitorsSelectionModel;
#UiField( provided = true )
CellList<Exhibitor> exhibitorList;
#UiField
Button editExhibitorButton;
// #UiField(provided = true)
// CellTable<Object> moduleList = new CellTable<Object>();
public ExhibitorsAdminView() {
exhibitorsSelectionModel = new
SingleSelectionModel<Exhibitor>(Exhibitor.KEY_PROVIDER);
exhibitorList = new CellList<Exhibitor>(new ExhibitorCell(),
Exhibitor.KEY_PROVIDER);
exhibitorList.setSelectionModel(exhibitorsSelectionModel);
exhibitorsDataProvider = new
ListDataProvider<Exhibitor>(getExhibitors());
exhibitorsDataProvider.addDataDisplay(exhibitorList);
exhibitorList.setPageSize(exhibitorsDataProvider.getList().size());
initWidget( binder.createAndBindUi( this ) );
}
public SingleSelectionModel<Exhibitor> getExhibitorsSelectionModel()
{
return exhibitorsSelectionModel;
}
public ListDataProvider<Exhibitor> getExhibitorsDataProvider() {
return exhibitorsDataProvider;
}
private List<Exhibitor> getExhibitors() {
List<Exhibitor> exhibitors = new ArrayList<Exhibitor>();
for (int i = 0; i < 10; i++) {
exhibitors.add(new Exhibitor(i, "aaaaaaaaaaaaaaa"));
}
return exhibitors;
}
public HasClickHandlers getEditExhibitorButton() {
return editExhibitorButton;
}
}
The presenter class:
#Presenter(view = ExhibitorsAdminView.class)
public class ExhibitorsAdminPresenter extends
BasePresenter<ExhibitorsAdminPresenter.IExhibitorsAdminView,
ExhibitorsEventBus> {
public interface IExhibitorsAdminView {
SingleSelectionModel<Exhibitor> getExhibitorsSelectionModel();
ListDataProvider<Exhibitor> getExhibitorsDataProvider();
HasClickHandlers getEditExhibitorButton();
}
private DispatchAsync dispatch = null;
#Inject
public ExhibitorsAdminPresenter(final DispatchAsync dispatch) {
this.dispatch = dispatch;
}
#Override
public void bind() {
getView().getExhibitorsSelectionModel().addSelectionChangeHandler(
new SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
Exhibitor selected =
getView().getExhibitorsSelectionModel().getSelectedObject();
if (selected != null) {
Window.alert("You selected: " + selected.getName());
}
}
});
getView().getEditExhibitorButton().addClickHandler(
new ClickHandler() {
public void onClick(ClickEvent event) {
}
});
}
public void onGoToExhibitorsAdmin() {
}
public void onLoadExhibitors() {
dispatch.execute(new GetExhibitors(), new
AsyncCallback<GetExhibitorsResult>() {
public void onSuccess(GetExhibitorsResult result) {
getView().getExhibitorsDataProvider().setList(
result.getExhibitors());
getView().getExhibitorsDataProvider().refresh();
}
public void onFailure(Throwable caught) {
GWT.log("error executing command ", caught);
}
});
}
}
Thanks.
I solved it. I'm sorry, it was an issue related with mvp4g. I was doing something wrong that was causing to have to different instances of the view where the CellList was placed. The update operations I was doing on the list of the ListDataProvider were being done on the view instance that wasn't being shown.
You have to manipulate the list by getting it first of your provider like provider.getList().add(...). See How to add or remove a single element from/to CellList? for a minimal example.
Just call exhibitorsDataProvider.refresh() after all operations with underlying list.

GWT: Add an event handler on all div elements

How to add an event handler on all div elements with GWT?
I tried the following code but the window alerts are not fired up (But "etvoila" class is set up):
private NodeList<Element> pageDIVElements;
public void initDiv() {
MyDIVEventHandler handler = new MyDIVEventHandler();
pageDIVElements = Document.get().getElementsByTagName("div");
for (int i = 0; i < pageDIVElements.getLength(); i++) {
Element elem = pageDIVElements.getItem(i);
elem.addClassName("etvoila");
com.google.gwt.user.client.Element castedElem = (com.google.gwt.user.client.Element) elem;
DOM.setEventListener(castedElem, handler);
}
class MyDIVEventHandler implements EventListener {
private Element divElement;
#Override
public void onBrowserEvent(Event event) {
Window.alert("Yeepee");
if (event.equals(Event.ONMOUSEOVER)) {
Window.alert("ONMOUSEOVER");
divElement = Element.as(((NativeEvent) event).getEventTarget());
divElement.setPropertyString("background-color", "#C6D4E6");
} else if (event.equals(Event.ONMOUSEOUT)) {
divElement = Element.as(((NativeEvent) event).getEventTarget());
divElement.setAttribute("background-color", "");
}else if (event.equals(Event.ONCLICK)) {
divElement = Element.as(((NativeEvent) event).getEventTarget());
divElement.setAttribute("background-color", "");
Window.alert("ONCLICK");
}
}
}
What is wrong in this method?
Looks like you negelected to sink the events that you want the listener to be notified of.
In this case for example I would add the following into initDiv to element or the relevant children.
DOM.sinkEvents(elem, Event.ONCLICK | Event.ONMOUSEOUT | Event.ONMOUSEOVER);
Similarly to this question, you can wrap it in a Label.
NodeList<Element> elems = Document.get().getElementsByTagName("div");
for (int i = 0; i < elems.getLength(); i++) {
Element elem = elems.get(i);
Label l = Label.wrap(elem);
l.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
Window.alert("yay!");
}
});
}