001: package org.osbl.orga.gui;
002:
003: import org.osbl.client.wings.form.*;
004: import org.osbl.client.wings.form.editor.*;
005: import org.osbl.client.wings.form.layouter.*;
006: import org.osbl.client.wings.shell.*;
007: import org.osbl.client.wings.XButton;
008: import org.osbl.client.ClientServiceProvider;
009: import org.osbl.client.action.*;
010: import org.osbl.ServiceProvider;
011: import org.osbl.plugin.*;
012: import org.osbl.persistence.model.Concrete;
013: import org.conform.*;
014: import org.conform.wings.editor.StringsEditor;
015: import org.conform.hibernate.HibernateEnvironment;
016: import org.conform.modifier.InlineModifier;
017: import org.osbl.orga.model.Organisation;
018: import org.osbl.orga.logic.OrgaStructureLogic;
019: import org.osbl.identity.model.*;
020: import org.wings.*;
021: import org.wings.session.SessionManager;
022: import org.wingx.XScrollPane;
023:
024: import javax.swing.*;
025: import java.awt.event.ActionEvent;
026: import java.awt.*;
027: import java.util.*;
028: import java.util.List;
029: import java.sql.Timestamp;
030:
031: public class OrganisationForm extends GenericObjectForm {
032: OrgaStructureLogic logic = (OrgaStructureLogic) ServiceProvider
033: .getInstance().getService("OrgaStructureLogic");
034:
035: private AbstractDomainProvider identityTypeDomain = new AbstractDomainProvider() {
036: protected List createValues() {
037: final PluginManager pluginManager = (PluginManager) ClientServiceProvider
038: .getInstance().getService("PluginManager");
039: final ExtensionPoint extensionPoint = new ExtensionPoint(
040: Identity.class.getName() + ".concrete",
041: Concrete.class);
042: final List<Extension> list = pluginManager
043: .getExtensions(extensionPoint);
044:
045: List<String> types = new ArrayList<String>(list.size());
046: types.add(null);
047: for (Extension extension : list)
048: types
049: .add(extension.getImplementation()
050: .getSimpleName());
051: return types;
052: }
053: };
054: private MemberEditor memberEditor;
055: private MemberList memberList;
056: private SPanel membersPanel;
057: private ImportMembersAction importMembersAction = new ImportMembersAction();
058: private DeleteMembersAction deleteMembersAction = new DeleteMembersAction();
059: private ManageMembersAction manageMembersAction = new ManageMembersAction();
060:
061: @Override
062: protected Object getObject(String path) {
063: if (ComponentProvider.ROOT.equals(path))
064: return object;
065:
066: return null;
067: }
068:
069: @Override
070: protected void setObject(String path, Object object) {
071: if (ComponentProvider.ROOT.equals(path))
072: this .object = object;
073: }
074:
075: public void setObject(Object object) {
076: context.putObject("organisation", object);
077: memberList.setContext(context);
078: super .setObject(object);
079: }
080:
081: @Override
082: protected Instruction createLayoutInstruction() {
083: return new Page(
084: Organisation.class.getName(),
085: new Division(Organisation.class.getName(), new Column(
086: new LabelAndEditor(ComponentProvider.ROOT,
087: "key"), new LabelAndEditor(
088: ComponentProvider.ROOT, "name"),
089: new LabelAndEditor(ComponentProvider.ROOT,
090: "groupType"), new LabelAndEditor(
091: ComponentProvider.ROOT, "memberType")),
092: new Column(new LabelAndEditors(
093: "businessobject.created", new Editor(
094: ComponentProvider.ROOT,
095: "created"), new Editor(
096: ComponentProvider.ROOT,
097: "createdBy")),
098: new LabelAndEditor(
099: ComponentProvider.ROOT,
100: "active"))),
101: new Division(Organisation.class.getName()
102: + ".rolesRelations", new Column(
103: new LabelAndEditor(ComponentProvider.ROOT,
104: "roles")), new Column(
105: new LabelAndEditor(ComponentProvider.ROOT,
106: "relations"))),
107: new Division(Organisation.class.getName() + ".members",
108: new Column(new LabelAndCustomEditor("members"))));
109: }
110:
111: @Override
112: protected BeanMeta createBeanMeta(String path) {
113: return new VariationBeanMetaProvider(Client.getInstance()
114: .getBeanMetaProvider()).addModifier(
115: new InlineModifier() {
116: protected void configure() {
117: property("id").setAttribute(
118: PropertyMeta.ATTRIBUTE_CHANGE_LISTENER,
119: new PropertyChangeHandler() {
120: protected void propertyChange(
121: Object oldValue,
122: Object newValue) {
123: enableMembers(newValue != null);
124: }
125: });
126: property("key").setWritable(true);
127: property("roles").setAttribute(
128: org.conform.wings.Editor.CUSTOM_EDITOR,
129: new StringsEditor());
130: property("relations").setAttribute(
131: org.conform.wings.Editor.CUSTOM_EDITOR,
132: new StringsEditor());
133:
134: property("groupType").setDomainProvider(
135: identityTypeDomain);
136: property("memberType").setDomainProvider(
137: identityTypeDomain);
138: }
139: }).getBeanMeta(Organisation.class);
140: }
141:
142: private void enableMembers(boolean enable) {
143: importMembersAction.setEnabled(enable);
144: deleteMembersAction.setEnabled(enable);
145: manageMembersAction.setEnabled(enable);
146: memberList.setEnabled(enable);
147: }
148:
149: protected SComponent createCustomComponent(String key) {
150: if ("members".equals(key)) {
151: memberList = new MemberList();
152: memberList
153: .setLinkAction("identity", new EditMemberAction());
154:
155: XButton importMembersButton = new XButton(
156: importMembersAction);
157: XButton deleteMembersButton = new XButton(
158: deleteMembersAction);
159: XButton manageMembersButton = new XButton(
160: manageMembersAction);
161: XScrollPane scrollPane = new XScrollPane(memberList, 6);
162:
163: membersPanel = new SPanel(new SGridBagLayout());
164: GridBagConstraints constraints = new GridBagConstraints();
165:
166: constraints.gridwidth = GridBagConstraints.REMAINDER;
167: membersPanel.add(scrollPane, constraints);
168: constraints.insets = new Insets(10, 0, 0, 10);
169: constraints.weightx = .1;
170: constraints.gridwidth = 1;
171: membersPanel.add(importMembersButton, constraints);
172: membersPanel.add(deleteMembersButton, constraints);
173: membersPanel.add(manageMembersButton, constraints);
174:
175: constraints.weightx = .7;
176: constraints.gridwidth = GridBagConstraints.REMAINDER;
177:
178: membersPanel.add(new SLabel(), constraints);
179:
180: return membersPanel;
181: }
182:
183: throw new UnsupportedOperationException("No component for '"
184: + key + "'.");
185: }
186:
187: protected SLabel createCustomLabel(String key) {
188: return getComponentProvider().createTitle(
189: Organisation.class.getName() + ".members");
190: }
191:
192: public MemberEditor getMemberEditor() {
193: if (memberEditor == null) {
194: memberEditor = new MemberEditor();
195: memberEditor.setContext(getContext());
196: }
197: return memberEditor;
198: }
199:
200: private class ImportMembersAction extends AbstractEditorAction {
201: public ImportMembersAction() {
202: putValue(Action.NAME, Client.getInstance()
203: .getResourceProvider().getMessage(
204: Organisation.class.getName()
205: + ".buttons.importMembers"));
206: putValue(Action.ACTION_COMMAND_KEY, Organisation.class
207: .getName()
208: + ".buttons.importMembers");
209: putValue(AuthorizedAction.PERMISSION_KEY,
210: Organisation.class.getName() + "[w]");
211: }
212:
213: public void actionPerformed(ActionEvent event) {
214: Class memberType = getMemberType();
215: if (memberType == null)
216: return;
217: try {
218: HibernateEnvironment.getInstance().beginTransaction();
219:
220: Organisation organisation = (Organisation) getObject();
221: Timestamp created = new Timestamp(System
222: .currentTimeMillis());
223: String createdBy = SessionManager.getSession()
224: .getServletRequest().getUserPrincipal()
225: .getName();
226:
227: int result = logic.importMembers(organisation,
228: createdBy);
229: if (result > 0)
230: memberList.refresh();
231: } finally {
232: HibernateEnvironment.getInstance().endTransaction();
233: }
234: }
235: }
236:
237: private class DeleteMembersAction extends AbstractEditorAction {
238: public DeleteMembersAction() {
239: putValue(Action.NAME, Client.getInstance()
240: .getResourceProvider().getMessage(
241: Organisation.class.getName()
242: + ".buttons.delete"));
243: putValue(Action.ACTION_COMMAND_KEY, Organisation.class
244: .getName()
245: + ".buttons.delete");
246: putValue(AuthorizedAction.PERMISSION_KEY,
247: Organisation.class.getName() + "[w]");
248: }
249:
250: public void actionPerformed(ActionEvent event) {
251: List selectedObjects = memberList.getSelectedObjects();
252: for (Object selectedObject : selectedObjects) {
253: getMemberEditor().deleteObject(selectedObject);
254: }
255:
256: if (selectedObjects.size() > 0)
257: memberList.refresh();
258: }
259: }
260:
261: public class ManageMembersAction extends AbstractEditorAction {
262: public ManageMembersAction() {
263: putValue(Action.ACTION_COMMAND_KEY, ".buttons.manage");
264: putValue(AuthorizedAction.PERMISSION_KEY,
265: Organisation.class.getName() + "[w]");
266: }
267:
268: public void actionPerformed(ActionEvent e) {
269: EditorWindow window = getEditorWindow();
270: ObjectEditor editor = window.getEditor();
271: editor.setObject(null);
272: editor.showList();
273: editor.getList().refresh();
274: org.osbl.client.wings.shell.Window parent = org.osbl.client.wings.shell.Window
275: .currentWindow(getEnvironment());
276: Client.getInstance().pushWindow(parent, window);
277: }
278:
279: private EditorWindow getEditorWindow() {
280: EditorWindow window = (EditorWindow) getComponent()
281: .getClientProperty("editorWindow");
282: if (window == null) {
283: ObjectEditor editor = getObjectEditor();
284: window = new EditorWindow(editor);
285: window.addPopAction(new PopAction());
286: getComponent()
287: .putClientProperty("editorWindow", window);
288: }
289: return window;
290: }
291:
292: private MemberEditor getObjectEditor() {
293: return getMemberEditor();
294: }
295: }
296:
297: public class EditMemberAction extends AbstractObjectAction {
298: public EditMemberAction() {
299: putValue(Action.ACTION_COMMAND_KEY, ".buttons.edit");
300: putValue(AuthorizedAction.PERMISSION_KEY,
301: Organisation.class.getName() + "[w]");
302: }
303:
304: public void actionPerformed(ObjectActionEvent event) {
305: EditorWindow window = getEditorWindow();
306: ObjectEditor editor = window.getEditor();
307: editor.setObject(event.getTargets()[0]);
308: editor.showForm();
309: org.osbl.client.wings.shell.Window parent = org.osbl.client.wings.shell.Window
310: .currentWindow(getEnvironment());
311: Client.getInstance().pushWindow(parent, window);
312: }
313:
314: private EditorWindow getEditorWindow() {
315: EditorWindow window = (EditorWindow) getComponent()
316: .getClientProperty("editorWindow");
317: if (window == null) {
318: ObjectEditor editor = getObjectEditor();
319: window = new EditorWindow(editor);
320: window.addPopAction(new PopAction());
321: getComponent()
322: .putClientProperty("editorWindow", window);
323: }
324: return window;
325: }
326:
327: private MemberEditor getObjectEditor() {
328: return getMemberEditor();
329: }
330: }
331:
332: private class PopAction extends AbstractObjectAction {
333: public PopAction() {
334: putValue(Action.ACTION_COMMAND_KEY, ".buttons.back");
335: }
336:
337: public void actionPerformed(ObjectActionEvent event) {
338: memberList.refresh();
339: }
340:
341: public void setEnabled(boolean enabled) {
342: }
343: }
344:
345: private Class getMemberType() {
346: String concrete = (String) getBeanData(ComponentProvider.ROOT)
347: .getPropertyData(
348: getBeanMeta(ComponentProvider.ROOT)
349: .getProperty("memberType")).getValue();
350: if (concrete == null)
351: return null;
352:
353: final PluginManager pluginManager = (PluginManager) ClientServiceProvider
354: .getInstance().getService("PluginManager");
355: final ExtensionPoint extensionPoint = new ExtensionPoint(
356: Identity.class.getName() + ".concrete", Concrete.class);
357: final List<Extension> list = pluginManager
358: .getExtensions(extensionPoint);
359:
360: for (Extension extension : list) {
361: Class<Concrete> concreteType = (Class<Concrete>) extension
362: .getImplementation();
363: if (concrete.equals(concreteType.getSimpleName())) {
364: return concreteType;
365: }
366: }
367:
368: return null;
369: }
370: }
|