Friday, August 21, 2009

Screenshots of professional software tools.

I am putting the screen shots in no particular or thought.
On seeing each one of them
1) Guess what software it is.
2) Where all it was used.
3) Where all it could have been used.

TODO:// Had a limit of only 5 images. So would edit later.

Monday, August 17, 2009

Listener pattern implementation in java taught by Manju

//
// LISTENER PATTERN IMPLEMENTATION
// 1 NOTIFIER CLASS
// 3 RECEVIER CLASSES
// 2 EVENTS
//

import java.util.ArrayList;
import java.util.List;

public class ListenerImpl
{
public static void main(String[] args)
{
new ListenerImpl().run();
}

private void run()
{
// Base class ; sender of the message
Employee emp = new Employee("Ramesh", 24);

// Instantiate recevier classes that needs notification
Admin admin = new Admin();
Manager manager = new Manager();
CEO ceo = new CEO();

// NOTE: we could go ahead and add new classes for extension.

// add them all
emp.addEmployeeAttrChangeListener(admin);
emp.addEmployeeAttrChangeListener(manager);
emp.addEmployeeAttrChangeListener(ceo);

// Event 1
// notifies all classes
emp.setName("Manju");

System.out.println("\n\n");

// Event 2
// notifies all classes
emp.setAge(29);


}

class Employee
{
List listeners;
String name;
int age;

public Employee(String name, int age)
{
listeners = new ArrayList();
this.name = name;
this.age = age;
}

public void addEmployeeAttrChangeListener(EmployeeAttrChangeListener listener)
{
this.listeners.add(listener);
}

public void removeEmployeeAttrChangeListener(EmployeeAttrChangeListener listener)
{
this.listeners.remove(listener);
}

public void fireNameChangedEvent(String oldName, String newName)
{
for (EmployeeAttrChangeListener listener: listeners)
{
listener.nameChanged(oldName, newName);
}
}

public void fireAgeChangedEvent(int oldAge, int newAge)
{
for (EmployeeAttrChangeListener listener: listeners)
{
listener.ageChanged(oldAge, newAge);
}
}

public int getAge()
{
return age;
}

public void setAge(int age)
{
// notify and change
this.fireAgeChangedEvent(this.age, age);
this.age = age;

// NOTE : for notify after change swap statments
}

public String getName()
{
return name;
}

public void setName(String name)
{
// notify and change
fireNameChangedEvent(this.name, name);
this.name = name;

// NOTE : for notify after change swap statments
}
}
// Class A must implement appropriate listener interface.
class Admin implements EmployeeAttrChangeListener
{
public void nameChanged(String oldName, String newName)
{
System.out.println("In Admin block : Name change ");
System.out.println(oldName + " has been changed to " + newName);
}

public void ageChanged(int oldAge, int newAge)
{
System.out.println("In Admin block : Age change ");
System.out.println(oldAge + " has been changed to " + newAge);
}
}
// Class B must implement appropriate listener interface.
class Manager implements EmployeeAttrChangeListener
{
public void nameChanged(String oldName, String newName)
{
System.out.println("In Manager block : Name change ");
System.out.println(oldName + " has been changed to " + newName);
}

public void ageChanged(int oldAge, int newAge)
{
System.out.println("In Manager block : Age change");
System.out.println(oldAge + " has been changed to " + newAge);
}
}

// Class C must implement appropriate listener interface.
class CEO implements EmployeeAttrChangeListener
{
public void nameChanged(String oldName, String newName)
{
System.out.println("In CEO block : Name change ");
System.out.println(oldName + " has been changed to " + newName);
}

public void ageChanged(int oldAge, int newAge)
{
System.out.println("In CEO block : Age change");
System.out.println(oldAge + " has been changed to " + newAge);
}
}


// Listener interface class ( in real world there would be marker implementations )

interface EmployeeAttrChangeListener
{
public void nameChanged(String oldName, String newName);

public void ageChanged(int oldAge, int newAge);
}
}


// SAMPLE OUTPUT
/*

In Admin block : Name change
Ramesh has been changed to Manju
In Manager block : Name change
Ramesh has been changed to
Manju
In CEO block : Name change
Ramesh has been changed to
Manju



In Admin block : Age change
24 has been changed to 29
In Manager block : Age change
24 has been changed to 29
In CEO block : Age change
24 has been changed to 29


*/

// Usage notes :
//
// Save entire file as ListenerImpl.java
// javac ListenerImpl.java
// java ListenerImpl

// The above example receiver classes are inner classes for sake of simplication
// It need not be the case in real situations.

My Qt Experience

Qt is a quick UI protyping tool. You used this in your university days to get
the presentation logic going. Thanks for Sujeeth and Sreenivas N Keeni for pursuing
and encouraging use of Open technologies. And definitely an advantage since it works on Linux and a de-facto platform for servers-class machinces.

Often I find that you would create a ui file but struggle to get build steps and
generate executable. Here goes the steps.

Step 0:
Ensure uic, moc and g++ is on your path doing which on each of these executeables

Step 1:
uic -o project.h project.ui

Step 2:
uic -i project.h -o project.cpp project.ui

Step 3:
moc -o moc_project.cpp project.h

Step 4:
if in your ui file you have renamed Form1 to something else (like in here it is MyForm) make appropriate changes
Ensure file main.cpp exists

And main.cpp :
-----
cat main.cpp
#include "qapplication.h"
#include "project.h"

int main(int argc, char* argv[])
{
QApplication app(argc,argv);
Form1 f1;
app.setMainWidget(&f1);

f1.show();
return app.exec();

}
----

Step 5:
Generate executable:

g++ -o test -I$QTDIR/include -L$QTDIR/lib main.cpp project.cpp moc_project.cpp -lkdevelop
NOTE : the link option that worked in Redhat 7.2 was -lqt , on RHEL I had to use -lkdevelop

Step 6:
this should have created Linux executable named test.
Run it ./test

Time to enjoy.

Followers