Thursday, April 28, 2011

Creating a plugin interface in Java in only 16 lines of code

I have been working on refactoring a java graphics package for the graphics program at EWU and noticed that it was adding tabs to a JPanel, so I figured that it could put all the tabs in a package then scan the directory for appropriate classes and load them dynamically. Voila instant plugin architecture.

It turned out to be pretty simple using Java's ClassLoader interface. The only complication was that my tabs had parameters in their initialization functions, so I needed to create a super class(MasterPanel) that held that functionality and call that instead of casting the class as a JPanel, since I don't know what the panel names might be in the future.

//Get the path of the package relative to the project directory
String packageName = "";
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 
String path = "./" + packageName.replace('.', '/');
//Check all the files in the package directory for Panel.class files
//If there are any add them to this panel as tabs 
File dir = new File(path); 
File[] files = dir.listFiles(); 
for(File curFile : files) 
  if(curFile.getName().endsWith("Panel.class") && !curFile.getName().endsWith("MasterPanel.class"))
    String curPanel = curFile.getName(); 
    int index = curPanel.indexOf('.'); 
    curPanel = curPanel.substring(0, index); 
    Class<MasterPanel> loadPanel = (Class<MasterPanel>) Class.forName(packageName + "." + curPanel); 
    Constructor<MasterPanel> constructPanel = loadPanel.getConstructor(new Class[]{Scene.class}); 
    MasterPanel newPanel = constructPanel.newInstance(new Object[]{theScene}); 
    if (newPanel != null) 
      tabPane.addTab(, newPanel); 

Thursday, April 21, 2011

Time to bite the bullet.

I've been interested in making my own website for quite a while, but being so public I always felt shy about putting my first attempts up.  Also, I never felt I had anything interesting to put on a web site.

But I decided to go ahead with it.  And I'm starting with Linode as my hosting service, since they have a suite of wonder tutorials that will guide me though all the steps I need to get going.

What finally pushed me over the edge to build one was Nai over at pragmaticstartup)). This guy is starting from ground zero and letting people know about his successes and failures. So, why can't I do the same? Of course my first website will suck, but that's where everyone starts.

I guess I've just let my pride in knowing what I'm doing get in the way of trying new things.

Stupid solarmist, get over yourself or you'll never grow.