Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 92bf6ef08c182476eba764a404ce9df21df602fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * Copyright (c) 2013, 2015, 2016 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Christian W. Damus (CEA LIST) - initial API and implementation
 */
package org.eclipse.emf.cdo.security.internal.ui.editor;

import org.eclipse.emf.cdo.security.Directory;
import org.eclipse.emf.cdo.security.Realm;
import org.eclipse.emf.cdo.security.SecurityFactory;
import org.eclipse.emf.cdo.security.SecurityPackage;
import org.eclipse.emf.cdo.security.internal.ui.messages.Messages;
import org.eclipse.emf.cdo.security.internal.ui.util.ActionBarsHelper;
import org.eclipse.emf.cdo.security.internal.ui.util.ObjectExistsConverter.ObjectWritableConverter;
import org.eclipse.emf.cdo.security.internal.ui.util.SecurityUIUtil;
import org.eclipse.emf.cdo.security.internal.ui.util.TableLabelProvider;
import org.eclipse.emf.cdo.ui.shared.SharedIcons;

import org.eclipse.net4j.util.ui.UIUtil;
import org.eclipse.net4j.util.ui.actions.SelectionListenerAction;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.command.CommandActionDelegate;
import org.eclipse.emf.edit.command.CommandParameter;
import org.eclipse.emf.edit.command.CreateChildCommand;
import org.eclipse.emf.edit.command.DeleteCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;

import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.ViewerDropAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import java.util.Collections;

/**
 * Common framework for section parts of a form that present a list of
 * similar objects.  It presents buttons for adding and deleting
 * objects in the list.  The objects presented are the contents
 * (of the appropriate type) of some {@link Directory} in the security
 * realm model.
 *
 * @author Christian W. Damus (CEA LIST)
 */
public abstract class TableSection<T extends EObject> extends AbstractSectionPart<Directory>
{
  private final Class<T> elementType;

  private final EClass elementEClass;

  private TableViewer viewer;

  public TableSection(Class<T> elementType, EClass elementEClass, EditingDomain domain, AdapterFactory adapterFactory)
  {
    super(Directory.class, SecurityPackage.Literals.DIRECTORY, domain, adapterFactory);
    this.elementType = elementType;
    this.elementEClass = elementEClass;
  }

  @Override
  protected void createContents(Composite parent, FormToolkit toolkit)
  {
    parent.setLayout(new GridLayout());
    Table table = toolkit.createTable(parent, SWT.H_SCROLL | SWT.V_SCROLL);
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    viewer = new TableViewer(table);

    viewer.setContentProvider(new AdapterFactoryContentProvider(getAdapterFactory()));
    viewer.setLabelProvider(new TableLabelProvider(getAdapterFactory()));
    addFilters(viewer);
    forwardSelection(viewer);

    getContext().bindValue(ViewersObservables.observeInput(viewer), getValue());

    configureDragSupport(viewer);
    configureDropSupport(viewer);
  }

  @Override
  public void setFocus()
  {
    if (viewer != null)
    {
      viewer.getControl().setFocus();
    }
    else
    {
      super.setFocus();
    }
  }

  protected void addFilters(TableViewer viewer)
  {
    SecurityUIUtil.applyTypeFilter(viewer, elementEClass);
    SecurityUIUtil.applySupportedElementFilter(viewer, elementEClass);
  }

  @Override
  public boolean setFormInput(Object input)
  {
    if (elementType.isInstance(input))
    {
      viewer.setSelection(new StructuredSelection(input), true);
      return true;
    }
    else if (input instanceof Directory && input == getDirectory(((Directory)input).getRealm()))
    {
      // It's my directory
      boolean result = super.setFormInput(input);
      checkForUnsupportedModelContent();
      return result;
    }
    else if (input instanceof Realm)
    {
      return setFormInput(getDirectory((Realm)input));
    }

    return false;
  }

  protected Directory getDirectory(Realm realm)
  {
    return SecurityUIUtil.getDirectory(realm, elementEClass);
  }

  @Override
  protected void createActionToolbar(Section section, FormToolkit toolkit)
  {
    ToolBarManager mgr = new ToolBarManager(SWT.FLAT);
    ToolBar toolbar = mgr.createControl(section);
    toolbar.setCursor(section.getDisplay().getSystemCursor(SWT.CURSOR_HAND));

    mgr.add(createAddNewAction());

    IAction deleteAction = createDeleteAction();
    mgr.add(deleteAction);
    if (deleteAction instanceof ISelectionChangedListener)
    {
      ISelectionChangedListener scl = (ISelectionChangedListener)deleteAction;
      viewer.addSelectionChangedListener(scl);
      scl.selectionChanged(new SelectionChangedEvent(viewer, viewer.getSelection()));
    }

    mgr.update(true);
    section.setTextClient(toolbar);

    ActionBarsHelper actionBarsHelper = new ActionBarsHelper(getEditorActionBars());
    ActionBarsHelper globalAction = actionBarsHelper.addGlobalAction(ActionFactory.DELETE.getId(), deleteAction);
    globalAction.install(viewer);
  }

  @SuppressWarnings("unchecked")
  protected IAction createAddNewAction()
  {
    Command dummy = createCreateNewCommand();
    ImageDescriptor image = null;

    if (dummy instanceof CommandActionDelegate)
    {
      image = ExtendedImageRegistry.getInstance().getImageDescriptor(((CommandActionDelegate)dummy).getImage());
    }

    IAction result = new Action(dummy.getLabel(), image)
    {
      @Override
      public void run()
      {
        final Command command = createCreateNewCommand();
        if (command.canExecute())
        {
          getEditingDomain().getCommandStack().execute(command);
          viewer.getControl().getDisplay().asyncExec(new Runnable()
          {
            public void run()
            {
              viewer.getControl().setFocus();
              viewer.setSelection(new StructuredSelection(command.getResult().toArray()));
            }
          });
        }
      }
    };

    getContext().bindValue(PojoProperties.value("enabled").observe(getContext().getValidationRealm(), result), //$NON-NLS-1$
        getValue(), null, ObjectWritableConverter.createUpdateValueStrategy());

    return result;
  }

  protected Command createCreateNewCommand()
  {
    Object input = viewer.getInput();
    Directory parent = input instanceof Directory ? (Directory)input : SecurityFactory.eINSTANCE.createDirectory();
    Object child = EcoreUtil.create(elementEClass);
    CommandParameter param = new CommandParameter(parent, SecurityPackage.Literals.DIRECTORY__ITEMS, child);
    return CreateChildCommand.create(getEditingDomain(), parent, param, Collections.singleton(parent));
  }

  protected IAction createDeleteAction()
  {
    Command dummy = createDeleteCommand(EcoreUtil.create(elementEClass));

    return new SelectionListenerAction<EObject>(dummy.getLabel(), SharedIcons.getDescriptor("etool16/delete.gif")) //$NON-NLS-1$
    {
      @Override
      public void run()
      {
        Command delete = createDeleteCommand(getSelectedObject());
        if (delete.canExecute())
        {
          getEditingDomain().getCommandStack().execute(delete);
        }
      }

      @Override
      protected boolean updateSelection(IStructuredSelection selection)
      {
        return super.updateSelection(selection) && SecurityUIUtil.isEditable(getInput());
      }

      @Override
      protected Class<EObject> getType()
      {
        return EObject.class;
      }
    };
  }

  protected Command createDeleteCommand(EObject toDelete)
  {
    return DeleteCommand.create(getEditingDomain(), toDelete);
  }

  private void forwardSelection(StructuredViewer viewer)
  {
    viewer.addSelectionChangedListener(new ISelectionChangedListener()
    {
      public void selectionChanged(SelectionChangedEvent event)
      {
        IManagedForm form = getManagedForm();
        if (form != null)
        {
          form.fireSelectionChanged(TableSection.this, event.getSelection());
        }
      }
    });
  }

  protected void configureDragSupport(TableViewer viewer)
  {
    UIUtil.addDragSupport(viewer);
  }

  protected void configureDropSupport(final TableViewer viewer)
  {
    viewer.addDropSupport(DND.DROP_LINK | DND.DROP_MOVE | DND.DROP_COPY, new Transfer[] { LocalSelectionTransfer.getTransfer() }, new ViewerDropAdapter(viewer)
    {
      {
        // We don't want it to look like you can insert new elements, only drop onto existing elements
        setFeedbackEnabled(false);
      }

      @Override
      public boolean validateDrop(Object target, int operation, TransferData transferType)
      {
        boolean result = false;

        if (target instanceof EObject && LocalSelectionTransfer.getTransfer().isSupportedType(transferType))
        {
          EObject objectToDrop = getObjectToDrop(transferType);
          if (objectToDrop != null)
          {
            result = getDropReference((EObject)target, objectToDrop) != null;

            if (result && (getCurrentEvent().operations | DND.DROP_COPY) != 0)
            {
              overrideOperation(DND.DROP_COPY);
            }
          }
        }

        return result;
      }

      @Override
      public boolean performDrop(Object data)
      {
        IStructuredSelection selection = (IStructuredSelection)data;
        EObject objectToDrop = UIUtil.getElement(selection, EObject.class);
        EObject target = (EObject)getCurrentTarget();

        Command command = AddCommand.create(getEditingDomain(), target, getDropReference(target, objectToDrop), selection.toList());

        boolean result = execute(command);
        if (result)
        {
          viewer.getControl().setFocus();
          viewer.setSelection(new StructuredSelection(target));
        }

        return result;
      }

      private EObject getObjectToDrop(TransferData transferType)
      {
        return UIUtil.getElement(LocalSelectionTransfer.getTransfer().getSelection(), EObject.class);
      }
    });
  }

  protected EReference getDropReference(EObject target, EObject objectToDrop)
  {
    return null;
  }

  protected boolean execute(Command command)
  {
    boolean result = command.canExecute();
    if (result)
    {
      getEditingDomain().getCommandStack().execute(command);
    }

    return result;
  }

  protected void checkForUnsupportedModelContent()
  {
    if (getInput() == null)
    {
      getManagedForm().getMessageManager().addMessage(this, Messages.TableSection_2, null, IStatus.WARNING, viewer.getControl());
    }
    else
    {
      // Anything not matching filters?
      if (viewer.getTable().getItemCount() < getInput().getItems().size())
      {
        getManagedForm().getMessageManager().addMessage(this, Messages.TableSection_3, null, IStatus.WARNING, viewer.getControl());
      }
    }
  }
}

Back to the top