1 11 package org.eclipse.pde.internal.ui.preferences; 12 13 import java.io.File ; 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 KEY_LABEL = "SourcePreferencePage.label"; public static final String KEY_SELECT_ALL = 42 "WizardCheckboxTablePart.selectAll"; public static final String KEY_DESELECT_ALL = 44 "WizardCheckboxTablePart.deselectAll"; private static final String KEY_ADD = "SourcePreferencePage.add"; private static final String KEY_DELETE = "SourcePreferencePage.delete"; private static final String KEY_DESC = "SourcePreferencePage.desc"; 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 [] getElements(Object input) { 60 return getLocations(); 61 } 62 } 63 64 class SourceLabelProvider extends LabelProvider { 65 66 public String getText(Object obj) { 67 SourceLocation location = (SourceLocation) obj; 68 return location.getPath().toOSString(); 69 } 70 71 public Image getImage(Object obj) { 72 SourceLocation location = (SourceLocation) obj; 73 return (location.isUserDefined()) ? userImage : extensionImage; 74 } 75 } 76 77 class LocationPart extends CheckboxTablePart { 78 public LocationPart(String [] 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 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 131 protected void elementChecked(Object element, boolean checked) { 132 ((SourceLocation)element).setEnabled(checked); 133 } 134 135 } 136 137 public SourcePreferencePage() { 138 tablePart = 139 new LocationPart( 140 new String [] { 141 PDEPlugin.getResourceString(KEY_ADD), 142 PDEPlugin.getResourceString("SourcePreferencePage.edit"), 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 encodeSourceLocations(Object [] locations) { 174 StringBuffer buf = new StringBuffer (); 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 encodeSourceLocation(SourceLocation location) { 185 return location.getPath().toOSString() + "," + (location.isEnabled() ? "t" : "f"); } 187 188 public void dispose() { 189 super.dispose(); 190 userImage.dispose(); 191 } 192 193 196 public void init(IWorkbench workbench) { 197 } 198 199 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 [] getLocations() { 225 Object [] merged = new Object [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 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 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 filterPath) { 269 if (dialog == null) 270 dialog = new DirectoryDialog(getShell()); 271 dialog.setMessage(PDEPlugin.getResourceString("SourcePreferencePage.dialogMessage")); 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 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 |