5 Nisan 2012 Perşembe

A Printer class for GWT


import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.Element;

public class Printer {
    public static native void it() /*-{
        $wnd.print();
    }-*/;
    public static native void it(String html) /*-{
        var frame = $doc.getElementById('__printingFrame');
        if (!frame) {
            $wnd.alert("Error: Can't find printing frame.");
            return;
        }
        frame = frame.contentWindow;
        var doc = frame.document;
        doc.open();
        doc.write(html);
        doc.close();
        frame.focus();
        frame.print();
    }-*/;
    public static void it(UIObject obj) {
        it("", obj.getElement().toString());
    }
    public static void it(Element element) {
        it("", element.toString());
    }
    public static void it(String style, String it) {
        it("<it><header>"+style+"</header><body>"+it+"</body></it>");
    }
    public static void it(String style, UIObject obj) {
        it(style, obj.getElement().toString());
    }
    public static void it(String style, Element element) {
        it(style, element.toString());
    }
}

Open a new window in GWT


public static void openNewWindow(String name, String url) {
   com.google.gwt.user.client.Window.open(url, name.replace(" ", "_"),
          "menubar=no," +
          "location=false," +
          "resizable=yes," +
          "scrollbars=yes," +
          "status=no," +
          "dependent=true");
}

28 Ocak 2012 Cumartesi

Hashing with MD5 in JAVA

This guy has explained it very well.

JSF 1.2 Email Validation

In JSF 2.0 we have
@Pattern(regexp=".+@.+\\.[a-z]+",message="Username must be in e-mail format");
We define it just above the mailadress variable in Managedbean and life is so good. But inJSF1.2 we have to write our own mailValidator class. Here is how we can do it:

  1. First of all, create a validator class :/**
     * @author Burhan ARAS
     *
     */
    public class EmailValidator implements Validator {

        /* (non-Javadoc)
         * @see javax.faces.validator.Validator#validate(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
         */
        @Override
        public void validate(FacesContext facesContext, UIComponent uIComponent, Object object)
        throws ValidatorException {
            // TODO Auto-generated method stub
             String enteredEmail = (String)object;
                //Set the email pattern string
                Pattern p = Pattern.compile(".+@.+\\\\.[a-z]+");
               
                //Match the given string with the pattern
                Matcher m = p.matcher(enteredEmail);
               
                //Check whether match is found
                boolean matchFound = m.matches();
               
                if (!matchFound) {
                    FacesMessage message = new FacesMessage();
                    message.setDetail("Email geçersiz.");
                    message.setSummary("Kullanıcı adi e-mail formatında değil.");
                    message.setSeverity(FacesMessage.SEVERITY_ERROR);
                    throw new ValidatorException(message);
                }
        }

    }
  2. Secondly we should define this validator in faces-config.xml file. <validator>
      <validator-id>emailvalidator</validator-id>
      <validator-class>com.blog.util.EmailValidator</validator-class>
     </validator>
  3. And finally, we are gonna declare it in our preview file like html, jsf or whatever.

5 Ocak 2012 Perşembe

EXCEL formatting

=CONCATENATE(REPT("0", 9 - LEN(A2)),A2," ",B2,REPT(" ",20 - LEN(B2))," ",C2,REPT("0",8 - LEN(C2))," ",REPT("0",2 - LEN(D2)),D2," ",REPT("0",9 - LEN(E2)),E2)