PageRenderTime 30ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/src/main/java/org/isatools/isacreator/gui/menu/CreateProfileMenu.java

http://github.com/ISA-tools/ISAcreator
Java | 361 lines | 246 code | 66 blank | 49 comment | 23 complexity | 4eed000956f3f20ec6918011e43fe4a8 MD5 | raw file
  1. /**
  2. ISAcreator is a component of the ISA software suite (http://www.isa-tools.org)
  3. License:
  4. ISAcreator is licensed under the Common Public Attribution License version 1.0 (CPAL)
  5. EXHIBIT A. CPAL version 1.0
  6. The contents of this file are subject to the CPAL version 1.0 (the License);
  7. you may not use this file except in compliance with the License. You may obtain a
  8. copy of the License at http://isa-tools.org/licenses/ISAcreator-license.html.
  9. The License is based on the Mozilla Public License version 1.1 but Sections
  10. 14 and 15 have been added to cover use of software over a computer network and
  11. provide for limited attribution for the Original Developer. In addition, Exhibit
  12. A has been modified to be consistent with Exhibit B.
  13. Software distributed under the License is distributed on an AS IS basis,
  14. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  15. the specific language governing rights and limitations under the License.
  16. The Original Code is ISAcreator.
  17. The Original Developer is the Initial Developer. The Initial Developer of the
  18. Original Code is the ISA Team (Eamonn Maguire, eamonnmag@gmail.com;
  19. Philippe Rocca-Serra, proccaserra@gmail.com; Susanna-Assunta Sansone, sa.sanson@gmail.com;
  20. http://www.isa-tools.org). All portions of the code written by the ISA Team are
  21. Copyright (c) 2007-2011 ISA Team. All Rights Reserved.
  22. EXHIBIT B. Attribution Information
  23. Attribution Copyright Notice: Copyright (c) 2008-2011 ISA Team
  24. Attribution Phrase: Developed by the ISA Team
  25. Attribution URL: http://www.isa-tools.org
  26. Graphic Image provided in the Covered Code as file: http://isa-tools.org/licenses/icons/poweredByISAtools.png
  27. Display of Attribution Information is required in Larger Works which are defined in the CPAL as a work which combines Covered Code or portions thereof with code not governed by the terms of the CPAL.
  28. Sponsors:
  29. The ISA Team and the ISA software suite have been funded by the EU Carcinogenomics project (http://www.carcinogenomics.eu), the UK BBSRC (http://www.bbsrc.ac.uk), the UK NERC-NEBC (http://nebc.nerc.ac.uk) and in part by the EU NuGO consortium (http://www.nugo.org/everyone).
  30. */
  31. package org.isatools.isacreator.gui.menu;
  32. import org.isatools.isacreator.api.CreateProfile;
  33. import org.isatools.isacreator.common.UIHelper;
  34. import org.isatools.isacreator.common.button.ButtonType;
  35. import org.isatools.isacreator.common.button.FlatButton;
  36. import org.isatools.isacreator.gui.HistoricalSelectionGUI;
  37. import org.isatools.isacreator.launch.ISAcreatorCLArgs;
  38. import org.isatools.isacreator.managers.ApplicationManager;
  39. import org.isatools.isacreator.orcid.OrcidClient;
  40. import org.isatools.isacreator.orcid.gui.OrcidContactSelectedEvent;
  41. import org.isatools.isacreator.orcid.gui.OrcidContactSelectionCancelledEvent;
  42. import org.isatools.isacreator.orcid.gui.OrcidLookupUI;
  43. import org.isatools.isacreator.orcid.impl.OrcidClientImpl;
  44. import org.isatools.isacreator.orcid.model.OrcidAuthor;
  45. import org.jdesktop.fuse.InjectedResource;
  46. import javax.swing.*;
  47. import java.awt.*;
  48. import java.awt.event.ActionEvent;
  49. import java.awt.event.ActionListener;
  50. import java.awt.event.MouseAdapter;
  51. import java.awt.event.MouseEvent;
  52. /**
  53. * CreateProfileGUI provides interface to allow users to construct a new profile
  54. * so that their settings such as previously selected ontology terms, factors,
  55. * protocols, and contacts can be retrieved.
  56. *
  57. * @author Eamonn Maguire
  58. */
  59. public class CreateProfileMenu extends UserCreationMenu {
  60. @InjectedResource
  61. private ImageIcon createProfileButton, createProfileButtonOver, backButtonSml, backButtonSmlOver, searchOrcid;
  62. private JTextField firstnameVal;
  63. private JTextField institutionVal;
  64. private JTextField surnameVal;
  65. private JTextField orcid;
  66. private OrcidLookupUI orcidLookupUI;
  67. public CreateProfileMenu(ISAcreatorMenu menu) {
  68. super(menu);
  69. status = new JLabel("");
  70. status.setForeground(UIHelper.RED_COLOR);
  71. setPreferredSize(new Dimension(390, 400));
  72. setLayout(new BorderLayout());
  73. setOpaque(false);
  74. }
  75. public void createGUI() {
  76. Box fields = Box.createVerticalBox();
  77. fields.add(Box.createVerticalStrut(11));
  78. fields.setOpaque(false);
  79. Action createProfileAction = new AbstractAction() {
  80. public void actionPerformed(ActionEvent e) {
  81. createProfile();
  82. }
  83. };
  84. Action lookupUserInfoFromOrcidAction = new AbstractAction() {
  85. public void actionPerformed(ActionEvent actionEvent) {
  86. lookupUserInfoFromOrcid();
  87. }
  88. };
  89. JPanel userNameCont = createUsernamePanel(createProfileAction);
  90. JPanel passwordCont = createPasswordPanel(createProfileAction);
  91. JPanel confirmPasswordCont = createConfirmPasswordPanel(createProfileAction);
  92. JPanel orcidIdCont = createOrcidPanel(lookupUserInfoFromOrcidAction);
  93. JPanel firstNameCont = createForenamePanel(createProfileAction);
  94. JPanel surnameCont = createSurnamePanel(createProfileAction);
  95. JPanel institutionCont = createInstitutionPanel(createProfileAction);
  96. JPanel emailCont = createEmailPanel(createProfileAction);
  97. fields.add(orcidIdCont);
  98. fields.add(Box.createVerticalStrut(8));
  99. fields.add(userNameCont);
  100. fields.add(Box.createVerticalStrut(8));
  101. fields.add(passwordCont);
  102. fields.add(Box.createVerticalStrut(8));
  103. fields.add(confirmPasswordCont);
  104. fields.add(Box.createVerticalStrut(8));
  105. fields.add(firstNameCont);
  106. fields.add(Box.createVerticalStrut(8));
  107. fields.add(surnameCont);
  108. fields.add(Box.createVerticalStrut(8));
  109. fields.add(institutionCont);
  110. fields.add(Box.createVerticalStrut(8));
  111. fields.add(emailCont);
  112. JLabel info = new JLabel(
  113. "<html><b>* </b> indicates required field </html>", SwingConstants.RIGHT);
  114. info.setForeground(UIHelper.DARK_GREEN_COLOR);
  115. fields.add(UIHelper.wrapComponentInPanel(info));
  116. JPanel northPanel = new JPanel();
  117. northPanel.add(new JLabel(
  118. new ImageIcon(getClass()
  119. .getResource("/images/gui/createNewProfile.png")),
  120. JLabel.RIGHT), BorderLayout.NORTH);
  121. northPanel.add(fields, BorderLayout.CENTER);
  122. JPanel buttonContainer = new JPanel(new BorderLayout());
  123. createBackJLabel();
  124. buttonContainer.add(back, BorderLayout.WEST);
  125. JButton createProfile = new FlatButton(ButtonType.GREEN, "Save");
  126. createProfile.addActionListener(new ActionListener() {
  127. public void actionPerformed(ActionEvent actionEvent) {
  128. createProfile();
  129. }
  130. });
  131. JButton backButton = new FlatButton(ButtonType.GREY, "Back", UIHelper.GREY_COLOR);
  132. backButton.addActionListener(new ActionListener() {
  133. public void actionPerformed(ActionEvent actionEvent) {
  134. menu.changeView(menu.getAuthenticationGUI());
  135. }
  136. });
  137. buttonContainer.add(backButton, BorderLayout.WEST);
  138. buttonContainer.add(createProfile, BorderLayout.EAST);
  139. JPanel southPanel = new JPanel(new GridLayout(2, 1));
  140. southPanel.setOpaque(false);
  141. JPanel statusContainer = new JPanel(new BorderLayout());
  142. statusContainer.setOpaque(false);
  143. statusContainer.setPreferredSize(new Dimension(390, 30));
  144. statusContainer.add(status, BorderLayout.CENTER);
  145. southPanel.add(UIHelper.wrapComponentInPanel(statusContainer));
  146. southPanel.add(buttonContainer);
  147. northPanel.add(southPanel, BorderLayout.SOUTH);
  148. northPanel.setOpaque(false);
  149. add(northPanel, BorderLayout.CENTER);
  150. }
  151. private JPanel createInstitutionPanel(Action createProfileAction) {
  152. // institution
  153. JPanel institutionCont = createPanel();
  154. JLabel institutionLabel = createLabel("institution *");
  155. institutionCont.add(institutionLabel);
  156. institutionVal = createTextField();
  157. institutionCont.add(institutionVal);
  158. assignKeyActionToComponent(createProfileAction, institutionVal);
  159. return institutionCont;
  160. }
  161. private JPanel createSurnamePanel(Action createProfileAction) {
  162. //surname
  163. JPanel surnameCont = createPanel();
  164. JLabel surnameLabel = createLabel("surname *");
  165. surnameCont.add(surnameLabel);
  166. surnameVal = createTextField();
  167. surnameCont.add(surnameVal);
  168. assignKeyActionToComponent(createProfileAction, surnameVal);
  169. return surnameCont;
  170. }
  171. private JPanel createForenamePanel(Action createProfileAction) {
  172. //forename
  173. JPanel firstNameCont = createPanel();
  174. JLabel firstNameLabel = createLabel("forename *");
  175. firstNameCont.add(firstNameLabel);
  176. firstnameVal = createTextField();
  177. firstNameCont.add(firstnameVal);
  178. assignKeyActionToComponent(createProfileAction, firstnameVal);
  179. return firstNameCont;
  180. }
  181. private JPanel createOrcidPanel(Action lookupOrcid) {
  182. JPanel orcidCont = new JPanel(new GridLayout(1, 2));
  183. orcidCont.setOpaque(false);
  184. JLabel orcidLabel = createLabel("orcid");
  185. orcidCont.add(orcidLabel);
  186. orcid = createTextField();
  187. assignKeyActionToComponent(lookupOrcid, orcid);
  188. JLabel searchOrcidLabel = UIHelper.createLabel("Look up", UIHelper.VER_11_BOLD, UIHelper.BELIZE_HOLE, JLabel.RIGHT);
  189. searchOrcidLabel.addMouseListener(new MouseAdapter() {
  190. @Override
  191. public void mouseEntered(MouseEvent mouseEvent) {
  192. super.mouseEntered(mouseEvent);
  193. ((JComponent) mouseEvent.getSource()).setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  194. }
  195. @Override
  196. public void mouseExited(MouseEvent mouseEvent) {
  197. super.mouseExited(mouseEvent);
  198. ((JComponent) mouseEvent.getSource()).setCursor(Cursor.getDefaultCursor());
  199. }
  200. @Override
  201. public void mouseClicked(MouseEvent mouseEvent) {
  202. SwingUtilities.invokeLater(new Runnable() {
  203. public void run() {
  204. if (orcidLookupUI == null) {
  205. orcidLookupUI = new OrcidLookupUI();
  206. orcidLookupUI.createGUI();
  207. orcidLookupUI.installListeners();
  208. }
  209. orcidLookupUI.setLocationRelativeTo(ApplicationManager.getCurrentApplicationInstance());
  210. orcidLookupUI.setVisible(true);
  211. orcidLookupUI.addPropertyChangeListener("selectedOrcid", new OrcidContactSelectedEvent(orcidLookupUI, orcid, firstnameVal, surnameVal, emailVal));
  212. orcidLookupUI.addPropertyChangeListener("noSelectedOrcid", new OrcidContactSelectionCancelledEvent(orcidLookupUI));
  213. }//run
  214. });//runnable
  215. }
  216. });
  217. JPanel orcidInfoContainer = new JPanel();
  218. orcidInfoContainer.setLayout(new BoxLayout(orcidInfoContainer, BoxLayout.LINE_AXIS));
  219. orcidInfoContainer.add(orcid);
  220. orcidInfoContainer.add(searchOrcidLabel);
  221. orcidCont.add(orcidInfoContainer);
  222. return orcidCont;
  223. }
  224. private void lookupUserInfoFromOrcid() {
  225. OrcidClient client = new OrcidClientImpl();
  226. OrcidAuthor author = client.getAuthorInfo(orcid.getText());
  227. if (author == null)
  228. return;
  229. firstnameVal.setText(author.getGivenNames());
  230. surnameVal.setText(author.getFamilyName());
  231. emailVal.setText(author.getEmail());
  232. }
  233. private void createProfile() {
  234. // check password is not empty and that the password and the confirmation match!
  235. if (CreateProfile.emptyPassword(passwordVal.getPassword())) {
  236. status.setText(
  237. "<html><b>password is required!</b></html>");
  238. return;
  239. }
  240. if (!CreateProfile.matchingPasswords(passwordVal.getPassword(), confirmPasswordVal.getPassword())) {
  241. status.setText(
  242. "<html><b>passwords do not match!</b> the password and confirmation must match!</html>");
  243. return;
  244. }
  245. // check the rest of the fields to ensure values have been entered and proceed to creating the
  246. // profile if everything is ok!
  247. if (!CreateProfile.emptyField(usernameVal.getText())) {
  248. if (!CreateProfile.emptyField(firstnameVal.getText())) {
  249. if (!CreateProfile.emptyField(surnameVal.getText())) {
  250. if (!CreateProfile.emptyField(institutionVal.getText())) {
  251. if (!CreateProfile.emptyField(emailVal.getText())) {
  252. if (CreateProfile.validEmail(emailVal.getText())) {
  253. if (CreateProfile.duplicateUser(usernameVal.getText())) {
  254. status.setText(
  255. "<html><b>user name taken!</b> this username is already in use</html>");
  256. } else {
  257. CreateProfile.createProfile(usernameVal.getText(), passwordVal.getPassword(), firstnameVal.getText(), surnameVal.getText(), institutionVal.getText(), emailVal.getText());
  258. if (ISAcreatorCLArgs.configDir() == null) {
  259. menu.changeView(menu.getImportConfigurationGUI());
  260. } else {
  261. menu.changeView(menu.getMainMenuGUI());
  262. }
  263. }
  264. } else {
  265. status.setText(
  266. "<html><b>email is not valid!</b> please enter a valid email address</html>");
  267. }
  268. } else {
  269. status.setText(
  270. "<html><b>email is required!</b> please enter an email</html>");
  271. }
  272. } else {
  273. status.setText(
  274. "<html><b>institution is required!</b> please enter a surname</html>");
  275. }
  276. } else {
  277. status.setText(
  278. "<html><b>surname is required!</b> please enter a surname</html>");
  279. }
  280. } else {
  281. status.setText(
  282. "<html><b>forename is required!</b> please enter a forename</html>");
  283. }
  284. } else {
  285. status.setText(
  286. "<html><b>username is required!</b> please enter a username</html>");
  287. }
  288. }//createProfile method
  289. }