Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 14ab431690ffb22460498fe66a25607eba7ac99e (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
380
/*
 * Copyright (c) 2013, 2015, 2016 Eike Stepper (Loehne, 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.internal.ui.bundle.OM;
import org.eclipse.emf.cdo.security.internal.ui.messages.Messages;
import org.eclipse.emf.cdo.security.internal.ui.util.ObjectExistsConverter.ObjectWritableConverter;
import org.eclipse.emf.cdo.security.internal.ui.util.OneToManyBlock;
import org.eclipse.emf.cdo.security.internal.ui.util.OneToManyTableBlock;
import org.eclipse.emf.cdo.security.provider.SecurityEditPlugin;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.databinding.edit.EMFEditObservables;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;

import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IFilter;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IDetailsPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.TableWrapData;

/**
 * Common framework for the right-hand side object details pages of the
 * master/detail form.
 *
 * @author Christian W. Damus (CEA LIST)
 */
public abstract class AbstractDetailsPage<T extends EObject> extends AbstractSectionPart<T> implements IDetailsPage
{
  public AbstractDetailsPage(Class<T> elementType, EClass elementEClass, EditingDomain domain, AdapterFactory adapterFactory)
  {
    super(elementType, elementEClass, domain, adapterFactory);
  }

  @Override
  protected String getTitle()
  {
    return NLS.bind(Messages.AbstractDetailsPage_0, SecurityEditPlugin.INSTANCE.getString(String.format("_UI_%s_type", getInputEClass().getName()))); //$NON-NLS-1$
  }

  @Override
  public void createContents(Composite parent)
  {
    GridLayout layout = new GridLayout();
    parent.setLayout(layout);

    super.createContents(parent);
  }

  @Override
  protected void createContents(Composite parent, FormToolkit toolkit)
  {
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    parent.setLayout(layout);
  }

  protected void execute(Command command)
  {
    if (command.canExecute())
    {
      getEditingDomain().getCommandStack().execute(command);
    }
  }

  @SuppressWarnings("unchecked")
  protected Text text(Composite parent, FormToolkit toolkit, String label, EAttribute attribute)
  {
    toolkit.createLabel(parent, label);
    Text result = toolkit.createText(createDecorationComposite(parent, toolkit, layoutData(parent, SWT.FILL, false, 1)), ""); //$NON-NLS-1$
    getContext().bindValue(observeText(result), EMFEditObservables.observeDetailValue(getRealm(), getEditingDomain(), getValue(), attribute));
    getContext().bindValue(WidgetProperties.enabled().observe(result), getValue(), null, ObjectWritableConverter.createUpdateValueStrategy(attribute));

    addRevertDecoration(result, attribute);
    return result;
  }

  private static GridData horzAlign(GridData gridData, int align, boolean grab, int span)
  {
    gridData.horizontalAlignment = align;
    gridData.grabExcessHorizontalSpace = grab;
    gridData.horizontalSpan = span;
    return gridData;
  }

  private static GridData vertAlign(GridData gridData, int align, boolean grab, int span)
  {
    gridData.verticalAlignment = align;
    gridData.grabExcessVerticalSpace = grab;
    gridData.verticalSpan = span;
    return gridData;
  }

  private static TableWrapData horzAlign(TableWrapData tableData, int align, boolean grab, int span)
  {
    int tableWrapAlign = 0;
    switch (align)
    {
    case SWT.LEFT:
      tableWrapAlign = TableWrapData.LEFT;
      break;
    case SWT.CENTER:
      tableWrapAlign = TableWrapData.CENTER;
      break;
    case SWT.RIGHT:
      tableWrapAlign = TableWrapData.RIGHT;
      break;
    case SWT.FILL:
      tableWrapAlign = TableWrapData.FILL;
      break;
    }

    tableData.align = tableWrapAlign;
    tableData.grabHorizontal = grab;
    tableData.colspan = span;
    return tableData;
  }

  private static TableWrapData vertAlign(TableWrapData tableData, int align, boolean grab, int span)
  {
    int tableWrapAlign = 0;
    switch (align)
    {
    case SWT.TOP:
      tableWrapAlign = TableWrapData.TOP;
      break;
    case SWT.CENTER:
      tableWrapAlign = TableWrapData.MIDDLE;
      break;
    case SWT.BOTTOM:
      tableWrapAlign = TableWrapData.BOTTOM;
      break;
    case SWT.FILL:
      tableWrapAlign = TableWrapData.FILL;
      break;
    }

    tableData.valign = tableWrapAlign;
    tableData.grabVertical = grab;
    tableData.rowspan = span;
    return tableData;
  }

  protected static Object layoutData(Composite parent, int horzAlign, boolean horzGrab, int horzSpan)
  {
    if (parent.getLayout() instanceof GridLayout)
    {
      return horzAlign(new GridData(), horzAlign, horzGrab, horzSpan);
    }

    return horzAlign(new TableWrapData(), horzAlign, horzGrab, horzSpan);
  }

  protected static Object layoutData(Composite parent, int horzAlign, boolean horzGrab, int horzSpan, int vertAlign, boolean vertGrab, int vertSpan)
  {
    if (parent.getLayout() instanceof GridLayout)
    {
      return vertAlign(horzAlign(new GridData(), horzAlign, horzGrab, horzSpan), vertAlign, vertGrab, vertSpan);
    }

    return vertAlign(horzAlign(new TableWrapData(), horzAlign, horzGrab, horzSpan), vertAlign, vertGrab, vertSpan);
  }

  protected static <T> T heightHint(T layoutData, int hint)
  {
    if (layoutData instanceof GridData)
    {
      ((GridData)layoutData).heightHint = hint;
    }
    else
    {
      ((TableWrapData)layoutData).heightHint = hint;
    }

    return layoutData;
  }

  protected Control space(Composite parent, FormToolkit toolkit)
  {
    Control result = toolkit.createComposite(parent);
    result.setLayoutData(heightHint(layoutData(parent, SWT.FILL, false, 2), 15));
    return result;
  }

  protected OneToManyBlock oneToMany(Composite parent, FormToolkit toolkit, String label, EReference reference)
  {
    return oneToMany(parent, toolkit, label, reference, reference.getEReferenceType());
  }

  protected OneToManyBlock oneToMany(Composite parent, FormToolkit toolkit, String label, EReference reference, EClass itemType)
  {
    return oneToMany(parent, toolkit, label, reference, itemType, null);
  }

  protected OneToManyBlock oneToMany(Composite parent, FormToolkit toolkit, String label, EReference reference, EClass itemType, IFilter itemFilter)
  {
    toolkit.createLabel(parent, label).setLayoutData(layoutData(parent, SWT.LEFT, false, 2));
    Composite blockParent = toolkit.createComposite(parent);
    blockParent.setLayoutData(layoutData(parent, SWT.FILL, true, 2, SWT.FILL, true, 1));

    OneToManyBlock result = new OneToManyBlock(getContext(), getEditingDomain(), getAdapterFactory(),
        new OneToManyBlock.OneToManyConfiguration(getManagedForm(), reference, itemType, itemFilter));

    result.setEditorActionBars(getEditorActionBars());
    result.setInput(getValue());
    result.createControl(blockParent, toolkit);
    return result;
  }

  protected OneToManyTableBlock table(Composite parent, FormToolkit toolkit, String label, OneToManyTableBlock.ITableConfiguration config)
  {
    toolkit.createLabel(parent, label).setLayoutData(layoutData(parent, SWT.LEFT, false, 2));
    Composite blockParent = toolkit.createComposite(parent);
    blockParent.setLayoutData(layoutData(parent, SWT.FILL, true, 2, SWT.FILL, true, 1));

    OneToManyTableBlock result = new OneToManyTableBlock(getContext(), getEditingDomain(), getAdapterFactory(), config);

    result.setEditorActionBars(getEditorActionBars());
    result.setInput(getValue());
    result.createControl(blockParent, toolkit);
    return result;
  }

  @SuppressWarnings("unchecked")
  protected Button checkbox(Composite parent, FormToolkit toolkit, String label, EAttribute attribute)
  {
    Button result = toolkit.createButton(createDecorationComposite(parent, toolkit, layoutData(parent, SWT.LEFT, false, 2)), label, SWT.CHECK);
    getContext().bindValue(WidgetProperties.enabled().observe(result),
        EMFEditObservables.observeDetailValue(getRealm(), getEditingDomain(), getValue(), attribute));
    getContext().bindValue(WidgetProperties.enabled().observe(result), getValue(), null, ObjectWritableConverter.createUpdateValueStrategy(attribute));

    addRevertDecoration(result, attribute);
    return result;
  }

  @SuppressWarnings("unchecked")
  protected Button button(Composite parent, FormToolkit toolkit, String label, SelectionListener selectionListener)
  {
    Button result = toolkit.createButton(parent, label, SWT.PUSH);
    result.setLayoutData(layoutData(parent, SWT.LEFT, false, 2));

    getContext().bindValue(WidgetProperties.enabled().observe(result), getValue(), null, ObjectWritableConverter.createUpdateValueStrategy());

    result.addSelectionListener(selectionListener);
    return result;
  }

  @SuppressWarnings("unchecked")
  protected ComboViewer combo(Composite parent, FormToolkit toolkit, String label, EAttribute attribute)
  {
    toolkit.createLabel(parent, label);
    ComboViewer result = new ComboViewer(createDecorationComposite(parent, toolkit, layoutData(parent, SWT.LEFT, false, 1)), SWT.READ_ONLY | SWT.DROP_DOWN);
    result.setLabelProvider(new AdapterFactoryLabelProvider(getAdapterFactory()));
    result.setContentProvider(new ArrayContentProvider());
    result.setInput(attribute.getEAttributeType().getInstanceClass().getEnumConstants());

    getContext().bindValue(ViewersObservables.observeSingleSelection(result),
        EMFEditObservables.observeDetailValue(getRealm(), getEditingDomain(), getValue(), attribute));
    getContext().bindValue(WidgetProperties.enabled().observe(result.getControl()), getValue(), null,
        ObjectWritableConverter.createUpdateValueStrategy(attribute));

    addRevertDecoration(result.getControl(), attribute);
    return result;
  }

  protected Composite createDecorationComposite(Composite parent, FormToolkit toolkit, Object layoutData)
  {
    Composite result = toolkit.createComposite(parent);
    result.setLayoutData(layoutData);

    GridLayout layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    layout.marginRight = 18; // 16 for image and 2 for offset
    result.setLayout(layout);

    return result;
  }

  protected void addRevertDecoration(final Control control, final EStructuralFeature feature)
  {
    final ControlDecoration decoration = new ControlDecoration(control, SWT.RIGHT | SWT.CENTER);
    decoration.hide();
    decoration.setDescriptionText(Messages.AbstractDetailsPage_3);
    decoration.setImage(ExtendedImageRegistry.getInstance().getImage(URI.createPlatformPluginURI(OM.BUNDLE_ID + "/icons/full/elcl16/revert.gif", true))); //$NON-NLS-1$
    decoration.setMarginWidth(2);

    decoration.addSelectionListener(new SelectionAdapter()
    {
      @Override
      public void widgetSelected(SelectionEvent e)
      {
        Command command = SetCommand.create(getEditingDomain(), getInput(), feature, SetCommand.UNSET_VALUE);
        if (command.canExecute())
        {
          getEditingDomain().getCommandStack().execute(command);
        }
      }
    });

    control.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

    // Show the decoration when the user hovers over the control and auto-hide after leaving the control
    MouseTrackListener showHideListener = new MouseTrackAdapter()
    {
      private Runnable hideRunnable;

      @Override
      public void mouseHover(MouseEvent e)
      {
        hideRunnable = null;
        if (control.isEnabled())
        {
          decoration.show();
        }
      }

      @Override
      public void mouseExit(MouseEvent e)
      {
        if (control.isEnabled())
        {
          hideRunnable = new Runnable()
          {

            public void run()
            {
              if (hideRunnable == this)
              {
                hideRunnable = null;
                decoration.hide();
              }
            }
          };

          control.getDisplay().timerExec(1000, hideRunnable);
        }
      }
    };

    control.addMouseTrackListener(showHideListener);
    control.getParent().addMouseTrackListener(showHideListener);
  }
}

Back to the top