KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > preferences > TargetSourceTab


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.pde.internal.ui.preferences;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.core.runtime.Preferences;
19 import org.eclipse.jface.dialogs.Dialog;
20 import org.eclipse.jface.dialogs.DialogPage;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.jface.viewers.ISelectionChangedListener;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.viewers.ITreeContentProvider;
25 import org.eclipse.jface.viewers.LabelProvider;
26 import org.eclipse.jface.viewers.SelectionChangedEvent;
27 import org.eclipse.jface.viewers.StructuredSelection;
28 import org.eclipse.jface.viewers.TreeViewer;
29 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
30 import org.eclipse.pde.core.plugin.IPluginModelBase;
31 import org.eclipse.pde.internal.core.ICoreConstants;
32 import org.eclipse.pde.internal.core.PDECore;
33 import org.eclipse.pde.internal.core.PDEStateHelper;
34 import org.eclipse.pde.internal.core.SourceLocation;
35 import org.eclipse.pde.internal.core.SourceLocationManager;
36 import org.eclipse.pde.internal.core.itarget.ITarget;
37 import org.eclipse.pde.internal.ui.IHelpContextIds;
38 import org.eclipse.pde.internal.ui.PDEPluginImages;
39 import org.eclipse.pde.internal.ui.PDEUIMessages;
40 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
41 import org.eclipse.pde.internal.ui.search.ShowDescriptionAction;
42 import org.eclipse.pde.internal.ui.util.OverlayIcon;
43 import org.eclipse.pde.internal.ui.util.SWTUtil;
44 import org.eclipse.swt.SWT;
45 import org.eclipse.swt.events.KeyAdapter;
46 import org.eclipse.swt.events.KeyEvent;
47 import org.eclipse.swt.events.SelectionAdapter;
48 import org.eclipse.swt.events.SelectionEvent;
49 import org.eclipse.swt.graphics.Image;
50 import org.eclipse.swt.layout.GridData;
51 import org.eclipse.swt.layout.GridLayout;
52 import org.eclipse.swt.widgets.Button;
53 import org.eclipse.swt.widgets.Composite;
54 import org.eclipse.swt.widgets.Control;
55 import org.eclipse.swt.widgets.DirectoryDialog;
56 import org.eclipse.swt.widgets.Link;
57 import org.eclipse.ui.ISharedImages;
58 import org.eclipse.ui.PlatformUI;
59
60
61 public class TargetSourceTab {
62     private Image fFolderImage;
63     private TreeViewer fTreeViewer;
64     private Image fExtensionImage;
65     private Image fUserImage;
66     
67     private SourceLocation[] fExtensionLocations = new SourceLocation[0];
68     private ArrayList JavaDoc fUserLocations = new ArrayList JavaDoc();
69     private NamedElement fSystemNode;
70     private NamedElement fUserNode;
71     private Button fAddButton;
72     private Button fRemoveButton;
73     private String JavaDoc fLastUserPath = null;
74     
75     private DialogPage fPage = null;
76
77     class NamedElement {
78         String JavaDoc text;
79         public NamedElement(String JavaDoc text) {
80             this.text = text;
81         }
82         public String JavaDoc toString() {
83             return text;
84         }
85     }
86
87     class SourceProvider extends DefaultContentProvider implements ITreeContentProvider {
88         public Object JavaDoc[] getElements(Object JavaDoc input) {
89             return new Object JavaDoc[] {fSystemNode, fUserNode};
90         }
91
92         public Object JavaDoc[] getChildren(Object JavaDoc element) {
93             if (element.equals(fUserNode))
94                 return fUserLocations.toArray();
95             if (element.equals(fSystemNode))
96                 return fExtensionLocations;
97             return new Object JavaDoc[0];
98         }
99
100         public Object JavaDoc getParent(Object JavaDoc element) {
101             if (element instanceof SourceLocation) {
102                 SourceLocation loc = (SourceLocation)element;
103                 return loc.isUserDefined() ? fUserNode : fSystemNode;
104             }
105             return null;
106         }
107
108         public boolean hasChildren(Object JavaDoc element) {
109             if (element.equals(fSystemNode))
110                 return fExtensionLocations.length > 0;
111             if (element.equals(fUserNode))
112                 return fUserLocations.size() > 0;
113             return false;
114         }
115     }
116
117     class SourceLabelProvider extends LabelProvider {
118         public String JavaDoc getText(Object JavaDoc obj) {
119             if (obj instanceof SourceLocation) {
120                 SourceLocation location = (SourceLocation) obj;
121                 return location.getPath().toOSString();
122             }
123             return super.getText(obj);
124         }
125         public Image getImage(Object JavaDoc obj) {
126             if (obj instanceof SourceLocation)
127                 return fFolderImage;
128         
129             return obj.equals(fUserNode)? fUserImage : fExtensionImage;
130         }
131     }
132
133     public TargetSourceTab(DialogPage page) {
134         initializeImages();
135         fSystemNode = new NamedElement(PDEUIMessages.SourceBlock_target);
136         fUserNode = new NamedElement(PDEUIMessages.SourceBlock_additional);
137         SourceLocationManager manager = PDECore.getDefault().getSourceLocationManager();
138         fExtensionLocations = manager.getExtensionLocations();
139         fUserLocations.addAll(Arrays.asList(manager.getUserLocations()));
140         fPage = page;
141     }
142     
143     private void initializeImages() {
144         fExtensionImage = PDEPluginImages.DESC_SOURCE_ATTACHMENT_OBJ.createImage();
145         ImageDescriptor userDesc =
146             new OverlayIcon(
147                 PDEPluginImages.DESC_SOURCE_ATTACHMENT_OBJ,
148                 new ImageDescriptor[][] { { PDEPluginImages.DESC_DOC_CO }
149         });
150         fUserImage = userDesc.createImage();
151         fFolderImage = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
152     }
153     
154     public void resetExtensionLocations(IPluginModelBase[] models) {
155         fExtensionLocations = SourceLocationManager.computeSourceLocations(models);
156         fTreeViewer.refresh(fSystemNode);
157     }
158
159     private String JavaDoc encodeSourceLocations() {
160         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
161         for (int i = 0; i < fUserLocations.size(); i++) {
162             if (i > 0)
163                 buf.append(File.pathSeparatorChar);
164             buf.append(((SourceLocation) fUserLocations.get(i)).getPath().toOSString());
165         }
166         return buf.toString();
167     }
168     
169     public void dispose() {
170         fExtensionImage.dispose();
171         fUserImage.dispose();
172     }
173
174     /**
175      * @see IPreferencePage#performOk()
176      */

177     public boolean performOk() {
178         Preferences preferences = PDECore.getDefault().getPluginPreferences();
179         preferences.setValue(ICoreConstants.P_SOURCE_LOCATIONS, encodeSourceLocations());
180         PDECore.getDefault().getSourceLocationManager().setExtensionLocations(fExtensionLocations);
181         PDECore.getDefault().getJavadocLocationManager().reset();
182         return true;
183     }
184     
185     public void performDefaults() {
186         fUserLocations.clear();
187         fTreeViewer.refresh();
188     }
189     
190     protected void loadTargetProfile(ITarget target) {
191         // this tab should do nothing. It may later be removed altogether
192
}
193
194     protected void handleAdd() {
195         String JavaDoc path = getDirectoryDialog(fLastUserPath).open();
196         if (path != null) {
197             SourceLocation location = new SourceLocation(new Path(path));
198             fUserLocations.add(location);
199             fTreeViewer.add(fUserNode, location);
200             fTreeViewer.setSelection(new StructuredSelection(location));
201             fLastUserPath = path;
202         }
203     }
204     
205     private DirectoryDialog getDirectoryDialog(String JavaDoc filterPath) {
206         DirectoryDialog dialog = new DirectoryDialog(fPage.getShell());
207         dialog.setMessage(PDEUIMessages.SourcePreferencePage_dialogMessage);
208         if (filterPath != null)
209             dialog.setFilterPath(filterPath);
210         return dialog;
211     }
212
213     protected void handleRemove() {
214         IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection();
215         Object JavaDoc object = selection.getFirstElement();
216         if (object instanceof SourceLocation) {
217             SourceLocation location = (SourceLocation) object;
218             if (location.isUserDefined()) {
219                 fUserLocations.remove(location);
220                 fTreeViewer.remove(location);
221             }
222         }
223     }
224
225     /**
226      * @see IDialogPage#createControl(Composite)
227      */

228     public Control createContents(Composite parent) {
229         Composite container = new Composite(parent, SWT.NULL);
230         GridLayout layout = new GridLayout();
231         layout.numColumns = 2;
232         layout.verticalSpacing = 10;
233         container.setLayout(layout);
234
235         Link text = new Link(container, SWT.WRAP);
236         text.setText(PDEUIMessages.SourceBlock_desc);
237         GridData gd = new GridData();
238         gd.horizontalSpan = 2;
239         gd.widthHint = 400;
240         text.setLayoutData(gd);
241         text.addSelectionListener(new SelectionAdapter() {
242             public void widgetSelected(SelectionEvent e) {
243             IPluginExtensionPoint point = PDEStateHelper.findExtensionPoint("org.eclipse.pde.core.source"); //$NON-NLS-1$
244
if (point != null)
245                 new ShowDescriptionAction(point, true).run();
246             }
247         });
248
249         fTreeViewer = new TreeViewer(container, SWT.BORDER);
250         fTreeViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
251         fTreeViewer.setContentProvider(new SourceProvider());
252         fTreeViewer.setLabelProvider(new SourceLabelProvider());
253         fTreeViewer.setInput(this);
254         fTreeViewer.expandAll();
255         fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
256             public void selectionChanged(SelectionChangedEvent event) {
257                 IStructuredSelection ssel = (IStructuredSelection)event.getSelection();
258                 boolean removeEnabled = false;
259                 if (ssel != null && ssel.size() > 0) {
260                     Object JavaDoc object = ssel.getFirstElement();
261                     removeEnabled = (object instanceof SourceLocation && ((SourceLocation)object).isUserDefined());
262                 }
263                 fRemoveButton.setEnabled(removeEnabled);
264             }
265         });
266         fTreeViewer.getTree().addKeyListener(new KeyAdapter() {
267             public void keyPressed(KeyEvent event) {
268                 if (event.character == SWT.DEL && event.stateMask == 0) {
269                     handleRemove();
270                 }
271             }
272         });
273     
274         Composite buttonContainer = new Composite(container, SWT.NONE);
275         layout = new GridLayout();
276         layout.marginWidth = layout.marginHeight = 0;
277         buttonContainer.setLayout(layout);
278         buttonContainer.setLayoutData(new GridData(GridData.FILL_VERTICAL));
279         
280         fAddButton = new Button(buttonContainer, SWT.PUSH);
281         fAddButton.setText(PDEUIMessages.SourceBlock_add);
282         fAddButton.setLayoutData(new GridData(GridData.FILL | GridData.VERTICAL_ALIGN_BEGINNING));
283         SWTUtil.setButtonDimensionHint(fAddButton);
284         fAddButton.addSelectionListener(new SelectionAdapter() {
285             public void widgetSelected(SelectionEvent e) {
286                 handleAdd();
287             }
288         });
289         
290         fRemoveButton = new Button(buttonContainer, SWT.PUSH);
291         fRemoveButton.setText(PDEUIMessages.SourceBlock_remove);
292         fRemoveButton.setLayoutData(new GridData(GridData.FILL | GridData.VERTICAL_ALIGN_BEGINNING));
293         SWTUtil.setButtonDimensionHint(fRemoveButton);
294         fRemoveButton.addSelectionListener(new SelectionAdapter() {
295             public void widgetSelected(SelectionEvent e) {
296                 handleRemove();
297             }
298         });
299         fRemoveButton.setEnabled(false);
300         
301         Dialog.applyDialogFont(parent);
302         PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.SOURCE_PREFERENCE_PAGE);
303         return container;
304     }
305
306 }
307
Popular Tags