Mental Jetsam

By Peter Finch

Hiding the Android soft keyboard

Posted by pcfinch on July 19, 2012

This sounded so simple… but it wasn’t as easy as I thought. All I wanted to do was hide the soft keyboard when I was switching (using a ViewFlipper) to a view that did not require it. This is what I ended up doing and it seems to work.

ViewFlipper flipper = (ViewFlipper)this.findViewById(R.id.viewFlipper_main) ; // Any active view will do
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE) ;
imm.hideSoftInputFromWindow(flipper.getApplicationWindowToken(), 0);
flipper.setDisplayedChild(1) ;
Advertisements

Posted in Programming | 1 Comment »

Java Hello World

Posted by pcfinch on July 11, 2012

This is a standard “hello world” program that I use when testing a java installation to make sure everything works.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class MyFrame extends JFrame {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MyFrame mf = new MyFrame() ;
	}
	
	protected MyFrame() {
		this.setTitle("MyFrame") ;
		this.setSize(800, 600) ;
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
		this.getContentPane().add(new JPanel() {
			public void paintComponent(Graphics g) {
				Graphics2D g2 = (Graphics2D)g ;
				g2.setColor(Color.blue) ;				
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.drawLine(0, 0, this.getWidth(),  this.getHeight()) ;
				g2.drawOval(0, 0, this.getWidth(),  this.getHeight()) ;
				g2.drawString("Hello World", this.getWidth() /2 , this.getHeight() /2) ;
			}			
		}) ;
		this.setVisible(true) ;
	}

}

Posted in Uncategorized | Leave a Comment »

Custom taglib in Tomcat 7

Posted by pcfinch on April 12, 2012

The following is a simple example for creating a custom tag library (taglib) for the Apache Tomcat JSP server and could be used as a simple template engine.

  1. Create a taglib definition file in “WEB-INF/jsp/mytaglib.tld”.

    <taglib>  
        <tlib-version>1.0</tlib-version>  
        <jsp-version>1.2</jsp-version>  
        <short-name>example tags</short-name>  
        <uri>http://www.peterfinch.me/mytag</uri>  
        <tag>  
            <name>menu</name>  
            <tag-class>me.peterfinch.MenuTag</tag-class>  
        </tag>  
        <tag>  
            <name>master</name>  
            <tag-class>me.peterfinch.MasterTag</tag-class>  
        </tag>  
    </taglib>
    
  2. Add taglib section to web.xml

    <web-app>
    ...
    <jsp-config>
      <taglib>
        <taglib-uri>http://www.peterfinch.me/mytag</taglib-uri>
        <taglib-location>/WEB-INF/jsp/mytaglib.tld</taglib-location>
      </taglib>
    </jsp-config>
    </web-app>
    
  3. Create MasterTag Java class to handle the tag events. This class wraps the encoded HTML (generated by the JSP) and could be used as a simple page template.

    package me.peterfinch;
    import java.io.IOException;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.tagext.BodyContent;
    import javax.servlet.jsp.tagext.BodyTagSupport;
    
    public class MasterTag extends BodyTagSupport {
    	
    	@Override
    	public int doAfterBody() throws JspException {
    		try {
    			BodyContent bodycontent = getBodyContent();
    			String body = bodycontent.getString();
    			JspWriter out = bodyContent.getEnclosingWriter();
    			out.print("<div class='master' style='border:1px solid black'>") ;
    			out.print(body) ;
    			out.print("</div>") ;
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		return SKIP_BODY;
    	}
    }
    
  4. Add MenuTag Java class handler. This just replaces the tag, and it’s contents with some predefined HTML and could be used as a Menu (or anything else you desire)

    package me.peterfinch;
    import java.io.IOException;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.tagext.BodyTagSupport;
    
    public class MenuTag extends BodyTagSupport {
    	
    	@Override
    	public int doAfterBody() throws JspException {
    		try {
    			String i = this.pageContext.getRequest().getParameter("i") ;
    			JspWriter out = bodyContent.getEnclosingWriter();
    			out.print("<a href='index.jsp'>Home</a> " + i) ;
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		return SKIP_BODY;
    	}
    }
    
  5. Add taglib definition to JSP page

    <%@ taglib uri="http://www.peterfinch.me/mytag" prefix="my" %>
    
  6. Add the custom tags to to your page

    <my:master>
    <my:menu></my:menu>
    <h1>Hello from index.jsp</h1>
    </my:master>
    

Posted in JSP | Leave a Comment »

Google Chrome multiple browser session instances

Posted by pcfinch on February 6, 2012

Have you ever wanted to login to a website using the same browser, but with multiple different sessions. For example, log in to GMail, using Google Chrome, but as two different users. Normally, the moment you login as the second user you will be logged out from the first session because the session information [cookie] is “shared” between the browser windows.

Here is a simple trick. Create a new short-cut to Chrome and add “–user-data-dir=c:\temp\user2” to the Chrome command and you’ll get a second “instance” of chrome, with completely different session information. If you want another instance, use a different directory name, etc. As a new instance it will not have any of the settings from your “normal” instance but it’s great for testing websites.

C:\Users\[username]\AppData\Local\Google\Chrome\Application\chrome.exe --user-data-dir=c:\temp\user2

BTW… unless you want to use the same browser information in the future, don’t forget to remove c:\temp\user2 when your done.

Posted in Web | 1 Comment »