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

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.view.CDOView;

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

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.TreeItem;

import java.util.Set;

/**
 * @author Eike Stepper
 */
public abstract class ItemsProcessor
{
  private CDOView view;

  public ItemsProcessor(CDOView view)
  {
    this.view = view;
  }

  public CDOView getView()
  {
    return view;
  }

  public void processCDOObjects(TreeViewer viewer)
  {
    processCDOObjects(viewer, null);
  }

  public void processCDOObjects(final TreeViewer viewer, final Set<? extends CDOObject> ids)
  {
    try
    {
      viewer.getControl().getDisplay().asyncExec(new Runnable()
      {
        public void run()
        {
          try
          {
            processObject(viewer, ids, viewer.getInput());
            processItems(viewer, ids, viewer.getTree().getItems());
          }
          catch (Exception ignore)
          {
          }
        }
      });
    }
    catch (Exception ignore)
    {
    }
  }

  protected InternalCDOObject getCDOObject(Object object)
  {
    if (object instanceof InternalCDOObject)
    {
      return (InternalCDOObject)object;
    }

    if (object != null && view != null)
    {
      return FSMUtil.adapt(object, view);
    }

    return null;
  }

  protected abstract void processCDOObject(TreeViewer viewer, InternalCDOObject cdoObject);

  private void processItems(TreeViewer viewer, Set<? extends CDOObject> ids, TreeItem[] items)
  {
    for (TreeItem item : items)
    {
      Object object = item.getData();
      processObject(viewer, ids, object);
      if (item.getItemCount() != 0)
      {
        processItems(viewer, ids, item.getItems());
      }
    }
  }

  private void processObject(TreeViewer viewer, Set<? extends CDOObject> ids, Object object)
  {
    InternalCDOObject cdoObject = getCDOObject(object);
    if (ids.contains(cdoObject))
    {
      processCDOObject(viewer, cdoObject);
    }
  }
}

Back to the top