KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > SelectionProviderAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.actions;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.ISelectionChangedListener;
16 import org.eclipse.jface.viewers.ISelectionProvider;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.viewers.SelectionChangedEvent;
19 import org.eclipse.jface.viewers.StructuredSelection;
20
21 /**
22  * The abstract superclass for actions that listen to selection changes
23  * from a particular selection provider. This implementation splits the current
24  * selection along structured/unstructured lines, providing a convenient place
25  * to monitor selection changes that require adjusting action state.
26  * <p>
27  * Subclasses must implement the following <code>IAction</code> method:
28  * <ul>
29  * <li><code>run</code> - to do the action's work</li>
30  * </ul>
31  * </p>
32  * <p>
33  * Subclasses may reimplement either of the following methods:
34  * <ul>
35  * <li><code>selectionChanged(IStructuredSelection)</code></li>
36  * <li><code>selectionChanged(ISelection)</code></li>
37  * </ul>
38  * </p>
39  */

40 public abstract class SelectionProviderAction extends Action implements
41         ISelectionChangedListener {
42
43     /**
44      * The selection provider that is the target of this action.
45      */

46     private ISelectionProvider provider;
47
48     /**
49      * Creates a new action with the given text that monitors selection changes
50      * within the given selection provider.
51      * The resulting action is added as a listener on the selection provider.
52      *
53      * @param provider the selection provider that will provide selection notification
54      * @param text the string used as the text for the action,
55      * or <code>null</code> if there is no text
56      */

57     protected SelectionProviderAction(ISelectionProvider provider, String JavaDoc text) {
58         super(text);
59         this.provider = provider;
60         provider.addSelectionChangedListener(this);
61     }
62
63     /**
64      * Disposes this action by removing it as a listener from the selection provider.
65      * This must be called by the creator of the action when the action is no longer needed.
66      */

67     public void dispose() {
68         provider.removeSelectionChangedListener(this);
69     }
70
71     /**
72      * Returns the current selection in the selection provider.
73      *
74      * @return the current selection in the selection provider
75      */

76     public ISelection getSelection() {
77         return provider.getSelection();
78     }
79
80     /**
81      * Returns the selection provider that is the target of this action.
82      *
83      * @return the target selection provider of this action
84      */

85     public ISelectionProvider getSelectionProvider() {
86         return provider;
87     }
88
89     /**
90      * Returns the current structured selection in the selection provider, or an
91      * empty selection if nothing is selected or if selection does not include
92      * objects (for example, raw text).
93      *
94      * @return the current structured selection in the selection provider
95      */

96     public IStructuredSelection getStructuredSelection() {
97         ISelection selection = provider.getSelection();
98         if (selection instanceof IStructuredSelection) {
99             return (IStructuredSelection) selection;
100         } else {
101             return new StructuredSelection();
102         }
103     }
104
105     /**
106      * Notifies this action that the given (non-structured) selection has changed
107      * in the selection provider.
108      * <p>
109      * The <code>SelectionProviderAction</code> implementation of this method
110      * does nothing. Subclasses may reimplement to react to this selection change.
111      * </p>
112      *
113      * @param selection the new selection
114      */

115     public void selectionChanged(ISelection selection) {
116     }
117
118     /**
119      * Notifies this action that the given structured selection has changed
120      * in the selection provider.
121      * <p>
122      * The <code>SelectionProviderAction</code> implementation of this method
123      * does nothing. Subclasses may reimplement to react to this selection change.
124      * </p>
125      *
126      * @param selection the new selection
127      */

128     public void selectionChanged(IStructuredSelection selection) {
129         // Hook in subclass.
130
}
131
132     /**
133      * The <code>SelectionProviderAction</code> implementation of this
134      * <code>ISelectionChangedListener</code> method calls
135      * <code>selectionChanged(IStructuredSelection)</code> if the selection is
136      * a structured selection but <code>selectionChanged(ISelection)</code> if it is
137      * not. Subclasses should override either of those methods method to react to
138      * selection changes.
139      */

140     public final void selectionChanged(SelectionChangedEvent event) {
141         ISelection selection = event.getSelection();
142         if (selection instanceof IStructuredSelection) {
143             selectionChanged((IStructuredSelection) selection);
144         } else {
145             selectionChanged(selection);
146         }
147     }
148 }
149
Popular Tags