Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: df573f9c872d61125e76e6b88390c1c43bd9a3a9 (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
/*
 * Copyright (c) 2013, 2015 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.eresource.CDOResource;
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.provider.SecurityItemProviderAdapterFactory;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.ui.CDOEditorInput;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.emf.common.command.BasicCommandStack;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.common.command.CommandStackListener;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.EMFEditPlugin;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.IFormPage;
import org.eclipse.ui.statushandlers.IStatusAdapterConstants;
import org.eclipse.ui.statushandlers.StatusAdapter;
import org.eclipse.ui.statushandlers.StatusManager;

import java.util.EventObject;

/**
 * The "Security Manager" editor.
 *
 * @author Christian W. Damus (CEA LIST)
 */
public class CDOSecurityFormEditor extends FormEditor implements IEditingDomainProvider
{
  public static final String ID = "org.eclipse.emf.cdo.security.ui.CDOSecurityFormEditor"; //$NON-NLS-1$

  private ComposedAdapterFactory adapterFactory;

  private CDOView view;

  private EditingDomain editingDomain;

  private CDOSecurityPage mainPage;

  private CommandStackListener dirtyStackListener;

  public CDOSecurityFormEditor()
  {
  }

  @Override
  protected void addPages()
  {
    try
    {
      // Don't show tabs because we have only the one
      ((CTabFolder)getContainer()).setTabHeight(0);

      mainPage = new CDOSecurityPage(this);
      addPage(mainPage);
    }
    catch (PartInitException e)
    {
      OM.LOG.error(e);
    }
  }

  @Override
  public boolean isDirty()
  {
    return view != null && view.isDirty();
  }

  @Override
  public void doSave(IProgressMonitor monitor)
  {
    if (view instanceof CDOTransaction)
    {
      try
      {
        ((CDOTransaction)view).commit(monitor);
        fireDirtyStateChanged();
      }
      catch (CommitException e)
      {
        StatusAdapter status = new StatusAdapter(new Status(IStatus.ERROR, OM.BUNDLE_ID, Messages.CDOSecurityFormEditor_0, e));
        status.setProperty(IStatusAdapterConstants.TITLE_PROPERTY, Messages.CDOSecurityFormEditor_1);
        status.setProperty(IStatusAdapterConstants.TIMESTAMP_PROPERTY, System.currentTimeMillis());
        StatusManager.getManager().handle(status, StatusManager.SHOW);
      }
    }
  }

  protected void fireDirtyStateChanged()
  {
    Display display = getContainer().getDisplay();
    if (display == Display.getCurrent())
    {
      firePropertyChange(IEditorPart.PROP_DIRTY);
    }
    else
    {
      display.asyncExec(new Runnable()
      {
        public void run()
        {
          fireDirtyStateChanged();
        }
      });
    }
  }

  @Override
  public void doSaveAs()
  {
  }

  @Override
  public boolean isSaveAsAllowed()
  {
    // Cannot create new security realms using Save As
    return false;
  }

  boolean isReadOnly()
  {
    return view == null || view.isReadOnly();
  }

  @Override
  public void init(IEditorSite site, IEditorInput input) throws PartInitException
  {
    super.init(site, input);
    initializeEditingDomain();
  }

  @Override
  public void dispose()
  {
    try
    {
      if (dirtyStackListener != null)
      {
        if (editingDomain != null)
        {
          editingDomain.getCommandStack().removeCommandStackListener(dirtyStackListener);
        }

        dirtyStackListener = null;
      }
    }
    finally
    {
      super.dispose();
    }
  }

  protected void initializeEditingDomain()
  {
    try
    {
      CDOResource resource = getResource();
      view = resource.cdoView();
      dirtyStackListener = new CommandStackListener()
      {
        public void commandStackChanged(final EventObject event)
        {
          fireDirtyStateChanged();
        }
      };

      BasicCommandStack commandStack = new BasicCommandStack();
      commandStack.addCommandStackListener(dirtyStackListener);

      ResourceSet resourceSet = view.getResourceSet();
      editingDomain = createEditingDomain(commandStack, resourceSet);

      // This adapter provides the EditingDomain of the Editor
      resourceSet.eAdapters().add(new AdapterFactoryEditingDomain.EditingDomainProvider(editingDomain));
    }
    catch (RuntimeException ex)
    {
      OM.LOG.error(ex);
      throw ex;
    }
  }

  protected EditingDomain createEditingDomain(CommandStack commandStack, ResourceSet resourceSet)
  {
    ComposedAdapterFactory.Descriptor.Registry registry = EMFEditPlugin.getComposedAdapterFactoryDescriptorRegistry();
    adapterFactory = new ComposedAdapterFactory(registry);
    adapterFactory.addAdapterFactory(new SecurityItemProviderAdapterFactory());
    adapterFactory.addAdapterFactory(new ResourceItemProviderAdapterFactory());
    adapterFactory.addAdapterFactory(new ReflectiveItemProviderAdapterFactory());

    return new AdapterFactoryEditingDomain(adapterFactory, commandStack, resourceSet);
  }

  protected CDOResource getResource()
  {
    CDOResource result = null;

    IEditorInput input = getEditorInput();
    if (input instanceof CDOEditorInput)
    {
      CDOEditorInput cdoInput = (CDOEditorInput)input;
      result = cdoInput.getView().getResource(cdoInput.getResourcePath());
    }

    return result;
  }

  protected CDOView getView()
  {
    return view;
  }

  protected AdapterFactory getAdapterFactory()
  {
    return adapterFactory;
  }

  public EditingDomain getEditingDomain()
  {
    return editingDomain;
  }

  public IActionBars getActionBars()
  {
    return ((CDOSecurityFormActionBarContributor)getEditorSite().getActionBarContributor()).getActionBars();
  }

  @Override
  public void setFocus()
  {
    super.setFocus();

    IFormPage page = getActivePageInstance();
    if (page != null && page instanceof CDOSecurityPage)
    {
      // Ensure that the first focusable part is focused so that the page's toolbar will not be focused
      // (which shows ugly blue highlights on the toolbar buttons, at least on Mac OS X)
      page.setFocus();
    }
  }
}

Back to the top