diff options
Diffstat (limited to 'propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation')
5 files changed, 506 insertions, 0 deletions
diff --git a/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/ButtonPropertyEditorPresentation.java b/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/ButtonPropertyEditorPresentation.java new file mode 100644 index 0000000..e33970d --- /dev/null +++ b/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/ButtonPropertyEditorPresentation.java @@ -0,0 +1,114 @@ +/******************************************************************************* + * 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.editor.presentation; + +import org.eclipse.wb.internal.core.DesignerPlugin; +import org.eclipse.wb.internal.core.EnvironmentUtils; +import org.eclipse.wb.internal.core.model.property.Property; +import org.eclipse.wb.internal.core.model.property.table.PropertyTable; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.widgets.Button; + +/** + * Implementation of {@link PropertyEditorPresentation} for displaying {@link Button}. + * + * @author scheglov_ke + * @author mitin_aa + * @coverage core.model.property.editor + */ +public abstract class ButtonPropertyEditorPresentation extends PropertyEditorPresentation { + private final int m_style; + private final ButtonPropertyEditorPresentationImpl m_impl; + + //////////////////////////////////////////////////////////////////////////// + // + // Constructors + // + //////////////////////////////////////////////////////////////////////////// + public ButtonPropertyEditorPresentation() { + this(SWT.NONE); + } + + public ButtonPropertyEditorPresentation(int style) { + m_style = style; + m_impl = + EnvironmentUtils.IS_MAC + ? new ButtonPropertyEditorPresentationImplMac(this) + : new ButtonPropertyEditorPresentationImpl(this); + } + + //////////////////////////////////////////////////////////////////////////// + // + // Access + // + //////////////////////////////////////////////////////////////////////////// + /** + * Sets "selection" property of {@link Button}. + */ + public final void setSelection(PropertyTable propertyTable, Property property, boolean selected) { + m_impl.setSelection(propertyTable, property, selected); + } + + //////////////////////////////////////////////////////////////////////////// + // + // PropertyEditorPresentation + // + //////////////////////////////////////////////////////////////////////////// + @Override + public final int show(final PropertyTable propertyTable, + final Property property, + final int x, + final int y, + final int width, + final int height) { + return m_impl.show(propertyTable, property, x, y, width, height); + } + + @Override + public final void hide(PropertyTable propertyTable, Property property) { + m_impl.hide(propertyTable, property); + } + + //////////////////////////////////////////////////////////////////////////// + // + // Access + // + //////////////////////////////////////////////////////////////////////////// + final int getStyle() { + return m_style; + } + + //////////////////////////////////////////////////////////////////////////// + // + // Implementation + // + //////////////////////////////////////////////////////////////////////////// + /** + * @return the {@link Image} to display on {@link Button}. + */ + protected Image getImage() { + return DesignerPlugin.getImage("properties/dots.gif"); + } + + /** + * @return the tooltip text to display for {@link Button}. + */ + protected String getTooltip() { + return null; + } + + /** + * Handles click on {@link Button}. + */ + protected abstract void onClick(PropertyTable propertyTable, Property property) throws Exception; +} diff --git a/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/ButtonPropertyEditorPresentationImpl.java b/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/ButtonPropertyEditorPresentationImpl.java new file mode 100644 index 0000000..209bf39 --- /dev/null +++ b/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/ButtonPropertyEditorPresentationImpl.java @@ -0,0 +1,224 @@ +/******************************************************************************* + * 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.editor.presentation; + +import com.google.common.collect.Maps; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Listener; +import org.eclipse.wb.internal.core.model.property.Property; +import org.eclipse.wb.internal.core.model.property.table.PropertyTable; +import org.eclipse.wb.internal.core.utils.Pair; + +import java.util.Map; + +/** + * Internal implementation of {@link PropertyEditorPresentation} for displaying {@link Button}. + * + * @author scheglov_ke + * @author mitin_aa + * @coverage core.model.property.editor + */ +class ButtonPropertyEditorPresentationImpl extends PropertyEditorPresentation { + protected final PropertyToControlMap m_propertyToControl = new PropertyToControlMap(); + private final ButtonPropertyEditorPresentation m_presentation; + + //////////////////////////////////////////////////////////////////////////// + // + // Constructor + // + //////////////////////////////////////////////////////////////////////////// + public ButtonPropertyEditorPresentationImpl(ButtonPropertyEditorPresentation presentation) { + m_presentation = presentation; + } + + //////////////////////////////////////////////////////////////////////////// + // + // PropertyEditorPresentation + // + //////////////////////////////////////////////////////////////////////////// + @Override + public final void hide(PropertyTable propertyTable, Property property) { + Control control = m_propertyToControl.remove(propertyTable, property); + if (control != null) { + control.dispose(); + } + } + + @Override + public final int show(PropertyTable propertyTable, + Property property, + int x, + int y, + int width, + int height) { + // prepare control + Control control = m_propertyToControl.get(propertyTable, property); + if (control == null) { + control = createControl(propertyTable, property); + } + // set bounds + final int controlWidth = height; + final int controlX = x + width - controlWidth; + setBounds(control, controlX, y, controlWidth, height); + return controlWidth; + } + + /** + * Finds and select the appropriate {@link Control} belonging to given property. + */ + public void setSelection(PropertyTable propertyTable, Property property, boolean selected) { + Button button = (Button) m_propertyToControl.get(propertyTable, property); + if (button != null) { + button.setSelection(selected); + } + } + + //////////////////////////////////////////////////////////////////////////// + // + // Control + // + //////////////////////////////////////////////////////////////////////////// + /** + * Creates the control for given property and initializes newly created control. + */ + private Control createControl(final PropertyTable propertyTable, final Property property) { + Control control = createControlImpl(propertyTable, property); + m_propertyToControl.put(propertyTable, property, control); + // when Control disposed, remove Control/Property from map to avoid memory leak + control.addListener(SWT.Dispose, new Listener() { + @Override + public void handleEvent(Event e) { + m_propertyToControl.remove(propertyTable, property); + } + }); + // activate property on mouse down + control.addListener(SWT.MouseDown, new Listener() { + @Override + public void handleEvent(Event event) { + propertyTable.deactivateEditor(true); + propertyTable.setActiveProperty(property); + } + }); + // return focus on propertyTable after click + control.addListener(SWT.MouseUp, new Listener() { + @Override + public void handleEvent(Event event) { + propertyTable.forceFocus(); + } + }); + // handle selection + control.addListener(SWT.Selection, new Listener() { + @Override + public void handleEvent(Event event) { + try { + getPresentation().onClick(propertyTable, property); + } catch (Throwable e) { + propertyTable.deactivateEditor(false); + propertyTable.handleException(e); + } + } + }); + return control; + } + + /** + * Creates the {@link Control} instance. By default, {@link Button} instance created. + */ + protected Control createControlImpl(final PropertyTable propertyTable, final Property property) { + Button button = new Button(propertyTable, getPresentation().getStyle()); + button.setImage(getPresentation().getImage()); + button.setToolTipText(getPresentation().getTooltip()); + return button; + } + + //////////////////////////////////////////////////////////////////////////// + // + // Access + // + //////////////////////////////////////////////////////////////////////////// + /** + * @return the 'parent' presentation. Internal usage only. + */ + protected final ButtonPropertyEditorPresentation getPresentation() { + return m_presentation; + } + + //////////////////////////////////////////////////////////////////////////// + // + // Utils + // + //////////////////////////////////////////////////////////////////////////// + /** + * Sets new bounds for {@link Control}, optimizing when possible. + */ + private static void setBounds(Control control, int newX, int newY, int newWidth, int newHeight) { + // check, may be Control is invisible, so no reason to change bounds + { + // is in negative zone + if (newY + newHeight < 0) { + control.setVisible(false); + return; + } + // is out of client area height + Rectangle parentArea = control.getParent().getClientArea(); + if (newY > parentArea.height) { + control.setVisible(false); + return; + } + } + // well, now we sure that Control is visible + if (!control.getVisible()) { + control.setVisible(true); + } + // prepare old size, remember new + Integer oldWidthObject = (Integer) control.getData("oldWidth"); + Integer oldHeightObject = (Integer) control.getData("oldHeight"); + control.setData("oldWidth", newWidth); + control.setData("oldHeight", newHeight); + // check, may be same size + if (oldWidthObject != null) { + int oldWidth = oldWidthObject.intValue(); + int oldHeight = oldHeightObject.intValue(); + if (oldWidth == newWidth && oldHeight == newHeight) { + control.setLocation(newX, newY); + return; + } + } + // no any optimization possible, just set bounds + control.setBounds(newX, newY, newWidth, newHeight); + } + + //////////////////////////////////////////////////////////////////////////// + // + // Controls map + // + //////////////////////////////////////////////////////////////////////////// + protected static final class PropertyToControlMap { + private final Map<Pair<PropertyTable, Property>, Control> m_map = Maps.newHashMap(); + + void put(PropertyTable propertyTable, Property property, Control control) { + m_map.put(Pair.create(propertyTable, property), control); + } + + Control remove(PropertyTable propertyTable, Property property) { + return m_map.remove(Pair.create(propertyTable, property)); + } + + Control get(PropertyTable propertyTable, Property property) { + return m_map.get(Pair.create(propertyTable, property)); + } + } +} diff --git a/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/ButtonPropertyEditorPresentationImplMac.java b/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/ButtonPropertyEditorPresentationImplMac.java new file mode 100644 index 0000000..d61a606 --- /dev/null +++ b/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/ButtonPropertyEditorPresentationImplMac.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * 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.editor.presentation; + +import org.eclipse.wb.core.controls.CFlatButton; +import org.eclipse.wb.internal.core.model.property.Property; +import org.eclipse.wb.internal.core.model.property.table.PropertyTable; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Control; + +/** + * Internal implementation of {@link PropertyEditorPresentation} for displaying special owner-draw + * button for Mac OSX. + * + * @author mitin_aa + * @coverage core.model.property.editor + */ +final class ButtonPropertyEditorPresentationImplMac extends ButtonPropertyEditorPresentationImpl { + //////////////////////////////////////////////////////////////////////////// + // + // Constructor + // + //////////////////////////////////////////////////////////////////////////// + public ButtonPropertyEditorPresentationImplMac(ButtonPropertyEditorPresentation presentation) { + super(presentation); + } + + //////////////////////////////////////////////////////////////////////////// + // + // Control + // + //////////////////////////////////////////////////////////////////////////// + @Override + protected final Control createControlImpl(final PropertyTable propertyTable, Property property) { + CFlatButton button = new CFlatButton(propertyTable, SWT.NONE); + button.setImage(getPresentation().getImage()); + button.setToolTipText(getPresentation().getTooltip()); + return button; + } + + @Override + public final void setSelection(PropertyTable propertyTable, Property property, boolean selected) { + CFlatButton button = (CFlatButton) m_propertyToControl.get(propertyTable, property); + if (button != null) { + button.setSelected(selected); + } + } +} diff --git a/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/CompoundPropertyEditorPresentation.java b/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/CompoundPropertyEditorPresentation.java new file mode 100644 index 0000000..39bfa7a --- /dev/null +++ b/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/CompoundPropertyEditorPresentation.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * 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.editor.presentation; + +import com.google.common.collect.Lists; + +import org.eclipse.wb.internal.core.model.property.Property; +import org.eclipse.wb.internal.core.model.property.table.PropertyTable; + +import java.util.List; + +/** + * Implementation of {@link PropertyEditorPresentation} that contains zero or more other + * {@link PropertyEditorPresentation}'s. + * + * @author scheglov_ke + * @coverage core.model.property.editor + */ +public class CompoundPropertyEditorPresentation extends PropertyEditorPresentation { + private final List<PropertyEditorPresentation> m_presentations = Lists.newArrayList(); + + //////////////////////////////////////////////////////////////////////////// + // + // Access + // + //////////////////////////////////////////////////////////////////////////// + /** + * Adds child {@link PropertyEditorPresentation}.<br> + * Child {@link PropertyEditorPresentation}'s are displayed from right to left. + */ + public void add(PropertyEditorPresentation presentation) { + m_presentations.add(presentation); + } + + //////////////////////////////////////////////////////////////////////////// + // + // PropertyEditorPresentation + // + //////////////////////////////////////////////////////////////////////////// + @Override + public int show(PropertyTable propertyTable, + Property property, + int x, + int y, + int width, + int height) { + int sumWidth = 0; + for (PropertyEditorPresentation presentation : m_presentations) { + int presentationWidth = presentation.show(propertyTable, property, x, y, width, height); + sumWidth += presentationWidth; + width -= presentationWidth; + } + return sumWidth; + } + + @Override + public void hide(PropertyTable propertyTable, Property property) { + for (PropertyEditorPresentation presentation : m_presentations) { + presentation.hide(propertyTable, property); + } + } +} diff --git a/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/PropertyEditorPresentation.java b/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/PropertyEditorPresentation.java new file mode 100644 index 0000000..843338e --- /dev/null +++ b/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/presentation/PropertyEditorPresentation.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * 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.editor.presentation; + +import org.eclipse.wb.internal.core.model.property.Property; +import org.eclipse.wb.internal.core.model.property.editor.PropertyEditor; +import org.eclipse.wb.internal.core.model.property.table.PropertyTable; + +/** + * Implementations of {@link PropertyEditorPresentation} are used to show some presentation for + * visible, but not activated yet {@link PropertyEditor}. + * + * @author scheglov_ke + * @coverage core.model.property.editor + */ +public abstract class PropertyEditorPresentation { + /** + * Shows presentation for given {@link Property}. + * + * @return the width that this presentation occupies on the right of given rectangle. + */ + public abstract int show(PropertyTable propertyTable, + Property property, + int x, + int y, + int width, + int height); + + /** + * Hides presentation. + */ + public abstract void hide(PropertyTable propertyTable, Property property); +} |