Android Series: Parsing JSON data with GSON

Posted by admin | Posted in Code Snippets, Mobile Development | Posted on 25-02-2010

0

Today I will describe how to parse json data using Gson open source library and how to convert it at the same time to java objects.
This is a typical cookbook example and should be very easy to follow.
1. Create an Android application with one activity, name it as you wish, if you are doing it in eclipse all the basic stuff will be generated for you which is exactly what we need.
2. Find an example url which responds in JSON, I have used Twitter Trends found here: http://search.twitter.com/trends.json
The example response looks like this:

{"as_of":"Thu, 25 Feb 2010 11:30:17 +0000","trends":[{"name":"#nowplaying","url":"http://search.twitter.com/search?q=%23nowplaying"},{"name":"#nothingworsethan","url":"http://search.twitter.com/search?q=%23nothingworsethan"},{"name":"Dubai Mall","url":"http://search.twitter.com/search?q=%22Dubai+Mall%22"},{"name":"iPad Gets","url":"http://search.twitter.com/search?q=%22iPad+Gets%22"},{"name":"#SuperJuniorTrot","url":"http://search.twitter.com/search?q=%23SuperJuniorTrot"},{"name":"Justin Bieber","url":"http://search.twitter.com/search?q=%22Justin+Bieber%22"},{"name":"Click","url":"http://search.twitter.com/search?q=Click"},{"name":"Jaebum","url":"http://search.twitter.com/search?q=Jaebum"},{"name":"#tosavemoney","url":"http://search.twitter.com/search?q=%23tosavemoney"},{"name":"Protection","url":"http://search.twitter.com/search?q=Protection"}]}

As you can see this json data contains date attribute called ‘as_of’ as well as array of items, each consisting of name and url attributes.
We will create two simple java classes wich will hold that information.
First one is TwitterTrends.java

package com.softwarepassion.jsonexample;

import java.util.Date;
import java.util.List;

public class TwitterTrends {

    private String as_of;
    private List<TwitterTrend> trends;
   
    public String getAs_of() {
        return as_of;
    }
    public void setAs_of(String asOf) {
        as_of = asOf;
    }
    public List<TwitterTrend> getTrends() {
        return trends;
    }
    public void setTrends(List<TwitterTrend> trends) {
        this.trends = trends;
    }
   
   
   
}

And the second one is a simple TwitterTrend.java

package com.softwarepassion.jsonexample;

public class TwitterTrend {
    private String name;
    private String url;
   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
}

TwitterTrends contains a list of TwitterTrend objects, both of them are simple beans and are very similar to the json response if you look closely :) .

Now the fun part begins.

Download the Gson library from http://code.google.com/p/google-gson/ and add it to your java build path in eclipse.
Once you do that, you can now transform your json response to Java object on Android Platform:
Get the response as InputStream:

public InputStream getJSONData(String url){
        DefaultHttpClient httpClient = new DefaultHttpClient();
        URI uri;
        InputStream data = null;
        try {
            uri = new URI(url);
            HttpGet method = new HttpGet(uri);
            HttpResponse response = httpClient.execute(method);
            data = response.getEntity().getContent();
        } catch (Exception e) {
            e.printStackTrace();
        }
       
        return data;
    }

Then use that input stream to create your java objects:

public void runJSONParser(){
        try{
        Log.i("MY INFO", "Json Parser started..");
        Gson gson = new Gson();
        Reader r = new InputStreamReader(getJSONData("http://search.twitter.com/trends.json"));
        Log.i("MY INFO", r.toString());
        TwitterTrends objs = gson.fromJson(r, TwitterTrends.class);
        Log.i("MY INFO", ""+objs.getTrends().size());
        for(TwitterTrend tr : objs.getTrends()){
            Log.i("TRENDS", tr.getName() + " - " + tr.getUrl());
        }
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

Easy ha!

Getting started with Apache Click and Eclipse Galileo

Posted by admin | Posted in Code Snippets, General Programming, Tools | Posted on 18-02-2010

3

Last couple of days I read here and there about Apache Click, which is now in version 2.1.0. It looks preety cool and easy to learn, so I decided to give it a try and see for myself.
I understand that its cool to learn things using vi and shell only, but when you tried many frameworks before, you know how things works, you just want to see how or how not, productive you can be with the new framework, thats why I jump straight to the IDE.
And here, first surprise, with apache click its’ not that easy, well, I’m mean its still easy but I have encountered a few problems, and right now I would like to share the solution.
First of all, I did not tried all the examples in the apache click user guide yet, haven’t build anything usefull and definietly I’m not an Apache Click expert, I think I have figured out how to plug it to Eclipse and I think this is good start :) .


Here you have a few points to build your Apache Click IDE:
1. First of all download latest and greatest Eclipse Galileo from here – choose Eclipse for Java EE developers
2. Download Apache Click libraries from here – choose version 2.1.0
3. Download Apache Click IDE from here – note that it contains Click version 1.5 and it was last updated in November 2008 – maybe I’m missing something here?
4. Unzip your ClickIDE and copy the ‘features’ and ‘plugins’ folders into your Eclipse home directory
5. Unzip your latest click libraries and keep it for later :)
6. Start your Eclipse IDE and create new ‘Dynamic Web Project’

Choose target name, in this case ‘ClickDemo2′, select your target runtime as Tomcat 6+ etc.
7. Under configuration combo box, select custom and click ‘Modify’

8. Select ‘Click’ in Project Facet List and save the configuration as click config

9. Once you have your configuration created, dynamic web project screen should look like in the following screenshot:

10. The rest of the settings you can leave as it is, just click ‘Next’ up to the point when you finish creating new dynamic web project.
11. After your project is created it will have the structure like in the following screen:



Notice that there are click libraries from version 1.5, which is no good, we want to use the latest version which is 2.1.0.
12. Copy the files ‘click-2.1.0.jar’ and ‘click-extras-2.1.0.jar’ into the same lib folder where the old libraries are found.
13. Delete old libraries and refresh the project tree under eclipse (right click on the project node and select ‘Refresh’), now you will see the new libraries come up in the lib folder.
14. Open your web.xml file under ‘WebContent/WEB-INF/’ folder and change the class name of the main Click Servlet, from ‘net.sf.click.ClickServlet’ to ‘org.apache.click.ClickServlet’ in ’servlet-class’ declaration.
15. With Eclipse, select the project node and click ‘New’ -> ‘Other’, choose ‘Click Page’ under ‘click’ directory.

16. Fill out the necessary fields for your new click page as below:

17. Name the file ‘index.htm’ and click ‘Finish’
18. Under the page java source file, fix the Page class by using the page from the: org.apache.click.Page instead of the old path.

package com.softwarepassion;

import java.util.Date;

import org.apache.click.Page;


public class Index extends Page {
   
    private Date time = new Date();
   
    public Index(){
        addModel("time", time);
    }

}

Modify your ‘index.htm’ using html editor to look something like this:

<html>
  <head>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=UTF-8">
    <title>Blank</title>
    $imports
  </head>
  <body>
  $time
  </body>
</html>

19. Start up your project in tomcat. The page should display current time!


Additional Comments:
I know that this is just a quickfix, and I just cannot belive that there is no proper eclipse plugin using the latest click framework, which would be up to date, if you know any, than please comment and save me and my readers precious time.

Thank you.

Jasperberry – the most dynamic reports ever.

Posted by admin | Posted in Code Snippets, General Programming, Tools | Posted on 17-02-2010

0

Today I will describe in short my latest and simplest quick and dirty trick, generating jasper report dynamically. Jasperberry is a component written very quickly to help me
generate pdf report in a situation where I don’t know the number of columns I will have, I don’t know their widths, the data they contain etc, all is generated at runtime dynamically.
I know that there is DynamicJasper project but it seems to be dead, and its dead in a very strange way, I mean, I see that the authors commit new code but there is no chance to get any support unless, I guess, you pay for it.
With dynamic jasper I couldn’t generate report with polish fonts displayed properly, tried many different tricks, no support on the forum, no support on stack overflow, no nothing. After 2 days I have realized that I will write my own library quicker to solve my problem than get an answer from the dynamic jasper community (if there is any).
So here we have: jasperberry!


Don’t expect too much from it but if you want to display simple table with data, dynamic columns, adjustable fonts and colors than this is it.

In fact you don’t have to even know object properties to generate the report, we will use Apache DynaBeans for that:

This is a simple example code snipped showing you how to get started with the simplest and most dynamic reporting library ever (as well as lacking features probably but we hope its enough for simple projects)

This example assumes the following:

1. You want simple report
2. You dont have to know the object which properties you want to display in advance, by this I mean you can create report columns dynamically, and by using apache dyna beans your objects don’t have to even exists. You just dynamically create them and inject them into the report.

Here is an example test class showing you how to use jasperberry, for more info please contact us directly at softberries.com
Code:

public class JRXMLBuilderTest {

    private static List<JBColumnBean> columns;

    public JRXMLBuilderTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
        columns = new ArrayList<JBColumnBean>();
        JBColumnBean col1 = new JBColumnBean();
        JBColumnBean col2 = new JBColumnBean();
        JBColumnBean col3 = new JBColumnBean();
        JBColumnBean col4 = new JBColumnBean();
        JBColumnBean col5 = new JBColumnBean();
        JBColumnBean col6 = new JBColumnBean();
        JBColumnBean col7 = new JBColumnBean();
        col1.setPreferredWidth(60);
        col1.setProperty("name");
        col1.setTitle("Imię");
        col2.setPreferredWidth(60);
        col2.setProperty("surname");
        col2.setTitle("Nazwisko");
        col3.setPreferredWidth(160);
        col3.setProperty("comment");
        col3.setTitle("Komentarz");
        col4.setPreferredWidth(100);
        col4.setProperty("comment2");
        col4.setTitle("Komentarz");
        col5.setPreferredWidth(100);
        col5.setProperty("comment3");
        col5.setTitle("Komentarz");
        col6.setPreferredWidth(100);
        col6.setProperty("comment4");
        col6.setTitle("Komentarz");
        col7.setPreferredWidth(100);
        col7.setProperty("comment5");
        col7.setTitle("Komentarz");
        columns.add(col1);
        columns.add(col2);
        columns.add(col3);
        columns.add(col4);
        columns.add(col5);
        columns.add(col6);
        columns.add(col7);
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    /** * Test of build method, of class JRXMLBuilder. */
    @Test
    public void testBuild() {
        try {
            System.out.println("build");
            List<JBReportElement> elements = new ArrayList<JBReportElement>();
            //col header font
            JBFont colHeaderFont = new JBFont();
            colHeaderFont.setBold(true);
            colHeaderFont.setSize(10);
            colHeaderFont.setForegroundColor("#555555");
            //detail font
            JBFont detailFont = new JBFont();
            detailFont.setSize(6);
            detailFont.setForegroundColor("#000000");
            detailFont.setPdfEncoding("Cp1257");
            JBPage page = new JBPage();
            JBReport report = new JBReport(page, colHeaderFont, detailFont, "#CCCCFF", "#DEDEDE", "#FFFFFF");
            JBFont titleFont = new JBFont();
            titleFont.setBold(true);
            titleFont.setSize(20);
            titleFont.setForegroundColor("#FF0000");
            titleFont.setPdfEncoding("Cp1257");
            //add title elements
            elements.add(new JBStaticTextElement("Hello World!", 450, 10, 300, titleFont));
            elements.add(new JBImageElement("C:\\\\Documents and Settings\\\\admin\\\\Moje dokumenty\\\\NetBeansProjects\\\\JASPER_BERRY\\\\test.jpg", 0, 0, 350, 242));
            report.setTitleElements(elements);
            JRXMLBuilder instance = new JRXMLBuilder(columns, report);
            boolean expResult = true;
            boolean result = instance.build();
            JasperReport jr = JasperCompileManager.compileReport("jasperberry_by_softberries.jrxml");
            //PREPARE SOME DATA
            DynaProperty[] props = new DynaProperty[columns.size()];
            for (int i = 0; i < columns.size(); i++) {
                props[i] = new DynaProperty(columns.get(i).getProperty(), String.class);
            }
            BasicDynaClass dynaClass = new BasicDynaClass("tester", null, props);
            List<DynaBean> beans = new ArrayList<DynaBean>();
            for (int i = 0; i < 100; i++) {
                DynaBean db = dynaClass.newInstance();
                db.set("name", "Krzysztof " + i);
                db.set("surname", "Grajek " + i);
                db.set("comment", "Komentarz ");
                db.set("comment2", "Komentarz ");
                db.set("comment3", "Komentarz ");
                db.set("comment4", "Komentarz ");
                //
                db.set("comment5", "Komentarz ");
                beans.add(db);
            }
            JasperPrint jp = JasperFillManager.fillReport(jr, null, new JRBeanCollectionDataSource(beans));
            JasperExportManager.exportReportToPdfFile(jp, "jasperberry.pdf");
            assertEquals(expResult, result);
        } catch (Exception ex) {
            Logger.getLogger(JRXMLBuilderTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

You should find similar snippet in a test suite of the project.
The project itself is licensed under LGPL and its available from sourceforge here

Deploy your Java Application with IzPack Installer

Posted by admin | Posted in Code Snippets, General Programming | Posted on 06-02-2010

1

This is a short tutorial on how to create simple IzPack based installation solution on windows platform with all standard features like: desktop shortcut, start menu item shortcut, copying libraries, folders, documentation into one of the ProgramFiles folder on the windows machine.
I had some problems creating windows shortcuts mysefl and I wanted to share the experience so other people can find it quicker.
Here you go.


1. First download and install IzPack itself from: izpack website

2. Prepare the application you want to make installable, I use Netbeans to build most of my Java apps, but I guess for all other ide’s the results after all are the same, we end up with SomeApp.jar file and a bunch of libraries we used to build it.
On the Netbeans platform all the files needed to launch the application we have build end up in ‘dist’ folder as a subfolder of our project main folder. If you have used any libraries to build your desktop application they will be included in the lib folder (yourproject/dist/lib/).

3. Now, once we have all the main ingredients (our application and installer) we need to connect it together. Create the folder anywhere on your machine, and call it for example ’softwarepassion’.

4. Put your jar file together with the ‘lib’ folder into the ’softwarepassion’ catalog.

5. Now we need to add some native libraries for Izpack to be able to add windows shortcuts on the desktop and start menu. Create ‘bin’ folder under the ’softwarepassion’ folder and copy whole ‘native’ content found in your izpack installation into the ‘bin’ folder of your root ’softwarepassion’ catalog.
You can find the native folder inside your IzPack installation directory, normally under: ‘C:\Program Files\IzPack\bin’

6. Create your application icon file and add it to your ’softwarepasison’ catalog. I call mine ‘exe.ico’.

7. Now comes the hardest part to get initially but after you build your first installer it all gets clear and easy :)
We need to prepare two separate xml files: one called ‘install.xml’ and another one called ’shortcutSpec.xml’.

The content of both example files is listed below:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>

<!--
   A sample installation file.
   Use it as a base for your own installers :-)
   
   To compile it :
   - go in the bin directory where you installed IzPack
   - call "compile ../sample/install.xml -b ../sample"
-->

<installation version="1.0">

    <!--
       The info section.
       The meaning of the tags should be natural ...
   -->
    <info>
        <appname>SoftwarePassion Application</appname>
        <appversion>1.0</appversion>
        <authors>
            <author name="Softberries Krzysztof Grajek" email="customer.service@softberries.com"/>
        </authors>
        <url>http://www.softwarepassion.com/</url>
    </info>

    <!--
       The gui preferences indication.
       Sets the installer window to 640x480. It will not be able to change the size.
   -->
    <guiprefs width="640" height="480" resizable="yes"/>
    <variables>
        <variable name="DesktopShortcutCheckboxEnabled" value="true"/>
    </variables>
    <!--
       The locale section.
       Asks here to include the English and French langpacks.
   -->
    <locale>
        <langpack iso3="pol"/>
        <langpack iso3="eng"/>
    </locale>

    <!--
       The resources section.
       The ids must be these ones if you want to use the LicencePanel and/or the InfoPanel.
   -->
    <resources>
        <res src="shortcutSpec.xml" id="shortcutSpec.xml"/>
        <res id="LicencePanel.licence" src="Licence.txt"/>
        <res id="InfoPanel.info" src="Readme.txt"/>
    </resources>

    <!--
       The panels section.
       We indicate here which panels we want to use. The order will be respected.
   -->
    <panels>
        <panel classname="HelloPanel"/>
        <panel classname="LicencePanel"/>
        <panel classname="TargetPanel"/>
        <panel classname="PacksPanel"/>
        <panel classname="InstallPanel"/>
        <panel classname="ShortcutPanel"/>
        <panel classname="FinishPanel"/>
    </panels>

    <!--
       The packs section.
       We specify here our packs.
   -->
    <packs>
        <pack name="Base" required="yes">
            <description>The base files</description>
            <file src="Readme.txt" targetdir="$INSTALL_PATH"/>
            <file src="Licence.txt" targetdir="$INSTALL_PATH"/>
            <file src="exe.ico" targetdir="$INSTALL_PATH"/>
            <file src="SoftwarePassionHelloIzPack.jar" targetdir="$INSTALL_PATH"/>
            <fileset dir="lib" targetdir="$INSTALL_PATH\lib">
                <include name="**"/>
            </fileset>
        </pack>
    </packs>
    <native type="izpack" name="ShellLink.dll"/>
    <native type="3rdparty" name="COIOSHelper.dll" stage="both">
        <os family="windows"/>
    </native>
</installation>

And the one for windows shortcuts:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

<shortcuts>

    <skipIfNotSupported/>

    <programGroup defaultName="Siemens AWORT" location="applications"/>

      <shortcut
        name="SoftwarePassion Application"
        programGroup="yes"
        desktop="yes"
        applications="no"
        startMenu="yes"
        startup="no"
        target="$INSTALL_PATH\SoftwarePassionHelloIzPack.jar"
        commandLine=""
        description="Software Passion Example App"
        iconFile="$INSTALL_PATH\exe.ico"
        iconIndex="0"
        initialState="noShow">

         <createForPack name="Base"/>

       </shortcut>
 

    <shortcut
           name="Software Passion Example App Uninstaller"
           programGroup="yes"
           desktop="no"
           applications="no"
           startMenu="no"
           startup="no"
           target="$INSTALL_PATH\Uninstaller\uninstaller.jar"
           commandLine=""
           iconFile="%SystemRoot%\system32\SHELL32.dll"
           iconIndex="31"
           description="Uninstall SoftwarePassion Example">

        <createForPack name="Base"/>
    </shortcut>

</shortcuts>

8. Example installation script contains both Readme.txt and Licence.txt which you can add to your ’softwarepassion’ folder.
9. Once you have it all in one place, you should have a structure like in the following screenshot:

izpack_directory

10. Now assuming that your ’softwarepassion’ folder has been placed directly on the c: drive, execute the following command:

C:\Program Files\IzPack\bin>compile c:\softwarepassion\install.xml -b c:\softwarepassion

Assuming that IzPack itself was installed at: C:\Program Files\IzPack

This will produce install.jar file which is your installer.

11. If you dont’ want your end users to click on the ‘jar’ file as not everybody knows they can do that, you can add Izpack launcher app which will launch it automatically, or use another tool to create an executable called launch4j

You have to admit that it was an easy one!!!!!

Happy Installing!


Download example files from here

Add ‘count #’ column to JTable

Posted by admin | Posted in Code Snippets, General Programming, Graphics And Design, Learning Materials | Posted on 05-02-2010

0

Important note first: This is mostly not my code, I was strugling with this problem for some time, I couldn’t google it at all, and one day, I’ve just found it. Now I use it in one of my projects but I cannot find the original post again, therefore I have decided to post it on my blog (slightly modified version of the original) so other people with similar problem will have a better chance of finding the solution.
So, the idea is very simple. The whole point of displaying additional column which is kind of independent from the other columns is to simply use another table. This way you can sort one table by column and the number column will not sort with it. – thats exactly what I was looking for and I guess its kind of popular problem.
Becouse you put two tables in one scroll pane the gui looks like its just one column!


I have created one simple method to apply this technique to any tables in my application (I use JXTable actually but JTable will do as well)

private void addLPColumn(JXTable table, JScrollPane scp){
        JTable rowTable = new RowNumberTable(table);
        scp.setRowHeaderView(rowTable);
        scp.setCorner(JScrollPane.UPPER_LEFT_CORNER, rowTable.getTableHeader());
        scp.repaint();
    }


RowNumberTable is simple one column table displaying the ‘count’ number of rows in the second table.


public class RowNumberTable extends JTable
    implements ChangeListener, PropertyChangeListener
{
    private JTable main;

    public RowNumberTable(JTable table)
    {
        main = table;
        main.addPropertyChangeListener( this );

        setFocusable( false );
        setAutoCreateColumnsFromModel( false );
        setModel( main.getModel() );
        setSelectionModel( main.getSelectionModel() );

        TableColumn column = new TableColumn();
        column.setHeaderValue("Lp.");
        addColumn( column );
        column.setCellRenderer(new RowNumberRenderer());

        getColumnModel().getColumn(0).setPreferredWidth(40);
        setPreferredScrollableViewportSize(getPreferredSize());
    }

    @Override
    public void addNotify()
    {
        super.addNotify();

        Component c = getParent();

        //  Keep scrolling of the row table in sync with the main table.

        if (c instanceof JViewport)
        {
            JViewport viewport = (JViewport)c;
            viewport.addChangeListener( this );
        }
    }

    /*
     *  Delegate method to main table
     */

    @Override
    public int getRowCount()
    {
            if(main != null)
                return main.getRowCount();
            else
                return 0;
    }

    @Override
    public int getRowHeight(int row)
    {
        return main.getRowHeight(row);
    }

    /*
     *  This table does not use any data from the main TableModel,
     *  so just return a value based on the row parameter.
     */

    @Override
    public Object getValueAt(int row, int column)
    {
        return Integer.toString(row + 1);
    }

    /*
     *  Don't edit data in the main TableModel by mistake
     */

    @Override
    public boolean isCellEditable(int row, int column)
    {
        return false;
    }
//
//  Implement the ChangeListener
//
    public void stateChanged(ChangeEvent e)
    {
        //  Keep the scrolling of the row table in sync with main table

        JViewport viewport = (JViewport) e.getSource();
        JScrollPane scrollPane = (JScrollPane)viewport.getParent();
        scrollPane.getVerticalScrollBar().setValue(viewport.getViewPosition().y);
    }
//
//  Implement the PropertyChangeListener
//
    public void propertyChange(PropertyChangeEvent e)
    {
        //  Keep the row table in sync with the main table

        if ("selectionModel".equals(e.getPropertyName()))
        {
            setSelectionModel( main.getSelectionModel() );
        }

        if ("model".equals(e.getPropertyName()))
        {
            setModel( main.getModel() );
        }
    }

    /*
     *  Borrow the renderer from JDK1.4.2 table header
     */

    private static class RowNumberRenderer extends DefaultTableCellRenderer
    {
        public RowNumberRenderer()
        {
            setHorizontalAlignment(JLabel.CENTER);
        }

        public Component getTableCellRendererComponent(
            JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        {
            if (table != null)
            {
                JTableHeader header = table.getTableHeader();

                if (header != null)
                {
                    setForeground(header.getForeground());
                    setBackground(header.getBackground());
                    setFont(header.getFont());
                }
            }

            if (isSelected)
            {
                setFont( getFont().deriveFont(Font.BOLD) );
            }

            setText((value == null) ? "" : value.toString());
            setBorder(UIManager.getBorder("TableHeader.cellBorder"));

            return this;
        }
    }
}

count_jtable_column

In my case the ‘count’ column is called ‘Lp.’ – in polish



I have created one simple method to apply this technique to any tables in my application (I use JXTable actually but JTable will do as well)

private void addLPColumn(JXTable table, JScrollPane scp){
        JTable rowTable = new RowNumberTable(table);
        scp.setRowHeaderView(rowTable);
        scp.setCorner(JScrollPane.UPPER_LEFT_CORNER, rowTable.getTableHeader());
        scp.repaint();
    }


5 Simple, But Useful CSS Properties

Posted by admin | Posted in Code Snippets, Graphics And Design | Posted on 19-11-2009

0

useful_css_properties
5 Simple, But Useful CSS Properties


5 Simple, But Useful CSS Properties

Your personal SMS Gateway with C#

Posted by admin | Posted in Code Snippets | Posted on 08-05-2009

0

Today I will describe simple but effective way to send sms from your computer using your mobile phone and a short C# application. I’m using Nokia 6070 but I guess any phone which has the ability to send SMS through AT commands will do.
First of all you would have to have a mobile phone connected to the computer through a cable and a driver for this cable installed. You need a driver to transfrom your usb cable into a serial interface.
To check if your phone is able to send sms using AT Commands please visit the following link and test it through the hyper terminal.
Once this is working you can easily do the same using C# and SerialPort class.
Here is a complete source which will transform your computer into your personal SMS Gateway:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;

namespace SMSSender
{
    class Program
    {
        static SerialPort p9 = new SerialPort("COM9", 9600);
        static void Main(string[] args)
        {
            p9.DataReceived += new SerialDataReceivedEventHandler(p9_DataReceived);
            p9.Open();
            p9.Write("ATrn");
            Thread.Sleep(500);
            p9.Write("AT+CMGF=1rn");
            Thread.Sleep(500);
            p9.Write("AT+CMGS="123456789"rn");
            Thread.Sleep(500);
            p9.Write("!personal gateway!n");
            Thread.Sleep(500);
            p9.Write("Hello Worldx1Arn");
            p9.Close();
        }

        static void p9_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                Console.WriteLine("Data received.." + p9.ReadLine());

            }catch(Exception ex){

            }
        }
    }
}

“123456789″ stands for the mobile phone number where you want to send SMS to, and ‘x1A’ is a replacement for + which you have to issue when you want to send sms on the GSM modem.

Enjoy!

Windows Forms application with Splash Screen

Posted by admin | Posted in Code Snippets | Posted on 28-04-2009

0

Today I will describe how to add simple yet effective splash screen functionality using Visual Basic assembly goodness. To show exactly how to do that I will create a simple project describing step by step what you have to do to create splash screen for your win forms application.
First of all, lets create a simple windows forms application, open up Visual Studio and select File -> New -> Project.

screen1

Select ‘Windows Forms Application’, set the name for your project and click ‘OK’.
This creates simple windows forms application which you can start up by clicking ‘F5′.

Lets say that my Form1.cs is the main form of the application I want to start up. Before launching Form1 I want to create a splash screen to load some additional resources, connect to the database or remote server. The project structure for now should look like this:

screen2

Now, when we have our application created and ready to be launched we create a splash screen for it. We create the splash screen itself first and than modify a bit the code to launch our splash before the main application starts.
Our splash itself would be another form which we modify a bit to look like, well, like a splash screen :) .
Lets add another form to our project:

screen3

and name it ‘SplashForm.cs’. This form will be launched until our main form loads. To make it look like a proper splash screen we have to find the property named ‘FormBorderStyle’ and set it to ‘None’, as well as set the ‘StartPostion’ to ‘CenterScreen’.
You can prettify the splash form by adding picture boxes, labels etc to add your custom graphics etc. I’m going to add a simple label and animated gif loader to my form. You can use animated gifs generated on this website which I found very cool.

screen4

Ok, our splash screen is ready, now we need to modify the code which starts our application to show the splash screen and destroy it when the main form is ready.
Originally, when you create the windows forms type of project a simple class called ‘Program’ gets created. You can find the source for this class inside ‘Program.cs’ file. It should look somthing like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace TestFormApplication
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

In order to use our splash form created earlier correctly we need to modify this code a bit. First, add a reference to Microsoft.VisualBasic assembly found under .NET tab. Go to you project references and find the following:
screen5
Next, add the folowing piece of code among other ‘using’ statements on top of the ‘Program’ class file:

using Microsoft.VisualBasic;
using Microsoft.VisualBasic.ApplicationServices;

Once this is done you are ready to use the VB goodness. Add the following code below the Program class, inside the same namespace as the Program class itself:

class SplashApp : WindowsFormsApplicationBase
        {
            protected override void OnCreateSplashScreen()
            {
                this.SplashScreen = new SplashForm();
            }
            protected override void OnCreateMainForm()
            {
                //Connect to db, remote server or anything you like in here
                System.Threading.Thread.Sleep(2000);
                //create your main form and the SplashForm will close here automatically
                this.MainForm = new Form1();
            }
        }

You have to modify the main method a bit as well, use the main with arguments option and start you application using our newly created SplashApp class, the full source code for the Program.cs will look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.ApplicationServices;

namespace TestFormApplication
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            new SplashApp().Run(args);
        }
        class SplashApp : WindowsFormsApplicationBase
        {
            protected override void OnCreateSplashScreen()
            {
                this.SplashScreen = new SplashForm();
            }
            protected override void OnCreateMainForm()
            {
                //Connect to db, remote server or anything you like in here
                System.Threading.Thread.Sleep(2000);
                //create your main form and the SplashForm will close here automatically
                this.MainForm = new Form1();
            }
        }
    }
}

You can download the whole VS 2008 solution file here.

Reading XML from the web using C#

Posted by admin | Posted in Code Snippets | Posted on 27-04-2009

0

Today I will describe quick and dirty way to read xml data received as a web response. This can get very usefull if you want to send some information in a quck way without creating whole bunch of web service references etc.

Lets say I want to get some data from the server in the xml like this:


&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
  &lt;root&gt;
  &lt;order&gt;
  &lt;order_id&gt;100000001&lt;/order_id&gt;
  &lt;order_date&gt;2009-04-20 15:09:29&lt;/order_date&gt;
  &lt;b_name1&gt;Kris Gr&lt;/b_name1&gt;
  &lt;b_name2&gt;softberrieseshop&lt;/b_name2&gt;
  &lt;b_address_line1&gt;adres 1 adres 2&lt;/b_address_line1&gt;
  &lt;b_city&gt;Elblag&lt;/b_city&gt;
  &lt;b_postcode&gt;2341234&lt;/b_postcode&gt;
  &lt;b_tel&gt;65464564666&lt;/b_tel&gt;
  &lt;b_fax&gt;6546876465&lt;/b_fax&gt;
  &lt;item&gt;
      &lt;item_sku&gt;DZFOREVER2&lt;/item_sku&gt;
      &lt;qty_ordered&gt;1.0000&lt;/qty_ordered&gt;
      &lt;qty_invoiced&gt;1.0000&lt;/qty_invoiced&gt;
      &lt;qty_refunded&gt;0.0000&lt;/qty_refunded&gt;
      &lt;qty_shipped&gt;0.0000&lt;/qty_shipped&gt;
      &lt;qty_canceled&gt;0.0000&lt;/qty_canceled&gt;
  &lt;/item&gt;
  &lt;/order&gt;
  &lt;/root&gt;

Retrive the xml information contained within XML document using the url to the document itself (urlString):

             XmlDocument document = new XmlDocument();
             document.Load(urlString);
             XmlElement root = doc.DocumentElement;
             XmlNodeList nodes = root.SelectNodes(&quot;/root/order&quot;); 

             foreach (XmlNode node in nodes)
             {
                 string idStr = node[&quot;order_id&quot;].InnerText;
                 string dateStr = node[&quot;order_date&quot;].InnerText;

                 XmlElement root2 = doc.DocumentElement;
                 XmlNodeList nodes2 = root.SelectNodes(&quot;/root/order/item&quot;);
                  foreach (XmlNode nodex in nodes)
                 {
                       string sku = nodex[&quot;item_sku&quot;].InnerText;
                 }
             }

Simple and dead easy!

Launch your application automatically when the system starts with C#

Posted by admin | Posted in Code Snippets | Posted on 23-04-2009

0

In this article I will describe automatic startup of your c# application on windows machine using the registry settings. This article is somehow opposite to the one posted earlier about automatic shutdown.
To set the registry for your application to start up automatically you need to find the key responsible for automatic startup and set value of your application on this key:

RegistryKey rkey = Registry.CurrentUser.CreateSubKey(@"SoftwareMicrosoftWindowsCurrentVersionRun");
             rkey.SetValue("YourApplicationName", Assembly.GetExecutingAssembly().Location);

Executing this from your program will automatically add your assembly to auto start, this is thanks to Assembly.GetExecutingAssembly().Location piece.
When running your application you can easily check if you app has the key set:

RegistryKey rkey = Registry.CurrentUser.OpenSubKey(@"SoftwareMicrosoftWindowsCurrentVersionRun");
              if(rkey == null)
                      //not set
              if((string)rkey.GetValue("YourApplicationName") == null)
                      //not set either

If you decide that you don’t want the application to be started automatically when the system starts, you have to delete the value for the key:

RegistryKey rkey = Registry.CurrentUser.CreateSubKey(@"SoftwareMicrosoftWindowsCurrentVersionRun");
              rkey.DeleteValue("YourApplicationName");

And thats it. Registry looks scary at the beginning but hey, this was fun and easy!