KickJava   Java API By Example, From Geeks To Geeks.

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


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

174     public boolean performOk() {
175         Preferences preferences = PDECore.getDefault().getPluginPreferences();
176         preferences.setValue(ICoreConstants.P_SOURCE_LOCATIONS, encodeSourceLocations());
177         PDECore.getDefault().getSourceLocationManager().setExtensionLocations(fExtensionLocations);
178         PDECore.getDefault().getJavadocLocationManager().reset();
179         return true;
180     }
181     
182     public void performDefaults() {
183         fUserLocations.clear();
184         fTreeViewer.refresh();
185     }
186
187     protected void handleAdd() {
188         String JavaDoc path = getDirectoryDialog(null).open();
189         if (path != null) {
190             SourceLocation location = new SourceLocation(new Path(path));
191             fUserLocations.add(location);
192             fTreeViewer.add(fUserNode, location);
193             fTreeViewer.setSelection(new StructuredSelection(location));
194         }
195     }
196     
197     private DirectoryDialog getDirectoryDialog(String JavaDoc filterPath) {
198         DirectoryDialog dialog = new DirectoryDialog(PDEPlugin.getActiveWorkbenchShell());
199         dialog.setMessage(PDEUIMessages.SourcePreferencePage_dialogMessage); //$NON-NLS-1$
200
if (filterPath != null)
201             dialog.setFilterPath(filterPath);
202         return dialog;
203     }
204
205     protected void handleRemove() {
206         IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection();
207         Object JavaDoc object = selection.getFirstElement();
208         if (object instanceof SourceLocation) {
209             SourceLocation location = (SourceLocation) object;
210             if (location.isUserDefined()) {
211                 fUserLocations.remove(location);
212                 fTreeViewer.remove(location);
213             }
214         }
215     }
216
217     /**
218      * @see IDialogPage#createControl(Composite)
219      */

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