Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3ed3d5492e568a000bd02437228a86ebe85c81a0 (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
/*
 * Copyright (c) 2015, 2016, 2019 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;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Tree;

import java.util.Collection;

/**
 * @author Eike Stepper
 */
public final class ViewerUtil
{
  public static final Object[] NO_CHILDREN = {};

  private ViewerUtil()
  {
  }

  public static void update(StructuredViewer viewer, Object element)
  {
    update(viewer, element, true);
  }

  public static void update(final StructuredViewer viewer, final Object element, boolean async)
  {
    if (viewer != null)
    {
      final Control control = viewer.getControl();
      if (!control.isDisposed())
      {
        Runnable runnable = new Runnable()
        {
          @Override
          public void run()
          {
            if (!control.isDisposed())
            {
              if (element instanceof Object[])
              {
                Object[] array = (Object[])element;
                viewer.update(array, null);
              }
              else if (element instanceof Collection)
              {
                Collection<?> collection = (Collection<?>)element;
                viewer.update(collection.toArray(), null);
              }
              else
              {
                viewer.update(element, null);
              }
            }
          }
        };

        Display display = control.getDisplay();
        if (async)
        {
          display.asyncExec(runnable);
        }
        else
        {
          display.syncExec(runnable);
        }
      }
    }
  }

  public static void refresh(StructuredViewer viewer, Object element)
  {
    refresh(viewer, element, true);
  }

  public static void refresh(final StructuredViewer viewer, final Object element, boolean async)
  {
    refresh(viewer, element, async, false);
  }

  public static void refresh(final StructuredViewer viewer, final Object element, boolean async, final boolean setSelectionBack)
  {
    if (viewer != null)
    {
      Control control = viewer.getControl();
      if (!control.isDisposed())
      {
        Runnable runnable = new Runnable()
        {
          @Override
          public void run()
          {
            if (!viewer.getControl().isDisposed())
            {
              ISelection selection = null;
              if (setSelectionBack)
              {
                selection = viewer.getSelection();
              }

              if (element == null)
              {
                viewer.refresh();
              }
              else if (element instanceof Collection)
              {
                Collection<?> collection = (Collection<?>)element;
                for (Object object : collection)
                {
                  viewer.refresh(object);
                }
              }
              else
              {
                viewer.refresh(element);
              }

              if (selection != null)
              {
                viewer.setSelection(selection);
              }
            }
          }
        };

        Display display = control.getDisplay();
        if (async)
        {
          display.asyncExec(runnable);
        }
        else
        {
          display.syncExec(runnable);
        }
      }
    }
  }

  public static void expand(final TreeViewer viewer, final Object element, final boolean expanded)
  {
    if (viewer != null)
    {
      Tree tree = viewer.getTree();
      if (!tree.isDisposed())
      {
        Display display = tree.getDisplay();
        display.asyncExec(new Runnable()
        {
          @Override
          public void run()
          {
            if (!viewer.getControl().isDisposed())
            {
              viewer.setExpandedState(element, expanded);
            }
          }
        });
      }
    }
  }

  /**
   * @author Eike Stepper
   */
  public static final class Pending
  {
    private final Object parent;

    private final String text;

    public Pending(Object parent, String text)
    {
      this.parent = parent;
      this.text = text;
    }

    public Object getParent()
    {
      return parent;
    }

    public String getText()
    {
      return text;
    }
  }
}

Back to the top