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.editor;
12
13import org.eclipse.swt.graphics.GC;
14import org.eclipse.wb.internal.core.model.property.Property;
15import org.eclipse.wb.internal.core.model.property.table.PropertyTable;
16import org.eclipse.wb.internal.core.model.property.table.PropertyTooltipProvider;
17import org.eclipse.wb.internal.core.utils.ui.DrawUtils;
18
19/**
20 * Abstract {@link PropertyEditor} for displaying text as {@link Property} value in
21 * {@link PropertyTable}.
22 *
23 * @author scheglov_ke
24 * @coverage core.model.property.editor
25 */
26public abstract class TextDisplayPropertyEditor extends PropertyEditor {
27  ////////////////////////////////////////////////////////////////////////////
28  //
29  // Presentation
30  //
31  ////////////////////////////////////////////////////////////////////////////
32  @Override
33  public void paint(Property property, GC gc, int x, int y, int width, int height) throws Exception {
34    String text = getText(property);
35    if (text != null) {
36      DrawUtils.drawStringCV(gc, text, x, y, width, height);
37    }
38  }
39
40  /**
41   * @return the text for displaying value of given {@link Property} or <code>null</code> if value
42   *         of {@link Property} is unknown.
43   */
44  protected abstract String getText(Property property) throws Exception;
45
46  ////////////////////////////////////////////////////////////////////////////
47  //
48  // IAdaptable
49  //
50  ////////////////////////////////////////////////////////////////////////////
51  @Override
52  public <T> T getAdapter(Class<T> adapter) {
53    // tooltip for value text
54    if (adapter == PropertyTooltipProvider.class) {
55      return adapter.cast(createPropertyTooltipProvider());
56    }
57    return super.getAdapter(adapter);
58  }
59
60  /**
61   * @return the {@link PropertyTooltipProvider} to display value tooltip.
62   */
63  protected PropertyTooltipProvider createPropertyTooltipProvider() {
64    return null;
65  }
66}
67