summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/model/property/Property.java
blob: 28afcd35231d7969fa5fcb055ec957badf7a35ef (plain)
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
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * 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:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.wb.internal.core.model.property;

import com.google.common.collect.Maps;

import org.eclipse.wb.internal.core.model.property.category.PropertyCategory;
import org.eclipse.wb.internal.core.model.property.editor.PropertyEditor;

import java.util.Comparator;
import java.util.Map;

/**
 * {@link Property} is used to display/change properties of ObjectInfo's.
 *
 * @author scheglov_ke
 * @coverage core.model.property
 */
public abstract class Property {
  /**
   * The value that should be used when we don't know real value of {@link Property}. We can not use
   * <code>null</code> because <code>null</code> can be valid value.
   */
  public static final Object UNKNOWN_VALUE = new Object() {
    @Override
    public String toString() {
      return "UNKNOWN_VALUE";
    }
  };
  ////////////////////////////////////////////////////////////////////////////
  //
  // Instance fields
  //
  ////////////////////////////////////////////////////////////////////////////
  protected final PropertyEditor m_editor;

  ////////////////////////////////////////////////////////////////////////////
  //
  // Constructor
  //
  ////////////////////////////////////////////////////////////////////////////
  public Property(PropertyEditor editor) {
    m_category = PropertyCategory.NORMAL;
    m_editor = editor;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Presentation
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * @return the title displayed to the user to identify the property.
   */
  public abstract String getTitle();

  /**
   * @return <code>true</code> if this property has a non-default value
   */
  public abstract boolean isModified() throws Exception;

  ////////////////////////////////////////////////////////////////////////////
  //
  // Category
  //
  ////////////////////////////////////////////////////////////////////////////
  private PropertyCategory m_category;

  /**
   * @return current {@link PropertyCategory}.
   */
  public final PropertyCategory getCategory() {
    return m_category;
  }

  /**
   * Sets the {@link PropertyCategory} for this {@link Property}.
   */
  public final void setCategory(PropertyCategory category) {
    m_category = category;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Value
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * @return the current value of this {@link Property} or {@link #UNKNOWN_VALUE}.
   */
  public abstract Object getValue() throws Exception;

  /**
   * Sets the new value of this {@link Property}.
   *
   * @param the
   *          new value of {@link Property} or {@link #UNKNOWN_VALUE} if {@link Property}
   *          modification should be removed.
   */
  public abstract void setValue(Object value) throws Exception;

  ////////////////////////////////////////////////////////////////////////////
  //
  // Editor
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * @return the {@link PropertyEditor}.
   */
  public final PropertyEditor getEditor() {
    return m_editor;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Composite
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * @return the composite {@link Property} for given array of {@link Property}'s or
   *         <code>null</code> if no composite {@link Property} can be created.
   */
  public Property getComposite(Property[] properties) {
    return null;
  }

  public <T> T getAdapter(Class<T> adapter) {
    return null;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Arbitrary values map
  //
  ////////////////////////////////////////////////////////////////////////////
  private Map<Object, Object> m_arbitraryMap;

  /**
   * Associates the given value with the given key.
   */
  public final void putArbitraryValue(Object key, Object value) {
    if (m_arbitraryMap == null) {
      m_arbitraryMap = Maps.newHashMap();
    }
    m_arbitraryMap.put(key, value);
  }

  /**
   * @return the value to which the given key is mapped, or <code>null</code>.
   */
  public final Object getArbitraryValue(Object key) {
    if (m_arbitraryMap != null) {
      return m_arbitraryMap.get(key);
    }
    return null;
  }

  /**
   * Removes the mapping for a key.
   */
  public final void removeArbitraryValue(Object key) {
    if (m_arbitraryMap != null) {
      m_arbitraryMap.remove(key);
    }
  }

  // BEGIN ADT MODIFICATIONS

    /**
     * Returns the name of the property (which is not always the same as the
     * title; for example, the "maxWidth" property has title "Max Width" and
     * name "maxWidth".
     * <p>
     * This is shown in tooltips to users etc to make it clear what they should
     * use in their own code.
     *
     * @return the name of the property
     */
  public String getName() {
      return getTitle();
  }

  private int mPriority;

  /**
   * Gets the custom sort priority of this property
   *
   * @return the sort priority
   */
  public int getPriority() {
      return mPriority;
  }

  /**
   * Sets the custom sort priority of this property
   *
   * @param priority the new priority to use
   */
  public void setPriority(int priority) {
      this.mPriority = priority;
  }

  /** Sort {@link Property} instances alphabetically by property name */
  public static final Comparator<Property> ALPHABETICAL = new Comparator<Property>() {
      @Override
      public int compare(Property p1, Property p2) {
          return p1.getName().compareTo(p2.getName());
      }
  };

  /** Sort {@link Property} instances by priority */
  public static final Comparator<Property> PRIORITY = new Comparator<Property>() {
      @Override
      public int compare(Property p1, Property p2) {
          int delta = p1.mPriority - p2.mPriority;
          if (delta != 0) {
              return delta;
          }

          return p1.getName().compareTo(p2.getName());
      }
  };
  // END ADT MODIFICATIONS
}