Sunday 30 October 2011

Resizing jQuery dialogs

I have a quick way of producing dialogs by specifying class="modal" with a href="the modal id", and a div with class="modal" and id="the modal id".

View the demo

What was annoying me is that when the page was resized to a small screen it wasn't displaying the dialog well. The dialog itself was too big and wasn't centred.

Add this script after the dialog opening to change the width and recenter for smaller windows:

 
if(window.innerWidth<=480){
    $('#'+($(this).attr("href"))).dialog( "option", "width", 300 );
    $('#'+($(this).attr("href"))).dialog("option", "position", "center");
}


then to modify the text within so that there is no scrolling need, change the css:

 
div.modal p {
    width: 480px;
    word-break: break-all; /*hyphenate*/
}
@media only screen and (max-width: 480px) {
    div.modal p {
        width: 280px;
        word-break: break-all;
    }
}


I'll be working soon on changing the header size for small screens on my boilerplate and making the dialogs work for ios5.

Tuesday 25 October 2011

Writing UTF-8 XML files with Java

I had a method to write XML files with FileWriter but discovered when I wanted to (...allow my customers to...) use special characters it fell over. I needed to use a FileOutputStream in order to deal with bytes.

Original method:

 
public void oldWriteXML() throws Exception{
        storeDoc.normalize(); //storeDoc is type Document
        if (storeFile!=null){
            storeFile.delete(); //delete the old file to overwrite
        }
        OutputStreamWriter fos = new FileWriter(filename);

        OutputFormat format = new OutputFormat(storeDoc);
        format.setLineWidth(65);
        format.setIndenting(true);
        format.setIndent(2);
        XMLSerializer serializer = new XMLSerializer(fos, format);
        serializer.serialize(storeDoc);

        fos.close();
    }


New method to allow UTF-8:

 
public void writeXML() throws Exception{
        storeDoc.normalize();
        if (storeFile!=null){
            storeFile.delete();
        }
        OutputStreamWriter fos = 
                new OutputStreamWriter(new FileOutputStream(filename),"UTF-8");
        
        OutputFormat format = new OutputFormat(storeDoc, "UTF-8", true);
        format.setLineWidth(65);
        format.setIndent(2);
        
        XMLSerializer serializer = new XMLSerializer(fos, format);
        serializer.serialize(storeDoc);

        fos.close();        
    }