Skip to main content
summaryrefslogtreecommitdiffstats
blob: be8cb1ba2e1050d05fa30c4abd88e5d6abb69f84 (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
/*
 * Copyright (c) 2013 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.common.notify.AdapterFactory;
import org.eclipse.emf.databinding.EMFDataBindingContext;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.domain.EditingDomain;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.forms.AbstractFormPart;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

/**
 * Common framework for a part of a form page that is an SWT {@link Section}
 * presenting some object, list/table of objects, or attributes of an object.
 *
 * @author Christian W. Damus (CEA LIST)
 */
public abstract class AbstractSectionPart<T extends EObject> extends AbstractFormPart
{
  private final EditingDomain domain;

  private final AdapterFactory adapterFactory;

  private final Class<T> inputType;

  private final EClass inputEClass;

  private T input;

  private DataBindingContext context;

  private WritableValue value;

  private IActionBars editorActionBars;

  public AbstractSectionPart(Class<T> inputType, EClass inputEClass, EditingDomain domain, AdapterFactory adapterFactory)
  {
    this.inputType = inputType;
    this.inputEClass = inputEClass;
    this.domain = domain;
    this.adapterFactory = adapterFactory;
  }

  @Override
  public void initialize(IManagedForm form)
  {
    super.initialize(form);
    initDatabindings();
  }

  public void setEditorActionBars(IActionBars actionBars)
  {
    this.editorActionBars = actionBars;
  }

  protected IActionBars getEditorActionBars()
  {
    return editorActionBars;
  }

  protected void initDatabindings()
  {
    context = new EMFDataBindingContext();
    value = new WritableValue(context.getValidationRealm());
  }

  @Override
  public void dispose()
  {
    if (context != null)
    {
      context.dispose();
    }

    super.dispose();
  }

  @Override
  public boolean setFormInput(Object input)
  {
    boolean result = false;
    this.input = null;

    if (inputType.isInstance(input))
    {
      this.input = inputType.cast(input);
      result = true;
    }

    value.setValue(this.input);
    return result;
  }

  public void selectionChanged(IFormPart part, ISelection selection)
  {
    if (selection instanceof IStructuredSelection)
    {
      IStructuredSelection sel = (IStructuredSelection)selection;
      if (!sel.isEmpty())
      {
        setFormInput(sel.getFirstElement());
      }
    }
  }

  public final Class<T> getInputType()
  {
    return inputType;
  }

  public final EClass getInputEClass()
  {
    return inputEClass;
  }

  protected T getInput()
  {
    return input;
  }

  protected EditingDomain getEditingDomain()
  {
    return domain;
  }

  protected AdapterFactory getAdapterFactory()
  {
    return adapterFactory;
  }

  protected Realm getRealm()
  {
    return getContext().getValidationRealm();
  }

  protected DataBindingContext getContext()
  {
    return context;
  }

  protected IObservableValue getValue()
  {
    return value;
  }

  protected ISWTObservableValue observeText(Text text)
  {
    return SWTObservables.observeText(text, new int[] { SWT.DefaultSelection, SWT.FocusOut });
  }

  public void createContents(Composite parent)
  {
    FormToolkit toolkit = getManagedForm().getToolkit();

    Section section = toolkit.createSection(parent, Section.SHORT_TITLE_BAR);
    section.setText(getTitle());

    if (parent.getLayout() instanceof TableWrapLayout)
    {
      section.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.FILL_GRAB));
    }
    else if (parent.getLayout() instanceof GridLayout)
    {
      section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    }

    Composite body = toolkit.createComposite(section);
    section.setClient(body);

    createContents(body, toolkit);
    createActionToolbar(section, toolkit);
  }

  protected abstract String getTitle();

  protected abstract void createContents(Composite parent, FormToolkit toolkit);

  protected void createActionToolbar(Section section, FormToolkit toolkit)
  {
    // Pass
  }
}

Back to the top