Skip to main content
summaryrefslogtreecommitdiffstats
blob: e10faf59ae3232d609b5ea2aae8daf281292f71b (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
/**
 * 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
 */
package org.eclipse.net4j.util.ui;

import org.eclipse.net4j.util.properties.IPropertyProvider;
import org.eclipse.net4j.util.properties.Property;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Eike Stepper
 * @since 3.2
 */
public class DefaultPropertySource<RECEIVER> implements IPropertySource
{
  private List<IPropertyDescriptor> descriptors = new ArrayList<IPropertyDescriptor>();

  private RECEIVER receiver;

  public DefaultPropertySource(RECEIVER receiver)
  {
    this.receiver = receiver;
  }

  public DefaultPropertySource(RECEIVER object, IPropertyProvider<RECEIVER> provider)
  {
    this(object);
    addDescriptors(provider);
  }

  public RECEIVER getReceiver()
  {
    return receiver;
  }

  public PropertyDescriptor addDescriptor(String category, Object id, String displayName, String description)
  {
    PropertyDescriptor descriptor = new PropertyDescriptor(id, displayName);
    descriptor.setCategory(category);
    descriptor.setDescription(description);

    descriptors.add(descriptor);
    return descriptor;
  }

  public void addDescriptors(IPropertyProvider<RECEIVER> provider)
  {
    for (Property<RECEIVER> property : provider.getProperties())
    {
      if (property.getLabel() != null)
      {
        descriptors.add(new DelegatingPropertyDescriptor<RECEIVER>(property));
      }
    }
  }

  public IPropertyDescriptor[] getPropertyDescriptors()
  {
    return descriptors.toArray(new IPropertyDescriptor[descriptors.size()]);
  }

  public IPropertyDescriptor getPropertyDescriptor(Object id)
  {
    IPropertyDescriptor[] propertyDescriptors = getPropertyDescriptors();
    for (int i = 0; i < propertyDescriptors.length; i++)
    {
      IPropertyDescriptor propertyDescriptor = propertyDescriptors[i];
      if (propertyDescriptor.getId().equals(id))
      {
        return propertyDescriptor;
      }
    }

    return null;
  }

  public Property<RECEIVER> getProperty(Object id)
  {
    IPropertyDescriptor propertyDescriptor = getPropertyDescriptor(id);
    if (propertyDescriptor instanceof DelegatingPropertyDescriptor)
    {
      @SuppressWarnings("unchecked")
      DelegatingPropertyDescriptor<RECEIVER> delegating = (DelegatingPropertyDescriptor<RECEIVER>)propertyDescriptor;
      return delegating.getProperty();
    }

    return null;
  }

  public Object getPropertyValue(Object id)
  {
    Property<RECEIVER> property = getProperty(id);
    if (property != null)
    {
      return property.getValue(receiver);
    }

    return null;
  }

  public boolean isPropertySet(Object id)
  {
    return true;
  }

  public void resetPropertyValue(Object id)
  {
  }

  public void setPropertyValue(Object id, Object value)
  {
  }

  public Object getEditableValue()
  {
    return null;
  }

  /**
   * @author Eike Stepper
   */
  public static final class DelegatingPropertyDescriptor<RECEIVER> implements IPropertyDescriptor
  {
    private final Property<RECEIVER> property;

    private ILabelProvider labelProvider;

    public DelegatingPropertyDescriptor(Property<RECEIVER> property)
    {
      this.property = property;
    }

    public Property<RECEIVER> getProperty()
    {
      return property;
    }

    public String getCategory()
    {
      return property.getCategory();
    }

    public String getId()
    {
      return property.getName();
    }

    public String getDisplayName()
    {
      return property.getLabel();
    }

    public String getDescription()
    {
      return property.getDescription();
    }

    public boolean isCompatibleWith(IPropertyDescriptor anotherProperty)
    {
      return anotherProperty.getCategory().equals(getCategory()) && anotherProperty.getId().equals(getId());
    }

    public ILabelProvider getLabelProvider()
    {
      if (labelProvider != null)
      {
        return labelProvider;
      }

      return new LabelProvider();
    }

    public void setLabelProvider(ILabelProvider labelProvider)
    {
      this.labelProvider = labelProvider;
    }

    public Object getHelpContextIds()
    {
      return null;
    }

    public String[] getFilterFlags()
    {
      return null;
    }

    public CellEditor createPropertyEditor(Composite parent)
    {
      return null;
    }
  }
}

Back to the top