use of org.cytoscape.session.CyNetworkNaming in project cytoscape-impl by cytoscape.
the class CyNetworkManagerImpl method addNetwork.
@Override
public void addNetwork(final CyNetwork network, final boolean setCurrent) {
if (network == null)
throw new NullPointerException("Network is null");
synchronized (lock) {
logger.debug("Adding new Network Model: Model ID = " + network.getSUID());
// Make sure the network has a name
final CyRow row = network.getRow(network);
final String name = row.get(CyNetwork.NAME, String.class);
final String sharedName = row.get(CyRootNetwork.SHARED_NAME, String.class);
if (name != null && !name.trim().isEmpty() && (sharedName == null || sharedName.trim().isEmpty())) {
row.set(CyRootNetwork.SHARED_NAME, name);
} else if (sharedName != null && !sharedName.trim().isEmpty() && (name == null || name.trim().isEmpty())) {
row.set(CyNetwork.NAME, sharedName);
} else if ((sharedName == null || sharedName.trim().isEmpty()) && (name == null || name.trim().isEmpty())) {
final CyNetworkNaming namingUtil = serviceRegistrar.getService(CyNetworkNaming.class);
final String newName = namingUtil.getSuggestedNetworkTitle("Network");
row.set(CyNetwork.NAME, newName);
row.set(CyRootNetwork.SHARED_NAME, newName);
}
// Add the new network to the internal map
networkMap.put(network.getSUID(), network);
}
final CyEventHelper cyEventHelper = serviceRegistrar.getService(CyEventHelper.class);
cyEventHelper.fireEvent(new NetworkAddedEvent(CyNetworkManagerImpl.this, network));
if (setCurrent) {
final CyApplicationManager applicationManager = serviceRegistrar.getService(CyApplicationManager.class);
if (// It may be null when running unit tests
applicationManager != null)
applicationManager.setCurrentNetwork(network);
}
}
use of org.cytoscape.session.CyNetworkNaming in project cytoscape-impl by cytoscape.
the class TestCyNetworkFactory method mockCyServiceRegistrar.
private static CyServiceRegistrar mockCyServiceRegistrar(CyEventHelper deh) {
CyServiceRegistrar serviceRegistrar = mock(CyServiceRegistrar.class);
CyNetworkNaming namingUtil = mock(CyNetworkNaming.class);
EquationCompiler compiler = new EquationCompilerImpl(new EquationParserImpl(serviceRegistrar));
final Interpreter interpreter = new InterpreterImpl();
when(serviceRegistrar.getService(CyEventHelper.class)).thenReturn(deh);
when(serviceRegistrar.getService(CyNetworkNaming.class)).thenReturn(namingUtil);
when(serviceRegistrar.getService(EquationCompiler.class)).thenReturn(compiler);
when(serviceRegistrar.getService(Interpreter.class)).thenReturn(interpreter);
return serviceRegistrar;
}
use of org.cytoscape.session.CyNetworkNaming in project cytoscape-impl by cytoscape.
the class GenerateNetworkViewsTask method run.
@Override
public void run(final TaskMonitor taskMonitor) throws Exception {
final CyNetwork[] networks = viewReader.getNetworks();
if (networks == null || networks.length == 0)
return;
taskMonitor.setProgress(0.0);
final CyNetworkNaming networkNaming = serviceRegistrar.getService(CyNetworkNaming.class);
double numNets = (double) networks.length;
int i = 0;
results = new ArrayList<>();
largeNetworks = new ArrayList<>();
for (CyNetwork net : networks) {
// Use original name if exists
String networkName = net.getRow(net).get(CyNetwork.NAME, String.class);
if (networkName == null || networkName.trim().length() == 0)
networkName = (name != null) ? name : "? (Name is missing)";
net.getRow(net).set(CyNetwork.NAME, networkNaming.getSuggestedNetworkTitle(networkName));
serviceRegistrar.getService(CyNetworkManager.class).addNetwork(net, false);
final int numGraphObjects = net.getNodeCount() + net.getEdgeCount();
if (numGraphObjects < viewThreshold)
createNetworkView(net);
else
largeNetworks.add(net);
taskMonitor.setProgress((double) (++i) / numNets);
}
// If there is no name yet for the root network, set it the same as its base subnetwork
if (networks.length == 1) {
if (networks[0] instanceof CySubNetwork) {
CySubNetwork subnet = (CySubNetwork) networks[0];
final CyRootNetwork rootNet = subnet.getRootNetwork();
String rootNetName = rootNet.getRow(rootNet).get(CyNetwork.NAME, String.class);
if (rootNetName == null || rootNetName.trim().length() == 0) {
// The root network does not have a name yet, set it the same as the base subnetwork
rootNet.getRow(rootNet).set(CyNetwork.NAME, networks[0].getRow(networks[0]).get(CyNetwork.NAME, String.class));
}
}
}
// Make sure rootNetwork has a name
for (CyNetwork net : networks) {
if (net instanceof CySubNetwork) {
CySubNetwork subNet = (CySubNetwork) net;
CyRootNetwork rootNet = subNet.getRootNetwork();
String networkName = rootNet.getRow(rootNet).get(CyNetwork.NAME, String.class);
if (networkName == null || networkName.trim().length() == 0) {
networkName = name;
if (networkName == null)
networkName = "? (Name is missing)";
rootNet.getRow(rootNet).set(CyNetwork.NAME, networkNaming.getSuggestedNetworkTitle(networkName));
}
}
}
setCurrentNetworkAndViewTask(networks[0]);
if (!largeNetworks.isEmpty())
insertTasksAfterCurrentTask(new ConfirmCreateNetworkViewsTask(largeNetworks));
}
use of org.cytoscape.session.CyNetworkNaming in project cytoscape-impl by cytoscape.
the class NewNetworkSelectedNodesOnlyTaskFactoryTest method testObserver.
@Test
public void testObserver() throws Exception {
NetworkViewTestSupport viewSupport = new NetworkViewTestSupport();
NetworkTestSupport networkSupport = new NetworkTestSupport();
CyNetworkFactory networkFactory = networkSupport.getNetworkFactory();
UndoSupport undoSupport = mock(UndoSupport.class);
CyRootNetworkManager crnf = new CyRootNetworkManagerImpl();
CyNetworkViewFactory cnvf = viewSupport.getNetworkViewFactory();
CyNetworkManager netmgr = mock(CyNetworkManager.class);
CyNetworkViewManager networkViewManager = mock(CyNetworkViewManager.class);
CyNetworkNaming naming = mock(CyNetworkNaming.class);
VisualMappingManager vmm = mock(VisualMappingManager.class);
CyApplicationManager appManager = mock(CyApplicationManager.class);
CyEventHelper eventHelper = mock(CyEventHelper.class);
CyGroupManager groupMgr = mock(CyGroupManager.class);
RenderingEngineManager renderingEngineMgr = mock(RenderingEngineManager.class);
CyLayoutAlgorithm defLayout = mock(CyLayoutAlgorithm.class);
CyLayoutAlgorithmManager layoutMgr = mock(CyLayoutAlgorithmManager.class);
when(layoutMgr.getDefaultLayout()).thenReturn(defLayout);
CyServiceRegistrar serviceRegistrar = mock(CyServiceRegistrar.class);
when(serviceRegistrar.getService(CyLayoutAlgorithmManager.class)).thenReturn(layoutMgr);
NewNetworkSelectedNodesOnlyTaskFactoryImpl factory = new NewNetworkSelectedNodesOnlyTaskFactoryImpl(undoSupport, crnf, cnvf, netmgr, networkViewManager, naming, vmm, appManager, eventHelper, groupMgr, renderingEngineMgr, serviceRegistrar);
CyNetwork network = networkFactory.createNetwork();
CyNode node = network.addNode();
network.getRow(node).set(CyNetwork.SELECTED, true);
TaskObserver observer = mock(TaskObserver.class);
TaskMonitor taskMonitor = mock(TaskMonitor.class);
TaskIterator iterator = factory.createTaskIterator(network);
while (iterator.hasNext()) {
Task t = iterator.next();
t.run(taskMonitor);
if (t instanceof ObservableTask)
observer.taskFinished((ObservableTask) t);
}
verify(observer, times(1)).taskFinished(any(ObservableTask.class));
}
Aggregations