KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > sourcelookup > SourceContainerAction


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 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.debug.internal.ui.sourcelookup;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.actions.SelectionListenerAction;
24
25 /**
26  * The abstract class for all source lookup actions.
27  *
28  * @since 3.0
29  */

30 public abstract class SourceContainerAction extends SelectionListenerAction {
31     //the viewer that the action is operating on
32
private SourceContainerViewer fViewer;
33     //the button that is used to invoke the action
34
private Button fButton;
35     //the shell used to realize this action's dialog (if any)
36
private Shell fShell;
37     
38     /**
39      * The constructor for the action
40      * @param label the label for the action's button
41      */

42     public SourceContainerAction(String JavaDoc label) {
43         super(label);
44     }
45     
46     /**
47      * Sets the viewer on which this action operates.
48      *
49      * @param viewer the viewer on which this action operates
50      */

51     public void setViewer(SourceContainerViewer viewer) {
52         if (fViewer != null) {
53             fViewer.removeSelectionChangedListener(this);
54         }
55         fViewer = viewer;
56         if (fViewer != null) {
57             fViewer.addSelectionChangedListener(this);
58             update();
59         }
60     }
61     
62     /**
63      * Returns the viewer on which this action operates.
64      *
65      * @return the viewer on which this action operates
66      */

67     protected SourceContainerViewer getViewer() {
68         return fViewer;
69     }
70     
71     /**
72      * Returns the selected items in the list, in the order they are
73      * displayed.
74      *
75      * @return targets for an action
76      */

77     protected List JavaDoc getOrderedSelection() {
78         List JavaDoc targets = new ArrayList JavaDoc();
79         List JavaDoc selection =
80             ((IStructuredSelection) getViewer().getSelection()).toList();
81         ISourceContainer[] entries = getViewer().getEntries();
82         for (int i = 0; i < entries.length; i++) {
83             ISourceContainer target = entries[i];
84             if (selection.contains(target)) {
85                 targets.add(target);
86             }
87         }
88         return targets;
89     }
90     
91     /**
92      * Returns a list (copy) of the entries in the viewer
93      */

94     protected List JavaDoc getEntriesAsList() {
95         ISourceContainer[] entries = getViewer().getEntries();
96         List JavaDoc list = new ArrayList JavaDoc(entries.length);
97         for (int i = 0; i < entries.length; i++) {
98             list.add(entries[i]);
99         }
100         return list;
101     }
102     
103     /**
104      * Updates the entries to the entries in the given list
105      */

106     protected void setEntries(List JavaDoc list) {
107         getViewer().setEntries(
108                 (ISourceContainer[]) list.toArray(new ISourceContainer[list.size()]));
109         // update all selection listeners
110
getViewer().setSelection(getViewer().getSelection());
111     }
112     
113     /**
114      * Returns whether the item at the given index in the list
115      * (visually) is selected.
116      */

117     protected boolean isIndexSelected(
118             IStructuredSelection selection,
119             int index) {
120         if (selection.isEmpty()) {
121             return false;
122         }
123         Iterator JavaDoc entries = selection.iterator();
124         List JavaDoc list = getEntriesAsList();
125         while (entries.hasNext()) {
126             Object JavaDoc next = entries.next();
127             if (list.indexOf(next) == index) {
128                 return true;
129             }
130         }
131         return false;
132     }
133     
134     /**
135      * Sets the button that invokes this action
136      */

137     public void setButton(Button button) {
138         fButton = button;
139         button.addSelectionListener(new SelectionAdapter() {
140             public void widgetSelected(SelectionEvent evt) {
141                 run();
142             }
143         });
144     }
145     
146     /**
147      * @see IAction#setEnabled(boolean)
148      */

149     public void setEnabled(boolean enabled) {
150         super.setEnabled(enabled);
151         if (fButton != null) {
152             fButton.setEnabled(enabled);
153         }
154     }
155     
156     /**
157      * Updates the enabled state.
158      */

159     protected void update() {
160         selectionChanged((IStructuredSelection) getViewer().getSelection());
161     }
162     
163     /**
164      * Returns the shell used to realize this action's dialog (if any).
165      */

166     protected Shell getShell() {
167         if (fShell == null) {
168             fShell = getViewer().getControl().getShell();
169         }
170         return fShell;
171     }
172     
173     /**
174      * Sets the shell used to realize this action's dialog (if any).
175      */

176     public void setShell(Shell shell) {
177         fShell = shell;
178     }
179     
180 }
181
Popular Tags