]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/editors/GroupMainPage.java
f56f1cf670ee995fb80899a7a7892e9bfdc910ca
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / editors / GroupMainPage.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.security.ui.admin.editors;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.jcr.RepositoryException;
22
23 import org.argeo.ArgeoException;
24 import org.argeo.eclipse.ui.EclipseUiUtils;
25 import org.argeo.jcr.ArgeoNames;
26 import org.argeo.security.ui.admin.internal.ColumnDefinition;
27 import org.argeo.security.ui.admin.internal.CommonNameLP;
28 import org.argeo.security.ui.admin.internal.MailLP;
29 import org.argeo.security.ui.admin.internal.RoleIconLP;
30 import org.argeo.security.ui.admin.internal.UserAdminConstants;
31 import org.argeo.security.ui.admin.internal.UserNameLP;
32 import org.argeo.security.ui.admin.internal.UserTableDefaultDClickListener;
33 import org.argeo.security.ui.admin.internal.UserTableViewer;
34 import org.eclipse.jface.viewers.TableViewer;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Text;
41 import org.eclipse.ui.forms.AbstractFormPart;
42 import org.eclipse.ui.forms.IManagedForm;
43 import org.eclipse.ui.forms.SectionPart;
44 import org.eclipse.ui.forms.editor.FormEditor;
45 import org.eclipse.ui.forms.editor.FormPage;
46 import org.eclipse.ui.forms.widgets.FormToolkit;
47 import org.eclipse.ui.forms.widgets.ScrolledForm;
48 import org.eclipse.ui.forms.widgets.Section;
49 import org.osgi.service.useradmin.Group;
50 import org.osgi.service.useradmin.Role;
51 import org.osgi.service.useradmin.User;
52 import org.osgi.service.useradmin.UserAdmin;
53
54 /** Display/edit main properties of a given group */
55 public class GroupMainPage extends FormPage implements ArgeoNames {
56 final static String ID = "GroupEditor.mainPage";
57
58 private final UserEditor editor;
59 private UserAdmin userAdmin;
60
61 // Local configuration
62 private final int PRE_TITLE_INDENT = 10;
63
64 public GroupMainPage(FormEditor editor, UserAdmin userAdmin) {
65 super(editor, ID, "Main");
66 this.editor = (UserEditor) editor;
67 this.userAdmin = userAdmin;
68 }
69
70 protected void createFormContent(final IManagedForm mf) {
71 try {
72 ScrolledForm form = mf.getForm();
73 refreshFormTitle();
74
75 // Body
76 Composite body = form.getBody();
77 GridLayout mainLayout = new GridLayout();
78 body.setLayout(mainLayout);
79 appendOverviewPart(body);
80 appendMembersPart(body);
81 } catch (RepositoryException e) {
82 throw new ArgeoException("Cannot create form content", e);
83 }
84 }
85
86 /** Creates the general section */
87 protected void appendOverviewPart(Composite parent) {
88 FormToolkit tk = getManagedForm().getToolkit();
89 Composite body = addSection(tk, parent, "Main information");
90 GridLayout layout = new GridLayout(2, false);
91 body.setLayout(layout);
92
93 Text distinguishedName = createLT(body, "Group Name",
94 editor.getProperty(UserAdminConstants.KEY_UID));
95 distinguishedName.setEnabled(false);
96
97 final Text commonName = createLT(body, "Common Name",
98 editor.getProperty(UserAdminConstants.KEY_CN));
99 commonName.setEnabled(false);
100
101 // create form part (controller)
102 AbstractFormPart part = new SectionPart((Section) body.getParent()) {
103 public void commit(boolean onSave) {
104 super.commit(onSave);
105 }
106 };
107 getManagedForm().addPart(part);
108 }
109
110 /** Filtered table with members. Has drag & drop ability */
111 protected void appendMembersPart(Composite parent)
112 throws RepositoryException {
113
114 FormToolkit tk = getManagedForm().getToolkit();
115 Section section = tk.createSection(parent, Section.TITLE_BAR);
116 section.setLayoutData(EclipseUiUtils.fillAll());
117 section.setText("Members of group "
118 + editor.getProperty(UserAdminConstants.KEY_CN));
119
120 // Composite body = tk.createComposite(section, SWT.NONE);
121 Composite body = new Composite(section, SWT.NO_FOCUS);
122 section.setClient(body);
123 body.setLayoutData(EclipseUiUtils.fillAll());
124
125 createMemberPart(body);
126
127 // create form part (controller)
128 AbstractFormPart part = new SectionPart(section) {
129 public void commit(boolean onSave) {
130 super.commit(onSave);
131 }
132 };
133
134 getManagedForm().addPart(part);
135 }
136
137 // UI Objects
138 private UserTableViewer userTableViewerCmp;
139 private TableViewer userViewer;
140 private List<ColumnDefinition> columnDefs = new ArrayList<ColumnDefinition>();
141
142 public void createMemberPart(Composite parent) {
143 // parent.setLayout(EclipseUiUtils.noSpaceGridLayout());
144 // parent2.setLayoutData(EclipseUiUtils.fillAll());
145 // Composite parent = new Composite(parent2, SWT.NO_FOCUS);
146 // parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
147 // parent.setLayoutData(EclipseUiUtils.fillAll());
148
149 parent.setLayout(EclipseUiUtils.noSpaceGridLayout());
150 // Define the displayed columns
151 columnDefs.add(new ColumnDefinition(new RoleIconLP(), "", 0, 24));
152 columnDefs.add(new ColumnDefinition(new UserNameLP(),
153 "Distinguished Name", 240));
154 columnDefs.add(new ColumnDefinition(new CommonNameLP(), "Common Name",
155 150));
156 columnDefs.add(new ColumnDefinition(new MailLP(), "Primary Mail", 150));
157
158 // Create and configure the table
159 userTableViewerCmp = new MyUserTableViewer(parent, SWT.MULTI
160 | SWT.H_SCROLL | SWT.V_SCROLL, userAdmin);
161
162 userTableViewerCmp.setColumnDefinitions(columnDefs);
163 userTableViewerCmp.populate(true, false);
164 // userTableViewerCmp.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
165 // false, false));
166 userTableViewerCmp.setLayoutData(EclipseUiUtils.fillAll());
167
168 // Links
169 userViewer = userTableViewerCmp.getTableViewer();
170 userViewer.addDoubleClickListener(new UserTableDefaultDClickListener());
171 // Really?
172 userTableViewerCmp.refresh();
173 }
174
175 private class MyUserTableViewer extends UserTableViewer {
176 private static final long serialVersionUID = 8467999509931900367L;
177
178 public MyUserTableViewer(Composite parent, int style,
179 UserAdmin userAdmin) {
180 super(parent, style, userAdmin, true);
181 }
182
183 @Override
184 protected List<User> listFilteredElements(String filter) {
185 Group group = (Group) editor.getDisplayedUser();
186 Role[] roles = group.getMembers();
187 List<User> users = new ArrayList<User>();
188 for (Role role : roles)
189 // if (role.getType() == Role.GROUP)
190 users.add((User) role);
191 return users;
192 }
193 }
194
195 private void refreshFormTitle() throws RepositoryException {
196 getManagedForm().getForm().setText(
197 editor.getProperty(UserAdminConstants.KEY_CN));
198 }
199
200 private Composite addSection(FormToolkit tk, Composite parent, String title) {
201 Section section = tk.createSection(parent, Section.TITLE_BAR);
202 GridData gd = EclipseUiUtils.fillWidth();
203 gd.verticalAlignment = PRE_TITLE_INDENT;
204 section.setLayoutData(gd);
205 section.setText(title);
206 Composite body = tk.createComposite(section, SWT.WRAP);
207 body.setLayoutData(EclipseUiUtils.fillAll());
208 section.setClient(body);
209 return body;
210 }
211
212 /** Creates label and text. */
213 protected Text createLT(Composite body, String label, String value) {
214 FormToolkit toolkit = getManagedForm().getToolkit();
215 Label lbl = toolkit.createLabel(body, label);
216 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
217 Text text = toolkit.createText(body, value, SWT.BORDER);
218 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
219 return text;
220 }
221
222 // private class FormPartML implements ModifyListener {
223 // private static final long serialVersionUID = 6299808129505381333L;
224 // private AbstractFormPart formPart;
225 //
226 // public FormPartML(AbstractFormPart generalPart) {
227 // this.formPart = generalPart;
228 // }
229 //
230 // public void modifyText(ModifyEvent e) {
231 // formPart.markDirty();
232 // }
233 // }
234 }