Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: df2acbb61c393f0b6cf6b23d342253601fd5896e (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
/**
 * Copyright (c) 2004 - 2010 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:
 *    Simon McDuff - initial API and implementation
 *    Eike Stepper - maintenance
 *    Victor Roldan Betancort - maintenance
 */
package org.eclipse.emf.cdo.internal.ui.actions;

import org.eclipse.emf.cdo.CDOLock;
import org.eclipse.emf.cdo.internal.ui.messages.Messages;
import org.eclipse.emf.cdo.ui.CDOEditorUtil;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.emf.spi.cdo.InternalCDOObject;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPage;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Simon McDuff
 */
public abstract class AbstractLockObjectsAction extends EditingDomainAction
{
  private List<InternalCDOObject> objects = new ArrayList<InternalCDOObject>();

  private List<InternalCDOObject> lockObjects = new ArrayList<InternalCDOObject>();

  private Boolean lock;

  public AbstractLockObjectsAction(String title)
  {
    super(title);
  }

  public void selectionChanged(IStructuredSelection selection)
  {
    objects.clear();
    lock = null;
    if (selection != null)
    {
      for (Iterator<?> it = selection.iterator(); it.hasNext();)
      {
        Object object = it.next();
        if (object instanceof InternalCDOObject)
        {
          objects.add((InternalCDOObject)object);
        }
      }
    }
  }

  @Override
  public void update()
  {
    updateLockInfo();
    setEnabled(!lockObjects.isEmpty() && lock != null);
    setChecked(lock != null && lock);
  }

  @Override
  protected void doRun(IProgressMonitor progressMonitor) throws Exception
  {
    if (!objects.isEmpty())
    {
      InternalCDOObject[] array = objects.toArray(new InternalCDOObject[objects.size()]);
      for (InternalCDOObject object : lockObjects)
      {
        if (lock)
        {
          getLock(object).unlock();
        }
        else
        {
          if (!getLock(object).tryLock())
          {
            getDisplay().asyncExec(new Runnable()
            {
              public void run()
              {
                MessageDialog
                    .openError(
                        getShell(),
                        Messages.getString("AbstractLockObjectsAction.0"), Messages.getString("AbstractLockObjectsAction.1")); //$NON-NLS-1$ //$NON-NLS-2$
              }
            });
          }
        }
      }

      IWorkbenchPage page = getPage();
      if (page != null)
      {
        CDOView view = array[0].cdoView();
        CDOEditorUtil.refreshEditors(page, view);
      }
    }
  }

  protected abstract CDOLock getLock(InternalCDOObject object);

  private void updateLockInfo()
  {
    lock = null;
    lockObjects.clear();
    for (InternalCDOObject object : objects)
    {
      boolean isLocked = getLock(object).isLocked();
      if (lock == null || isLocked == lock)
      {
        lock = isLocked;
        lockObjects.add(object);
      }
    }
  }
}

Back to the top