From 661a23a46674f7b85139bc7bf408dd344fcb5d6e Mon Sep 17 00:00:00 2001 From: Florian Hoelzl <hoelzl@fortiss.org> Date: Sun, 1 Jan 2012 15:52:09 +0000 Subject: [PATCH] added hold checkbox in input/output view refs 425 --- .../trunk/META-INF/MANIFEST.MF | 1 + .../CheckBoxEditingSupport.java | 79 ++++++++++++ .../CheckBoxLabelProvider.java | 117 ++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/checkboxtablecell/CheckBoxEditingSupport.java create mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/checkboxtablecell/CheckBoxLabelProvider.java diff --git a/org.fortiss.tooling.base.ui/trunk/META-INF/MANIFEST.MF b/org.fortiss.tooling.base.ui/trunk/META-INF/MANIFEST.MF index d99161f76..7f35acd96 100644 --- a/org.fortiss.tooling.base.ui/trunk/META-INF/MANIFEST.MF +++ b/org.fortiss.tooling.base.ui/trunk/META-INF/MANIFEST.MF @@ -11,6 +11,7 @@ Bundle-ActivationPolicy: lazy Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Bundle-Vendor: fortiss GmbH Export-Package: org.fortiss.tooling.base.ui, + org.fortiss.tooling.base.ui.checkboxtablecell, org.fortiss.tooling.base.ui.compose, org.fortiss.tooling.base.ui.contentprovider, org.fortiss.tooling.base.ui.databinding, diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/checkboxtablecell/CheckBoxEditingSupport.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/checkboxtablecell/CheckBoxEditingSupport.java new file mode 100644 index 000000000..0f9cfbbe7 --- /dev/null +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/checkboxtablecell/CheckBoxEditingSupport.java @@ -0,0 +1,79 @@ +/*--------------------------------------------------------------------------+ +$Id$ +| | +| Copyright 2011 ForTISS GmbH | +| | +| Licensed under the Apache License, Version 2.0 (the "License"); | +| you may not use this file except in compliance with the License. | +| You may obtain a copy of the License at | +| | +| http://www.apache.org/licenses/LICENSE-2.0 | +| | +| Unless required by applicable law or agreed to in writing, software | +| distributed under the License is distributed on an "AS IS" BASIS, | +| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | +| See the License for the specific language governing permissions and | +| limitations under the License. | ++--------------------------------------------------------------------------*/ +package org.fortiss.tooling.base.ui.checkboxtablecell; + +import org.eclipse.jface.viewers.CellEditor; +import org.eclipse.jface.viewers.CheckboxCellEditor; +import org.eclipse.jface.viewers.ColumnViewer; +import org.eclipse.jface.viewers.EditingSupport; +import org.eclipse.jface.viewers.TableViewer; + +/** + * {@link EditingSupport} for checkbox cells. + * + * @author hoelzl + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating YELLOW Hash: A39EE0F9184DD86FD5FC5D46A164976A + */ +public abstract class CheckBoxEditingSupport extends EditingSupport { + + /** + * Stores the shared cell editor. + */ + private CellEditor cellEditor; + + /** Constructor */ + public CheckBoxEditingSupport(ColumnViewer viewer) { + super(viewer); + cellEditor = new CheckboxCellEditor(((TableViewer) viewer).getTable()); + } + + /** {@inheritDoc} */ + @Override + protected boolean canEdit(Object element) { + return true; + } + + /** {@inheritDoc} */ + @Override + protected final CellEditor getCellEditor(Object element) { + return cellEditor; + } + + /** {@inheritDoc} */ + @Override + protected final Object getValue(Object element) { + return getBooleanValue(element); + } + + /** Get the boolean value of this editor. */ + protected abstract Boolean getBooleanValue(Object element); + + /** {@inheritDoc} */ + @Override + protected final void setValue(Object element, Object value) { + if (value instanceof Boolean) { + setBooleanValue(element, (Boolean) value); + getViewer().update(element, null); + } + } + + /** Sets the boolean value to the model. */ + protected abstract void setBooleanValue(Object element, Boolean value); +} diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/checkboxtablecell/CheckBoxLabelProvider.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/checkboxtablecell/CheckBoxLabelProvider.java new file mode 100644 index 000000000..63584260a --- /dev/null +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/checkboxtablecell/CheckBoxLabelProvider.java @@ -0,0 +1,117 @@ +/*--------------------------------------------------------------------------+ +$Id$ +| | +| Copyright 2011 ForTISS GmbH | +| | +| Licensed under the Apache License, Version 2.0 (the "License"); | +| you may not use this file except in compliance with the License. | +| You may obtain a copy of the License at | +| | +| http://www.apache.org/licenses/LICENSE-2.0 | +| | +| Unless required by applicable law or agreed to in writing, software | +| distributed under the License is distributed on an "AS IS" BASIS, | +| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | +| See the License for the specific language governing permissions and | +| limitations under the License. | ++--------------------------------------------------------------------------*/ +package org.fortiss.tooling.base.ui.checkboxtablecell; + +import static org.eclipse.jface.resource.JFaceResources.getImageRegistry; +import static org.eclipse.swt.SWT.CHECK; +import static org.eclipse.swt.SWT.DEFAULT; +import static org.eclipse.swt.SWT.NO_TRIM; + +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.jface.viewers.ColumnLabelProvider; +import org.eclipse.jface.viewers.ColumnViewer; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Shell; + +/** + * Checkbox label provider using checkbox button images as representation. This + * class supports checked and enabled state. + * + * @author hoelzl + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating YELLOW Hash: BDA0B84E86A88AC31CFF2B2C1B367A98 + */ +public abstract class CheckBoxLabelProvider extends ColumnLabelProvider { + /** {@link JFaceResources} id for checked image. */ + private static final String CHECKED_KEY = "CHECKED"; + + /** {@link JFaceResources} id for unchecked image. */ + private static final String UNCHECKED_KEY = "UNCHECKED"; + + /** {@link JFaceResources} id for disabled, unchecked image. */ + private static final String DISABLED_UNCHECKED_KEY = "DISABLED_UNCHECKED"; + + /** {@link JFaceResources} id for disabled, checked image. */ + private static final String DISABLED_CHECKED_KEY = "DISABLED_CHECKED"; + + /** Constructor. */ + public CheckBoxLabelProvider(ColumnViewer viewer) { + if (getImageRegistry().getDescriptor(CHECKED_KEY) == null) { + getImageRegistry().put(UNCHECKED_KEY, + makeShot(viewer.getControl().getShell(), false, true)); + getImageRegistry().put(CHECKED_KEY, + makeShot(viewer.getControl().getShell(), true, true)); + getImageRegistry().put(DISABLED_UNCHECKED_KEY, + makeShot(viewer.getControl().getShell(), false, false)); + getImageRegistry().put(DISABLED_CHECKED_KEY, + makeShot(viewer.getControl().getShell(), true, false)); + } + } + + /** Creates an image of checkbox SWT control. */ + private Image makeShot(Shell shell, boolean checked, boolean enabled) { + Shell s = new Shell(shell, NO_TRIM); + Button b = new Button(s, CHECK); + b.setSelection(checked); + b.setEnabled(enabled); + Point bsize = b.computeSize(DEFAULT, DEFAULT); + b.setSize(bsize); + b.setLocation(0, 0); + s.setSize(bsize); + s.open(); + + GC gc = new GC(b); + Image image = new Image(shell.getDisplay(), bsize.x, bsize.y); + gc.copyArea(image, 0, 0); + gc.dispose(); + s.close(); + return image; + } + + /** {@inheritDoc} */ + @Override + public Image getImage(Object element) { + if (isEnabled(element)) { + if (isChecked(element)) { + return getImageRegistry().get(CHECKED_KEY); + } + return getImageRegistry().get(UNCHECKED_KEY); + } + if (isChecked(element)) { + return getImageRegistry().get(DISABLED_CHECKED_KEY); + } + return getImageRegistry().get(DISABLED_UNCHECKED_KEY); + + } + + /** {@inheritDoc} */ + @Override + public String getText(Object element) { + return ""; + } + + /** Tests the model objects checked state. */ + protected abstract boolean isChecked(Object element); + + /** Tests the model objects enabled state. */ + protected abstract boolean isEnabled(Object element); +} \ No newline at end of file -- GitLab