KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > RuntimeClasspathAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.debug.ui.actions;
12
13  
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer;
19 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.actions.SelectionListenerAction;
26
27 /**
28  * Action used with a runtime classpath viewer.
29  */

30 public abstract class RuntimeClasspathAction extends SelectionListenerAction {
31     
32     public static final int DEFAULT= 0;
33     public static final int ADD= 1;
34     public static final int REMOVE= 2;
35     public static final int MOVE= 3;
36     
37     private IClasspathViewer fViewer;
38     private Button fButton;
39     private Shell fShell;
40     
41     public RuntimeClasspathAction(String JavaDoc label, IClasspathViewer viewer) {
42         super(label);
43         setViewer(viewer);
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(IClasspathViewer 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 IClasspathViewer 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 = ((IStructuredSelection)getViewer().getSelection()).toList();
80         IRuntimeClasspathEntry[] entries = getViewer().getEntries();
81         for (int i = 0; i < entries.length; i++) {
82             IRuntimeClasspathEntry target = entries[i];
83             if (selection.contains(target)) {
84                 targets.add(target);
85             }
86         }
87         return targets;
88     }
89     
90     /**
91      * Returns a list (copy) of the entries in the viewer
92      */

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

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

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

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

145     public void setEnabled(boolean enabled) {
146         super.setEnabled(enabled);
147         if (fButton != null) {
148             fButton.setEnabled(enabled);
149         }
150     }
151
152     /**
153      * Updates the enabled state.
154      */

155     protected void update() {
156         selectionChanged((IStructuredSelection)getViewer().getSelection());
157     }
158     
159     /**
160      * Returns the shell used to realize this action's dialog (if any).
161      */

162     protected Shell getShell() {
163         if (fShell == null) {
164             fShell= getViewer().getShell();
165         }
166         return fShell;
167     }
168     
169     /**
170      * Sets the shell used to realize this action's dialog (if any).
171      */

172     public void setShell(Shell shell) {
173         fShell= shell;
174     }
175     
176     
177     /* (non-Javadoc)
178      * @see org.eclipse.ui.actions.SelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
179      */

180     protected boolean updateSelection(IStructuredSelection selection) {
181         return getViewer().updateSelection(getActionType(), selection);
182     }
183
184
185     protected int getActionType() {
186         return DEFAULT;
187     }
188 }
189
Popular Tags