Skip to main content
summaryrefslogtreecommitdiffstats
blob: e46f5973768967f90f67988bb88eeab6e7bfc8fb (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
/*
 * 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.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
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 AbstractHandler
{
  public ManageSecurityHandler()
  {
  }

  public Object execute(ExecutionEvent event) throws ExecutionException
  {
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    CDOSession session = UIUtil.adaptElement(selection, CDOSession.class);
    if (session != null && !session.isClosed())
    {
      IWorkbenchPart part = HandlerUtil.getActivePart(event);
      if (part != null)
      {
        final IWorkbenchPage page = part.getSite().getPage();

        IEditorPart existing = findEditor(page, session);
        if (existing != null)
        {
          // Activate this editor
          page.activate(existing);
        }
        else
        {
          // Open a new security editor
          ISecurityManagementContext context = getContext(event);
          CDOView view = context.connect(session);
          if (view == null || view.isClosed())
          {
            MessageDialog.openWarning(HandlerUtil.getActiveShell(event), Messages.ManageSecurityHandler_0,
                Messages.ManageSecurityHandler_1);
          }
          else
          {
            try
            {
              CDOResource resource = context.getSecurityResource(view);
              if (resource == null)
              {
                MessageDialog.openWarning(HandlerUtil.getActiveShell(event), Messages.ManageSecurityHandler_0,
                    Messages.ManageSecurityHandler_2);
              }
              else
              {
                IEditorInput input = CDOEditorUtil.createCDOEditorInput(view, resource.getPath(), false);

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

    return null;
  }

  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