PageRenderTime 68ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/components/tools/OmeroImageJ/Omero_ImageJ/src/ome/ij/LoginScreenManager.java

https://github.com/aherbert/openmicroscopy
Java | 296 lines | 180 code | 23 blank | 93 comment | 40 complexity | 69b1528ed8e1a9ff198bfc13913d144c MD5 | raw file
  1. /*
  2. * ome.ij.LoginScreenManager
  3. *
  4. *------------------------------------------------------------------------------
  5. * Copyright (C) 2006-2009 University of Dundee. All rights reserved.
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. *------------------------------------------------------------------------------
  22. */
  23. package ome.ij;
  24. //Java imports
  25. import java.awt.Dimension;
  26. import java.awt.Rectangle;
  27. import java.awt.Toolkit;
  28. import java.awt.event.WindowEvent;
  29. import java.awt.event.WindowFocusListener;
  30. import java.awt.event.WindowStateListener;
  31. import java.beans.PropertyChangeEvent;
  32. import java.beans.PropertyChangeListener;
  33. import javax.swing.Icon;
  34. import javax.swing.JFrame;
  35. //Third-party libraries
  36. import ij.IJ;
  37. //Application-internal dependencies
  38. import ome.ij.data.ServicesFactory;
  39. import ome.ij.dm.TreeViewer;
  40. import ome.ij.dm.TreeViewerFactory;
  41. import org.openmicroscopy.shoola.util.ui.IconManager;
  42. import org.openmicroscopy.shoola.util.ui.login.LoginCredentials;
  43. import org.openmicroscopy.shoola.util.ui.login.ScreenLogin;
  44. import org.openmicroscopy.shoola.util.ui.login.ScreenLogo;
  45. /**
  46. * Handles the log in sequence.
  47. *
  48. * @author Jean-Marie Burel     
  49. * <a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a>
  50. * @author Donald MacDonald &nbsp;&nbsp;&nbsp;&nbsp;
  51. * <a href="mailto:donald@lifesci.dundee.ac.uk">donald@lifesci.dundee.ac.uk</a>
  52. * @version 3.0
  53. * <small>
  54. * (<b>Internal version:</b> $Revision: $Date: $)
  55. * </small>
  56. * @since 3.0-Beta4
  57. */
  58. class LoginScreenManager
  59. implements PropertyChangeListener, WindowFocusListener, WindowStateListener
  60. {
  61. /** The title of the splash screens. */
  62. static final String TITLE = "Open Microscopy Environment";
  63. /** This should not be there.*/
  64. private static final String version = "4.3.1";
  65. /** The version the plugin. */
  66. private static final String VERSION = "v1.0 (for server version: "+version+")";
  67. /** The default port value. */
  68. private static final String PORT = ""+4064;
  69. /** The title of the dialog if we cannot connect. */
  70. private static final String CONNECTION = "Connection";
  71. /** Reference to the logo. */
  72. private ScreenLogo logo;
  73. /** Reference to the login. */
  74. private ScreenLogin login;
  75. /**
  76. * Sets the state of the specified window.
  77. *
  78. * @param f The window to handle.
  79. * @param state The state to set.
  80. */
  81. private void setWindowState(JFrame f, int state)
  82. {
  83. if (f == null) return;
  84. f.removeWindowStateListener(this);
  85. f.setState(state);
  86. f.addWindowStateListener(this);
  87. }
  88. /**
  89. * Returns <code>true</code> if the server and the client are compatible,
  90. * <code>false</code> otherwise.
  91. *
  92. * @param server The version of the server.
  93. * @param client The version of the client.
  94. * @return See above.
  95. */
  96. private boolean checkClientServerCompatibility(String server, String client)
  97. {
  98. if (server == null || client == null) return false;
  99. if (server.contains("-"))
  100. server = server.split("-")[0];
  101. if (client.contains("-"))
  102. client = client.split("-")[0];
  103. if (client.startsWith("Beta"))
  104. client = client.substring(4);
  105. String[] values = server.split("\\.");
  106. String[] valuesClient = client.split("\\.");
  107. //Integer.parseInt(values[0]);
  108. if (values.length < 2 || valuesClient.length < 2) return false;
  109. int s1 = Integer.parseInt(values[0]);
  110. int s2 = Integer.parseInt(values[1]);
  111. int c1 = Integer.parseInt(valuesClient[0]);
  112. int c2 = Integer.parseInt(valuesClient[1]);
  113. if (s1 < c1) return false;
  114. if (s2 < c2) return false;
  115. return true;
  116. }
  117. /**
  118. * Attempts to log in.
  119. *
  120. * @param lc The login credentials.
  121. */
  122. private void login(LoginCredentials lc)
  123. {
  124. int index = ServicesFactory.getInstance().login(lc);
  125. switch (index) {
  126. case ServicesFactory.NAME_FAILURE_INDEX:
  127. IJ.showMessage(CONNECTION, "User name is not valid.");
  128. login.cleanFields();
  129. login.requestFocusOnField();
  130. return;
  131. case ServicesFactory.PASSWORD_FAILURE_INDEX:
  132. IJ.showMessage(CONNECTION, "Password is not valid.");
  133. login.cleanField(ScreenLogin.PASSWORD_FIELD);
  134. login.requestFocusOnField();
  135. return;
  136. case ServicesFactory.DNS_INDEX:
  137. IJ.showMessage(CONNECTION, "Failed to log onto OMERO.\n" +
  138. "Please check the server address or try again later.");
  139. login.setControlsEnabled(true);
  140. return;
  141. case ServicesFactory.CONNECTION_INDEX:
  142. IJ.showMessage(CONNECTION, "Failed to log onto OMERO.\n" +
  143. "Please check the port or try again later.");
  144. login.setControlsEnabled(true);
  145. return;
  146. case ServicesFactory.ACTIVE_INDEX:
  147. IJ.showMessage(CONNECTION, "Your user account is no " +
  148. "longer active.\nPlease" +
  149. " contact your administrator.");
  150. login.setControlsEnabled(true);
  151. return;
  152. case ServicesFactory.CONFIGURATION_INDEX:
  153. IJ.showMessage(CONNECTION, "Please unset ICE_CONFIG.");
  154. login.setControlsEnabled(true);
  155. return;
  156. case ServicesFactory.PERMISSION_INDEX:
  157. IJ.showMessage(CONNECTION, "Failed to log onto OMERO.\n" +
  158. "Please check the username and/or password\n " +
  159. "or try again later.");
  160. login.setControlsEnabled(true);
  161. login.cleanField(ScreenLogin.PASSWORD_FIELD);
  162. login.requestFocusOnField();
  163. return;
  164. case ServicesFactory.SUCCESS_INDEX:
  165. //check version
  166. String v = ServicesFactory.getInstance().getServerVersion();
  167. if (!checkClientServerCompatibility(v, version)) {
  168. IJ.showMessage(CONNECTION, "The plugin is not " +
  169. "compatible with \nserver version:"+v);
  170. login.setControlsEnabled(true);
  171. login.requestFocusOnField();
  172. ServicesFactory.getInstance().logout();
  173. return;
  174. }
  175. close(false);
  176. TreeViewer viewer = TreeViewerFactory.getTreeViewer();
  177. if (viewer != null) viewer.activate();
  178. }
  179. }
  180. /**
  181. * Closes the application.
  182. *
  183. * @param cancel Pass <code>true</code> to cancel any on-going
  184. * connection's attempt, <code>false</code> otherwise.
  185. */
  186. private void close(boolean cancel)
  187. {
  188. if (cancel) ServicesFactory.getInstance().exitPlugin();
  189. logo.close();
  190. login.close();
  191. }
  192. /** Creates a new instance. */
  193. LoginScreenManager()
  194. {
  195. }
  196. /** Starts. */
  197. void start()
  198. {
  199. //if we are already connected, bring up the window.
  200. if (ServicesFactory.getInstance().isConnected()) {
  201. TreeViewer viewer = TreeViewerFactory.getTreeViewer();
  202. if (viewer != null) viewer.activate();
  203. } else {
  204. Icon splashLogin = IconManager.getLoginBackground();
  205. login = new ScreenLogin(TITLE, splashLogin,
  206. IconManager.getOMEImageIcon(), VERSION, PORT);
  207. logo = new ScreenLogo(TITLE, IconManager.getImageJSplashscreen(),
  208. IconManager.getOMEImageIcon());
  209. logo.setStatusVisible(false);
  210. login.addPropertyChangeListener(this);
  211. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  212. Dimension d = logo.getExtendedSize();
  213. Dimension dlogin = login.getPreferredSize();
  214. int totalHeight = d.height+dlogin.height;
  215. logo.setBounds((screenSize.width-d.width)/2,
  216. (screenSize.height-totalHeight)/2, d.width,
  217. logo.getSize().height);
  218. logo.addWindowStateListener(this);
  219. logo.addWindowFocusListener(this);
  220. Rectangle r = logo.getBounds();
  221. login.setBounds(r.x, r.y+d.height, dlogin.width, dlogin.height);
  222. login.addWindowStateListener(this);
  223. login.addWindowFocusListener(this);
  224. logo.setVisible(true);
  225. login.setVisible(true);
  226. }
  227. }
  228. /**
  229. * Listens to property fired by the {@link ScreenLogin}.
  230. * @see PropertyChangeListener#propertyChange(PropertyChangeEvent)
  231. */
  232. public void propertyChange(PropertyChangeEvent evt)
  233. {
  234. String name = evt.getPropertyName();
  235. if (ScreenLogin.LOGIN_PROPERTY.equals(name)) {
  236. LoginCredentials lc = (LoginCredentials) evt.getNewValue();
  237. if (lc != null) login(lc);
  238. } else if (ScreenLogin.QUIT_PROPERTY.equals(name)) {
  239. close(true);
  240. }
  241. }
  242. /**
  243. * Reacts to state changes fired by the {@link ScreenLogo} and
  244. * {@link ScreenLogin}.
  245. * @see WindowStateListener#windowStateChanged(WindowEvent)
  246. */
  247. public void windowStateChanged(WindowEvent e)
  248. {
  249. Object src = e.getSource();
  250. int state = e.getNewState();
  251. if (src instanceof ScreenLogo) setWindowState(login, state);
  252. else if (src instanceof ScreenLogin) setWindowState(logo, state);
  253. if (login != null) login.setAlwaysOnTop(state == JFrame.NORMAL);
  254. logo.setAlwaysOnTop(state == JFrame.NORMAL);
  255. }
  256. /**
  257. * Resets the flag when one of the windows loses focus.
  258. * @see WindowFocusListener#windowLostFocus(WindowEvent)
  259. */
  260. public void windowLostFocus(WindowEvent e)
  261. {
  262. if (e.getOppositeWindow() == null) {
  263. if (login != null) login.setAlwaysOnTop(false);
  264. if (login != null) logo.setAlwaysOnTop(false);
  265. }
  266. }
  267. /**
  268. * Required by the {@link WindowFocusListener} I/F but no-operation
  269. * implementation in our case.
  270. * @see WindowFocusListener#windowGainedFocus(WindowEvent)
  271. */
  272. public void windowGainedFocus(WindowEvent e) {}
  273. }