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

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.ui.internal.ide.messages.Messages;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.net4j.util.AdapterUtil;
import org.eclipse.net4j.util.ui.UIUtil;
import org.eclipse.net4j.util.ui.actions.LongRunningActionDelegate;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import java.text.MessageFormat;

/**
 * @author Victor Roldan Betancort
 */
public abstract class TransactionalBackgroundActionDelegate extends LongRunningActionDelegate implements
    IObjectActionDelegate
{
  private IWorkbenchPart targetPart;

  private String text;

  private CDOObject transactionalObject;

  public TransactionalBackgroundActionDelegate(String text)
  {
    this.text = text;
  }

  public void setActivePart(IAction action, IWorkbenchPart targetPart)
  {
    this.targetPart = targetPart;
    selectionChanged(action, getSelection());
  }

  public IWorkbenchPart getTargetPart()
  {
    return targetPart;
  }

  @Override
  protected String getText()
  {
    return text;
  }

  protected int getCommitWorkPercent()
  {
    return 90;
  }

  @Override
  protected final void preRun() throws Exception
  {
    Object element = UIUtil.getElement(getSelection());
    CDOObject object = AdapterUtil.adapt(element, CDOObject.class);
    if (object != null)
    {
      transactionalObject = preRun(object);
      if (transactionalObject != null)
      {
        CDOView view = transactionalObject.cdoView();
        if (!(view instanceof CDOTransaction))
        {
          throw new IllegalStateException(MessageFormat.format(
              Messages.getString("TransactionalBackgroundAction_0"), transactionalObject)); //$NON-NLS-1$
        }

        // Bypass cancel()
        return;
      }
    }

    cancel();
  }

  /**
   * Usually opens a new transaction based on the passed object and its view/session and returns a "contextualized" copy
   * of this object. Clients may override to access the UI thread before the background job is started or change the
   * contextualization procedure.
   * 
   * @param object
   *          Usually an object in a read-only view that needs to be modified in a separate transaction.
   * @return A transactional copy of the passed object, or <code>null</code> to indicate cancelation of this action.
   */
  protected CDOObject preRun(CDOObject object)
  {
    CDOTransaction transaction = object.cdoView().getSession().openTransaction();
    CDOObject transactionalObject = transaction.getObject(object);
    return transactionalObject;
  }

  @Override
  protected final void doRun(IProgressMonitor progressMonitor) throws Exception
  {
    CDOTransaction transaction = (CDOTransaction)transactionalObject.cdoView();
    int commitWorkPercent = getCommitWorkPercent();
    progressMonitor.beginTask(Messages.getString("TransactionalBackgroundAction_1"), 100); //$NON-NLS-1$

    try
    {
      doRun(transaction, transactionalObject, new SubProgressMonitor(progressMonitor, 100 - commitWorkPercent));
      transaction.commit(new SubProgressMonitor(progressMonitor, commitWorkPercent));
    }
    finally
    {
      progressMonitor.done();
      transaction.close();
      transactionalObject = null;
    }
  }

  protected abstract void doRun(CDOTransaction transaction, CDOObject object, IProgressMonitor progressMonitor)
      throws Exception;
}

Back to the top