/**
*
*/
package com.waxayaz.TomcatMI.ui.components;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import com.waxayaz.TomcatMI.core.domains.TomcatInstance;
import com.waxayaz.TomcatMI.core.utils.TomcatInstancesManager;
import com.waxayaz.TomcatMI.ui.MainWindow;
/**
* @author Waxayaz
*
*/
public class InstancesTree {
/**
*
*/
private static final long serialVersionUID = -5440458629833386909L;
private Tree tree;
private Display display;
private InstanceTabPane tabPane;
public InstancesTree(Composite parent) {
tree = new Tree(parent, SWT.BORDER | SWT.SINGLE);
this.display = MainWindow.display;
tree.addMouseListener(new MouseListener() {
@Override
public void mouseUp(MouseEvent e) {}
@Override
public void mouseDown(MouseEvent e) {}
@Override
public void mouseDoubleClick(MouseEvent e) {
String instanceName = tree.getSelection()[0].getText();
TomcatInstance i = TomcatInstancesManager.getInstanceForName(instanceName);
if(i != null) {
tabPane.addInstanceTab(i);
}
}
});
reload();
}
public void reload() {
List<TomcatInstance> instances = TomcatInstancesManager.getAvailableInstances();
Map<String, TreeItem> homes = new HashMap<String, TreeItem>();
if(instances == null || instances.size() == 0) {
TreeItem item = new TreeItem(tree, SWT.NONE);
item.setText("No instance found");
}
else for(TomcatInstance instance : instances) {
String version = new String(instance.getTomcatVersion());
if(!homes.containsKey(version)) {
TreeItem item = new TreeItem(tree, SWT.NONE);
item.setText("Tomcat v"+version.substring(0, version.indexOf('.')));
item.setImage(new Image(display, "res/images/TomcatHome.png"));
homes.put(version, item);
}
TreeItem instanceItem = new TreeItem(homes.get(version), SWT.NONE);
instanceItem.setText(instance.getName());
instanceItem.setImage(new Image(display, "res/images/TomcatInstance.png"));
}
}
public void setTabPane(InstanceTabPane tabPane) {
this.tabPane = tabPane;
}
}