001: package org.osbl.authorization.gui;
002:
003: import org.conform.*;
004: import org.conform.wings.editor.*;
005: import org.conform.modifier.InlineModifier;
006: import org.osbl.client.wings.form.*;
007: import org.osbl.client.wings.form.layouter.*;
008: import org.osbl.client.wings.shell.Client;
009: import org.osbl.authorization.model.*;
010: import org.osbl.authorization.PatternPermissionCollection;
011: import org.wings.*;
012: import org.wings.event.SDocumentListener;
013: import org.wings.event.SDocumentEvent;
014:
015: import java.awt.event.ActionListener;
016: import java.awt.event.ActionEvent;
017: import java.awt.*;
018: import java.util.*;
019: import java.util.List;
020: import java.beans.PropertyChangeListener;
021: import java.beans.PropertyChangeEvent;
022:
023: public class ProfileForm extends GenericObjectForm {
024: private STextArea effectTextArea;
025: private PatternPermissionCollection patternPermissionCollection;
026:
027: protected Instruction createLayoutInstruction() {
028: return new PageSet(new Page(Profile.class.getName(),
029: new Division(Profile.class.getName(), new Column(
030: new LabelAndEditor(ComponentProvider.ROOT,
031: "key"), new LabelAndEditor(
032: ComponentProvider.ROOT, "name")),
033: new Column(new LabelAndEditors(
034: "businessobject.created", new Editor(
035: ComponentProvider.ROOT,
036: "created"), new Editor(
037: ComponentProvider.ROOT,
038: "createdBy")))), new Division(
039: Profile.class.getName() + ".patterns",
040: new Column(new LabelAndEditor(
041: ComponentProvider.ROOT, "patterns")),
042: new Column(new CustomEditor("effect")))));
043: }
044:
045: private void updateEffect(List<Pattern> patterns) {
046: System.out.println("patterns = " + patterns);
047: if (patterns == null)
048: return;
049:
050: patterns = new ArrayList<Pattern>(patterns);
051: for (Iterator<Pattern> iterator = patterns.iterator(); iterator
052: .hasNext();) {
053: Pattern pattern = iterator.next();
054: if (pattern == null || pattern.getAction() == null
055: || pattern.getPattern() == null)
056: iterator.remove();
057: }
058:
059: long millis = System.currentTimeMillis();
060: Map<String, SortedSet<String>> map = matchPermissions(patterns);
061: System.out.println("millis: "
062: + (System.currentTimeMillis() - millis));
063:
064: List<String> actions = new ArrayList<String>(map.keySet());
065: Collections.sort(actions);
066:
067: StringBuilder builder = new StringBuilder();
068: for (String action : actions) {
069: SortedSet<String> permissions = map.get(action);
070: if (permissions.size() > 0) {
071: builder.append(action);
072: builder.append(":\n");
073: for (String permission : permissions) {
074: builder.append(permission);
075: builder.append("\n");
076: }
077: builder.append("\n");
078: }
079: }
080: effectTextArea.setText(builder.toString());
081: }
082:
083: private Map<String, SortedSet<String>> matchPermissions(
084: List<Pattern> patterns) {
085: if (patternPermissionCollection == null)
086: patternPermissionCollection = new PatternPermissionCollection();
087:
088: patternPermissionCollection.clearPermissions();
089:
090: for (Iterator<Pattern> iterator = patterns.iterator(); iterator
091: .hasNext();) {
092: Pattern pattern = iterator.next();
093: if (pattern.getType() == PatternType.INCLUDE)
094: patternPermissionCollection.include(pattern
095: .getPattern(), pattern.getAction());
096: else
097: patternPermissionCollection.exclude(pattern
098: .getPattern(), pattern.getAction());
099: }
100: return patternPermissionCollection.matchPermissions();
101: }
102:
103: protected SComponent createCustomComponent(String key) {
104: effectTextArea = new STextArea();
105: effectTextArea.setEditable(false);
106: effectTextArea.setPreferredSize(SDimension.FULLWIDTH);
107: effectTextArea.setRows(20);
108: return effectTextArea;
109: }
110:
111: protected Object getObject(String path) {
112: return object;
113: }
114:
115: protected void setObject(String path, Object object) {
116: this .object = object;
117: }
118:
119: protected BeanMeta createBeanMeta(String path) {
120: BeanMetaProvider metaProvider = Client.getInstance()
121: .getBeanMetaProvider();
122:
123: if (ComponentProvider.ROOT == path) {
124: VariationBeanMetaProvider provider = new VariationBeanMetaProvider(
125: metaProvider);
126: provider.addModifier(new InlineModifier() {
127: protected void configure() {
128: property("key").setWritable(true);
129: property("key").setWritable(true);
130: property("patterns").setAttribute(
131: org.conform.wings.Editor.CUSTOM_EDITOR,
132: new PatternsEditor());
133: property("patterns")
134: .setInitializer(
135: new NewInstanceInitializer(
136: ArrayList.class));
137: property("patterns").addValidator(new Validator() {
138: public Object validate(Object value)
139: throws ValidationException {
140: List<Pattern> patterns = (List<Pattern>) value;
141: if (patterns == null)
142: return null;
143:
144: for (Iterator<Pattern> iterator = patterns
145: .iterator(); iterator.hasNext();) {
146: Pattern pattern = iterator.next();
147: if (pattern.getPattern() == null)
148: iterator.remove();
149: }
150: return patterns;
151: }
152: });
153: property("patterns").setAttribute(
154: PropertyMeta.ATTRIBUTE_CHANGE_LISTENER,
155: new PropertyChangeListener() {
156: public void propertyChange(
157: PropertyChangeEvent evt) {
158: updateEffect((List<Pattern>) evt
159: .getNewValue());
160: }
161: });
162: }
163: });
164: return provider.getBeanMeta(Profile.class);
165: } else if ("patterns".equals(path)) {
166: VariationBeanMetaProvider provider = new VariationBeanMetaProvider(
167: metaProvider);
168: provider.addModifier(new InlineModifier() {
169: protected void configure() {
170: }
171: });
172: return provider.getBeanMeta(Pattern.class);
173: }
174: throw new RuntimeException("No BeanMeta for path " + path);
175: }
176:
177: class PatternsEditor implements org.conform.wings.Editor {
178: public SComponent createComponent(PropertyMeta propertyMeta) {
179: PatternsPanel panel = new PatternsPanel(propertyMeta);
180: configureComponent(propertyMeta, panel, false);
181: return panel;
182: }
183:
184: public void configureComponent(PropertyMeta propertyMeta,
185: SComponent component, boolean erroneous) {
186: component.setVisible(propertyMeta.isReadable());
187: component.setEnabled(propertyMeta.isWritable());
188: DefaultEditorStyles.applyEditorAlignment(propertyMeta,
189: component);
190: }
191:
192: public PropertyData getPropertyData(SComponent component) {
193: return ((PatternsPanel) component).getPropertyData();
194: }
195:
196: public void setPropertyData(final SComponent component,
197: final PropertyData propertyData) {
198: BeanData beanData = propertyData.getBeanData();
199: if (beanData != null)
200: beanData.addPropertyChangeListener(propertyData
201: .getPropertyMeta().getName(),
202: new PropertyChangeListener() {
203: public void propertyChange(
204: PropertyChangeEvent evt) {
205: ((PatternsPanel) component)
206: .setPropertyData(propertyData);
207: }
208: });
209: ((PatternsPanel) component).setPropertyData(propertyData);
210: }
211: }
212:
213: class PatternsPanel extends SPanel {
214: private PropertyMeta propertyMeta;
215: private PropertyData propertyData;
216: private List<PatternPanel> panels = new ArrayList<PatternPanel>();
217: private boolean initializing;
218:
219: public PatternsPanel(PropertyMeta propertyMeta) {
220: SGridLayout gridLayout = new SGridLayout(1);
221: gridLayout.setVgap(6);
222: setLayout(gridLayout);
223: setPreferredSize(SDimension.FULLWIDTH);
224: this .propertyMeta = propertyMeta;
225: }
226:
227: public void setPropertyData(PropertyData propertyData) {
228: this .propertyData = propertyData;
229: if (propertyData == null)
230: return;
231:
232: int i = 0;
233: List<Pattern> patterns = (List<Pattern>) propertyData
234: .getValue();
235: if (patterns == null)
236: return;
237:
238: if (initializing)
239: return;
240:
241: initializing = true;
242: for (Pattern pattern : patterns) {
243: PatternPanel panel;
244: if (i == panels.size()) {
245: panel = new PatternPanel(new PanelListener(i));
246: panels.add(panel);
247: add(panel);
248: } else
249: panel = panels.get(i);
250:
251: panel.setPattern(pattern);
252: i++;
253: }
254:
255: int num = patterns.size() + 1;
256: if (panels.size() < num) {
257: PatternPanel panel = new PatternPanel(
258: new PanelListener(i));
259: panels.add(panel);
260: add(panel);
261: } else
262: panels.get(patterns.size()).setPattern(new Pattern());
263:
264: while (panels.size() > num) {
265: PatternPanel panel = panels.remove(panels.size() - 1);
266: remove(panel);
267: }
268: initializing = false;
269: }
270:
271: public PropertyData getPropertyData() {
272: return propertyData;
273: }
274:
275: class PanelListener implements ActionListener {
276: int i;
277:
278: public PanelListener(int i) {
279: this .i = i;
280: }
281:
282: public void actionPerformed(ActionEvent e) {
283: if (initializing)
284: return;
285:
286: List<Pattern> patterns = (List<Pattern>) propertyData
287: .getValue();
288: PatternPanel panel = panels.get(i);
289: Pattern pattern = panel.getPattern();
290:
291: if (i == panels.size() - 1) {
292: if (pattern.getPattern() != null
293: && pattern.getPattern().length() != 0) {
294: patterns.add(pattern);
295: panel = new PatternPanel(new PanelListener(
296: i + 1));
297: panels.add(panel);
298: add(panel);
299: }
300: }
301: if (!patterns.contains(pattern))
302: patterns.add(i, pattern);
303:
304: propertyData.setValue(new ArrayList<Pattern>(patterns));
305: updateEffect(patterns);
306: }
307: }
308: }
309:
310: class PatternPanel extends SPanel {
311: private SComboBox typeCombo = new SComboBox(new PatternType[] {
312: PatternType.INCLUDE, PatternType.EXCLUDE });
313: private SComboBox actionCombo = new SComboBox(new String[] {
314: "r", "w", "x" });
315: private STextField patternTextField = new STextField();
316:
317: public PatternPanel(final ActionListener listener) {
318: super (new SGridBagLayout());
319: setPreferredSize(SDimension.FULLWIDTH);
320:
321: GridBagConstraints constraints = new GridBagConstraints();
322: constraints.weightx = .1;
323: add(typeCombo, constraints);
324: add(actionCombo, constraints);
325: constraints.weightx = .8;
326: add(patternTextField, constraints);
327:
328: typeCombo.setRenderer(new FormattedListCellRenderer(
329: new EnumDomainProvider(PatternType.class)));
330: patternTextField.setPreferredSize(SDimension.FULLWIDTH);
331:
332: typeCombo.addActionListener(listener);
333: actionCombo.addActionListener(listener);
334: patternTextField
335: .addDocumentListener(new SDocumentListener() {
336: private final ActionEvent actionEvent = new ActionEvent(
337: PatternPanel.this , 0, null);
338:
339: public void insertUpdate(SDocumentEvent e) {
340: listener.actionPerformed(actionEvent);
341: }
342:
343: public void removeUpdate(SDocumentEvent e) {
344: listener.actionPerformed(actionEvent);
345: }
346:
347: public void changedUpdate(SDocumentEvent e) {
348: listener.actionPerformed(actionEvent);
349: }
350: });
351: }
352:
353: private Pattern pattern;
354:
355: public void setPattern(Pattern pattern) {
356: this .pattern = pattern;
357: typeCombo.setSelectedItem(pattern.getType());
358: actionCombo.setSelectedItem(pattern.getAction());
359: patternTextField.setText(pattern.getPattern());
360: }
361:
362: public Pattern getPattern() {
363: if (pattern == null)
364: pattern = new Pattern();
365:
366: this .pattern.setType((PatternType) typeCombo
367: .getSelectedItem());
368: this .pattern.setAction((String) actionCombo
369: .getSelectedItem());
370: String s = patternTextField.getText();
371: this .pattern.setPattern("".equals(s) ? null : s);
372:
373: return pattern;
374: }
375: }
376: }
|