diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/ModelElementLabelProvider.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/ModelElementLabelProvider.java index 14bf57e7bb7dfbb212bb01e23a8202b73a5df2bf..96c0225d1bf0f28f9f4da61dadec1cbfcc05a9ec 100644 --- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/ModelElementLabelProvider.java +++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/ModelElementLabelProvider.java @@ -24,11 +24,12 @@ import org.fortiss.tooling.kernel.ui.extension.IModelElementHandler; import org.fortiss.tooling.kernel.ui.service.IModelElementHandlerService; /** + * Label provider for model elements that have corresponding handlers. * * @author mou * @author $Author$ * @version $Rev$ - * @ConQAT.Rating RED Hash: + * @ConQAT.Rating GREEN Hash: 7C9C1135B33D4F3D58BEB826E63AB70A */ public class ModelElementLabelProvider extends LabelProvider { /** {@inheritDoc} */ diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/SelectionUtils.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/SelectionUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..76b9500559812dfb3b3dc08deb286dc64050d0e8 --- /dev/null +++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/SelectionUtils.java @@ -0,0 +1,74 @@ +/*--------------------------------------------------------------------------+ +$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $ +| | +| 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.kernel.ui.util; + +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionProvider; +import org.eclipse.jface.viewers.IStructuredSelection; + +/** + * Utility methods for dealing with selections. + * + * This class contains slight improvements with respect to + * {@link org.conqat.ide.commons.ui.selection.SelectionUtils}. + * + * @author ratiu + * @author $Author: hoelzl $ + * @version $Rev: 18709 $ + * @ConQAT.Rating YELLOW Hash: 9641C2CB35796514DC1145D3DA0570B5 + */ +public class SelectionUtils { + + /** + * For a selection provider object get the selected object, for simple + * selections a cast is performed to the desired type; for a structured + * selection returns the first element or <code>null</code> if the selection + * is empty. This return <code>null</code> if the provided selection is null + * or the element is not of the required type. + */ + public static <T> T checkAndPickFirst(ISelectionProvider selectionProvider, + Class<T> type) { + return checkAndPickFirst(selectionProvider.getSelection(), type); + } + + /** + * For simple selections a cast is performed to the desired type. For a + * structured selection returns the first element or <code>null</code> if + * the selection is empty. This return <code>null</code> if the provided + * selection is null or the element is not of the required type. + */ + @SuppressWarnings("unchecked") + public static <T> T checkAndPickFirst(ISelection selection, Class<T> type) { + Object selectedElement; + + if (selection == null || selection.isEmpty()) + return null; + + if (!(selection instanceof IStructuredSelection)) { + selectedElement = selection; + } else { + IStructuredSelection structuredSelection = (IStructuredSelection) selection; + selectedElement = structuredSelection.getFirstElement(); + } + + if (!type.isInstance(selectedElement)) { + return null; + } + return (T) selectedElement; + } +}