Pages

Wednesday, August 8, 2012

MGWT - A very simple example - click the button

MGWT

MGWT stands for developing mobile applications with GWT and mgwt.
Mgwt works on all kind of mobile devices that support webkit (like iPhone, iPad, iPod, Android, Blackberry, ...)
  • Native support for touchevent the GWT Way
  • Animations built in with GWT MVP
  • lots and lots of widgets
  • specific theme for each plattform (iOS, Android, ...)

 Let' se a simple example starting from a Google Web Toolkit(GWT) project:

1. Create a GWT project
2. In your gwt xml file add this line:
   <inherits name='com.googlecode.mgwt.MGWT'/>

3. Add the MGWT library to your project
(It can be downloaded from: http://www.m-gwt.com/ )

4. And your EntryPoint will be this:


public class mobileEntryPoint implements EntryPoint {

    //global variables
    int countI;
    MTextBox lbl;
   
    public void onModuleLoad() {
                //initialization
                lbl = new MTextBox();
                countI=0;
       
                //set viewport and other settings for mobile
                MGWT.applySettings(MGWTSettings.getAppSetting());
                //build animation helper and attach it
                AnimationHelper animationHelper = new AnimationHelper();
                RootPanel.get().add(animationHelper);

                //build some UI
                LayoutPanel layoutPanel = new LayoutPanel();
                Button button = new Button("Hello User!");               
                //for click event
                button.addTapHandler(new TapHandler() {
                    @Override
                    public void onTap(TapEvent event) {
                        countI++;                                               
                        lbl.setText("You clicked: "+countI+" times");
                    }                   
                }) ;                                                                                             
                layoutPanel.add(button);
                layoutPanel.add(lbl);

                //animate at click
                animationHelper.goTo(layoutPanel, Animation.SLIDE);              
    }
}

By using gwt-phonegap and mgwt you can write applications that can be deployed into the app store or the market place with GWT.

Thursday, June 14, 2012

Wednesday, June 6, 2012

JavaFx - CHART example

We would like to have a chart of the imported fruits. 
Let' see in JavaFx the PieChart diagram:

public class JavaFXApplication1 extends Application {

    public static void main(String[] args) {
        launch(args);
    }
   
    @Override
    public void start(Stage primaryStage) {
       
      primaryStage.setTitle("Chart example");
      ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
                new PieChart.Data("Grapefruit", 13),
                new PieChart.Data("Oranges", 25),
                new PieChart.Data("Plums", 10),
                new PieChart.Data("Pears", 22),
                new PieChart.Data("Apples", 30));
        PieChart chart = new PieChart(pieChartData);
        chart.setTitle("Imported Fruits");       
       
        StackPane root = new StackPane();
        root.getChildren().add(chart);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
               
    }
}

Java - read from console, dos prompt

The JAVA file is the following, for an example of reading input values from console:

package javaapplication1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {
       
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
       
        String prompt = "";       
        System.out.println("Enter your name: ");
        prompt = reader.readLine();
        if (prompt.equals("\t"))
            System.out.println("You entered a TAB from your keyboard.");
        else
            System.out.println("Your name is: "+prompt);
       
    }
}

Friday, June 1, 2012

Java - sorting strings with Hungarian characters

        Let's sort some Hungarian strings in alphabetical order:

        String[] words = {"Bruce","Géza","Ákos"};

        Collator c = Collator.getInstance(new Locale("hu"));      
        Arrays.sort(words, c);


        for(int m=0; m<words.length; m++)
            System.out.print(words[m]+", ");

Tuesday, May 29, 2012

Java - Enumerated types

To enumerate some constant values, and/or associate to them methods or values, we can use enum in java. Enum types in Java are considered like classes, they can have constructors too, can implement interfaces, but can have no descendants. Let see, how we work with enum in java, through an example that classifies programming languages.
Create your own test class, and then test the following code:

    private enum prog_language {
        //enumerated constants
        java(true), c(true), basic(true), php(true), delphi(true), csharp(false), cplus(false);
       
        //fields to associate with the enum types
        private int position;
        private boolean usedbyme;
       
        //constructor
        prog_language(boolean bUsedByMe) {
            usedbyme = bUsedByMe;
        }           
        //getters and setters of the fields
        public int getPosition() {
            return position;
        }
        public void setPosition(int position) {
            this.position = position;
        }          
        public boolean isUsedbyme() {
            return usedbyme;
        }       
    };

.......................................       
        int i = 0;
        for (prog_language p : prog_language.values()) {
            i++;
            p.setPosition(i);
        }                              
        System.out.print(prog_language.valueOf("java").name());      
        System.out.println(" - at position: "+prog_language.valueOf("java").getPosition());
        System.out.println("I am using this language: "+prog_language.valueOf("java").isUsedbyme());

   
 
Enum types can also participate in switch-case statements too.

Thursday, May 24, 2012

Java - Data Structures

Data Structure       Advantages                Disadvantages



Array
Quick insertion
(very fast access if index known.)
Slow search, 
Slow deletion, 
fixed size.
Ordered Array
Quicker search
Slow insertion,
Slow deletion,
fixed size. 
Stack
Provides last-in, first-out access
Slow access to other items
Queue
Provides first-in, first-out access
Slow access to other items
Linked list
Quick insertion, quick deletion
Slow search
       ArrayList           
(is same as a Vector. Vectors are
synchronized, can be thread safe, but
slower)
Binary Search, Sort
Slow insertion, deletion
Binary tree
Quick insertion, deletion (if tree remains balanced), and search.
Deletion algorithm is complex
Red-black tree
Quick insertion, deletion (this tree is always balanced), and search.
Complex
2-3-4 tree
Quick insertion, deletion (this tree is always balanced, is good for disk storage progr.), and search.
Complex
Hash table (~arrays)
Very fast access if key known. Fast insertion
Slow deletion, access slow if key not known, inefficient memory usage.
Heap (~LinkedList)
Fast insertion, deletion, access to largest item.
Slow access to other items
Graph
Models real-world situations.
Slow and complex.
                       

Java - Maps / storing values in key-value pair

        Map<String, Integer> mPair = new HashMap<String, Integer>();       

        mPair.put("one", new Integer(1));
        mPair.put("two", new Integer(2));
        mPair.put("three", new Integer(3));
        System.out.println(mPair.values());

        if (mPair.containsKey("two"))
            System.out.println(mPair.get("two"));

Thursday, May 17, 2012

Vaadin - TextField componet, value change listener

Working with TextField component in Vaadin framework web environment, is much like, working with components in swing/awt applications. Here, we also have events and listeners.
For example, changing a value in a TextField componet, can be captured right after the field, loses focus.



Just have the following class and add to your Vaadin web application:

public class MyApplication extends Application {
    //global components
    TextField tfInput;
    Window mainWindow;
   
    //main method - init()
    @Override
    public void init() {
    mainWindow = new Window("MyApplication");
        tfInput = new TextField("Enter your Name: ");
        tfInput.setValue("empty");
       
        //handling the change of the texfield value property:
        tfInput.addListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(ValueChangeEvent event) {
                // Assuming that the value type is a String
                String value = (String) tfInput.getValue();               
                Window.Notification notif = new Window.Notification("Be warned "+value+"!",
                        "<br/>This message warning, after a field value change, stays centered, till you do sthg!",
                        Window.Notification.TYPE_WARNING_MESSAGE);
                notif.setPosition(Window.Notification.POSITION_CENTERED);
                mainWindow.showNotification(notif);
            }           
        });       
        // Fire value changes immediately when the field loses focus
        tfInput.setImmediate(true);       
               
    mainWindow.addComponent(tfInput);
    setMainWindow(mainWindow);
    }
}

The same thing you can achieve by binding the TextField component to an ObejectProperty value. The change of this ObjectProperty value, is immediately reflected anywhere.

public class MyApplication extends Application {
    Double trouble = 42.0;
    final ObjectProperty<Double> property = new ObjectProperty<Double>(trouble);
      
    @Override
    public void init() {
    Window mainWindow = new Window("MyApplication");
        TextField tf = new TextField("The Answer",property);
        tf.setImmediate(true);
       
        Label feedback = new Label(property);
        feedback.setCaption("The Value");
       
        Panel pnl = new Panel();
        pnl.addComponent(tf);
        pnl.addComponent(feedback);
       
    mainWindow.addComponent(pnl);
    setMainWindow(mainWindow);
    }
}

Just try it and test, this code on: http://lehelsipos.appspot.com/