6/25/2016

Removing Unwanted Tab Selection in JTabbedPane

JTabbedPane is  a Jcomponent under java swing. The JTabbedPane can have several tabs. 
There is a little bit confusion over left and right mouse click events. The confused scenario is when the user right clicks on one of the tabs, it is  set as the selected tab. It is too confusing when we need to display the right click popup menu for the selected tab. When the user needs to right click and get the pop up menu for selected tab, then it changes the selected tab because of the right click.

So here is the proper solution.
First we need to identify whether the mouse event comes from the right click or the left click.
If it is a left click, then simply change the selected tab, if it is a right click, then we need to display the popup menu or do nothing.
In order to do that we need to remove the unwanted tab selection when we click on the right mouse button.
That Mouse Listener comes under the JTabbedPane UI object. It is better to override rather than remove it for future code updates.

The BasicTabbedPaneUI  has an inner class called Handler. That handler class overrides the Mouse Listener. To stop the right click tab selection and show pop up menu, we need to override this method in BasicTabbedPaneUI,

protected MouseListener createMouseListener() {
    return getHandler();
}
To get a better look and feel we should override SynthTabbedPaneUI class as well. SynthTabbedPaneUI extends BasicTabbedPaneUI.
So our inner class is like this,

private class SynthTabbedPaneUIWrapper extends SynthTabbedPaneUI {
 private MouseAdapter menuAdapter;

 /**
  * this will return the pop up menu adapter.
  *
  * @return menuAdapter - MouseAdapter
  */
 private MouseAdapter getMenuAdapter() {
  if (menuAdapter == null) {
   menuAdapter =
    new MouseAdapter() {
     @Override
     public void mouseReleased(final MouseEvent e) {
      final JTabbedPane tabbedPane = (JTabbedPane) e.getSource();

      //show only PopupMenu for right click.
      if (!tabbedPane.isEnabled() || SwingUtilities.isRightMouseButton(e)) {
       if (e.isPopupTrigger() && (e.getClickCount() == 1)) {
        //show popup menu functions goes here
       }

       return;
      }

      //change the selected tab when left click.
      changeTabItemSelection(e);
     }
    };
  }

  return menuAdapter;
 }

 /**
  * Used to Change the tab item selection when right clicked or double clicked.
  *
  * @param e - MouseEvent
  */
 private void changeTabItemSelection(final MouseEvent e) {
  final JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
  final int tabIndex = tabbedPane.getUI().tabForCoordinate(tabbedPane, e.getX(), e.getY());

  if ((tabIndex >= 0) && tabbedPane.isEnabledAt(tabIndex)) {
   if ((tabIndex == tabbedPane.getSelectedIndex()) && tabbedPane.isRequestFocusEnabled()) {
    tabbedPane.requestFocus();
    tabbedPane.repaint(tabbedPane.getUI().getTabBounds(tabbedPane, tabIndex));
   } else {
    tabbedPane.setSelectedIndex(tabIndex);
   }
  }
 }

 @Override
 protected MouseListener createMouseListener() {
  return getMenuAdapter();
 }
} 

After that we can set our custom UI object into TabbedPane.

tabbedPane.setUI(new SynthTabbedPaneUIWrapper());
Here is my stackoverflow related answer for this type of problem. 

SMS Library for a voting application


I implemented a simple voting application using SMSLib. I used the following technologies: 
  • SMSLib: a universal API for SMS messaging
  • JAVA Swing: the main programming language
  • MongoDB: for ORM
  • gson:  to convert Java objects into JSON and back
  • GSM modem: to receive vote messages
When I was using the SMSLib API, it became a bit complicated when configuring my personal computer. There are lot of resources on how to setup the SMSLib . In my opinion, following are the best resources to configure your computer without any problem.

gsm modem tutoial by samyan
how to send and receive sms using sms lib api
How to use SMSLIB in 64 bit windows 7 ultimate and other 64 bit platforms

You can use any GSM modem, in my case I used a Huawei GSM modem. Please note that most of the SMSLib integration problems comes when we connect the modem with the SMSLib. After successful configuration,  you can use the SMSLib with sending and receiving messages.

The voting application I implemented has the following features:
  • A person can vote for only one candidate.
  • Can run only one voting event at a time.
  • Can add groups or individual participants for an event.
  • Participant's details are sorted according to the vote count.
  • Can add participants even for an on going event.
  • Can view previous events' history details.
  • Can re run previous events as well.
The technologies that I used is so easy to understand and can complete the task within a short time period. The application is developed using MVC design pattern in order to do any future enhancements. If you are going to use SMSLib for voting or any similar scenario, the business model will not be the same as mine, But I would like to share my code with you to help you achieve your target and learn SMSLib API.

Here is my GitHub location of SMSLib voting application

Feel free to download and use it. Please add a star if it was any help to you ;)

6/09/2015

How To Create Better API



Wikipedia definition - In computer programming, an application programming interface (APIspecifies how some software components should interact with each other.
Nowadays we can find a lot of open source APIs that are built and maintained by organizations as well as by developers. 

If we are going to create our own API, we should definitely follow some design principles to get better solutions. Most important fact we have to consider is our API should be appropriate to  related audience. 
In our 4th year research project we had to communicate with various open-source APIs and also we had to create our own APIs as well. 

So we found some good posts and tutorials to achieve our targets. I think these details are very useful to others as well.

Most valuable explanation

how-to-design-an-api-best-practises-concepts-technical-aspects

how-to-create-an-api-10-tutorials

creating-a-restful-api-with-php

how-do-you-define-a-good-or-bad-api

what-is-the-best-way-to-design-a-client-api



If you found any other useful resources, please make a comment and share with others as well. :)



6/02/2015

MongoDB Useful Resources




MongoDB is a leading NoSQL document database written in c++  that provides high performance, high availability, and easy scalability.


When you are going to learn and understand document database concepts best way is start with MongoDB.
In tutorialsPoint mogoDB tutorial explains clearly how to work with it.
You can test your code online without installation by using Try MongoDB (mongoDB shell in your browser). You can not create any database inside of the try mongoDB but you can use 'test' database and create any number of collections.
If you want to integrate java with mongoDB, I think this is the best tutorial.

After learning the basics of MongoDB,  I created a simple desktop application with java.
You can download it from my github project location and play around :)