Friday, December 11, 2009

Place any window to the center of the screen

Placing the component in the center of the visible are of the screen. This helps to place the component to
the center of the screen as we scroll the page content. It supports following browsers IE 6, IE 7, FireFox, Netscape
Below function, get the page horizontal/vertical scrolling offsets of the page.
getPageScrolling = function ()
{
    var scrollX = 0;
    var scrollY = 0;
    if (document.documentElement &&
            typeof document.documentElement.scrollLeft === "number")
    {   // IE compliant.  IE compliant must be before DOM compliant since IE
        // always returns 0 for the DOM compliant access. IE's hack always
        // returns the correct value.
        scrollX = document.documentElement.scrollLeft;
        scrollY = document.documentElement.scrollTop;
    }
    else if (typeof window.pageYOffset === "number")
    {   // Netscape compliant.
        scrollX = window.pageXOffset;
        scrollY = window.pageYOffset;
    }
    else if (document.body && typeof document.body.scrollLeft === "number")
    {   // DOM compliant. IE will always return 0 in this case.
        scrollX = document.body.scrollLeft;
        scrollY = document.body.scrollTop;
    }
    return { scrollX: scrollX, scrollY: scrollY };
};
Get the viewport width/height of the screen.  This returns the width/height of the visible area of the browser.
getViewportSize = function ()
{
    var width = 0;
    var height = 0;
    if (document.documentElement &&
        typeof document.documentElement.clientWidth === "number")
    {
        width = document.documentElement.clientWidth;
        height = document.documentElement.clientHeight;
    }
    else if (document.body && typeof document.body.clientWidth === "number")
    {
        width = document.body.clientWidth;
        height = document.body.clientHeight;
    }
    else if (typeof window.innerWidth === "number")
    {
        width = window.innerWidth - 18;
        height = window.innerHeight - 18;
    }
    return { width: width, height: height };
};
Function that is used to continuously update the component so that it is always centered in the current viewport.
/**
 * @param component Reference to the DOM element that represents the component
 *        "window" that is being centered.

 * @return  Centering function for component window - creates a closure around the
 *          DOM element that should be centered in the viewport.
 */
getUpdateFn = function (component) {
    return function () {
        // Calculate the Height/Width of the component
        var componentSize = getcomponentSize(component);
        // Calculate the Viewport Height/Width
        var viewportSize = getViewportSize();

        // Calculate the Page Scrolling
        var pageScroll = getPageScrolling();

        // Calculate the coordinates to center component
        var pageX =
            pageScroll.scrollX + ((viewportSize.width - componentSize.width) / 2);
        var pageY =
            pageScroll.scrollY + ((viewportSize.height - componentSize.height) / 2);

        // Update component Window coordinates
        component.style.top = pageY + "px";
        component.style.left = pageX + "px";
    };
};
Get the width/height of the specified component, that needs to placed to the center of the screen.  This returns the current width/height needed to display the specified component.
/**
 * @param component  The component to calculate the width/height of

 * @return  The width/height of the specified component
 */
getcomponentSize = function (component)
{
    return { width: component.offsetWidth, height: component.offsetHeight };
};

Tuesday, December 8, 2009

Upload file to Server


Upload the file to the server using the Http Connection. The File content is read as Input Stream and then uploaded to the server through the connections
public void sendFile(final String uri, final InputStream istream)
throws EvoxException
{
try
{
// Setup HTTP Connection
final URL evoxUrl = new URL(this.evoxServer + uri);
final HttpURLConnection connection =
 (HttpURLConnection) evoxUrl.openConnection();
connection.setRequestMethod(RequestType.POST.name());
connection.setDoInput(true);
connection.setDoOutput(true);
// Get Output Stream
final OutputStream ostream = connection.getOutputStream();
FileUtils.copyStreams(istream, ostream);
ostream.close();
final PrintWriter writer =
 new PrintWriter(connection.getOutputStream());
// Send File
writer.println(ostream);
writer.flush();
writer.close();
connection.getInputStream();
}
catch (IOException exception)
{
 throw new Exception("unable to send file: " + uri,
       exception);
}}
If the file is uploaded to the server make sure you close the connection if not needed, if not it will windup keeping lots of open connections in you applications




Download File From Server

We could retrieve/ download the file from the server and save it locally in to the PC.

public void downLoadFileFromDevice() throws IOException
{
final FacesContext ctx = FacesContext.getCurrentInstance();
if (!ctx.getResponseComplete())
{
InputStream fileFromEvox = null;
try
{
fileFromServer = receiveFile (“serverName/filename”);
}
catch (Exception e)
{
// Error Message.
 return;
}
final String contentType = "application/x-zip-compressed";
final StringBuilder sbuff = new StringBuilder(30);
sbuff.append(“newfilefromserver.xyz”);
final HttpServletResponse response = (HttpServletResponse)ctx
 .getExternalContext().getResponse();
response.setContentType(contentType);
response.setHeader("Content-Disposition",
 "attachment;filename=\"" + sbuff.toString() + "\"");
ServletOutputStream out = null;
InputStream istream = null;
try
{
istream = new BufferedInputStream(fileFromServer);
out = response.getOutputStream();
final byte[] buf = new byte[4096];
for (int c = istream.read(buf); c != -1; c =                
istream.read(buf))
{
out.write(buf, 0, c);
}}finally{
if(istream != null)
{istream.close();}
}
 out.flush();
 ctx.responseComplete();
}}
Receive file helps to establish the connections to the server and receives the file in the InputStream format

public InputStream receiveFile(final String uri)
throws EvoxException
{
try
{
final URL serverUrl = new URL(this.evoxServer + uri);
final HttpURLConnection connection =
 (HttpURLConnection) serverUrl.openConnection();
connection.setRequestMethod(RequestType.GET.name());
connection.setDoInput(true);
connection.setDoOutput(false);
// Read the Response
return connection.getInputStream();
}
catch (IOException exception)
{
throw new Exception("unable to receive file: " + uri,
 exception);
}}

Debug JSf LifeCycle

You can use a PhaseListener to trace the phases of the JSF lifecycle and execute some processes where required. But you can also use a "dummy" PhaseListener to debug the phases to see what is happening in which phase. Here is a basic example of such a LifeCycleListener:
package mypackage;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener; 
public class LifeCycleListener implements PhaseListener { 
    public PhaseId getPhaseId() {
        return PhaseId.ANY_PHASE;
    } 
    public void beforePhase(PhaseEvent event) {
        System.out.println("START PHASE " + event.getPhaseId());
    } 
    public void afterPhase(PhaseEvent event) {
        System.out.println("END PHASE " + event.getPhaseId());
    } 
}
Add the following lines to the faces-config.xml to activate the LifeCycleListener.
    mypackage.LifeCycleListener
This produces like the following in the system output:
START PHASE RESTORE_VIEW 1
END PHASE RESTORE_VIEW 1
START PHASE APPLY_REQUEST_VALUES 2
END PHASE APPLY_REQUEST_VALUES 2
START PHASE PROCESS_VALIDATIONS 3
END PHASE PROCESS_VALIDATIONS 3
START PHASE UPDATE_MODEL_VALUES 4
END PHASE UPDATE_MODEL_VALUES 4
START PHASE INVOKE_APPLICATION 5
END PHASE INVOKE_APPLICATION 5
START PHASE RENDER_RESPONSE 6
END PHASE RENDER_RESPONSE 6

Monday, December 7, 2009

FireShot

Unlike other extensions, this plugin provides a set of editing and annotation tools, which let users quickly modify captures and insert text and graphical annotations. Such functionality will be especially useful for web designers, testers and content reviewers.

Screenshots can be saved to disk (PNG, GIF, JPEG, BMP), printed (NEW), copied to clipboard, e-mailed and sent to external editor for further processing. Works both in IE and Firefox

Download at : http://screenshot-program.com/fireshot/