PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/src/org/rapla/gui/internal/edit/UserEditUI.java

http://rapla.googlecode.com/
Java | 258 lines | 192 code | 35 blank | 31 comment | 32 complexity | 0416396557ca4d115c45c9a096e42b44 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, CPL-1.0, LGPL-2.0, AGPL-1.0
  1. /*--------------------------------------------------------------------------*
  2. | Copyright (C) 2006 Christopher Kohlhaas |
  3. | |
  4. | This program is free software; you can redistribute it and/or modify |
  5. | it under the terms of the GNU General Public License as published by the |
  6. | Free Software Foundation. A copy of the license has been included with |
  7. | these distribution in the COPYING file, if not go to www.fsf.org |
  8. | |
  9. | As a special exception, you are granted the permissions to link this |
  10. | program with every library, which license fulfills the Open Source |
  11. | Definition as published by the Open Source Initiative (OSI). |
  12. *--------------------------------------------------------------------------*/
  13. package org.rapla.gui.internal.edit;
  14. import java.awt.BorderLayout;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.ActionListener;
  17. import java.awt.event.MouseAdapter;
  18. import java.awt.event.MouseEvent;
  19. import java.util.ArrayList;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import javax.swing.JComponent;
  23. import javax.swing.JPanel;
  24. import javax.swing.JToolBar;
  25. import javax.swing.JTree;
  26. import javax.swing.event.ChangeEvent;
  27. import javax.swing.event.ChangeListener;
  28. import javax.swing.tree.TreeNode;
  29. import javax.swing.tree.TreePath;
  30. import org.rapla.entities.Category;
  31. import org.rapla.entities.User;
  32. import org.rapla.entities.domain.Allocatable;
  33. import org.rapla.entities.dynamictype.Attribute;
  34. import org.rapla.entities.dynamictype.Classifiable;
  35. import org.rapla.entities.dynamictype.Classification;
  36. import org.rapla.entities.dynamictype.ClassificationFilter;
  37. import org.rapla.entities.dynamictype.DynamicType;
  38. import org.rapla.entities.dynamictype.DynamicTypeAnnotations;
  39. import org.rapla.framework.RaplaContext;
  40. import org.rapla.framework.RaplaException;
  41. import org.rapla.gui.TreeFactory;
  42. import org.rapla.gui.toolkit.DialogUI;
  43. import org.rapla.gui.toolkit.RaplaButton;
  44. import org.rapla.gui.toolkit.RaplaTree;
  45. /****************************************************************
  46. * This is the controller-class for the User-Edit-Panel *
  47. ****************************************************************/
  48. /*User
  49. 1. username, string
  50. 2. name,string
  51. 3. email,string,
  52. 4. isadmin,boolean
  53. */
  54. class UserEditUI extends AbstractEditUI {
  55. /**
  56. * @param sm
  57. * @throws RaplaException
  58. */
  59. public UserEditUI(RaplaContext sm) throws RaplaException {
  60. super(sm);
  61. EditField[] fields = new EditField[] {
  62. new TextField(sm,"username")
  63. // ,new BooleanField(sm,"maptoperson")
  64. ,new PersonSelectField(sm)
  65. ,new TextField(sm,"name")
  66. ,new TextField(sm,"email")
  67. ,new BooleanField(sm,"admin")
  68. ,new GroupListField(sm)
  69. };
  70. setFields(fields);
  71. }
  72. class PersonSelectField extends AbstractEditField implements ChangeListener, ActionListener {
  73. User user;
  74. JPanel panel = new JPanel();
  75. JToolBar toolbar = new JToolBar();
  76. RaplaButton newButton = new RaplaButton(RaplaButton.SMALL);
  77. RaplaButton removeButton = new RaplaButton(RaplaButton.SMALL);
  78. /**
  79. * @param sm
  80. * @throws RaplaException
  81. */
  82. public PersonSelectField(RaplaContext sm) throws RaplaException {
  83. super(sm);
  84. final Category rootCategory = getQuery().getUserGroupsCategory();
  85. if ( rootCategory == null )
  86. return;
  87. toolbar.add( newButton );
  88. toolbar.add( removeButton );
  89. toolbar.setFloatable( false );
  90. panel.setLayout( new BorderLayout() );
  91. panel.add( toolbar, BorderLayout.NORTH );
  92. newButton.addActionListener( this );
  93. removeButton.addActionListener( this );
  94. removeButton.setText( getString("remove") );
  95. removeButton.setIcon( getIcon( "icon.remove" ) );
  96. newButton.setText( getString("add") );
  97. newButton.setIcon( getIcon( "icon.new" ) );
  98. }
  99. private void updateButton() {
  100. final boolean personSet = user != null && user.getPerson() != null;
  101. removeButton.setEnabled( personSet) ;
  102. newButton.setEnabled( !personSet) ;
  103. fields[2].getComponent().setEnabled( !personSet);
  104. fields[3].getComponent().setEnabled( !personSet);
  105. }
  106. public JComponent getComponent() {
  107. return panel;
  108. }
  109. public boolean isBlock() {
  110. return false;
  111. }
  112. public boolean isVariableSized() {
  113. return false;
  114. }
  115. public String getName()
  116. {
  117. return getString("bind_with_person");
  118. }
  119. public void mapFrom(Object o) throws RaplaException {
  120. user = (User) o;
  121. updateButton();
  122. }
  123. protected Object getValue() {
  124. return null;
  125. }
  126. protected void setValue(Object object) {
  127. }
  128. public void mapTo(Object o) throws RaplaException {
  129. }
  130. public void actionPerformed(ActionEvent evt) {
  131. if ( evt.getSource() == newButton)
  132. {
  133. try {
  134. showAddDialog();
  135. } catch (RaplaException ex) {
  136. showException(ex,newButton);
  137. }
  138. }
  139. if ( evt.getSource() == removeButton)
  140. {
  141. user.setPerson( null );
  142. updateButton();
  143. }
  144. }
  145. public void stateChanged(ChangeEvent evt) {
  146. }
  147. private void showAddDialog() throws RaplaException {
  148. final DialogUI dialog;
  149. RaplaTree treeSelection = new RaplaTree();
  150. treeSelection.setMultiSelect(true);
  151. final TreeFactory treeFactory = getTreeFactory();
  152. treeSelection.getTree().setCellRenderer(treeFactory.createRenderer());
  153. final DynamicType[] personTypes = getQuery().getDynamicTypes(DynamicTypeAnnotations.VALUE_PERSON_CLASSIFICATION);
  154. List<ClassificationFilter> filters = new ArrayList<ClassificationFilter>();
  155. for (DynamicType personType: personTypes)
  156. {
  157. if ( personType.getAttribute("email") != null)
  158. {
  159. final ClassificationFilter filter = personType.newClassificationFilter();
  160. filters.add( filter);
  161. }
  162. }
  163. final Allocatable[] allocatables = getQuery().getAllocatables(filters.toArray(ClassificationFilter.CLASSIFICATIONFILTER_ARRAY));
  164. List<Classifiable> allocatablesWithEmail = new ArrayList();
  165. for ( Allocatable a: allocatables)
  166. {
  167. final Classification classification = a.getClassification();
  168. final Attribute attribute = classification.getAttribute("email");
  169. if ( attribute != null)
  170. {
  171. final String email = (String)classification.getValue(attribute);
  172. if (email != null && email.length() > 0)
  173. {
  174. allocatablesWithEmail.add( a );
  175. }
  176. }
  177. }
  178. final Classifiable[] allocatableArray = allocatablesWithEmail.toArray(Classifiable.CLASSIFIABLE_ARRAY);
  179. treeSelection.exchangeTreeModel(treeFactory.createClassifiableModel(allocatableArray));
  180. treeSelection.setMinimumSize(new java.awt.Dimension(300, 200));
  181. treeSelection.setPreferredSize(new java.awt.Dimension(400, 260));
  182. dialog = DialogUI.create(
  183. getContext()
  184. ,getComponent()
  185. ,true
  186. ,treeSelection
  187. ,new String[] { getString("apply"),getString("cancel")});
  188. final JTree tree = treeSelection.getTree();
  189. tree.addMouseListener(new MouseAdapter() {
  190. // End dialog when a leaf is double clicked
  191. public void mousePressed(MouseEvent e) {
  192. TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
  193. if (selPath != null && e.getClickCount() == 2) {
  194. final Object lastPathComponent = selPath.getLastPathComponent();
  195. if (((TreeNode) lastPathComponent).isLeaf() )
  196. dialog.getButton(0).doClick();
  197. }
  198. else if (selPath != null && e.getClickCount() == 1) {
  199. final Object lastPathComponent = selPath.getLastPathComponent();
  200. if (((TreeNode) lastPathComponent).isLeaf() )
  201. dialog.getButton(0).setEnabled(true);
  202. else
  203. dialog.getButton(0).setEnabled(false);
  204. }
  205. }
  206. });
  207. dialog.setTitle(getName());
  208. dialog.start();
  209. if (dialog.getSelectedIndex() == 0) {
  210. Iterator it = treeSelection.getSelectedElements().iterator();
  211. while (it.hasNext()) {
  212. user.setPerson((Allocatable) it.next());
  213. fields[2].mapFrom( user);
  214. fields[3].mapFrom( user);
  215. updateButton();
  216. }
  217. }
  218. }
  219. }
  220. final private TreeFactory getTreeFactory() {
  221. return getService(TreeFactory.class);
  222. }
  223. }