KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
14
15 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
16 import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
17 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
18 import org.eclipse.debug.ui.DebugUITools;
19 import org.eclipse.debug.ui.sourcelookup.ISourceContainerBrowser;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21
22 /**
23  * Action used to edit source containers on a source lookup path
24  */

25 public class EditContainerAction extends SourceContainerAction {
26     
27     private ISourceLookupDirector fDirector;
28     private ISourceContainer[] fContainers;
29     private ISourceContainerBrowser fBrowser;
30     
31     public EditContainerAction() {
32         super(SourceLookupUIMessages.EditContainerAction_0);
33     }
34     
35     /**
36      * Prompts for a project to add.
37      *
38      * @see org.eclipse.jface.action.IAction#run()
39      */

40     public void run() {
41         ISourceContainer[] replacements = fBrowser.editSourceContainers(getShell(), fDirector, fContainers);
42         int j = 0;
43         ISourceContainer[] existing = getViewer().getEntries();
44         for (int i = 0; i < existing.length && j < replacements.length; i++) {
45             ISourceContainer toBeReplaced = fContainers[j];
46             ISourceContainer container = existing[i];
47             if (container.equals(toBeReplaced)) {
48                 existing[i] = replacements[j];
49                 j++;
50             }
51         }
52         getViewer().setEntries(existing);
53     }
54     
55     public void setSourceLookupDirector(ISourceLookupDirector director) {
56         fDirector = director;
57     }
58     
59     /* (non-Javadoc)
60      * @see org.eclipse.ui.actions.BaseSelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
61      */

62     protected boolean updateSelection(IStructuredSelection selection) {
63         if(selection == null || selection.isEmpty()) {
64             return false;
65         }
66         if (getViewer().getTree().getSelection()[0].getParentItem()==null) {
67             // can only edit top level items of same type
68
fContainers = new ISourceContainer[selection.size()];
69             Iterator JavaDoc iterator = selection.iterator();
70             ISourceContainer container = (ISourceContainer) iterator.next();
71             ISourceContainerType type = container.getType();
72             fContainers[0] = container;
73             int i = 1;
74             while (iterator.hasNext()) {
75                 container = (ISourceContainer) iterator.next();
76                 fContainers[i] = container;
77                 i++;
78                 if (!container.getType().equals(type)) {
79                     return false;
80                 }
81             }
82             // all the same type, see if editing is supported
83
fBrowser = DebugUITools.getSourceContainerBrowser(type.getId());
84             if (fBrowser != null) {
85                 return fBrowser.canEditSourceContainers(fDirector, fContainers);
86             }
87         }
88         return false;
89     }
90 }
91
Popular Tags