Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Archive for the ‘Programming’ Category

From Java Swing Dimension JFormattedTextField bite

leave a comment »

Today, I poke around Java Swing to build up a simple data entry. One thing I wanted to do is to create a number input field so as usually I hack through the API document and found this little JFormattedTextField component. By the judge from the API point of view it is pretty easy to use. All we have to do is instantiate the component with preferred format or mask and that is pretty much about it.

numField = newJFormattedTextField(NumberFormat.getIntegerInstance());
container.add(numField);

Now, I wanted to hook an event listener to this field and retrive new value

/* this, container which implements PropertyChangeListener */
numField.addPropertyChangeListener("value", this);

So basically we got this:

numField = new JFormattedTextField(NumberFormat.getIntegerInstance());
numField.addPropertyChangeListener("value", this);
container.add(numField);

Now if you try to call .setValue(new Integer(100)) you will present with an exception java.lang.NullPointerException. After inspecting the PropertyChangeEvent class definition I believe that Java try to compare old value and new value, but we haven’t set any value before we hook up an event listener. Therefore, Java takes it liberties to raise an error in our face and that is fair enough right?

How to fix this? Well, it is simple set initial value before hook an event listener would solve the problem.

numField = new JFormattedTextField(NumberFormat.getIntegerInstance());
numField.setValue(new Integer(0)); // this line is your savier!
numField.addPropertyChangeListener("value", this);
container.add(numField);
Advertisements

Written by Chorn Sokun

May 13, 2014 at 8:06 am

Posted in Programming

Tagged with ,

Joljet mobile client

leave a comment »

A few years ago, I built a music portal to experience ExtJS and of cause to create my own music listening experience. It was fun and I have learnt a lot from this exercise from hand craft the app, built script and caching optimization. As always with great power come great responsibility. Due to the size of ExtJS core, the page load is not satisfy by most. However, the site hosted on cheap server and not using any CDN therefore I can’t push it too far.

Moving on, I have done with ExtJS and I know the site works perfectly on desktop platform. However, on mobile it is kind of suck. But well, as a pet project I’m not too worry about that until I’m need to listen to my favorite song on the go then mobile client is needed. So, I decided to built one for myself AGAIN! :D. Yeah, again. Why not after all I’m a craftman I love builting stuffs.

Now here is the plan

Image

I’m going to built mobile client which reuse all functionality from the portal and at the same time extending the functionalities to support offline, favorite & playlist customization or by the way I don’t want to be responsible for username & password so I am going to offload that task to Facebook.

Sound interesting right ;) to make it more interesting I shall say that I have no clue how to built a mobile app at this point. But, let see how long it will take for me to built one :D

 

Written by Chorn Sokun

May 12, 2014 at 8:07 am

Yii overwrite accessRules in base class

leave a comment »

Suppose you defined a base class Controller with basic accessRules

class Controller extends CController
{
  public function filters()
  {
    return array(
      'accessControl',
      'postOnly + delete',
    );
  }

  public function accessRules() {
    return array(
      // allow authenticated user to perform 'create' and 'update' actions
      array('allow', 'actions'=>; array('index', 'view'), 'users' => '@'),

      // deny all users
      array('deny', 'users'=>'*'),
    );
  }
}

and you want to add (overwrite) rules in derived class, using array_merge here how you do it:

class Site extends Controller
{
  public function accessRules()
  {
    return array_merge(
      // allow all user to perform 'help' actions
      array( array('allow', 'actions'=> array('help'), 'users' => '*') ),

      // parent go last
      parent::accessRules()
    );
  }
}

As best practice your base class default access rule is deny all by default.

Written by Chorn Sokun

October 22, 2012 at 3:34 pm

Code Reading Skills for ExtJS newbie

leave a comment »

@samnangchhun wrote a blog post talking about this particular subject, I just want to share my experience exploring a new tool especially the ExtJS library from sencha.com

Now there is something special about sencha.com which I love, they put alot of effort in documenting their library API as well as sample projects in every release.

But like any other developers out there the only page in the document that I would read slowly is getting start, once I could say hello I would try to solve my problem with this new library/framework. That the only way that keep me focus and dig deeper into the document, source code, sample project, testing unit whatever available. At the end of the day I would know that this is the right tool or not.

How do i get this quick in adapting with new environment? I walk the same path as what @samnangchhun described in his blog and I did it enough that I can quick capture the convention and pattern of the thing I want to know. So if you haven’t read enough code do it now ;)

Great weekend everyone.

Written by Chorn Sokun

May 5, 2012 at 4:23 pm

LINQ 101 .NET Reflection

leave a comment »

In this post I am going over three points listed bellow:

What is .NET Reflection? Cut short it refer to a set of API built-in .NET Framework, which give you a greate power to load and manipulate type within assembly (.dll) one of the most favor use is to support late binding. There a whole lot of explaination available through google! but that all I care for now.

How about LINQ? stand for .NET Language-Integrated Query,

 a set of general purpose standard query operators that allow traversal, filter, and projection operations to be expressed in a direct yet declarative way in any .NET-based programming language. The standard query operators allow queries to be applied to any IEnumerable<T>-based information source.

What it mean to me?
Assume that I had ITrap interface and a bunch of Trap implementation as show in the code bellow:

interface ITrap{
  void Setup();
  int OrderNr { get; };
}

class LevelOneTrap: ITrap{
  public void Setup(){
    Console.WriteLine("LevelOne Setup First");
  }
  public int OrderNr {
    get { return 1; }
  }
}

class LevelTwoTrap: ITrap{
  public void Setup(){
    Console.WriteLine("LevelTwo Setup Next");
  }
  public int OrderNr {
    get { return 2; }
  }
}

Now I want to Setup all trap by respecting it OrderNr which mean LevelOneTrap.Setup() must execute before LevelTwoTrap.Setup() so far so good? Let see how many lines of code needed:

var trapInterface = typeof(ITrap);

var trapImplementations = ( from t in trapInterface.Assembly.GetTypes()
                      where t.IsClass && t.GetInterfaces().Any(x => x == trapInterface)
                      select (ITrap)Activator.CreateInstance(t)
                  ).OrderBy(k=>k.OrderNr);

// time to play ;)
foreach( var trap in trapImplementations ) {
  trap.Setup();
}

//   LevelOne Setup First
//   LevelTwo Setup Next

Cool isn’t it? Life is short so our code!

Written by Chorn Sokun

May 7, 2011 at 12:39 pm

Posted in Programming, Tips & Tricks

Tagged with , ,

.NET Database Application Development (C#)

leave a comment »

If you are interested in .NET and want to jump start with Database Application Development in no time here is your chance

Lady & Gentlemen, boy & girl I present you a .NET Database Application Development (C#) fast track with the following content:

  1. C# Programming Introduction
  2. Introduction to NHibernate .NET Object Relational Mapping (ORM)
  3. Advance Data Processing with NHibernate Criteria API
  4. Hand-on practice

At the end of the course your will learn tools & technique to develop database application which could talk various Database Management System (DBMS) from Microsoft Access, Microsoft SQL Server, MySQL you name it.

Now the truth about doing this in no time :D I lie :-P I need minimum 10 hours before I can transform a newbie into a .NET Database Application Development (C#)

Requirement:

  • Basic Programming Concept
  • Know basic C# Programming
  • Be able to read and understand this blog post – otherwise get yourself English tutor

If you, your friend interested or your employer got budget to develop your capacity drop me a line I would love to hear about it ;)

Written by Chorn Sokun

December 5, 2010 at 4:36 pm

Mocking ASP.NET MVC 2 Context & Session using RhinoMock

leave a comment »

Watching video record from #mvcconf about “Making ASP.NET Apps Testable” by Eric B. Sowell. He showing how to test ASP.NET MVC Session make me wonder how difficult it is for RhinoMock so I load sample app and here how I test it. Since

[Test]
public void TestSession()
{
   // Arrange
   // HttpContextBase, HttpSessionStateBase are abstract classes
   // I can ask RhinoMock to generate stubs for those classes
   var httpContext = MockRepository.GenerateStub<HttpContextBase>();
   var session = MockRepository.GenerateStub<HttpSessionStateBase>();

   session["Name"] = "Sokun";
   httpContext.Stub(c => c.Session)
      .Return(session);

   // act
   controller.ControllerContext = new ControllerContext(httpContext, new RouteData(), controller);
   var result = controller.TestSession() as ViewResult;

   // assert
   Assert.IsNotNull(result);
}

and the action:

public ActionResult TestSession()
{
   ViewData["Name"] = Session["Name"];
   return View();
}

So, what you think? it just fine to love ASP.NET MVC 2

Written by Chorn Sokun

July 26, 2010 at 10:45 am