1/*******************************************************************************
2 * Copyright (c) 2011 Google, Inc.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *    Google, Inc. - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.wb.internal.core.model.property;
12
13import org.eclipse.wb.internal.core.model.property.editor.TextDisplayPropertyEditor;
14import org.eclipse.wb.internal.core.model.property.editor.complex.IComplexPropertyEditor;
15import org.eclipse.wb.internal.core.model.property.editor.presentation.PropertyEditorPresentation;
16import org.eclipse.wb.internal.core.model.property.table.PropertyTable;
17import org.eclipse.wb.internal.core.model.property.table.PropertyTooltipProvider;
18import org.eclipse.wb.internal.core.model.property.table.PropertyTooltipTextProvider;
19
20import org.eclipse.swt.graphics.Point;
21
22import java.util.List;
23
24/**
25 * Implementation of {@link Property} that shows given inner {@link Property}'s using
26 * {@link IComplexPropertyEditor}.
27 *
28 * @author scheglov_ke
29 * @coverage core.model.property
30 */
31public class ComplexProperty extends Property {
32  private final String m_title;
33  private String m_text;
34  private String m_tooltip;
35  private boolean m_modified;
36  private Property[] m_properties;
37
38  ////////////////////////////////////////////////////////////////////////////
39  //
40  // Constructors
41  //
42  ////////////////////////////////////////////////////////////////////////////
43  public ComplexProperty(String title, String text) {
44    this(title, text, new Property[0]);
45  }
46
47  public ComplexProperty(String title, String text, Property[] properties) {
48    super(new ComplexPropertyEditor());
49    m_title = title;
50    m_text = text;
51    setText(text);
52    setProperties(properties);
53  }
54
55  ////////////////////////////////////////////////////////////////////////////
56  //
57  // Access
58  //
59  ////////////////////////////////////////////////////////////////////////////
60  /**
61   * Sets the text.
62   */
63  public void setText(String text) {
64    m_text = text;
65  }
66
67  /**
68   * @return the text to display as value.
69   */
70  public String getText() throws Exception {
71    return m_text;
72  }
73
74  /**
75   * Sets the tooltip text.
76   */
77  public void setTooltip(String tooltip) {
78    m_tooltip = tooltip;
79  }
80
81  /**
82   * Specifies the {@link PropertyEditorPresentation}, for example to displaying "..." button.
83   */
84  public void setEditorPresentation(PropertyEditorPresentation presentation) {
85    ((ComplexPropertyEditor) getEditor()).m_presentation = presentation;
86  }
87
88  /**
89   * @return the sub-properties.
90   */
91  public Property[] getProperties() {
92    return m_properties;
93  }
94
95  /**
96   * Sets the sub-properties.
97   */
98  public void setProperties(Property[] properties) {
99    m_properties = properties;
100  }
101
102  /**
103   * Sets the sub-properties.
104   */
105  public void setProperties(List<Property> properties) {
106    Property[] propertiesArray = properties.toArray(new Property[properties.size()]);
107    setProperties(propertiesArray);
108  }
109
110  /**
111   * Sets the "modified" flag.
112   */
113  public void setModified(boolean modified) {
114    m_modified = modified;
115  }
116
117  ////////////////////////////////////////////////////////////////////////////
118  //
119  // Property
120  //
121  ////////////////////////////////////////////////////////////////////////////
122  @Override
123  public String getTitle() {
124    return m_title;
125  }
126
127  @Override
128  public boolean isModified() throws Exception {
129    return m_modified;
130  }
131
132  @Override
133  public Object getValue() throws Exception {
134    return null;
135  }
136
137  @Override
138  public void setValue(Object value) throws Exception {
139  }
140
141  ////////////////////////////////////////////////////////////////////////////
142  //
143  // Adapter
144  //
145  ////////////////////////////////////////////////////////////////////////////
146  @Override
147  public <T> T getAdapter(Class<T> adapter) {
148    if (adapter == PropertyTooltipProvider.class && m_tooltip != null) {
149      return adapter.cast(new PropertyTooltipTextProvider() {
150        @Override
151        protected String getText(Property property) throws Exception {
152          return m_tooltip;
153        }
154      });
155    }
156    return super.getAdapter(adapter);
157  }
158
159  ////////////////////////////////////////////////////////////////////////////
160  //
161  // ComplexPropertyEditor
162  //
163  ////////////////////////////////////////////////////////////////////////////
164  private static final class ComplexPropertyEditor extends TextDisplayPropertyEditor
165      implements
166        IComplexPropertyEditor {
167    private PropertyEditorPresentation m_presentation;
168
169    ////////////////////////////////////////////////////////////////////////////
170    //
171    // IComplexPropertyEditor
172    //
173    ////////////////////////////////////////////////////////////////////////////
174    public Property[] getProperties(Property property) throws Exception {
175      return ((ComplexProperty) property).getProperties();
176    }
177
178    ////////////////////////////////////////////////////////////////////////////
179    //
180    // TextDisplayPropertyEditor
181    //
182    ////////////////////////////////////////////////////////////////////////////
183    @Override
184    protected String getText(Property property) throws Exception {
185      return ((ComplexProperty) property).getText();
186    }
187
188    ////////////////////////////////////////////////////////////////////////////
189    //
190    // PropertyEditor
191    //
192    ////////////////////////////////////////////////////////////////////////////
193    @Override
194    public boolean activate(PropertyTable propertyTable, Property property, Point location)
195        throws Exception {
196      return false;
197    }
198
199    ////////////////////////////////////////////////////////////////////////////
200    //
201    // Presentation
202    //
203    ////////////////////////////////////////////////////////////////////////////
204    @Override
205    public PropertyEditorPresentation getPresentation() {
206      return m_presentation;
207    }
208  }
209}
210