1 /*
2
3 dsh-venn-cytoscape3-app Cytoscape3 app for venn and euler diagrams.
4 Copyright (c) 2012-2013 held jointly by the individual authors.
5
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 3 of the License, or (at
9 your option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this library; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19
20 > http://www.fsf.org/licensing/licenses/lgpl.html
21 > http://www.opensource.org/licenses/lgpl-license.php
22
23 */
24 package org.dishevelled.venn.cytoscape3.internal;
25
26 import java.awt.Toolkit;
27
28 import java.awt.event.ActionEvent;
29 import java.awt.event.KeyEvent;
30 import java.awt.event.WindowEvent;
31
32 import javax.swing.AbstractAction;
33 import javax.swing.Action;
34 import javax.swing.KeyStroke;
35 import javax.swing.JComponent;
36 import javax.swing.JDialog;
37 import javax.swing.JRootPane;
38
39 import org.cytoscape.group.CyGroup;
40 import org.cytoscape.model.CyNetwork;
41 import org.cytoscape.model.CyNode;
42 import org.cytoscape.model.CyRow;
43 import org.cytoscape.model.CyTable;
44
45 /**
46 * Utility methods.
47 *
48 * @author Michael Heuer
49 */
50 final class VennDiagramsUtils
51 {
52
53 /**
54 * Private no-arg constructor.
55 */
56 private VennDiagramsUtils()
57 {
58 // empty
59 }
60
61
62 /**
63 * Return the name of the specified group in the specified network.
64 *
65 * @param group group
66 * @param network network
67 * @return the name of the specified group in the specified network
68 */
69 static String nameOf(final CyGroup group, final CyNetwork network)
70 {
71 CyTable nodeTable = network.getDefaultNodeTable();
72 CyRow nodeRow = nodeTable.getRow(group.getGroupNode().getSUID());
73 String name = nodeRow.get(CyNetwork.NAME, String.class);
74 return (name == null) ? group.toString() : name;
75 }
76
77 /**
78 * Return the name of the specified node in the specified network.
79 *
80 * @param node node
81 * @param network network
82 * @return the name of the specified node in the specified network
83 */
84 static String nameOf(final CyNode node, final CyNetwork network)
85 {
86 CyTable nodeTable = network.getDefaultNodeTable();
87 CyRow nodeRow = nodeTable.getRow(node.getSUID());
88 String name = nodeRow.get(CyNetwork.NAME, String.class);
89 return (name == null) ? node.toString() : name;
90 }
91
92 /**
93 * Set the name of the specified group in the specified network to <code>name</code>.
94 *
95 * @param group group
96 * @param network network
97 * @param name name
98 */
99 static void rename(final CyGroup group, final CyNetwork network, final String name)
100 {
101 if (name == null)
102 {
103 return;
104 }
105 CyTable nodeTable = network.getDefaultNodeTable();
106 CyRow nodeRow = nodeTable.getRow(group.getGroupNode().getSUID());
107 nodeRow.set(CyNetwork.NAME, name);
108 }
109
110 /**
111 * Install a close action binding to <code>Ctrl-C</code>/<code>Command-C</code> for the specified dialog.
112 *
113 * @param dialog dialog, must not be null
114 */
115 static void installCloseKeyBinding(final JDialog dialog)
116 {
117 Action close = new AbstractAction()
118 {
119 /** {@inheritDoc} */
120 public void actionPerformed(final ActionEvent event)
121 {
122 dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
123 }
124 };
125 int menuKeyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
126 KeyStroke closeStroke = KeyStroke.getKeyStroke(KeyEvent.VK_W, menuKeyMask);
127 JRootPane rootPane = dialog.getRootPane();
128 rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(closeStroke, "close");
129 rootPane.getActionMap().put("close", close);
130 }
131 }