Showing posts with label Apache Wicket. Show all posts
Showing posts with label Apache Wicket. Show all posts

Wednesday, 24 April 2013

Panel which dynamically opens and closes in Wicket




Panel which dynamically opens and closes in Wicket

import java.text.MessageFormat;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.value.IValueMap;

public class CustomPanel extends Panel {
  private static final String SHOW_ALL_SCRIPT = "jQuery('a.panel-open').click()";

  private static final String HIDE_ALL_SCRIPT = "jQuery('a.panel-close').click()";

  public CustomPanel(final String id, final Component component) {
    this(id, component, new Model<Boolean>(true));
  }

  public CustomPanel(final String id, final Component component, final IModel<Boolean> open) {
    super(id, open);
    component.setOutputMarkupId(true);

    final WebMarkupContainer link = new WebMarkupContainer("link") {
      private static final String SCRIPT = "var panel = jQuery(''#{0}'');"
          + "var link = jQuery(this); var span = link.children();"
          + "link.toggleClass(''panel-close'').toggleClass(''panel-open'');"
          + "if (panel.css(''display'') == ''none'') "
          + "'{' panel.show(); span.text(''{1}''); link.attr(''title'', ''{3}''); '}'" + "else "
          + "'{' panel.hide(); span.text(''{2}''); link.attr(''title'', ''{4}''); '}'" + "return false;";

      @Override
      protected void onComponentTag(final ComponentTag tag) {
        super.onComponentTag(tag);
        if (!tag.isClose()) {
          final IValueMap attributes = tag.getAttributes();

          final Boolean isPanelOpen = getModel().getObject();
          if (isPanelOpen != null && isPanelOpen) {
            attributes.put("class", "panel-close");
            attributes.put("title", getCloseText());
          } else {
            attributes.put("class", "panel-open");
            attributes.put("title", getOpenText());
          }
          attributes.put("onclick", MessageFormat.format(SCRIPT, component.getMarkupId(), getCloseText(),
              getOpenText(), getClosePanelText(), getOpenPanelText()));
        }
      }

    };
    add(link);
    link.add(new Label("text", new AbstractReadOnlyModel<String>() {
      private static final long serialVersionUID = 1045024635866272771L;

      @Override
      public String getObject() {
        final Boolean isPanelOpen = getModel().getObject();
        if (isPanelOpen != null && isPanelOpen) {
          return getCloseText();
        } else {
          return getOpenText();
        }
      }
    }));
  }

  @Override
  protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) {
    renderComponentTagBody(markupStream, openTag);
    super.onComponentTagBody(markupStream, openTag);
    // Render the body of the link

  }

  @SuppressWarnings("unchecked")
  protected IModel<Boolean> getModel() {
    return (IModel<Boolean>) getDefaultModel();
  }

  private String getCloseText() {
    return getString("close");
  }

  private String getOpenText() {
    return getString("open");
  }

  private String getClosePanelText() {
    return getString("closePanel");
  }

  private String getOpenPanelText() {
    return getString("openPanel");
  }

  public static AttributeAppender getShowAllBehavior() {
    return new AttributeAppender("onclick", true, new Model<String>(SHOW_ALL_SCRIPT), " ");
  }

  public static AttributeAppender getHideAllBehavior() {
    return new AttributeAppender("onclick", true, new Model<String>(HIDE_ALL_SCRIPT), " ");
  }

}


CustomPanel.html

<wicket:panel xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">
<a wicket:id="link" href="#">
<span wicket:id="text"></span>
</a>

</wicket:panel>

Thursday, 11 October 2012

Component Frameworks


Welcome to the world of components!!!

Today, we are heading towards the component world. Everything we get in terms of component.Rather we are building everything as a component. Why I am saying this is because the kind of frameworks I came across.

I started my carrier with Struts framework. This is most popular MVC framework of that time. As it is today as well.But there were very few other frameworks competing with Struts.

At that time i didn't heard anything in terms of component.But later on I moved to other organizations and  started working on JSF.At that time JSF was the emerging framework. It was known as the component framework for fast web application development.It also had in built support for Ajax.

After that I came across another framework Apache Wicket. This is another frame that I learnt. This framework was quite different from JSF. As a Java developer u need not have to write anything on the HTML side or in JSP. There is a VERY nice separation of presentation and logic. Here HTML will remain as HTML. I enjoyed working on this framework.

Currently I am using the ExtJS which is another JavaScript framework which is also a component framework.

Yesterday I read about some other framework like "Vaadin", "KendoUI".These are the other emerging component based framework.There are many more in the market which are known as component framework. By using which u can fasten the development and it eases the maintenance of software.

I m not comparing these frameworks here. Just wanted share and highlight that the Technology world is moving fast and everything in coming to us in terms of "Component"...