|
| 1 | +package org.baderlab.csplugins.enrichmentmap.task; |
| 2 | + |
| 3 | +import java.awt.Color; |
| 4 | + |
| 5 | +import org.cytoscape.application.CyApplicationManager; |
| 6 | +import org.cytoscape.event.CyEventHelper; |
| 7 | +import org.cytoscape.view.model.CyNetworkView; |
| 8 | +import org.cytoscape.view.presentation.property.BasicVisualLexicon; |
| 9 | +import org.cytoscape.view.vizmap.VisualMappingManager; |
| 10 | +import org.cytoscape.view.vizmap.VisualStyle; |
| 11 | +import org.cytoscape.view.vizmap.VisualStyleFactory; |
| 12 | +import org.cytoscape.work.AbstractTask; |
| 13 | +import org.cytoscape.work.TaskMonitor; |
| 14 | + |
| 15 | +public class CreatePublicationVisualStyleTask extends AbstractTask { |
| 16 | + |
| 17 | + private static final String SUFFIX = "_publication"; |
| 18 | + |
| 19 | + private final CyApplicationManager applicationManager; |
| 20 | + private final VisualMappingManager visualMappingManager; |
| 21 | + private final VisualStyleFactory visualStyleFactory; |
| 22 | + private final CyEventHelper eventHelper; |
| 23 | + |
| 24 | + |
| 25 | + public CreatePublicationVisualStyleTask( |
| 26 | + CyApplicationManager applicationManager, |
| 27 | + VisualMappingManager visualMappingManager, |
| 28 | + VisualStyleFactory visualStyleFactory, |
| 29 | + CyEventHelper eventHelper) { |
| 30 | + |
| 31 | + this.applicationManager = applicationManager; |
| 32 | + this.visualMappingManager = visualMappingManager; |
| 33 | + this.visualStyleFactory = visualStyleFactory; |
| 34 | + this.eventHelper = eventHelper; |
| 35 | + } |
| 36 | + |
| 37 | + private VisualStyle attemptToGetExistingStyle(String vs_name) { |
| 38 | + for(VisualStyle vs : visualMappingManager.getAllVisualStyles()) { |
| 39 | + if(vs.getTitle() != null && vs.getTitle().equals(vs_name)) { |
| 40 | + return vs; |
| 41 | + } |
| 42 | + } |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void run(TaskMonitor taskMonitor) { |
| 48 | + taskMonitor.setTitle("EnrichmentMap"); |
| 49 | + taskMonitor.setStatusMessage("Create Publication-Ready Visual Style"); |
| 50 | + |
| 51 | + VisualStyle currentStyle = visualMappingManager.getCurrentVisualStyle(); |
| 52 | + if(currentStyle == null) |
| 53 | + return; |
| 54 | + String currentTitle = currentStyle.getTitle(); |
| 55 | + |
| 56 | + // If the current visual style is publication then attempt to switch back |
| 57 | + if(currentTitle.endsWith(SUFFIX)) { |
| 58 | + String title = currentTitle.substring(0, currentTitle.length() - SUFFIX.length()); |
| 59 | + VisualStyle existingStyle = attemptToGetExistingStyle(title); |
| 60 | + if(existingStyle != null) { |
| 61 | + visualMappingManager.setCurrentVisualStyle(existingStyle); |
| 62 | + } |
| 63 | + } |
| 64 | + else { |
| 65 | + String title = currentTitle + SUFFIX; |
| 66 | + VisualStyle visualStyle = attemptToGetExistingStyle(title); |
| 67 | + if(visualStyle == null) { |
| 68 | + // create a copy of the current style |
| 69 | + visualStyle = visualStyleFactory.createVisualStyle(currentStyle); |
| 70 | + visualStyle.setTitle(title); |
| 71 | + |
| 72 | + // Remove node labels |
| 73 | + visualStyle.removeVisualMappingFunction(BasicVisualLexicon.NODE_LABEL); |
| 74 | + visualStyle.setDefaultValue(BasicVisualLexicon.NODE_LABEL, ""); |
| 75 | + |
| 76 | + // Make background white |
| 77 | + visualStyle.removeVisualMappingFunction(BasicVisualLexicon.NETWORK_BACKGROUND_PAINT); |
| 78 | + visualStyle.setDefaultValue(BasicVisualLexicon.NETWORK_BACKGROUND_PAINT, Color.WHITE); |
| 79 | + |
| 80 | + visualMappingManager.addVisualStyle(visualStyle); |
| 81 | + } |
| 82 | + visualMappingManager.setCurrentVisualStyle(visualStyle); |
| 83 | + } |
| 84 | + |
| 85 | + eventHelper.flushPayloadEvents(); // view won't update properly without this |
| 86 | + CyNetworkView view = applicationManager.getCurrentNetworkView(); |
| 87 | + view.updateView(); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments