Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c1574b4db7556d0cda8690e6e7d41c25d78c1d3a (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
272
273
274
275
276
277
278
/*
 * Copyright (c) 2020 Eike Stepper (Loehne, 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.explorer.ui.checkouts;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.explorer.CDOExplorerUtil;
import org.eclipse.emf.cdo.explorer.checkouts.CDOCheckout;
import org.eclipse.emf.cdo.explorer.ui.bundle.OM;
import org.eclipse.emf.cdo.internal.ui.AbstractCDOEditorInput;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.ui.CDOEditorInput2;
import org.eclipse.emf.cdo.ui.CDOEditorInput3;
import org.eclipse.emf.cdo.ui.CDOItemProvider;
import org.eclipse.emf.cdo.util.CDOURIUtil;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.net4j.util.CheckUtil;
import org.eclipse.net4j.util.om.monitor.EclipseMonitor;
import org.eclipse.net4j.util.om.monitor.OMMonitor;
import org.eclipse.net4j.util.om.monitor.OMMonitor.Async;

import org.eclipse.emf.common.util.URI;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IElementFactory;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPersistableElement;

/**
 * @author Eike Stepper
 */
public class CDOCheckoutEditorInput extends PlatformObject implements CDOEditorInput2, CDOEditorInput3, IPersistableElement
{
  protected static final String URI_TAG = "uri";

  private final URI uri;

  private CDOCheckout checkout;

  private CDOView view;

  private String resourcePath;

  private CDOID objectID;

  public CDOCheckoutEditorInput(URI uri)
  {
    CheckUtil.checkArg(uri, "uri is null");
    this.uri = uri;
  }

  public URI getURI()
  {
    return uri;
  }

  public CDOCheckout getCheckout()
  {
    if (checkout == null)
    {
      checkout = CDOExplorerUtil.getCheckout(uri);
    }

    return checkout;
  }

  @Override
  public CDOView getView()
  {
    return view;
  }

  @Override
  public CDOView openView(IProgressMonitor progressMonitor)
  {
    if (view == null)
    {
      CDOCheckout checkout = getCheckout();

      OMMonitor monitor = new EclipseMonitor(progressMonitor, "Opening checkout...");
      monitor.begin();

      try
      {
        Async async = monitor.forkAsync();

        try
        {
          checkout.open();

          progressMonitor.setTaskName("Opening view...");
          view = checkout.openView();
          configureView(view);
        }
        catch (Exception ex)
        {
          OM.LOG.error(ex);

          String message = ex.getLocalizedMessage();
          if (message == null || message.isEmpty())
          {
            message = "Could not open checkout.";
          }

          message += "\nCheck the error log for details.";
          progressMonitor.setTaskName(message);
        }
        finally
        {
          async.stop();
        }
      }
      finally
      {
        monitor.done();
      }
    }

    return view;
  }

  @Override
  public boolean isViewOwned()
  {
    return true;
  }

  @Override
  public String getResourcePath()
  {
    if (resourcePath == null)
    {
      resourcePath = CDOURIUtil.extractResourcePath(uri);
    }

    return resourcePath;
  }

  @Override
  public CDOID getObjectID()
  {
    if (objectID == null)
    {
      objectID = CDOID.NULL;

      if (uri.hasFragment())
      {
        CDOObject cdoObject = CDOUtil.getCDOObject(getView().getResourceSet().getEObject(uri, true));
        if (cdoObject != null)
        {
          objectID = cdoObject.cdoID();
        }
      }
    }

    return objectID == CDOID.NULL ? null : objectID;
  }

  @Override
  public void setObjectID(CDOID objectID)
  {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean exists()
  {
    return true;
  }

  @Override
  public ImageDescriptor getImageDescriptor()
  {
    CDOView view = getView();
    return CDOItemProvider.getViewImageDescriptor(view);
  }

  @Override
  public String getName()
  {
    String resourcePath = getResourcePath();
    if (resourcePath != null)
    {
      return new Path(resourcePath).lastSegment();
    }

    return getView().getSession().getRepositoryInfo().getName();
  }

  @Override
  public String getToolTipText()
  {
    CDOView view = getView();
    String resourcePath = getResourcePath();
    return AbstractCDOEditorInput.formatToolTipText(view, resourcePath);
  }

  @Override
  public IPersistableElement getPersistable()
  {
    if (OM.PREF_REMEMBER_OPEN_EDITORS.getValue() == Boolean.TRUE)
    {
      return this;
    }

    return null;
  }

  @Override
  public void saveState(IMemento memento)
  {
    memento.putString(URI_TAG, uri.toString());
  }

  @Override
  public String getFactoryId()
  {
    return ElementFactory.ID;
  }

  @Override
  public int hashCode()
  {
    return uri.hashCode();
  }

  @Override
  public boolean equals(Object o)
  {
    return this == o || o instanceof CDOCheckoutEditorInput && uri.equals(((CDOCheckoutEditorInput)o).getURI());
  }

  protected void configureView(CDOView view)
  {
    if (view instanceof CDOTransaction)
    {
      configureTransaction((CDOTransaction)view);
    }
  }

  protected void configureTransaction(CDOTransaction transaction)
  {
    CDOCheckoutEditorOpener.addConflictResolver(transaction);
  }

  /**
   * @author Eike Stepper
   */
  public static class ElementFactory implements IElementFactory
  {
    public static final String ID = "org.eclipse.emf.cdo.explorer.ui.checkouts.CDOCheckoutEditorInput.ElementFactory";

    public ElementFactory()
    {
    }

    @Override
    public IAdaptable createElement(IMemento memento)
    {
      URI uri = URI.createURI(memento.getString(URI_TAG));
      return new CDOCheckoutEditorInput(uri);
    }
  }
}

Back to the top