/*
* JarefiApp.java
*/
package jarefi;
import jarefi.bl.ResourceFinder;
import jarefi.bl.ResourceFinderImpl;
import jarefi.ui.JarefiView;
import jarefi.ui.handler.UIHandlers;
import java.net.URL;
import java.util.List;
import java.util.logging.Logger;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
/**
* The main class of the application.
*/
public class JarefiApp extends SingleFrameApplication {
private static final Logger log = Logger.getLogger(JarefiApp.class.getName());
private ResourceFinder resoureFinder;
public ResourceFinder getResoureFinder() {
if (resoureFinder == null) {
resoureFinder = new ResourceFinderImpl(getUIHandlers().getJavaHomeTextFieldHandler().getJavaHome());
}
return resoureFinder;
}
private UIHandlers uiHandlers;
public final UIHandlers getUIHandlers() {
if (uiHandlers == null) {
uiHandlers = new UIHandlers();
}
return uiHandlers;
}
public void lookup(String resourceName) {
log.info("Lookup resource '" + resourceName + "'");
List<URL> classpathURLs = getUIHandlers().getClasspathTreeHandler().getClasspathList();
JTree resultTree = getUIHandlers().getResultTreeHandler().getResultTree();
DefaultTreeModel model = (DefaultTreeModel) resultTree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
root.removeAllChildren();
model.reload();
URL[] result = getResoureFinder().findResource(resourceName, classpathURLs);
if (result == null || result.length == 0) {
DefaultMutableTreeNode newChild = new DefaultMutableTreeNode("Resource Not Found!");
model.insertNodeInto(newChild, root, root.getChildCount());
resultTree.scrollPathToVisible(new TreePath(newChild.getPath()));
} else {
DefaultMutableTreeNode lastNode = root;
for (URL url : result) {
DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(url);
model.insertNodeInto(newChild, root, root.getChildCount());
lastNode = newChild;
}
resultTree.scrollPathToVisible(new TreePath(lastNode.getPath()));
}
}
/**
* At startup create and show the main frame of the application.
*/
@Override
protected void startup() {
show(new JarefiView(this));
}
/**
* This method is to initialize the specified window by injecting resources.
* Windows shown in our application come fully initialized from the GUI
* builder, so this additional configuration is not needed.
*/
@Override
protected void configureWindow(java.awt.Window root) {
}
/**
* A convenient static getter for the application instance.
* @return the instance of JarefiApp
*/
public static JarefiApp getApplication() {
return Application.getInstance(JarefiApp.class);
}
/**
* Main method launching the application.
*/
public static void main(String[] args) {
launch(JarefiApp.class, args);
}
}