package org.openbel.cytoscape.navigator;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import org.openbel.cytoscape.navigator.dialog.SearchKamDialog;
import org.openbel.cytoscape.navigator.dialog.SearchKamListDialog;
import org.openbel.cytoscape.webservice.dialog.SettingsDialog;
import cytoscape.CyNetwork;
import cytoscape.Cytoscape;
import cytoscape.CytoscapeVersion;
import cytoscape.logger.CyLogger;
import cytoscape.plugin.CytoscapePlugin;
import cytoscape.util.CytoscapeAction;
import cytoscape.view.CyNetworkView;
import cytoscape.view.CytoscapeDesktop;
import cytoscape.visual.CalculatorCatalog;
import cytoscape.visual.VisualMappingManager;
import cytoscape.visual.VisualStyle;
public static final String KAM_PLUGIN_SUBMENU = "KAM Navigator";
public static final String KAM_NODE_ID_ATTR = "KAM_NODE_ID";
public static final String KAM_NODE_FUNCTION_ATTR = "KAM_NODE_FUNCTION";
public static final String KAM_NODE_LABEL_ATTR = "KAM_NODE_LABEL";
public static final String KAM_EDGE_ID_ATTR = "KAM_EDGE_ID";
public static final String KAM_NAME_ATTR = "KAM_NAME";
public static final String KAM_COMPILE_DATE_ATTR = "KAM_COMPILE_DATE";
public static final String WSDL_URL_ATTR = "WSDL_URL";
private static final CyLogger log = CyLogger.getLogger(KamNavigatorPlugin.class);
private static final String KAM_NAVIGATOR_VERSION = "0.9";
private static final String KAM_STYLE = "KAM Visualization";
private static final String KAM_STYLE_FILE = "/org/openbel/cytoscape/navigator/style.props";
Cytoscape.getNodeAttributes().setUserEditable(KAM_NODE_ID_ATTR, false);
Cytoscape.getNodeAttributes().setUserVisible(KAM_NODE_ID_ATTR, false);
Cytoscape.getNodeAttributes().setUserEditable(KAM_NODE_FUNCTION_ATTR, false);
Cytoscape.getNodeAttributes().setUserVisible(KAM_NODE_FUNCTION_ATTR, true);
Cytoscape.getEdgeAttributes().setUserEditable(KAM_EDGE_ID_ATTR, false);
Cytoscape.getEdgeAttributes().setUserVisible(KAM_EDGE_ID_ATTR, false);
Cytoscape.getNodeAttributes().setUserEditable(KAM_NAME_ATTR, false);
Cytoscape.getNodeAttributes().setUserVisible(KAM_NAME_ATTR, true);
Cytoscape.getNodeAttributes().setUserEditable(KAM_COMPILE_DATE_ATTR, false);
Cytoscape.getNodeAttributes().setUserVisible(KAM_COMPILE_DATE_ATTR, false);
Cytoscape.getNodeAttributes().setUserEditable(WSDL_URL_ATTR, false);
Cytoscape.getNodeAttributes().setUserVisible(WSDL_URL_ATTR, false);
final KamNodeContextListener nctx = new KamNodeContextListener();
Cytoscape.getPropertyChangeSupport().addPropertyChangeListener(
CytoscapeDesktop.NETWORK_VIEW_CREATED, nctx);
Cytoscape.getPropertyChangeSupport().addPropertyChangeListener(
CytoscapeDesktop.NETWORK_VIEW_DESTROYED, nctx);
Cytoscape.getPropertyChangeSupport().addPropertyChangeListener(
CytoscapeDesktop.NETWORK_VIEW_CREATED, this);
Cytoscape.getPropertyChangeSupport().addPropertyChangeListener(
Cytoscape.NETWORK_CREATED, this);
Cytoscape.getPropertyChangeSupport().addPropertyChangeListener(
Cytoscape.NETWORK_DESTROYED, this);
final JMenu pluginMenu = Cytoscape.getDesktop().getCyMenus()
.getOperationsMenu();
JMenu kiMenu = getKamPluginMenu();
if (kiMenu == null) {
kiMenu = new JMenu(KAM_PLUGIN_SUBMENU);
pluginMenu.add(kiMenu);
}
kiMenu.add(new SearchKAMDialogAction());
kiMenu.add(new SearchKAMListDialogAction());
kiMenu.addSeparator();
kiMenu.add(new SettingsDialogAction());
JMenuItem feedbackItem = kiMenu.add(new FeedbackMailToAction());
feedbackItem.setEnabled(Desktop.isDesktopSupported() ? Desktop
.getDesktop().isSupported(Desktop.Action.MAIL) : false);
updateMenuState(false);
loadKAMStyle();
}
@Override
super.propertyChange(e);
if (CytoscapeDesktop.NETWORK_VIEW_CREATED.equals(e.getPropertyName())) {
CyNetwork cyn = ((CyNetworkView) e.getNewValue()).getNetwork();
cyn.addSelectEventListener(new NetworkDetailsListener());
} else if (Cytoscape.NETWORK_CREATED.equals(e.getPropertyName()) ||
Cytoscape.NETWORK_DESTROYED.equals(e.getPropertyName())) {
updateMenuState(Cytoscape.NETWORK_DESTROYED.equals(e
.getPropertyName()));
}
}
private static void (boolean networkDestroyed) {
JMenu kiMenu = getKamPluginMenu();
JMenuItem addNodesItem = kiMenu.getItem(0);
JMenuItem addListItem = kiMenu.getItem(1);
boolean hasNetworks = !Cytoscape.getNetworkSet().isEmpty();
if (networkDestroyed && Cytoscape.getNetworkSet().size() == 1) {
hasNetworks = false;
}
addNodesItem.setEnabled(hasNetworks);
addListItem.setEnabled(hasNetworks);
}
private static JMenu () {
final JMenu pluginMenu = Cytoscape.getDesktop().getCyMenus()
.getOperationsMenu();
JMenu kiMenu = null;
for (final Component menu : pluginMenu.getMenuComponents()) {
if (menu == null) {
continue;
}
if (menu instanceof JMenu
&& KAM_PLUGIN_SUBMENU.equals(((JMenu) menu).getText())) {
kiMenu = (JMenu) menu;
break;
}
}
return kiMenu;
}
final VisualMappingManager vismanager = Cytoscape.getVisualMappingManager();
final CalculatorCatalog ccat = vismanager.getCalculatorCatalog();
VisualStyle visualStyle = ccat.getVisualStyle(KAM_STYLE);
if (visualStyle == null) {
loadKAMStyleFromFile();
visualStyle = ccat.getVisualStyle(KAM_STYLE);
}
}
InputStream in = this.getClass().getResourceAsStream(KAM_STYLE_FILE);
File f = null;
try {
f = File.createTempFile("viz", null);
writeInputStreamIntoFile(in, f);
} catch (IOException e) {
log.warn("Error loading style", e);
return;
} finally {
Utility.closeSilently(in);
}
if (!f.exists() || !f.canRead()) {
return;
}
Cytoscape.firePropertyChange(Cytoscape.VIZMAP_LOADED, null, f.getAbsolutePath());
}
throws IOException {
BufferedInputStream bis = new BufferedInputStream(in);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = bis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
} finally {
Utility.closeSilently(bis);
Utility.closeSilently(fos);
}
}
private static final long serialVersionUID = -2109588518850444632L;
super("Send Feedback");
}
@Override
String supportEmail = "support@belframework.org";
String subject = "KAM%20Navigator%20Feedback";
String body = "Autogenerated information: ["
+ "KAM Navigator Version:" + KAM_NAVIGATOR_VERSION
+ ", Cytoscape Version:" + CytoscapeVersion.version
+ ", OS Name:" + System.getProperty("os.name")
+ ", OS Version:" + System.getProperty("os.version")
+ ", Java Version:" + System.getProperty("java.version")
+ "]";
body = urlEncode(body);
String uriString = "mailto:" + supportEmail + "?subject=" + subject
+ "&body=" + body;
URI uri = null;
try {
uri = new URI(uriString);
} catch (URISyntaxException e) {
log.error("Error generating support e-mail", e);
return;
}
try {
Desktop.getDesktop().mail(uri);
} catch (IOException e) {
log.error("Error generating support e-mail", e);
}
}
private static String
urlEncode(String input) {
StringBuilder resultStr = new StringBuilder();
for (char ch : input.toCharArray()) {
if (isUnsafe(ch)) {
resultStr.append('%');
resultStr.append(toHex(ch / 16));
resultStr.append(toHex(ch % 16));
} else {
resultStr.append(ch);
}
}
return resultStr.toString();
}
private static char toHex(
int ch) {
return (char) (ch < 10 ? '0' + ch : 'A' + ch - 10);
}
private static boolean isUnsafe(
char ch) {
if (ch > 128 || ch < 0) {
return true;
}
return " %$&+,/:;=?@<>#%".indexOf(ch) >= 0;
}
}
private static final long serialVersionUID = 2243171495622023060L;
super("Add KAM Nodes");
}
@Override
SearchKamDialog kcdialog = new SearchKamDialog();
kcdialog.setVisible(true);
}
}
private static final long serialVersionUID = -5051721582642478695L;
super(SearchKamListDialog.TITLE);
}
@Override
SearchKamListDialog dialog = new SearchKamListDialog();
dialog.setVisible(true);
}
}
private static final long serialVersionUID = 5424095704897475438L;
super("BELFramework Configuration");
}
@Override
SettingsDialog settingsDialog = new SettingsDialog();
settingsDialog.setVisible(true);
}
}
}