1 11 package org.eclipse.jdt.internal.ui.preferences.formatter; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.List ; 17 import java.util.Observable ; 18 import java.util.Observer ; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.preferences.DefaultScope; 22 import org.eclipse.core.runtime.preferences.IScopeContext; 23 import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener; 24 import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent; 25 26 import org.eclipse.core.resources.IProject; 27 import org.eclipse.core.resources.ProjectScope; 28 29 import org.eclipse.swt.SWT; 30 import org.eclipse.swt.events.SelectionEvent; 31 import org.eclipse.swt.events.SelectionListener; 32 import org.eclipse.swt.layout.GridData; 33 import org.eclipse.swt.layout.GridLayout; 34 import org.eclipse.swt.widgets.Button; 35 import org.eclipse.swt.widgets.Combo; 36 import org.eclipse.swt.widgets.Composite; 37 import org.eclipse.swt.widgets.FileDialog; 38 import org.eclipse.swt.widgets.Label; 39 import org.eclipse.swt.widgets.Shell; 40 41 import org.eclipse.jface.dialogs.MessageDialog; 42 import org.eclipse.jface.dialogs.StatusDialog; 43 import org.eclipse.jface.window.Window; 44 45 import org.eclipse.jdt.core.JavaCore; 46 47 import org.eclipse.jdt.internal.corext.util.Messages; 48 49 import org.eclipse.jdt.ui.JavaUI; 50 51 import org.eclipse.jdt.internal.ui.JavaPlugin; 52 import org.eclipse.jdt.internal.ui.preferences.PreferencesAccess; 53 import org.eclipse.jdt.internal.ui.preferences.PreferencesMessages; 54 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile; 55 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile; 56 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 57 import org.eclipse.jdt.internal.ui.util.PixelConverter; 58 import org.eclipse.jdt.internal.ui.util.SWTUtil; 59 60 import org.osgi.service.prefs.BackingStoreException; 61 62 public abstract class ProfileConfigurationBlock { 63 64 private class StoreUpdater implements Observer { 65 66 public StoreUpdater() { 67 fProfileManager.addObserver(this); 68 } 69 70 public void update(Observable o, Object arg) { 71 try { 72 fPreferenceListenerEnabled= false; 73 final int value= ((Integer )arg).intValue(); 74 switch (value) { 75 case ProfileManager.PROFILE_DELETED_EVENT: 76 case ProfileManager.PROFILE_RENAMED_EVENT: 77 case ProfileManager.PROFILE_CREATED_EVENT: 78 case ProfileManager.SETTINGS_CHANGED_EVENT: 79 try { 80 fProfileStore.writeProfiles(fProfileManager.getSortedProfiles(), fInstanceScope); fProfileManager.commitChanges(fCurrContext); 82 } catch (CoreException x) { 83 JavaPlugin.log(x); 84 } 85 break; 86 case ProfileManager.SELECTION_CHANGED_EVENT: 87 fProfileManager.commitChanges(fCurrContext); 88 break; 89 } 90 } finally { 91 fPreferenceListenerEnabled= true; 92 } 93 } 94 } 95 96 class ProfileComboController implements Observer , SelectionListener { 97 98 private final List fSortedProfiles; 99 100 public ProfileComboController() { 101 fSortedProfiles= fProfileManager.getSortedProfiles(); 102 fProfileCombo.addSelectionListener(this); 103 fProfileManager.addObserver(this); 104 updateProfiles(); 105 updateSelection(); 106 } 107 108 public void widgetSelected(SelectionEvent e) { 109 final int index= fProfileCombo.getSelectionIndex(); 110 fProfileManager.setSelected((Profile)fSortedProfiles.get(index)); 111 } 112 113 public void widgetDefaultSelected(SelectionEvent e) {} 114 115 public void update(Observable o, Object arg) { 116 if (arg == null) return; 117 final int value= ((Integer )arg).intValue(); 118 switch (value) { 119 case ProfileManager.PROFILE_CREATED_EVENT: 120 case ProfileManager.PROFILE_DELETED_EVENT: 121 case ProfileManager.PROFILE_RENAMED_EVENT: 122 updateProfiles(); 123 updateSelection(); 124 break; 125 case ProfileManager.SELECTION_CHANGED_EVENT: 126 updateSelection(); 127 break; 128 } 129 } 130 131 private void updateProfiles() { 132 fProfileCombo.setItems(fProfileManager.getSortedDisplayNames()); 133 } 134 135 private void updateSelection() { 136 fProfileCombo.setText(fProfileManager.getSelected().getName()); 137 } 138 } 139 140 class ButtonController implements Observer , SelectionListener { 141 142 public ButtonController() { 143 fProfileManager.addObserver(this); 144 fNewButton.addSelectionListener(this); 145 fEditButton.addSelectionListener(this); 146 fDeleteButton.addSelectionListener(this); 147 fLoadButton.addSelectionListener(this); 148 update(fProfileManager, null); 149 } 150 151 public void update(Observable o, Object arg) { 152 Profile selected= ((ProfileManager)o).getSelected(); 153 final boolean notBuiltIn= !selected.isBuiltInProfile(); 154 fDeleteButton.setEnabled(notBuiltIn); 155 } 156 157 public void widgetSelected(SelectionEvent e) { 158 final Button button= (Button)e.widget; 159 if (button == fEditButton) 160 modifyButtonPressed(); 161 else if (button == fDeleteButton) 162 deleteButtonPressed(); 163 else if (button == fNewButton) 164 newButtonPressed(); 165 else if (button == fLoadButton) 166 loadButtonPressed(); 167 } 168 169 public void widgetDefaultSelected(SelectionEvent e) { 170 } 171 172 private void modifyButtonPressed() { 173 final StatusDialog modifyDialog= createModifyDialog(fComposite.getShell(), fProfileManager.getSelected(), fProfileManager, fProfileStore, false); 174 modifyDialog.open(); 175 } 176 177 private void deleteButtonPressed() { 178 if (MessageDialog.openQuestion( 179 fComposite.getShell(), 180 FormatterMessages.CodingStyleConfigurationBlock_delete_confirmation_title, 181 Messages.format(FormatterMessages.CodingStyleConfigurationBlock_delete_confirmation_question, fProfileManager.getSelected().getName()))) { 182 fProfileManager.deleteSelected(); 183 } 184 } 185 186 private void newButtonPressed() { 187 final CreateProfileDialog p= new CreateProfileDialog(fComposite.getShell(), fProfileManager, fProfileVersioner); 188 if (p.open() != Window.OK) 189 return; 190 if (!p.openEditDialog()) 191 return; 192 final StatusDialog modifyDialog= createModifyDialog(fComposite.getShell(), p.getCreatedProfile(), fProfileManager, fProfileStore, true); 193 modifyDialog.open(); 194 } 195 196 private void loadButtonPressed() { 197 final FileDialog dialog= new FileDialog(fComposite.getShell(), SWT.OPEN); 198 dialog.setText(FormatterMessages.CodingStyleConfigurationBlock_load_profile_dialog_title); 199 dialog.setFilterExtensions(new String [] {"*.xml"}); final String lastPath= JavaPlugin.getDefault().getDialogSettings().get(fLastSaveLoadPathKey + ".loadpath"); if (lastPath != null) { 202 dialog.setFilterPath(lastPath); 203 } 204 final String path= dialog.open(); 205 if (path == null) 206 return; 207 JavaPlugin.getDefault().getDialogSettings().put(fLastSaveLoadPathKey + ".loadpath", dialog.getFilterPath()); 209 final File file= new File (path); 210 Collection profiles= null; 211 try { 212 profiles= fProfileStore.readProfilesFromFile(file); 213 } catch (CoreException e) { 214 final String title= FormatterMessages.CodingStyleConfigurationBlock_load_profile_error_title; 215 final String message= FormatterMessages.CodingStyleConfigurationBlock_load_profile_error_message; 216 ExceptionHandler.handle(e, fComposite.getShell(), title, message); 217 } 218 if (profiles == null || profiles.isEmpty()) 219 return; 220 221 final CustomProfile profile= (CustomProfile)profiles.iterator().next(); 222 223 if (!fProfileVersioner.getProfileKind().equals(profile.getKind())) { 224 final String title= FormatterMessages.CodingStyleConfigurationBlock_load_profile_error_title; 225 final String message= Messages.format(FormatterMessages.ProfileConfigurationBlock_load_profile_wrong_profile_message, new String [] {fProfileVersioner.getProfileKind(), profile.getKind()}); 226 MessageDialog.openError(fComposite.getShell(), title, message); 227 return; 228 } 229 230 if (profile.getVersion() > fProfileVersioner.getCurrentVersion()) { 231 final String title= FormatterMessages.CodingStyleConfigurationBlock_load_profile_error_too_new_title; 232 final String message= FormatterMessages.CodingStyleConfigurationBlock_load_profile_error_too_new_message; 233 MessageDialog.openWarning(fComposite.getShell(), title, message); 234 } 235 236 if (fProfileManager.containsName(profile.getName())) { 237 final AlreadyExistsDialog aeDialog= new AlreadyExistsDialog(fComposite.getShell(), profile, fProfileManager); 238 if (aeDialog.open() != Window.OK) 239 return; 240 } 241 fProfileVersioner.update(profile); 242 fProfileManager.addProfile(profile); 243 } 244 } 245 246 249 private Composite fComposite; 250 private Combo fProfileCombo; 251 private Button fEditButton; 252 private Button fDeleteButton; 253 private Button fNewButton; 254 private Button fLoadButton; 255 256 private PixelConverter fPixConv; 257 260 private final ProfileManager fProfileManager; 261 private final IScopeContext fCurrContext; 262 private final IScopeContext fInstanceScope; 263 private final ProfileStore fProfileStore; 264 private final IProfileVersioner fProfileVersioner; 265 private final String fLastSaveLoadPathKey; 266 private IPreferenceChangeListener fPreferenceListener; 267 private final PreferencesAccess fPreferenceAccess; 268 private boolean fPreferenceListenerEnabled; 269 270 public ProfileConfigurationBlock(IProject project, final PreferencesAccess access, String lastSaveLoadPathKey) { 271 272 fPreferenceAccess= access; 273 fLastSaveLoadPathKey= lastSaveLoadPathKey; 274 275 fProfileVersioner= createProfileVersioner(); 276 fProfileStore= createProfileStore(fProfileVersioner); 277 fInstanceScope= access.getInstanceScope(); 278 if (project != null) { 279 fCurrContext= access.getProjectScope(project); 280 } else { 281 fCurrContext= fInstanceScope; 282 } 283 284 List profiles= null; 285 try { 286 profiles= fProfileStore.readProfiles(fInstanceScope); 287 } catch (CoreException e) { 288 JavaPlugin.log(e); 289 } 290 if (profiles == null) { 291 try { 292 profiles= fProfileStore.readProfiles(new DefaultScope()); 294 } catch (CoreException e) { 295 JavaPlugin.log(e); 296 } 297 } 298 299 if (profiles == null) 300 profiles= new ArrayList (); 301 302 fProfileManager= createProfileManager(profiles, fCurrContext, access, fProfileVersioner); 303 304 new StoreUpdater(); 305 306 fPreferenceListenerEnabled= true; 307 fPreferenceListener= new IPreferenceChangeListener() { 308 public void preferenceChange(PreferenceChangeEvent event) { 309 if (fPreferenceListenerEnabled) { 310 preferenceChanged(event); 311 } 312 } 313 }; 314 access.getInstanceScope().getNode(JavaUI.ID_PLUGIN).addPreferenceChangeListener(fPreferenceListener); 315 316 } 317 318 protected void preferenceChanged(PreferenceChangeEvent event) { 319 320 } 321 322 protected abstract IProfileVersioner createProfileVersioner(); 323 324 protected abstract ProfileStore createProfileStore(IProfileVersioner versioner); 325 326 protected abstract ProfileManager createProfileManager(List profiles, IScopeContext context, PreferencesAccess access, IProfileVersioner profileVersioner); 327 328 protected abstract ModifyDialog createModifyDialog(Shell shell, Profile profile, ProfileManager profileManager, ProfileStore profileStore, boolean newProfile); 329 330 protected abstract void configurePreview(Composite composite, int numColumns, ProfileManager profileManager); 331 332 private static Button createButton(Composite composite, String text, final int style) { 333 final Button button= new Button(composite, SWT.PUSH); 334 button.setFont(composite.getFont()); 335 button.setText(text); 336 337 final GridData gd= new GridData(style); 338 gd.widthHint= SWTUtil.getButtonWidthHint(button); 339 button.setLayoutData(gd); 340 return button; 341 } 342 343 348 public Composite createContents(Composite parent) { 349 350 final int numColumns = 5; 351 352 fPixConv = new PixelConverter(parent); 353 fComposite = createComposite(parent, numColumns); 354 355 Label profileLabel= new Label(fComposite, SWT.NONE); 356 profileLabel.setText(PreferencesMessages.CleanUpPreferencePage_Description); 357 GridData data= new GridData(SWT.FILL, SWT.FILL, true, false); 358 data.horizontalSpan= numColumns; 359 profileLabel.setLayoutData(data); 360 361 fProfileCombo= createProfileCombo(fComposite, 3, fPixConv.convertWidthInCharsToPixels(20)); 362 fEditButton= createButton(fComposite, FormatterMessages.CodingStyleConfigurationBlock_edit_button_desc, GridData.HORIZONTAL_ALIGN_BEGINNING); 363 fDeleteButton= createButton(fComposite, FormatterMessages.CodingStyleConfigurationBlock_remove_button_desc, GridData.HORIZONTAL_ALIGN_BEGINNING); 364 365 fNewButton= createButton(fComposite, FormatterMessages.CodingStyleConfigurationBlock_new_button_desc, GridData.HORIZONTAL_ALIGN_BEGINNING); 366 fLoadButton= createButton(fComposite, FormatterMessages.CodingStyleConfigurationBlock_load_button_desc, GridData.HORIZONTAL_ALIGN_END); 367 createLabel(fComposite, "", 3); 369 configurePreview(fComposite, numColumns, fProfileManager); 370 371 new ButtonController(); 372 new ProfileComboController(); 373 374 return fComposite; 375 } 376 377 private static Combo createProfileCombo(Composite composite, int span, int widthHint) { 378 final GridData gd = new GridData(GridData.FILL_HORIZONTAL); 379 gd.horizontalSpan = span; 380 gd.widthHint= widthHint; 381 382 final Combo combo= new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY ); 383 combo.setFont(composite.getFont()); 384 combo.setLayoutData(gd); 385 return combo; 386 } 387 388 protected static Label createLabel(Composite composite, String text, int numColumns) { 389 final GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 390 gd.horizontalSpan = numColumns; 391 gd.widthHint= 0; 392 393 final Label label = new Label(composite, SWT.WRAP); 394 label.setFont(composite.getFont()); 395 label.setText(text); 396 label.setLayoutData(gd); 397 return label; 398 } 399 400 private Composite createComposite(Composite parent, int numColumns) { 401 final Composite composite = new Composite(parent, SWT.NONE); 402 composite.setFont(parent.getFont()); 403 404 final GridLayout layout = new GridLayout(numColumns, false); 405 layout.marginHeight = 0; 406 layout.marginWidth = 0; 407 composite.setLayout(layout); 408 return composite; 409 } 410 411 public final boolean hasProjectSpecificOptions(IProject project) { 412 if (project != null) { 413 return fProfileManager.hasProjectSpecificSettings(new ProjectScope(project)); 414 } 415 return false; 416 } 417 418 public boolean performOk() { 419 return true; 420 } 421 422 public void performApply() { 423 try { 424 fCurrContext.getNode(JavaUI.ID_PLUGIN).flush(); 425 fCurrContext.getNode(JavaCore.PLUGIN_ID).flush(); 426 if (fCurrContext != fInstanceScope) { 427 fInstanceScope.getNode(JavaUI.ID_PLUGIN).flush(); 428 fInstanceScope.getNode(JavaCore.PLUGIN_ID).flush(); 429 } 430 } catch (BackingStoreException e) { 431 JavaPlugin.log(e); 432 } 433 } 434 435 public void performDefaults() { 436 Profile profile= fProfileManager.getDefaultProfile(); 437 if (profile != null) { 438 int defaultIndex= fProfileManager.getSortedProfiles().indexOf(profile); 439 if (defaultIndex != -1) { 440 fProfileManager.setSelected(profile); 441 } 442 } 443 } 444 445 public void dispose() { 446 if (fPreferenceListener != null) { 447 fPreferenceAccess.getInstanceScope().getNode(JavaUI.ID_PLUGIN).removePreferenceChangeListener(fPreferenceListener); 448 fPreferenceListener= null; 449 } 450 } 451 452 public void enableProjectSpecificSettings(boolean useProjectSpecificSettings) { 453 if (useProjectSpecificSettings) { 454 fProfileManager.commitChanges(fCurrContext); 455 } else { 456 fProfileManager.clearAllSettings(fCurrContext); 457 } 458 } 459 460 } 461 | Popular Tags |