Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 714176ac8110a9ce3ba99de4e3fc5c03f3e38940 (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
/**
 * Copyright (c) 2009 Bestsolution.at and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: 
 *   Tom Schindl<tom.schindl@bestsolution.at> - Initial API and implementation
 */
package org.eclipse.emf.example.databinding.project.ui.rcp.dialogs;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.ObservablesManager;
import org.eclipse.core.databinding.conversion.Converter;
import org.eclipse.jface.databinding.dialog.TitleAreaDialogSupport;
import org.eclipse.jface.databinding.swt.IWidgetValueProperty;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.databinding.EMFDataBindingContext;
import org.eclipse.emf.databinding.EMFUpdateValueStrategy;
import org.eclipse.emf.databinding.edit.EMFEditProperties;
import org.eclipse.emf.databinding.edit.IEMFEditValueProperty;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.example.databinding.project.ui.rcp.databinding.EMFObservablesManager;
import org.eclipse.emf.example.databinding.project.ui.rcp.databinding.UpdateStrategyFactory;
import org.eclipse.emf.examples.databinding.project.core.IModelResource;
import org.eclipse.emf.examples.databinding.project.core.model.project.CommitterShip;
import org.eclipse.emf.examples.databinding.project.core.model.project.Person;
import org.eclipse.emf.examples.databinding.project.core.model.project.ProjectPackage;


/**
 * Dialog to edit the committer ship
 */
public class CommitterShipDialog extends TitleAreaDialog
{
  private final DataBindingContext ctx;
  private final CommitterShip committership;
  final IModelResource resource;
  private final boolean isnew;

  /**
   * Create new dialog
   * @param parentShell the parent shell
   * @param resource the resource
   * @param committership the committership instance
   * @param isnew flag to indicate if the committer ship is new
   */
  public CommitterShipDialog(Shell parentShell, IModelResource resource, CommitterShip committership, boolean isnew)
  {
    super(parentShell);
    this.committership = committership;
    this.resource = resource;
    this.isnew = isnew;
    this.ctx = new EMFDataBindingContext();
  }

  @Override
  protected Control createDialogArea(Composite parent)
  {
    getShell().setText("Edit Committership");
    setTitle("Edit Committership");
    setMessage("Edit the committership of a person in the project");

    final Composite comp = (Composite)super.createDialogArea(parent);

    ObservablesManager mgr = new EMFObservablesManager();
    mgr.runAndCollect(new Runnable()
      {

        public void run()
        {
          createForm(comp).setLayoutData(new GridData(GridData.FILL_BOTH));
        }

      });

    return comp;
  }

  private Control createForm(final Composite comp)
  {

    Composite container = new Composite(comp, SWT.NONE);
    container.setLayout(new GridLayout(3, false));

    {
      Label l = new Label(container, SWT.NONE);
      l.setText("Committer");

      IEMFEditValueProperty mprop = EMFEditProperties.value(resource.getEditingDomain(), ProjectPackage.Literals.COMMITTER_SHIP__PERSON);

      ViewForm form = new ViewForm(container, SWT.BORDER | SWT.FLAT);
      form.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
      CLabel label = new CLabel(form, SWT.NONE);
      form.setContent(label);

      ctx.bindValue(
        WidgetProperties.text().observe(label),
        mprop.observe(committership),
        null,
        new EMFUpdateValueStrategy().setConverter(new PersonToStringConverter()));

      Button b = new Button(container, SWT.PUSH);
      b.setText("...");
      b.setEnabled(isnew);
      b.addSelectionListener(new SelectionAdapter()
        {
          @Override
          public void widgetSelected(SelectionEvent e)
          {
            FilteredItemsSelectionDialog dialog = new PersonFilterDialog(comp.getShell(), resource);
            dialog.open();
            Person p = (Person)dialog.getFirstResult();

            if (p != null)
            {
              Command cmd = SetCommand.create(resource.getEditingDomain(), committership, ProjectPackage.Literals.COMMITTER_SHIP__PERSON, p);
              if (cmd.canExecute())
              {
                resource.executeCmd(cmd);
              }
            }
          }
        });
    }

    IWidgetValueProperty textProp = WidgetProperties.text(SWT.Modify);

    {
      Label l = new Label(container, SWT.NONE);
      l.setText("Startdate");

      IEMFEditValueProperty mprop = EMFEditProperties.value(resource.getEditingDomain(), ProjectPackage.Literals.COMMITTER_SHIP__START);

      Text t = new Text(container, SWT.BORDER);
      t.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT, true, false, 2, 1));

      ctx.bindValue(textProp.observe(t), mprop.observe(committership), UpdateStrategyFactory.stringToDateNotNull(
        "Can't convert start date value into a valid date",
        "The start date can not be null"), UpdateStrategyFactory.dateToString());
    }

    {
      Label l = new Label(container, SWT.NONE);
      l.setText("Enddate");

      IEMFEditValueProperty mprop = EMFEditProperties.value(resource.getEditingDomain(), ProjectPackage.Literals.COMMITTER_SHIP__END);

      Text t = new Text(container, SWT.BORDER);
      t.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT, true, false, 2, 1));

      ctx.bindValue(
        textProp.observe(t),
        mprop.observe(committership),
        UpdateStrategyFactory.stringToDate("Can't convert end date value into a valid date"),
        UpdateStrategyFactory.dateToString());
    }

    TitleAreaDialogSupport.create(this, ctx);

    return container;
  }

  private class PersonToStringConverter extends Converter
  {
    public PersonToStringConverter()
    {
      super(Person.class, String.class);
    }

    public Object convert(Object fromObject)
    {
      if (fromObject != null)
      {
        Person p = (Person)fromObject;
        return p.getLastname() + ", " + p.getFirstname();
      }

      return null;
    }
  };
}

Back to the top