001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.gui.access;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Dimension;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ItemEvent;
024: import java.awt.event.ItemListener;
025: import java.util.logging.Level;
026: import java.util.logging.Logger;
027:
028: import javax.swing.AbstractAction;
029: import javax.swing.AbstractButton;
030: import javax.swing.Action;
031: import javax.swing.JCheckBox;
032: import javax.swing.JComponent;
033: import javax.swing.JFrame;
034: import javax.swing.JOptionPane;
035: import javax.swing.JPanel;
036: import javax.swing.JScrollPane;
037: import javax.swing.ListSelectionModel;
038:
039: import com.jgoodies.forms.extras.SimpleFormBuilder;
040:
041: import de.finix.contelligent.client.base.ComponentFactory;
042: import de.finix.contelligent.client.base.ComponentNotFoundException;
043: import de.finix.contelligent.client.base.ContelligentComponent;
044: import de.finix.contelligent.client.base.ServerInfo;
045: import de.finix.contelligent.client.base.UserManager;
046: import de.finix.contelligent.client.event.ContelligentEvent;
047: import de.finix.contelligent.client.gui.AbstractComponentEditor;
048: import de.finix.contelligent.client.gui.ContelligentAction;
049: import de.finix.contelligent.client.i18n.Resources;
050: import de.finix.contelligent.client.security.ComponentAccess;
051: import de.finix.contelligent.client.security.Principal;
052: import de.finix.contelligent.client.util.ButtonComposer;
053: import de.finix.contelligent.client.util.ExceptionDialog;
054: import de.finix.contelligent.client.util.list.NameValueComboBox;
055:
056: public class AccessEditor extends AbstractComponentEditor {
057:
058: private static Logger logger = Logger.getLogger(AccessEditor.class
059: .getName());
060:
061: private OwnerTableModel ownerTableModel;
062:
063: private OwnerTable ownerTable;
064:
065: private AccessTableModel accessTableModel;
066:
067: private AccessTable accessTable;
068:
069: private JCheckBox secureCheckBox = null;
070:
071: boolean enableSecureCheckBox = true;
072:
073: private boolean executeModifiable = true;
074:
075: private boolean loadParent = true;
076:
077: private DeleteOwnerAction deleteOwnerAction = new DeleteOwnerAction();
078:
079: private DeleteAccessAction deleteAccessAction = new DeleteAccessAction();
080:
081: private AddOwnerAction addOwnerAction = new AddOwnerAction();
082:
083: private AddAccessAction addAccessAction = new AddAccessAction();
084:
085: private NameValueComboBox groupComboBox;
086:
087: private NameValueComboBox principalComboBox;
088:
089: private NameValueComboBox userGroupComboBox;
090:
091: private NameValueComboBox userComboBox;
092:
093: private boolean protocolVisible = false;
094:
095: public AccessEditor(boolean executeModifiable, boolean loadParent) {
096: super ();
097: this .executeModifiable = executeModifiable;
098: this .loadParent = loadParent;
099: protocolVisible = ServerInfo.getInstance()
100: .supportSecureComponents();
101: }
102:
103: public AccessEditor(boolean executeModifiable) {
104: this (executeModifiable, true);
105: }
106:
107: public void init() {
108: principalComboBox = new NameValueComboBox();
109: principalComboBox.setEnabled(editable);
110: groupComboBox = new NameValueComboBox(UserManager.getInstance()
111: .getGroupsDescriptions());
112: groupComboBox.setEnabled(editable);
113: groupComboBox.addItemListener(new ItemListener() {
114: public void itemStateChanged(ItemEvent e) {
115: String group = (String) groupComboBox
116: .getSelectedItemValue();
117: if (group != null) {
118: principalComboBox.setModel(UserManager
119: .getInstance().getPrincipalsDescriptions(
120: group, false, true));
121: if (principalComboBox.getItemCount() > 0) {
122: principalComboBox.setSelectedIndex(0);
123: }
124: }
125: }
126: });
127: if (groupComboBox.getItemCount() > 0) {
128: groupComboBox.setSelectedIndex(0);
129: }
130: userComboBox = new NameValueComboBox();
131: userComboBox.setEnabled(editable);
132: userGroupComboBox = new NameValueComboBox(UserManager
133: .getInstance().getGroupsDescriptions());
134: userGroupComboBox.setEnabled(editable);
135: userGroupComboBox.addItemListener(new ItemListener() {
136: public void itemStateChanged(ItemEvent e) {
137: String group = (String) userGroupComboBox
138: .getSelectedItemValue();
139: if (group != null) {
140: userComboBox.setModel(UserManager.getInstance()
141: .getPrincipalsDescriptions(group, false,
142: true));
143: if (userComboBox.getItemCount() > 0) {
144: userComboBox.setSelectedIndex(0);
145: }
146: }
147: }
148: });
149: if (userGroupComboBox.getItemCount() > 0) {
150: userGroupComboBox.setSelectedIndex(0);
151: }
152: secureCheckBox = new JCheckBox(Resources
153: .getLocalString("secure_transfer_req"));
154: secureCheckBox.setEnabled(false);
155: secureCheckBox.setOpaque(false);
156: update();
157: accessTable
158: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
159: accessTable.getColumnModel().getColumn(0)
160: .setPreferredWidth(150);
161: accessTable.getColumnModel().getColumn(1).setPreferredWidth(90);
162: accessTable.getColumnModel().getColumn(1).setMaxWidth(90);
163: accessTable.getColumnModel().getColumn(2).setPreferredWidth(60);
164: accessTable.getColumnModel().getColumn(2).setMaxWidth(60);
165: accessTable.getColumnModel().getColumn(3)
166: .setPreferredWidth(100);
167: accessTable.getColumnModel().getColumn(3).setMaxWidth(100);
168: AbstractButton addOwner = ButtonComposer.createButton(
169: addOwnerAction, false, null, true, true);
170: AbstractButton deleteOwner = ButtonComposer.createButton(
171: deleteOwnerAction, false, null, true, true);
172: JScrollPane ownerScrollPane = new JScrollPane(ownerTable);
173: ownerScrollPane.getViewport().setOpaque(false);
174: ownerScrollPane.setOpaque(false);
175: AbstractButton addAccess = ButtonComposer.createButton(
176: addAccessAction, false, null, true, true);
177: AbstractButton deleteAccess = ButtonComposer.createButton(
178: deleteAccessAction, false, null, true, true);
179: JScrollPane accessScrollPane = new JScrollPane(accessTable);
180: accessScrollPane.getViewport().setOpaque(false);
181: accessScrollPane.setOpaque(false);
182: int size = (int) principalComboBox.getPreferredSize()
183: .getHeight();
184: Dimension minimumSize = new Dimension(22, 22);
185: Dimension preferredSize = new Dimension(size, size);
186: Dimension maximumSize = new Dimension(28, 28);
187: addOwner.setMinimumSize(minimumSize);
188: addOwner.setPreferredSize(preferredSize);
189: addOwner.setMaximumSize(maximumSize);
190: deleteOwner.setMinimumSize(minimumSize);
191: deleteOwner.setPreferredSize(preferredSize);
192: deleteOwner.setMaximumSize(maximumSize);
193: addAccess.setMinimumSize(minimumSize);
194: addAccess.setPreferredSize(preferredSize);
195: addAccess.setMaximumSize(maximumSize);
196: deleteAccess.setMinimumSize(minimumSize);
197: deleteAccess.setPreferredSize(preferredSize);
198: deleteAccess.setMaximumSize(maximumSize);
199:
200: SimpleFormBuilder builder = new SimpleFormBuilder(
201: "80dlu,related,80dlu,glue,addButton=p,related,deleteButton=p",
202: "p,r,c:m,r,owners=min(30dlu;p):g(0.2),u,p,r,c:m,r,permissions=min(30dlu;p):g(0.8)");
203: builder.setDefaultDialogBorder();
204: JComponent sep = builder.addSeparator(Resources
205: .getLocalString("owner"), "1,1,7,1");
206: sep.setOpaque(false);
207: builder.add(userGroupComboBox, "1,3");
208: builder.add(userComboBox, "3,3");
209: builder.add(addOwner, "addButton,3");
210: builder.add(deleteOwner, "deleteButton,3");
211: builder.add(ownerScrollPane, "1, owners, 7, 1, fill, fill");
212: sep = builder.addSeparator(Resources.getLocalString("access"),
213: "1,7,7,1");
214: sep.setOpaque(false);
215: builder.add(groupComboBox, "1,9");
216: builder.add(principalComboBox, "3,9");
217: builder.add(addAccess, "5,9");
218: builder.add(deleteAccess, "7,9");
219: builder
220: .add(accessScrollPane,
221: "1,permissions, 7, 1, fill, fill");
222: if (protocolVisible) {
223: builder.setRow(11);
224: builder.nextLine();
225: sep = builder.appendSeparator(Resources
226: .getLocalString("protocol"));
227: sep.setOpaque(false);
228: builder.nextLine();
229: builder.append(secureCheckBox, 7);
230: }
231: JPanel form = builder.getPanel();
232: form.setOpaque(false);
233: add(form, BorderLayout.CENTER);
234:
235: secureCheckBox.addItemListener(new ItemListener() {
236: public void itemStateChanged(ItemEvent e) {
237: if (AccessEditor.this .enableSecureCheckBox) {
238: ContelligentComponent comp = AccessEditor.this
239: .getComponent();
240: if (e.getStateChange() == ItemEvent.DESELECTED) {
241: comp.setDefinesSecureTransfer(false);
242: comp.setAclModified(true);
243: } else if (e.getStateChange() == ItemEvent.SELECTED) {
244: comp.setDefinesSecureTransfer(true);
245: comp.setAclModified(true);
246: }
247: }
248: }
249: });
250: }
251:
252: // decide wheter execute permission can be assigned (must not be changed for
253: // Context)
254: public void setExecuteModifiable(boolean executeModifiable) {
255: this .executeModifiable = executeModifiable;
256: }
257:
258: public void update() {
259: if (ownerTableModel == null) {
260: ownerTableModel = new OwnerTableModel(getComponent());
261: ownerTable = new OwnerTable(ownerTableModel);
262: ownerTable.setEditable(isEditable());
263: accessTableModel = new AccessTableModel(getComponent());
264: accessTableModel.setEditable(isEditable());
265: accessTable = new AccessTable(accessTableModel);
266: accessTable.setEditable(isEditable());
267: } else {
268: ownerTableModel.updateData();
269: ownerTableModel.fireTableDataChanged();
270: accessTableModel.updateData();
271: accessTableModel.fireTableDataChanged();
272: }
273:
274: if (loadParent) {
275: secureCheckBox.setSelected(getComponent()
276: .getDefinesSecureTransfer());
277: ContelligentComponent parentComponent = getComponent()
278: .getParentComponent();
279: // This is null on the root component and in the context ACL editor
280: if (parentComponent != null) {
281: enableSecureCheckBox = !(parentComponent
282: .getNeedsSecureTransfer());
283: }
284: if (enableSecureCheckBox) {
285: secureCheckBox.setText(Resources
286: .getLocalString("secure_transfer_req"));
287: } else {
288: secureCheckBox.setText(Resources
289: .getLocalString("secure_transfer_req_parent"));
290: secureCheckBox.setSelected(true);
291: }
292: }
293: }
294:
295: public void setProtocolVisible(boolean protocolVisible) {
296: this .protocolVisible = protocolVisible;
297: }
298:
299: protected void updateComponent() {
300: }
301:
302: protected void componentChanged(ContelligentEvent event) {
303: update();
304: }
305:
306: protected void childComponentAdded(ContelligentEvent event) {
307: }
308:
309: protected void childComponentRemoved(ContelligentEvent event) {
310: }
311:
312: protected void childComponentChanged(ContelligentEvent event) {
313: }
314:
315: protected void descendentComponentChanged(ContelligentEvent event) {
316: }
317:
318: private final class AddOwnerAction extends AbstractAction implements
319: ContelligentAction {
320:
321: public AddOwnerAction() {
322: super ("add", Resources.genericAddIcon);
323: putValue(ROLLOVER_ICON, Resources.genericAddIconRollOver);
324: putValue(TYPE, PUSH_ACTION);
325: putValue(ACTION_TYPE, EDIT_ACTION);
326: putValue(MENU_TARGET, MENU);
327: putValue(BUTTON_TARGET, TOOLBAR);
328: }
329:
330: public void actionPerformed(ActionEvent e) {
331: Object principal = userComboBox.getSelectedItemValue();
332: if (principal != null) {
333: String principalGroupId;
334: String principalId;
335: if (principal instanceof UserManager.User) {
336: UserManager.User user = (UserManager.User) principal;
337: principalGroupId = user.getGroupId();
338: principalId = user.getId();
339: } else {
340: UserManager.Role role = (UserManager.Role) principal;
341: principalGroupId = role.getGroupId();
342: principalId = role.getId();
343: }
344: ownerTableModel.addOwner(new Principal(
345: principalGroupId, principalId));
346: }
347: }
348: }
349:
350: private final class DeleteOwnerAction extends AbstractAction
351: implements ContelligentAction {
352:
353: public DeleteOwnerAction() {
354: super ("delete", Resources.genericDeleteIcon);
355: putValue(ROLLOVER_ICON, Resources.genericDeleteIconRollOver);
356: putValue(TYPE, PUSH_ACTION);
357: putValue(ACTION_TYPE, EDIT_ACTION);
358: putValue(MENU_TARGET, MENU);
359: putValue(BUTTON_TARGET, TOOLBAR);
360: }
361:
362: public void actionPerformed(ActionEvent e) {
363: // delete owner
364: if (ownerTableModel.hasOwner()
365: && ownerTable.getSelectedRow() != -1) {
366: Principal principal = (Principal) ownerTableModel
367: .getValueAt(ownerTable.getSelectedRow(), 0);
368: ownerTableModel.removeOwner(principal);
369: ownerTableModel.fireTableDataChanged();
370: }
371: }
372: }
373:
374: private final class AddAccessAction extends AbstractAction
375: implements ContelligentAction {
376: public AddAccessAction() {
377: super ("add", Resources.genericAddIcon);
378: putValue(ROLLOVER_ICON, Resources.genericAddIconRollOver);
379: putValue(TYPE, PUSH_ACTION);
380: putValue(ACTION_TYPE, EDIT_ACTION);
381: putValue(MENU_TARGET, MENU);
382: putValue(BUTTON_TARGET, TOOLBAR);
383: }
384:
385: public void actionPerformed(ActionEvent e) {
386: PermissionOptionPane permissionOptionPane = new PermissionOptionPane(
387: executeModifiable);
388: if (permissionOptionPane.showPermissionDialog(Resources
389: .getLocalString("choose_permission"), Resources
390: .getLocalString("permission_to_grant")) == PermissionOptionPane.OK_OPTION) {
391: logger.log(Level.FINE, "Permissionchooser OK");
392: String permission = permissionOptionPane
393: .getPermission();
394: UserManager.Principal principal = (UserManager.Principal) principalComboBox
395: .getSelectedItemValue();
396: if (principal != null) {
397: String principalGroupId = principal.getGroupId();
398: String principalId = principal.getId();
399: accessTableModel
400: .addAclEntry(new ComponentAccess(
401: new Principal(principalGroupId,
402: principalId), permission,
403: ComponentAccess.ALLOW,
404: ComponentAccess.UNSET, -1, -1, -1,
405: -1));
406: }
407: }
408: }
409: }
410:
411: private final class DeleteAccessAction extends AbstractAction
412: implements ContelligentAction {
413: public DeleteAccessAction() {
414: super ("delete", Resources.genericDeleteIcon);
415: putValue(ROLLOVER_ICON, Resources.genericDeleteIconRollOver);
416: putValue(TYPE, PUSH_ACTION);
417: putValue(ACTION_TYPE, EDIT_ACTION);
418: putValue(MENU_TARGET, MENU);
419: putValue(BUTTON_TARGET, TOOLBAR);
420: }
421:
422: public void actionPerformed(ActionEvent e) {
423: // delete aclEntry
424: if (accessTable.getSelectedRow() != -1) {
425: ComponentAccess aclEntry = (ComponentAccess) accessTableModel
426: .getValueAt(accessTable.getSelectedRow(), 0);
427:
428: // check for inherited aclEntry
429: if (aclEntry.isInherited()) {
430: // this is a inherited value and can not be deleted
431: JOptionPane
432: .showMessageDialog(
433: new JFrame(),
434: Resources
435: .getLocalString("inherited_permisson_no_delete"));
436: } else if ((aclEntry.getParentMode() == null)
437: || (ComponentAccess.UNSET.equals(aclEntry
438: .getParentMode()))) {
439: accessTableModel.removeAclEntry(aclEntry);
440: accessTableModel.fireTableDataChanged();
441: } else {
442: // reset this entry to the inherited value
443: aclEntry.setMode(ComponentAccess.UNSET);
444: getComponent().setAclModified(true);
445: accessTableModel.fireTableDataChanged();
446: }
447: }
448: }
449: }
450:
451: public void setEditable(boolean editable) {
452: super .setEditable(editable);
453: if (ownerTableModel != null) {
454: ownerTable.setEditable(editable);
455: accessTableModel.setEditable(editable);
456: accessTable.setEditable(editable);
457: if (editable) {
458: secureCheckBox.setEnabled(enableSecureCheckBox);
459: } else {
460: secureCheckBox.setEnabled(false);
461: }
462: }
463: deleteOwnerAction.setEnabled(editable);
464: deleteAccessAction.setEnabled(editable);
465: addOwnerAction.setEnabled(editable);
466: addAccessAction.setEnabled(editable);
467:
468: if (groupComboBox != null)
469: groupComboBox.setEnabled(editable);
470: if (principalComboBox != null)
471: principalComboBox.setEnabled(editable);
472: if (userGroupComboBox != null)
473: userGroupComboBox.setEnabled(editable);
474: if (userComboBox != null)
475: userComboBox.setEnabled(editable);
476: }
477:
478: public void rollback() {
479: if (getComponent().isAclModified()) {
480: try {
481: ComponentFactory.getInstance().reload(getComponent());
482: } catch (ComponentNotFoundException cnfe) {
483: ExceptionDialog.show(cnfe);
484: }
485: }
486: update();
487: }
488:
489: public void commit() {
490: setEditable(false);
491: if (getComponent().isAclModified()) {
492: logger.log(Level.FINE, "Acl modified");
493: ComponentFactory.getInstance().save(getComponent());
494:
495: // invalidate this subtree because all permissions are inherited by
496: // the subcomponents
497: ComponentFactory.getInstance().invalidateSubtreeExcluding(
498: getComponent());
499: }
500: }
501:
502: public Action[] getActions() {
503: return new Action[] {};
504: }
505: }
|