Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6d9eb86ae9a805326dd810276b8700cd3dc8227e (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
/*
 * 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.handlers;

import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.security.internal.ui.editor.CDOSecurityFormEditor;
import org.eclipse.emf.cdo.security.internal.ui.messages.Messages;
import org.eclipse.emf.cdo.security.ui.ISecurityManagementContext;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.ui.CDOEditorInput;
import org.eclipse.emf.cdo.ui.CDOEditorUtil;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.net4j.util.ui.UIUtil;
import org.eclipse.net4j.util.ui.handlers.LongRunningHandler;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.statushandlers.StatusManager;

import java.util.Set;

/**
 * "Manage Security" command handler, which opens the Security Manager editor
 * in the context of the currently selected {@link CDOSession}, with the help
 * of an optional {@link ISecurityManagementContext} adapter.
 *
 * @author Christian W. Damus (CEA LIST)
 */
public class ManageSecurityHandler extends LongRunningHandler
{
  private IWorkbenchPart part;

  private ISecurityManagementContext context;

  private CDOSession session;

  public ManageSecurityHandler()
  {
  }

  @Override
  protected void extractEventDetails(ExecutionEvent event)
  {
    super.extractEventDetails(event);

    part = HandlerUtil.getActivePart(event);
    context = getContext(event);
  }

  @Override
  protected void preRun() throws Exception
  {
    if (part == null)
    {
      // No workbench page available in which to open the editor
      cancel();
      return;
    }

    session = getSession();
    if (session != null && !session.isClosed())
    {
      final IWorkbenchPage page = part.getSite().getPage();

      IEditorPart existing = findEditor(page, session);
      if (existing != null)
      {
        // Activate this editor and we're done
        cancel();
        page.activate(existing);
      }
    }
  }

  protected CDOSession getSession()
  {
    return UIUtil.adaptElement(getSelection(), CDOSession.class);
  }

  @Override
  protected void doExecute(IProgressMonitor progressMonitor) throws Exception
  {
    // Open a new security editor
    final CDOView[] view = new CDOView[] { context.connect(session) };
    if (view[0] == null || view[0].isClosed())
    {
      showWarning(Messages.ManageSecurityHandler_0, Messages.ManageSecurityHandler_1);
      return;
    }

    try
    {
      final CDOResource resource = context.getSecurityResource(view[0]);
      if (resource == null)
      {
        showWarning(Messages.ManageSecurityHandler_0, Messages.ManageSecurityHandler_2);
      }
      else
      {
        UIUtil.getDisplay().syncExec(new Runnable()
        {

          public void run()
          {
            IEditorInput input = CDOEditorUtil.createCDOEditorInput(view[0], resource.getPath(), false);

            try
            {
              IEditorPart editor = part.getSite().getPage().openEditor(input, CDOSecurityFormEditor.ID);
              if (editor != null)
              {
                hookCloseListener(editor, context, view[0]);
                view[0] = null; // Don't disconnect it until later
              }
            }
            catch (PartInitException e)
            {
              StatusManager.getManager().handle(e.getStatus(), StatusManager.SHOW);
            }
          }
        });
      }
    }
    finally
    {
      if (view[0] != null)
      {
        context.disconnect(view[0]);
      }
    }
  }

  protected void showWarning(final String title, final String message)
  {
    UIUtil.getDisplay().syncExec(new Runnable()
    {

      public void run()
      {
        MessageDialog.openWarning(part.getSite().getShell(), title, message);
      }
    });
  }

  IEditorPart findEditor(IWorkbenchPage page, CDOSession session)
  {
    for (IEditorReference next : page.getEditorReferences())
    {
      if (CDOSecurityFormEditor.ID.equals(next.getId()))
      {
        IEditorPart candidate = next.getEditor(false);
        if (candidate != null)
        {
          IEditorInput input = candidate.getEditorInput();
          if (input instanceof CDOEditorInput)
          {
            CDOView view = ((CDOEditorInput)input).getView();
            if (view != null && !view.isClosed() && session.equals(view.getSession()))
            {
              return candidate;
            }
          }
        }
      }
    }

    return null;
  }

  ISecurityManagementContext getContext(ExecutionEvent event)
  {
    ISecurityManagementContext result = null;

    IWorkbenchPart part = HandlerUtil.getActivePart(event);
    if (part != null)
    {
      result = (ISecurityManagementContext)part.getAdapter(ISecurityManagementContext.class);
    }

    if (result == null)
    {
      result = ISecurityManagementContext.DEFAULT;
    }

    return result;
  }

  private void hookCloseListener(final IEditorPart editor, final ISecurityManagementContext context, final CDOView view)
  {
    final IWorkbenchPage page = editor.getSite().getPage();
    page.addPartListener(new IPartListener()
    {
      private final IEditorInput input = editor.getEditorInput();

      private final Set<IEditorPart> openEditors = new java.util.HashSet<IEditorPart>();

      {
        openEditors.add(editor);
      }

      public void partClosed(IWorkbenchPart part)
      {
        openEditors.remove(part);
        if (openEditors.isEmpty())
        {
          // No more editors using this view
          context.disconnect(view);
          page.removePartListener(this);
        }
      }

      public void partOpened(IWorkbenchPart part)
      {
        if (part instanceof IEditorPart)
        {
          IEditorPart editor = (IEditorPart)part;
          if (input.equals(editor.getEditorInput()))
          {
            // The user opened the advanced-mode editor from the form editor
            openEditors.add(editor);
          }
        }
      }

      public void partDeactivated(IWorkbenchPart part)
      {
        // Pass
      }

      public void partBroughtToTop(IWorkbenchPart part)
      {
        // Pass
      }

      public void partActivated(IWorkbenchPart part)
      {
        // Pass
      }
    });
  }
}

Back to the top