merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / group / grantedauthority / CdmAuthorityRow.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.ui.group.grantedauthority;
10
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.events.DisposeEvent;
13 import org.eclipse.swt.events.DisposeListener;
14 import org.eclipse.swt.events.MouseAdapter;
15 import org.eclipse.swt.events.MouseEvent;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.ui.forms.widgets.FormToolkit;
23
24 import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
25 import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
26 import eu.etaxonomy.taxeditor.model.ImageResources;
27
28
29 /**
30 * Row widget for editing a single CDM Authority
31 *
32 * @author cmathew
33 * @created Mar 28, 2013
34 *
35 */
36 public class CdmAuthorityRow extends Composite {
37
38 private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
39 private Label lblEntity;
40 private Label lblUuid;
41 private CRUDOperationChooser operationChooser;
42 private Button btnDelete;
43
44 private GrantedAuthorityImpl grantedAuthorityI;
45 private Label lblDirtyFlag;
46
47 private CdmAuthorityCompositeViewer cdmaModel;
48
49 /**
50 * Create the composite, made up of 5 elements :
51 * 1. '*' or depending on whether the row is being edited
52 * 2. Classname of entity (e.g. TAXONNODE)
53 * 3. Uuid of entity
54 * 4. CRUD operations edit widget (this is essentially 4 checkboxes to select the 4 possible operations)
55 * 5. Delete button (to delete the row)
56 *
57 * @param parent
58 * @param style
59 */
60 public CdmAuthorityRow(Composite parent, int style) {
61 super(parent, SWT.NONE);
62 addDisposeListener(new DisposeListener() {
63 public void widgetDisposed(DisposeEvent e) {
64 toolkit.dispose();
65 }
66 });
67 toolkit.adapt(this);
68 toolkit.paintBordersFor(this);
69 setLayout(new GridLayout(5, false));
70
71
72
73 lblDirtyFlag = new Label(this, SWT.NONE);
74 lblDirtyFlag.setAlignment(SWT.CENTER);
75 GridData gd_lblDirtyFlag = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
76 gd_lblDirtyFlag.widthHint = 15;
77 lblDirtyFlag.setLayoutData(gd_lblDirtyFlag);
78 toolkit.adapt(lblDirtyFlag, true, true);
79 lblDirtyFlag.setText("*");
80
81 lblEntity = new Label(this, SWT.BORDER);
82 GridData gd_lblEntity = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
83 gd_lblEntity.heightHint = 30;
84 gd_lblEntity.widthHint = 200;
85 lblEntity.setLayoutData(gd_lblEntity);
86 lblEntity.setAlignment(SWT.CENTER);
87 toolkit.adapt(lblEntity, true, true);
88 lblEntity.setText("Entity");
89
90 lblUuid = new Label(this, SWT.BORDER);
91 GridData gd_lblUuid = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
92 gd_lblUuid.heightHint = 30;
93 gd_lblUuid.widthHint = 270;
94 lblUuid.setLayoutData(gd_lblUuid);
95 lblUuid.setAlignment(SWT.CENTER);
96 toolkit.adapt(lblUuid, true, true);
97 lblUuid.setText("Uuid");
98
99 operationChooser = new CRUDOperationChooser(this, SWT.BORDER);
100 operationChooser.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
101 toolkit.adapt(operationChooser);
102 toolkit.paintBordersFor(operationChooser);
103
104 btnDelete = new Button(this, SWT.NONE);
105
106 btnDelete.setImage(ImageResources.getImage(ImageResources.TRASH_ICON));
107 btnDelete.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
108 btnDelete.addMouseListener(new MouseAdapter() {
109 @Override
110 public void mouseUp(MouseEvent arg0) {
111 if(cdmaModel != null) {
112 cdmaModel.removeCdmAuthority(grantedAuthorityI);
113 }
114 }
115 });
116
117 toolkit.adapt(btnDelete, true, true);
118
119 }
120
121 /**
122 * Sets the dirty flag for this row
123 *
124 * @param isDirty
125 */
126 public void setDirty(boolean isDirty) {
127 if(isDirty) {
128 lblDirtyFlag.setText("*");
129 } else {
130 lblDirtyFlag.setText(" ");
131 }
132 }
133
134 /**
135 * Create a {@link CdmAuthority} from a {@link GrantedAuthorityImpl} and create a row from it.
136 *
137 * @param cdmaModel
138 * @param grantedAuthorityI
139 * @param isDirty
140 */
141 public void setRowCdmAuthority(CdmAuthorityCompositeViewer cdmaModel, GrantedAuthorityImpl grantedAuthorityI, boolean isDirty) {
142 this.grantedAuthorityI = grantedAuthorityI;
143 this.cdmaModel = cdmaModel;
144
145 try {
146 CdmAuthority cdmAuthority = CdmAuthority.fromGrantedAuthority(grantedAuthorityI);
147
148 setDirty(isDirty);
149
150 String entityStr = (cdmAuthority.getPermissionClass() == null)?"":cdmAuthority.getPermissionClass().toString();
151 lblEntity.setText(entityStr);
152 String uuidStr = (cdmAuthority.getTargetUUID() == null)?"":cdmAuthority.getTargetUUID().toString();
153 lblUuid.setText(uuidStr);
154
155 operationChooser.setAuthority(cdmaModel, grantedAuthorityI, cdmAuthority);
156
157 } catch (Exception e) {
158 // TODO Auto-generated catch block
159 e.printStackTrace();
160 }
161
162 }
163
164 }