com.github.andy2003.ui.messageview.BusFactory Maven / Gradle / Ivy
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.github.andy2003.ui.messageview;
import com.github.andy2003.core.Bus;
import com.github.andy2003.core.BusChangeListener;
import com.github.andy2003.core.description.BusDescription;
import com.github.andy2003.ui.projects.Project;
import com.github.andy2003.ui.projects.ProjectChangeListener;
import com.github.andy2003.ui.projects.ProjectManagementListener;
import com.github.andy2003.ui.projects.ProjectManager;
import java.util.List;
import java.util.Set;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.lookup.Lookups;
/**
*
* @author dschanoeh
*/
public class BusFactory extends ChildFactory {
Project project;
private ProjectManagementListener projectListener = new ProjectManagementListener() {
@Override
public void projectsUpdated() {
}
@Override
public void openProjectChanged(Project p) {
project = p;
project.addProjectChangeListener(changeListener);
for(Bus b : p.getBusses()) {
b.addBusChangeListener(busListener);
}
refresh(true);
}
};
private ProjectChangeListener changeListener = new ProjectChangeListener() {
@Override
public void projectNameChanged(Project p, String name) {
}
@Override
public void projectClosed(Project p) {
p.removeProjectChangeListener(changeListener);
for(Bus b : p.getBusses()) {
b.removeBusChangeListener(busListener);
}
project = null;
refresh(true);
}
@Override
public void projectOpened(Project p) {
}
@Override
public void projectBusAdded(Project p, Bus bus) {
bus.addBusChangeListener(busListener);
refresh(true);
}
@Override
public void projectBusRemoved(Project p, Bus bus) {
bus.removeBusChangeListener(busListener);
refresh(true);
}
};
private BusChangeListener busListener = new BusChangeListener() {
@Override
public void connectionChanged() {
}
@Override
public void nameChanged(String name) {
}
@Override
public void destroyed() {
}
@Override
public void descriptionChanged() {
refresh(true);
}
@Override
public void aliasChanged(String string) {
}
};
public BusFactory() {
ProjectManager.getGlobalProjectManager().addListener(projectListener);
project = ProjectManager.getGlobalProjectManager().getOpenedProject();
if(project != null) {
project.addProjectChangeListener(changeListener);
for(Bus b : project.getBusses()) {
b.addBusChangeListener(busListener);
}
}
}
@Override
protected boolean createKeys(List list) {
if(project != null) {
Set busses = project.getBusses();
for(Bus b : busses) {
BusDescription desc = b.getDescription();
if(desc != null) {
list.add(desc);
}
}
}
return true;
}
@Override
protected Node[] createNodesForKey(BusDescription key) {
Bus bus = null;
for(Bus b : project.getBusses()) {
if(b.getDescription() == key)
bus = b;
}
return new Node[] { new BusNode(key, bus) };
}
}