Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5e870db79b2968f49fc8cc960aae5f44158b3687 (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
/*
 * Copyright (c) 2015 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.explorer.CDOExplorerUtil;
import org.eclipse.emf.cdo.explorer.checkouts.CDOCheckout;
import org.eclipse.emf.cdo.explorer.ui.checkouts.CDOCheckoutState.ContentProvider;
import org.eclipse.emf.cdo.explorer.ui.checkouts.CDOCheckoutState.LabelProvider;

import org.eclipse.net4j.util.lifecycle.LifecycleException;

import org.eclipse.emf.edit.provider.ComposedAdapterFactory;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.resource.LocalResourceManager;
import org.eclipse.jface.resource.ResourceManager;
import org.eclipse.jface.viewers.TreeViewer;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Eike Stepper
 */
public final class CDOCheckoutStateManager
{
  private final ResourceManager resourceManager = new LocalResourceManager(JFaceResources.getResources());

  private final Map<CDOCheckout, CDOCheckoutState> states = new HashMap<CDOCheckout, CDOCheckoutState>();

  private final CDOCheckoutContentProvider mainContentProvider;

  private CDOCheckoutViewerRefresh viewerRefresh;

  public CDOCheckoutStateManager(CDOCheckoutContentProvider mainContentProvider)
  {
    this.mainContentProvider = mainContentProvider;
  }

  public CDOCheckoutContentProvider getMainContentProvider()
  {
    return mainContentProvider;
  }

  public ResourceManager getResourceManager()
  {
    return resourceManager;
  }

  public CDOCheckoutViewerRefresh getViewerRefresh()
  {
    return viewerRefresh;
  }

  public CDOCheckoutState[] getStates()
  {
    synchronized (states)
    {
      return states.values().toArray(new CDOCheckoutState[states.size()]);
    }
  }

  public CDOCheckoutState getState(Object object)
  {
    try
    {
      CDOCheckout checkout = CDOExplorerUtil.getCheckout(object);
      if (checkout != null)
      {
        synchronized (states)
        {
          CDOCheckoutState state = states.get(checkout);
          if (state == null)
          {
            state = new CDOCheckoutState(this, checkout);
            states.put(checkout, state);
          }

          return state;
        }
      }
    }
    catch (LifecycleException ex)
    {
      //$FALL-THROUGH$
    }

    return null;
  }

  public void inputChanged(TreeViewer newTreeViewer, Object oldInput, Object newInput)
  {
    viewerRefresh = new CDOCheckoutViewerRefresh(newTreeViewer);

    for (CDOCheckoutState state : getStates())
    {
      state.inputChanged(newTreeViewer, oldInput, newInput);
    }
  }

  public Object adapt(Object target, Object type)
  {
    CDOCheckoutState state = getState(target);
    if (state != null)
    {
      return state.getAdapterFactory().adapt(target, type);
    }

    return null;
  }

  public ComposedAdapterFactory getAdapterFactory(Object object)
  {
    CDOCheckoutState state = getState(object);
    if (state != null)
    {
      return state.getAdapterFactory();
    }

    return null;
  }

  public ContentProvider getContentProvider(Object object)
  {
    CDOCheckoutState state = getState(object);
    if (state != null)
    {
      return state.getContentProvider();
    }

    return null;
  }

  public LabelProvider getLabelProvider(Object object)
  {
    CDOCheckoutState state = getState(object);
    if (state != null)
    {
      return state.getLabelProvider();
    }

    return null;
  }

  public void dispose()
  {
    resourceManager.dispose();
  }
}

Back to the top