1 /******************************************************************************* 2 * Copyright (c) 2000, 2015 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.jface.viewers; 15 16 import java.util.EventObject; 17 18 import org.eclipse.core.runtime.Assert; 19 20 /** 21 * Event object describing a selection change. The source of these 22 * events is a selection provider. 23 * 24 * @see ISelection 25 * @see ISelectionProvider 26 * @see ISelectionChangedListener 27 */ 28 public class SelectionChangedEvent extends EventObject { 29 30 /** 31 * Generated serial version UID for this class. 32 * @since 3.1 33 */ 34 private static final long serialVersionUID = 3835149545519723574L; 35 36 /** 37 * The selection. 38 */ 39 protected ISelection selection; 40 41 /** 42 * Creates a new event for the given source and selection. 43 * 44 * @param source the selection provider 45 * @param selection the selection 46 */ SelectionChangedEvent(ISelectionProvider source, ISelection selection)47 public SelectionChangedEvent(ISelectionProvider source, ISelection selection) { 48 super(source); 49 Assert.isNotNull(selection); 50 this.selection = selection; 51 } 52 53 /** 54 * Returns the selection. 55 * 56 * @return the selection 57 */ getSelection()58 public ISelection getSelection() { 59 return selection; 60 } 61 62 /** 63 * Returns the selection. 64 * 65 * @return IStructuredSelection 66 * @throws ClassCastException 67 * if the selection is not an instance of IStructuredSelection 68 * @since 3.13 69 */ getStructuredSelection()70 public IStructuredSelection getStructuredSelection() throws ClassCastException { 71 ISelection selection = getSelection(); 72 if (selection instanceof IStructuredSelection) { 73 return (IStructuredSelection) selection; 74 } 75 throw new ClassCastException( 76 "ISelection is not an instance of IStructuredSelection."); //$NON-NLS-1$ 77 } 78 79 /** 80 * Returns the selection provider that is the source of this event. 81 * 82 * @return the originating selection provider 83 */ getSelectionProvider()84 public ISelectionProvider getSelectionProvider() { 85 return (ISelectionProvider) getSource(); 86 } 87 } 88