KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.*;
15
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.core.runtime.Preferences;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.preference.PreferencePage;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.eclipse.jface.viewers.*;
22 import org.eclipse.pde.internal.core.*;
23 import org.eclipse.pde.internal.ui.*;
24 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
25 import org.eclipse.pde.internal.ui.parts.CheckboxTablePart;
26 import org.eclipse.pde.internal.ui.util.*;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.layout.*;
30 import org.eclipse.swt.widgets.*;
31 import org.eclipse.ui.*;
32 import org.eclipse.ui.forms.widgets.*;
33 import org.eclipse.ui.help.WorkbenchHelp;
34
35
36 public class SourcePreferencePage
37     extends PreferencePage
38     implements IWorkbenchPreferencePage {
39     private static DirectoryDialog dialog;
40     private static final String JavaDoc KEY_LABEL = "SourcePreferencePage.label"; //$NON-NLS-1$
41
public static final String JavaDoc KEY_SELECT_ALL =
42         "WizardCheckboxTablePart.selectAll"; //$NON-NLS-1$
43
public static final String JavaDoc KEY_DESELECT_ALL =
44         "WizardCheckboxTablePart.deselectAll"; //$NON-NLS-1$
45
private static final String JavaDoc KEY_ADD = "SourcePreferencePage.add"; //$NON-NLS-1$
46
private static final String JavaDoc KEY_DELETE = "SourcePreferencePage.delete"; //$NON-NLS-1$
47
private static final String JavaDoc KEY_DESC = "SourcePreferencePage.desc"; //$NON-NLS-1$
48
private CheckboxTablePart tablePart;
49     private CheckboxTableViewer tableViewer;
50     private Image extensionImage;
51     private Image userImage;
52     private Preferences preferences;
53     private SourceLocation[] extensionLocations = new SourceLocation[0];
54     private ArrayList userLocations = new ArrayList();
55
56     class SourceProvider
57         extends DefaultContentProvider
58         implements IStructuredContentProvider {
59         public Object JavaDoc[] getElements(Object JavaDoc input) {
60             return getLocations();
61         }
62     }
63
64     class SourceLabelProvider extends LabelProvider {
65         
66         public String JavaDoc getText(Object JavaDoc obj) {
67             SourceLocation location = (SourceLocation) obj;
68             return location.getPath().toOSString();
69         }
70
71         public Image getImage(Object JavaDoc obj) {
72             SourceLocation location = (SourceLocation) obj;
73             return (location.isUserDefined()) ? userImage : extensionImage;
74         }
75     }
76
77     class LocationPart extends CheckboxTablePart {
78         public LocationPart(String JavaDoc[] buttonLabels) {
79             super(buttonLabels);
80         }
81         protected void buttonSelected(Button button, int index) {
82             switch (index) {
83                 case 0 :
84                     handleAdd();
85                     break;
86                 case 1 :
87                     handleEdit();
88                     break;
89                 case 2:
90                     handleDelete();
91                     break;
92                 case 4 :
93                     selectAll(true);
94                     break;
95                 case 5 :
96                     selectAll(false);
97                     break;
98             }
99         }
100         protected Button createButton(
101             Composite parent,
102             String JavaDoc label,
103             int index,
104             FormToolkit toolkit) {
105             Button button = super.createButton(parent, label, index, toolkit);
106             SWTUtil.setButtonDimensionHint(button);
107             return button;
108         }
109         protected void createMainLabel(
110             Composite parent,
111             int span,
112             FormToolkit toolkit) {
113             Label label = new Label(parent, SWT.NULL);
114             label.setText(PDEPlugin.getResourceString(KEY_LABEL));
115             GridData gd = new GridData(GridData.FILL);
116             gd.horizontalSpan = span;
117             label.setLayoutData(gd);
118         }
119         protected void selectionChanged(IStructuredSelection selection) {
120             boolean enabled = false;
121             if (!selection.isEmpty()) {
122                 SourceLocation loc = (SourceLocation)selection.getFirstElement();
123                 enabled = loc.isUserDefined();
124             }
125             tablePart.setButtonEnabled(1, enabled);
126             tablePart.setButtonEnabled(2, enabled);
127         }
128         /**
129          * @see org.eclipse.pde.internal.ui.parts.CheckboxTablePart#elementChecked(Object, boolean)
130          */

131         protected void elementChecked(Object JavaDoc element, boolean checked) {
132             ((SourceLocation)element).setEnabled(checked);
133         }
134
135     }
136
137     public SourcePreferencePage() {
138         tablePart =
139             new LocationPart(
140                 new String JavaDoc[] {
141                     PDEPlugin.getResourceString(KEY_ADD),
142                     PDEPlugin.getResourceString("SourcePreferencePage.edit"), //$NON-NLS-1$
143
PDEPlugin.getResourceString(KEY_DELETE),
144                     null,
145                     PDEPlugin.getResourceString(KEY_SELECT_ALL),
146                     PDEPlugin.getResourceString(KEY_DESELECT_ALL)
147                     });
148         extensionImage =
149             PlatformUI.getWorkbench().getSharedImages().getImage(
150                 ISharedImages.IMG_OBJ_FOLDER);
151         ImageDescriptor userDesc =
152             new OverlayIcon(
153                 PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
154                     ISharedImages.IMG_OBJ_FOLDER),
155                 new ImageDescriptor[][] { { PDEPluginImages.DESC_DOC_CO }
156         });
157         userImage = userDesc.createImage();
158         setDescription(PDEPlugin.getResourceString(KEY_DESC));
159         preferences = PDECore.getDefault().getPluginPreferences();
160         initializeExtensionLocations();
161         initializeUserlocations();
162
163     }
164
165     private void initializeExtensionLocations() {
166         extensionLocations = PDECore.getDefault().getSourceLocationManager().getExtensionLocations();
167     }
168     
169     private void initializeUserlocations() {
170         userLocations = PDECore.getDefault().getSourceLocationManager().getUserLocationArray();
171     }
172     
173     private String JavaDoc encodeSourceLocations(Object JavaDoc[] locations) {
174         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
175         for (int i = 0; i < locations.length; i++) {
176             SourceLocation loc = (SourceLocation) locations[i];
177             if (i > 0)
178                 buf.append(File.pathSeparatorChar);
179             buf.append(encodeSourceLocation(loc));
180         }
181         return buf.toString();
182     }
183
184     private String JavaDoc encodeSourceLocation(SourceLocation location) {
185         return location.getPath().toOSString() + "," + (location.isEnabled() ? "t" : "f"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
186
}
187     
188     public void dispose() {
189         super.dispose();
190         userImage.dispose();
191     }
192
193     /**
194      * @see IWorkbenchPreferencePage#init(IWorkbench)
195      */

196     public void init(IWorkbench workbench) {
197     }
198
199     /**
200      * @see IPreferencePage#performOk()
201      */

202     public boolean performOk() {
203         preferences.setValue(ICoreConstants.P_EXT_LOCATIONS, encodeSourceLocations(extensionLocations));
204         preferences.setValue(ICoreConstants.P_SOURCE_LOCATIONS, encodeSourceLocations(userLocations.toArray()));
205         PDECore.getDefault().savePluginPreferences();
206         return super.performOk();
207     }
208
209     public void performDefaults() {
210         for (int i = 0; i < extensionLocations.length; i++) {
211             SourceLocation location = (SourceLocation)extensionLocations[i];
212             location.setEnabled(true);
213             tableViewer.setChecked(location, true);
214         }
215         for (int i = 0; i < userLocations.size(); i++) {
216             SourceLocation location = (SourceLocation)userLocations.get(i);
217             location.setEnabled(false);
218             tableViewer.setChecked(location, false);
219         }
220         tableViewer.refresh();
221         super.performDefaults();
222     }
223
224     private Object JavaDoc[] getLocations() {
225         Object JavaDoc[] merged = new Object JavaDoc[extensionLocations.length + userLocations.size()];
226         System.arraycopy(extensionLocations, 0, merged, 0, extensionLocations.length);
227         System.arraycopy(
228             userLocations.toArray(),
229             0,
230             merged,
231             extensionLocations.length,
232             userLocations.size());
233         return merged;
234     }
235
236     private void selectAll(boolean selected) {
237         for (int i = 0; i < extensionLocations.length; i++) {
238             ((SourceLocation)extensionLocations[i]).setEnabled(selected);
239         }
240         for (int i = 0; i < userLocations.size(); i++) {
241             ((SourceLocation)userLocations.get(i)).setEnabled(selected);
242         }
243         tableViewer.setAllChecked(selected);
244     }
245
246     private void handleAdd() {
247         String JavaDoc path = getDirectoryDialog(null).open();
248         if (path != null) {
249             SourceLocation location = new SourceLocation(new Path(path), true);
250             userLocations.add(location);
251             tableViewer.add(location);
252             tableViewer.setChecked(location, location.isEnabled());
253         }
254     }
255     
256     private void handleEdit() {
257         IStructuredSelection ssel = (IStructuredSelection)tableViewer.getSelection();
258         SourceLocation loc = (SourceLocation)ssel.getFirstElement();
259         String JavaDoc path = getDirectoryDialog(loc.getPath().toOSString()).open();
260         if (path != null) {
261             loc.setPath(new Path(path));
262             tableViewer.refresh();
263         }
264         
265         
266     }
267     
268     private DirectoryDialog getDirectoryDialog(String JavaDoc filterPath) {
269         if (dialog == null)
270             dialog = new DirectoryDialog(getShell());
271         dialog.setMessage(PDEPlugin.getResourceString("SourcePreferencePage.dialogMessage")); //$NON-NLS-1$
272
if (filterPath != null)
273             dialog.setFilterPath(filterPath);
274         return dialog;
275     }
276
277     private void handleDelete() {
278         IStructuredSelection selection =
279             (IStructuredSelection) tableViewer.getSelection();
280         SourceLocation location = (SourceLocation) selection.getFirstElement();
281         userLocations.remove(location);
282         tableViewer.remove(location);
283     }
284
285     /**
286      * @see IDialogPage#createControl(Composite)
287      */

288     public Control createContents(Composite parent) {
289         Composite container = new Composite(parent, SWT.NULL);
290         GridLayout layout = new GridLayout();
291         layout.numColumns = 2;
292         container.setLayout(layout);
293         tablePart.setMinimumSize(150, 200);
294         tablePart.createControl(container, SWT.BORDER, 2, null);
295         tableViewer = tablePart.getTableViewer();
296         tableViewer.setContentProvider(new SourceProvider());
297         tableViewer.setLabelProvider(new SourceLabelProvider());
298         tableViewer.setInput(this);
299         initializeStates();
300         tablePart.setButtonEnabled(1, false);
301         tablePart.setButtonEnabled(2, false);
302         Dialog.applyDialogFont(parent);
303         WorkbenchHelp.setHelp(parent, IHelpContextIds.SOURCE_PREFERENCE_PAGE);
304         return container;
305     }
306     
307     private void initializeStates() {
308         for (int i = 0; i < extensionLocations.length; i++) {
309             SourceLocation loc = (SourceLocation) extensionLocations[i];
310             tableViewer.setChecked(loc, loc.isEnabled());
311         }
312         for (int i = 0; i < userLocations.size(); i++) {
313             SourceLocation loc = (SourceLocation) userLocations.get(i);
314             tableViewer.setChecked(loc, loc.isEnabled());
315         }
316     }
317 }
318
Popular Tags