PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/components/insight/SRC/org/openmicroscopy/shoola/agents/metadata/MetadataViewerAgent.java

https://github.com/will-moore/openmicroscopy
Java | 436 lines | 226 code | 37 blank | 173 comment | 46 complexity | 52ab428e5c00a836738caad17d986943 MD5 | raw file
  1. /*
  2. *------------------------------------------------------------------------------
  3. * Copyright (C) 2006-2015 University of Dundee. All rights reserved.
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. *------------------------------------------------------------------------------
  20. */
  21. package org.openmicroscopy.shoola.agents.metadata;
  22. import java.io.File;
  23. import java.util.Collection;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import ome.model.units.BigResult;
  27. import org.apache.commons.collections.CollectionUtils;
  28. import org.openmicroscopy.shoola.agents.events.iviewer.RndSettingsCopied;
  29. import org.openmicroscopy.shoola.agents.events.metadata.ChannelSavedEvent;
  30. import org.openmicroscopy.shoola.agents.events.treeviewer.DisplayModeEvent;
  31. import org.openmicroscopy.shoola.agents.metadata.view.MetadataViewer;
  32. import org.openmicroscopy.shoola.agents.metadata.view.MetadataViewerFactory;
  33. import org.openmicroscopy.shoola.agents.metadata.view.RndSettingsPasted;
  34. import org.openmicroscopy.shoola.agents.events.iviewer.CopyRndSettings;
  35. import org.openmicroscopy.shoola.env.Agent;
  36. import org.openmicroscopy.shoola.env.Environment;
  37. import org.openmicroscopy.shoola.env.LookupNames;
  38. import org.openmicroscopy.shoola.env.config.Registry;
  39. import org.openmicroscopy.shoola.env.data.AdminService;
  40. import org.openmicroscopy.shoola.env.data.events.ReconnectedEvent;
  41. import org.openmicroscopy.shoola.env.data.events.UserGroupSwitched;
  42. import org.openmicroscopy.shoola.env.data.util.AgentSaveInfo;
  43. import omero.gateway.SecurityContext;
  44. import org.openmicroscopy.shoola.env.event.AgentEvent;
  45. import org.openmicroscopy.shoola.env.event.AgentEventListener;
  46. import org.openmicroscopy.shoola.env.event.EventBus;
  47. import org.openmicroscopy.shoola.env.rnd.RenderingControl;
  48. import omero.gateway.model.ChannelData;
  49. import omero.gateway.model.ExperimenterData;
  50. import omero.gateway.model.GroupData;
  51. import omero.gateway.model.ImageData;
  52. /**
  53. * The MetadataViewerAgent agent. This agent displays metadata related to
  54. * an object.
  55. *
  56. * @author Jean-Marie Burel     
  57. * <a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a>
  58. * @author Donald MacDonald &nbsp;&nbsp;&nbsp;&nbsp;
  59. * <a href="mailto:donald@lifesci.dundee.ac.uk">donald@lifesci.dundee.ac.uk</a>
  60. * @version 3.0
  61. * @since OME3.0
  62. */
  63. public class MetadataViewerAgent
  64. implements Agent, AgentEventListener
  65. {
  66. /** Reference to the registry. */
  67. private static Registry registry;
  68. /** The display mode.*/
  69. private int displayMode = -1;
  70. /**
  71. * Helper method.
  72. *
  73. * @return A reference to the <code>Registry</code>
  74. */
  75. public static Registry getRegistry() { return registry; }
  76. /**
  77. * Returns the identifier of the plugin to run.
  78. *
  79. * @return See above.
  80. */
  81. public static int runAsPlugin()
  82. {
  83. Environment env = (Environment) registry.lookup(LookupNames.ENV);
  84. if (env == null) return -1;
  85. return env.runAsPlugin();
  86. }
  87. /**
  88. * Helper method returning the current user's details.
  89. *
  90. * @return See above.
  91. */
  92. public static ExperimenterData getUserDetails()
  93. {
  94. return (ExperimenterData) registry.lookup(
  95. LookupNames.CURRENT_USER_DETAILS);
  96. }
  97. /**
  98. * Returns the available user groups.
  99. *
  100. * @return See above.
  101. */
  102. public static Collection getAvailableUserGroups()
  103. {
  104. return (Collection) registry.lookup(LookupNames.USER_GROUP_DETAILS);
  105. }
  106. /**
  107. * Returns <code>true</code> if the currently logged in user
  108. * is an administrator, <code>false</code> otherwise.
  109. *
  110. * @return See above.
  111. */
  112. public static boolean isAdministrator()
  113. {
  114. Boolean b = (Boolean) registry.lookup(LookupNames.USER_ADMINISTRATOR);
  115. if (b == null) return false;
  116. return b.booleanValue();
  117. }
  118. /**
  119. * Returns the context for an administrator.
  120. *
  121. * @return See above.
  122. */
  123. public static SecurityContext getAdminContext()
  124. {
  125. if (!isAdministrator()) return null;
  126. Collection<GroupData> groups = getAvailableUserGroups();
  127. Iterator<GroupData> i = groups.iterator();
  128. GroupData g;
  129. AdminService svc = registry.getAdminService();
  130. while (i.hasNext()) {
  131. g = i.next();
  132. if (svc.isSecuritySystemGroup(g.getId(), GroupData.SYSTEM))
  133. return new SecurityContext(g.getId());
  134. }
  135. return null;
  136. }
  137. /**
  138. * Helper method returning <code>true</code> if the connection is fast,
  139. * <code>false</code> otherwise.
  140. *
  141. * @return See above.
  142. */
  143. public static boolean isFastConnection()
  144. {
  145. int value = (Integer) registry.lookup(LookupNames.IMAGE_QUALITY_LEVEL);
  146. return value == RenderingControl.UNCOMPRESSED;
  147. }
  148. /**
  149. * Returns the path of the 'omero home' directory e.g. user/omero.
  150. *
  151. * @return See above.
  152. */
  153. public static String getOmeroHome()
  154. {
  155. Environment env = (Environment) registry.lookup(LookupNames.ENV);
  156. String omeroDir = env.getOmeroHome();
  157. File home = new File(omeroDir);
  158. if (!home.exists()) home.mkdir();
  159. return omeroDir;
  160. }
  161. /**
  162. * Returns the path of the 'omero home' directory e.g. user/omero.
  163. *
  164. * @return See above.
  165. */
  166. public static String getOmeroFilesHome()
  167. {
  168. Environment env = (Environment) registry.lookup(LookupNames.ENV);
  169. String omeroDir = env.getOmeroFilesHome();
  170. File home = new File(omeroDir);
  171. if (!home.exists()) home.mkdir();
  172. return omeroDir;
  173. }
  174. /**
  175. * Returns the temporary directory.
  176. *
  177. * @return See above.
  178. */
  179. public static String getTmpDir()
  180. {
  181. Environment env = (Environment) registry.lookup(LookupNames.ENV);
  182. return env.getTmpDir();
  183. }
  184. /**
  185. * Returns the experimenter corresponding to the passed id.
  186. *
  187. * @param expID The experimenter's id.
  188. * @return See above.
  189. */
  190. public static ExperimenterData getExperimenter(long expID)
  191. {
  192. List l = (List) registry.lookup(LookupNames.USERS_DETAILS);
  193. if (l == null) return null;
  194. Iterator i = l.iterator();
  195. ExperimenterData exp;
  196. while (i.hasNext()) {
  197. exp = (ExperimenterData) i.next();
  198. if (exp.getId() == expID) return exp;
  199. }
  200. return null;
  201. }
  202. /**
  203. * Returns <code>true</code> if the binary data are available,
  204. * <code>false</code> otherwise.
  205. *
  206. * @return See above.
  207. */
  208. public static boolean isBinaryAvailable()
  209. {
  210. Boolean b = (Boolean) registry.lookup(LookupNames.BINARY_AVAILABLE);
  211. if (b == null) return true;
  212. return b.booleanValue();
  213. }
  214. /**
  215. * Convenience method for logging BigResult exceptions
  216. *
  217. * @param src
  218. * The origin of the exception
  219. * @param exception
  220. * The exception
  221. * @param property
  222. * The property which conversion triggered the exception
  223. */
  224. public static void logBigResultExeption(Object src, Object exception,
  225. String property) {
  226. if (exception instanceof BigResult) {
  227. MetadataViewerAgent
  228. .getRegistry()
  229. .getLogger()
  230. .warn(src,
  231. "Arithmetic overflow; "
  232. + property
  233. + " is "
  234. + ((BigResult) exception).result
  235. .doubleValue());
  236. }
  237. }
  238. /**
  239. * Handles the {@link UserGroupSwitched} event.
  240. *
  241. * @param evt The event to handle.
  242. */
  243. private void handleUserGroupSwitched(UserGroupSwitched evt)
  244. {
  245. if (evt == null) return;
  246. MetadataViewerFactory.onGroupSwitched(evt.isSuccessful());
  247. }
  248. /**
  249. * Indicates that it was possible to reconnect.
  250. *
  251. * @param evt The event to handle.
  252. */
  253. private void handleReconnectedEvent(ReconnectedEvent evt)
  254. {
  255. MetadataViewerFactory.onGroupSwitched(true);
  256. }
  257. /**
  258. * Updates the view when the channels have been updated.
  259. *
  260. * @param evt The event to handle.
  261. */
  262. private void handleChannelSavedEvent(ChannelSavedEvent evt)
  263. {
  264. List<ChannelData> channels = evt.getChannels();
  265. Iterator<Long> i = evt.getImageIds().iterator();
  266. MetadataViewer viewer;
  267. while (i.hasNext()) {
  268. viewer = MetadataViewerFactory.getViewerFromId(
  269. ImageData.class.getName(), i.next());
  270. if (viewer != null) {
  271. viewer.onUpdatedChannels(channels);
  272. }
  273. }
  274. }
  275. /**
  276. * Handles the {@link RndSettingsCopied} event.
  277. *
  278. * @param evt
  279. * The event to handle.
  280. */
  281. private void handleRndSettingsCopied(RndSettingsCopied evt) {
  282. Collection<Long> ids = evt.getImagesIDs();
  283. if (CollectionUtils.isEmpty(ids))
  284. return;
  285. Iterator<Long> i = ids.iterator();
  286. MetadataViewer viewer;
  287. while (i.hasNext()) {
  288. viewer = MetadataViewerFactory.getViewerFromId(
  289. ImageData.class.getName(), i.next());
  290. if (viewer != null && viewer.isRendererLoaded()) {
  291. viewer.resetRenderingControl();
  292. }
  293. }
  294. }
  295. /**
  296. * Handles a {@link CopyRndSettings} event, i. e. passes the image
  297. * reference on to the {@link MetadataViewer}s.
  298. * @param evt The event
  299. */
  300. private void handleCopyRndSettings(CopyRndSettings evt) {
  301. MetadataViewerFactory.setCopyRenderingSettingsFrom(evt.getImage(), evt.getRndDef());
  302. }
  303. /**
  304. * Handles a {@link RndSettingsPasted} event, i. e. notifies
  305. * the {@link MetadataViewer}s to apply the settings of an
  306. * previously set image; see also {@link CopyRndSettings}
  307. * @param e
  308. */
  309. private void handleRndSettingsPasted(RndSettingsPasted e) {
  310. MetadataViewerFactory.applyCopiedRndSettings(e.getImageId());
  311. }
  312. /**
  313. * Updates the view when the mode is changed.
  314. *
  315. * @param evt The event to handle.
  316. */
  317. private void handleDisplayModeEvent(DisplayModeEvent evt)
  318. {
  319. displayMode = evt.getDisplayMode();
  320. MetadataViewerFactory.setDiplayMode(displayMode);
  321. }
  322. /** Creates a new instance. */
  323. public MetadataViewerAgent() {}
  324. /**
  325. * Implemented as specified by {@link Agent}.
  326. * @see Agent#activate(boolean)
  327. */
  328. public void activate(boolean master) {}
  329. /**
  330. * Implemented as specified by {@link Agent}.
  331. * @see Agent#terminate()
  332. */
  333. public void terminate()
  334. {
  335. Environment env = (Environment) registry.lookup(LookupNames.ENV);
  336. if (env.isRunAsPlugin())
  337. MetadataViewerFactory.onGroupSwitched(true);
  338. }
  339. /**
  340. * Implemented as specified by {@link Agent}.
  341. * @see Agent#setContext(Registry)
  342. */
  343. public void setContext(Registry ctx)
  344. {
  345. registry = ctx;
  346. EventBus bus = ctx.getEventBus();
  347. bus.register(this, UserGroupSwitched.class);
  348. bus.register(this, ReconnectedEvent.class);
  349. bus.register(this, ChannelSavedEvent.class);
  350. bus.register(this, DisplayModeEvent.class);
  351. bus.register(this, RndSettingsCopied.class);
  352. bus.register(this, CopyRndSettings.class);
  353. bus.register(this, RndSettingsPasted.class);
  354. }
  355. /**
  356. * Implemented as specified by {@link Agent}.
  357. * @see Agent#canTerminate()
  358. */
  359. public boolean canTerminate() { return true; }
  360. /**
  361. * Implemented as specified by {@link Agent}.
  362. * @see Agent#getDataToSave()
  363. */
  364. public AgentSaveInfo getDataToSave()
  365. {
  366. List<Object> instances = MetadataViewerFactory.getInstancesToSave();
  367. if (instances == null || instances.size() == 0) return null;
  368. return new AgentSaveInfo("Edition", instances);
  369. }
  370. /**
  371. * Implemented as specified by {@link Agent}.
  372. * @see Agent#save(List)
  373. */
  374. public void save(List<Object> instances)
  375. {
  376. MetadataViewerFactory.saveInstances(instances);
  377. }
  378. /**
  379. * Responds to events fired trigger on the bus.
  380. * @see AgentEventListener#eventFired(AgentEvent)
  381. */
  382. public void eventFired(AgentEvent e)
  383. {
  384. if (e instanceof UserGroupSwitched)
  385. handleUserGroupSwitched((UserGroupSwitched) e);
  386. else if (e instanceof ReconnectedEvent)
  387. handleReconnectedEvent((ReconnectedEvent) e);
  388. else if (e instanceof ChannelSavedEvent)
  389. handleChannelSavedEvent((ChannelSavedEvent) e);
  390. else if (e instanceof DisplayModeEvent)
  391. handleDisplayModeEvent((DisplayModeEvent) e);
  392. else if (e instanceof RndSettingsCopied)
  393. handleRndSettingsCopied((RndSettingsCopied) e);
  394. else if (e instanceof CopyRndSettings)
  395. handleCopyRndSettings((CopyRndSettings) e);
  396. else if (e instanceof RndSettingsPasted)
  397. handleRndSettingsPasted((RndSettingsPasted) e);
  398. }
  399. }