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 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.events.SelectionEvent; 24 import org.eclipse.swt.events.SelectionListener; 25 import org.eclipse.swt.layout.GridData; 26 import org.eclipse.swt.layout.GridLayout; 27 import org.eclipse.swt.widgets.Button; 28 import org.eclipse.swt.widgets.Combo; 29 import org.eclipse.swt.widgets.Composite; 30 import org.eclipse.swt.widgets.FileDialog; 31 import org.eclipse.swt.widgets.Label; 32 33 import org.eclipse.jface.dialogs.IDialogConstants; 34 import org.eclipse.jface.dialogs.MessageDialog; 35 import org.eclipse.jface.preference.IPreferenceStore; 36 import org.eclipse.jface.window.Window; 37 38 import org.eclipse.jdt.ui.JavaUI; 39 40 import org.eclipse.jdt.internal.ui.JavaPlugin; 41 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile; 42 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile; 43 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 44 import org.eclipse.jdt.internal.ui.util.PixelConverter; 45 import org.eclipse.jdt.internal.ui.util.SWTUtil; 46 47 48 49 52 53 public class CodingStyleConfigurationBlock { 54 55 private static final String PREF_LASTLOADPATH= JavaUI.ID_PLUGIN + ".codeformatter.loadpath"; private static final String PREF_LASTSAVEPATH= JavaUI.ID_PLUGIN + ".codeformatter.savepath"; 58 59 private class StoreUpdater implements Observer { 60 61 public StoreUpdater() { 62 fProfileManager.addObserver(this); 63 } 64 65 public void update(Observable o, Object arg) { 66 final int value= ((Integer )arg).intValue(); 67 switch (value) { 68 case ProfileManager.PROFILE_DELETED_EVENT: 69 case ProfileManager.PROFILE_RENAMED_EVENT: 70 case ProfileManager.PROFILE_CREATED_EVENT: 71 case ProfileManager.SETTINGS_CHANGED_EVENT: 72 try { 73 ProfileStore.writeProfiles(fProfileManager.getSortedProfiles()); 74 } catch (CoreException x) { 75 JavaPlugin.log(x); 76 } 77 } 78 } 79 } 80 81 82 83 private class ProfileComboController implements Observer , SelectionListener { 84 85 private final List fSortedProfiles; 86 87 public ProfileComboController() { 88 fSortedProfiles= fProfileManager.getSortedProfiles(); 89 fProfileCombo.addSelectionListener(this); 90 fProfileManager.addObserver(this); 91 updateProfiles(); 92 updateSelection(); 93 } 94 95 public void widgetSelected(SelectionEvent e) { 96 final int index= fProfileCombo.getSelectionIndex(); 97 fProfileManager.setSelected((Profile)fSortedProfiles.get(index)); 98 } 99 100 public void widgetDefaultSelected(SelectionEvent e) {} 101 102 public void update(Observable o, Object arg) { 103 if (arg == null) return; 104 final int value= ((Integer )arg).intValue(); 105 switch (value) { 106 case ProfileManager.PROFILE_CREATED_EVENT: 107 case ProfileManager.PROFILE_DELETED_EVENT: 108 case ProfileManager.PROFILE_RENAMED_EVENT: 109 updateProfiles(); 110 case ProfileManager.SELECTION_CHANGED_EVENT: 111 updateSelection(); 112 } 113 } 114 115 private void updateProfiles() { 116 fProfileCombo.setItems(fProfileManager.getSortedNames()); 117 } 118 119 private void updateSelection() { 120 fProfileCombo.setText(fProfileManager.getSelected().getName()); 121 } 122 } 123 124 private class ButtonController implements Observer , SelectionListener { 125 126 public ButtonController() { 127 fProfileManager.addObserver(this); 128 fNewButton.addSelectionListener(this); 129 fRenameButton.addSelectionListener(this); 130 fEditButton.addSelectionListener(this); 131 fDeleteButton.addSelectionListener(this); 132 fSaveButton.addSelectionListener(this); 133 fLoadButton.addSelectionListener(this); 134 update(fProfileManager, null); 135 } 136 137 public void update(Observable o, Object arg) { 138 final boolean state= ((ProfileManager)o).getSelected() instanceof CustomProfile; 139 fEditButton.setText(state ? FormatterMessages.getString("CodingStyleConfigurationBlock.edit_button.desc") : FormatterMessages.getString("CodingStyleConfigurationBlock.show_button.desc")); fDeleteButton.setEnabled(state); 142 fSaveButton.setEnabled(state); 143 fRenameButton.setEnabled(state); 144 } 145 146 public void widgetSelected(SelectionEvent e) { 147 final Button button= (Button)e.widget; 148 if (button == fSaveButton) 149 saveButtonPressed(); 150 else if (button == fEditButton) 151 modifyButtonPressed(); 152 else if (button == fDeleteButton) 153 deleteButtonPressed(); 154 else if (button == fNewButton) 155 newButtonPressed(); 156 else if (button == fLoadButton) 157 loadButtonPressed(); 158 else if (button == fRenameButton) 159 renameButtonPressed(); 160 } 161 162 public void widgetDefaultSelected(SelectionEvent e) { 163 } 164 165 private void renameButtonPressed() { 166 if (!(fProfileManager.getSelected() instanceof CustomProfile)) return; 167 final CustomProfile profile= (CustomProfile)fProfileManager.getSelected(); 168 final RenameProfileDialog renameDialog= new RenameProfileDialog(fComposite.getShell(), profile, fProfileManager); 169 renameDialog.open(); 170 } 171 172 private void modifyButtonPressed() { 173 final ModifyDialog modifyDialog= new ModifyDialog(fComposite.getShell(), fProfileManager.getSelected(), fProfileManager, false); 174 modifyDialog.open(); 175 } 176 177 private void deleteButtonPressed() { 178 if (MessageDialog.openQuestion( 179 fComposite.getShell(), 180 FormatterMessages.getString("CodingStyleConfigurationBlock.delete_confirmation.title"), FormatterMessages.getFormattedString("CodingStyleConfigurationBlock.delete_confirmation.question", fProfileManager.getSelected().getName()))) { fProfileManager.deleteSelected(); 183 } 184 } 185 186 private void newButtonPressed() { 187 final CreateProfileDialog p= new CreateProfileDialog(fComposite.getShell(), fProfileManager); 188 if (p.open() != Window.OK) 189 return; 190 if (!p.openEditDialog()) 191 return; 192 final ModifyDialog modifyDialog= new ModifyDialog(fComposite.getShell(), p.getCreatedProfile(), fProfileManager, true); 193 modifyDialog.open(); 194 } 195 196 private void saveButtonPressed() { 197 final FileDialog dialog= new FileDialog(fComposite.getShell(), SWT.SAVE); 198 dialog.setText(FormatterMessages.getString("CodingStyleConfigurationBlock.save_profile.dialog.title")); dialog.setFilterExtensions(new String [] {"*.xml"}); 201 final String lastPath= JavaPlugin.getDefault().getDialogSettings().get(PREF_LASTSAVEPATH); 202 if (lastPath != null) { 203 dialog.setFilterPath(lastPath); 204 } 205 final String path= dialog.open(); 206 if (path == null) 207 return; 208 209 JavaPlugin.getDefault().getDialogSettings().put(PREF_LASTSAVEPATH, dialog.getFilterPath()); 210 211 final File file= new File (path); 212 final Collection profiles= new ArrayList (); 213 profiles.add(fProfileManager.getSelected()); 214 try { 215 ProfileStore.writeProfilesToFile(profiles, file); 216 } catch (CoreException e) { 217 final String title= FormatterMessages.getString("CodingStyleConfigurationBlock.save_profile.error.title"); final String message= FormatterMessages.getString("CodingStyleConfigurationBlock.save_profile.error.message"); ExceptionHandler.handle(e, fComposite.getShell(), title, message); 220 } 221 } 222 223 private void loadButtonPressed() { 224 final FileDialog dialog= new FileDialog(fComposite.getShell(), SWT.OPEN); 225 dialog.setText(FormatterMessages.getString("CodingStyleConfigurationBlock.load_profile.dialog.title")); dialog.setFilterExtensions(new String [] {"*.xml"}); final String lastPath= JavaPlugin.getDefault().getDialogSettings().get(PREF_LASTLOADPATH); 228 if (lastPath != null) { 229 dialog.setFilterPath(lastPath); 230 } 231 final String path= dialog.open(); 232 if (path == null) 233 return; 234 JavaPlugin.getDefault().getDialogSettings().put(PREF_LASTLOADPATH, dialog.getFilterPath()); 235 236 final File file= new File (path); 237 Collection profiles= null; 238 try { 239 profiles= ProfileStore.readProfilesFromFile(file); 240 } catch (CoreException e) { 241 final String title= FormatterMessages.getString("CodingStyleConfigurationBlock.load_profile.error.title"); final String message= FormatterMessages.getString("CodingStyleConfigurationBlock.load_profile.error.message"); ExceptionHandler.handle(e, fComposite.getShell(), title, message); 244 } 245 if (profiles == null || profiles.isEmpty()) 246 return; 247 248 final CustomProfile profile= (CustomProfile)profiles.iterator().next(); 249 250 if (ProfileVersioner.getVersionStatus(profile) > 0) { 251 final String title= FormatterMessages.getString("CodingStyleConfigurationBlock.load_profile.error_too_new.title"); final String message= FormatterMessages.getString("CodingStyleConfigurationBlock.load_profile.error_too_new.message"); MessageDialog.openWarning(fComposite.getShell(), title, message); 254 } 255 256 if (fProfileManager.containsName(profile.getName())) { 257 final AlreadyExistsDialog aeDialog= new AlreadyExistsDialog(fComposite.getShell(), profile, fProfileManager); 258 if (aeDialog.open() != Window.OK) 259 return; 260 } 261 ProfileVersioner.updateAndComplete(profile); 262 fProfileManager.addProfile(profile); 263 } 264 } 265 266 private class PreviewController implements Observer { 267 268 public PreviewController() { 269 fProfileManager.addObserver(this); 270 fJavaPreview.setWorkingValues(fProfileManager.getSelected().getSettings()); 271 fJavaPreview.update(); 272 } 273 274 public void update(Observable o, Object arg) { 275 final int value= ((Integer )arg).intValue(); 276 switch (value) { 277 case ProfileManager.PROFILE_CREATED_EVENT: 278 case ProfileManager.PROFILE_DELETED_EVENT: 279 case ProfileManager.SELECTION_CHANGED_EVENT: 280 case ProfileManager.SETTINGS_CHANGED_EVENT: 281 fJavaPreview.setWorkingValues(((ProfileManager)o).getSelected().getSettings()); 282 fJavaPreview.update(); 283 } 284 } 285 286 } 287 288 289 292 private final static String PREVIEW= 293 "/**\n* " + FormatterMessages.getString("CodingStyleConfigurationBlock.preview.title") + "\n*/\n\n" + "package mypackage; import java.util.LinkedList; public class MyIntStack {" + "private final LinkedList fStack;" + "public MyIntStack(){fStack= new LinkedList();}" + "public int pop(){return ((Integer)fStack.removeFirst()).intValue();}" + "public void push(int elem){fStack.addFirst(new Integer(elem));}" + "public boolean isEmpty() {return fStack.isEmpty();}" + "}"; 304 305 308 protected Composite fComposite; 309 protected Combo fProfileCombo; 310 protected Button fEditButton; 311 protected Button fRenameButton; 312 protected Button fDeleteButton; 313 protected Button fNewButton; 314 protected Button fLoadButton; 315 protected Button fSaveButton; 316 317 320 protected final ProfileManager fProfileManager; 321 322 325 protected CompilationUnitPreview fJavaPreview; 326 private PixelConverter fPixConv; 327 328 329 332 public CodingStyleConfigurationBlock() { 333 List profiles= null; 334 try { 335 profiles= ProfileStore.readProfiles(); 336 } catch (CoreException e) { 337 JavaPlugin.log(e); 338 } 339 340 if (profiles == null) 341 profiles= new ArrayList (); 342 343 fProfileManager= new ProfileManager(profiles); 344 345 new StoreUpdater(); 346 } 347 348 351 public Composite createContents(Composite parent) { 352 353 final int numColumns = 5; 354 355 fPixConv = new PixelConverter(parent); 356 fComposite = createComposite(parent, numColumns, false); 357 358 fProfileCombo= createProfileCombo(fComposite, numColumns - 3, fPixConv.convertWidthInCharsToPixels(20)); 359 fEditButton= createButton(fComposite, FormatterMessages.getString("CodingStyleConfigurationBlock.edit_button.desc"), GridData.HORIZONTAL_ALIGN_BEGINNING); fRenameButton= createButton(fComposite, FormatterMessages.getString("CodingStyleConfigurationBlock.rename_button.desc"), GridData.HORIZONTAL_ALIGN_BEGINNING); fDeleteButton= createButton(fComposite, FormatterMessages.getString("CodingStyleConfigurationBlock.remove_button.desc"), GridData.HORIZONTAL_ALIGN_BEGINNING); 363 final Composite group= createComposite(fComposite, 4, false); 364 final GridData groupData= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 365 groupData.horizontalSpan= numColumns; 366 group.setLayoutData(groupData); 367 368 fNewButton= createButton(group, FormatterMessages.getString("CodingStyleConfigurationBlock.new_button.desc"), GridData.HORIZONTAL_ALIGN_BEGINNING); ((GridData)createLabel(group, "", 1).getLayoutData()).grabExcessHorizontalSpace= true; fLoadButton= createButton(group, FormatterMessages.getString("CodingStyleConfigurationBlock.load_button.desc"), GridData.HORIZONTAL_ALIGN_END); fSaveButton= createButton(group, FormatterMessages.getString("CodingStyleConfigurationBlock.save_button.desc"), GridData.HORIZONTAL_ALIGN_END); 373 createLabel(fComposite, FormatterMessages.getString("CodingStyleConfigurationBlock.preview_label.text"), numColumns); configurePreview(fComposite, numColumns); 375 376 new ButtonController(); 377 new ProfileComboController(); 378 new PreviewController(); 379 380 return fComposite; 381 } 382 383 384 private static Button createButton(Composite composite, String text, final int style) { 385 final Button button= new Button(composite, SWT.PUSH); 386 button.setText(text); 387 388 final GridData gd= new GridData(style); 389 gd.widthHint= SWTUtil.getButtonWidthHint(button); 390 gd.heightHint= SWTUtil.getButtonHeightHint(button); 391 button.setLayoutData(gd); 392 return button; 393 } 394 395 private static Combo createProfileCombo(Composite composite, int span, int widthHint) { 396 final GridData gd = new GridData(GridData.FILL_HORIZONTAL); 397 gd.horizontalSpan = span; 398 gd.widthHint= widthHint; 399 400 final Combo combo= new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY ); 401 combo.setLayoutData(gd); 402 return combo; 403 } 404 405 private Label createLabel(Composite composite, String text, int numColumns) { 406 final GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 407 gd.horizontalSpan = numColumns; 408 gd.widthHint= 0; 409 410 final Label label = new Label(composite, SWT.WRAP); 411 label.setText(text); 412 label.setLayoutData(gd); 413 return label; 414 } 415 416 private Composite createComposite(Composite parent, int numColumns, boolean margins) { 417 final Composite composite = new Composite(parent, SWT.NONE); 418 final GridLayout layout = new GridLayout(numColumns, false); 419 if (margins) { 420 layout.marginHeight= fPixConv.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); 421 layout.marginWidth= fPixConv.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); 422 } else { 423 layout.marginHeight = 0; 424 layout.marginWidth = 0; 425 } 426 layout.horizontalSpacing= fPixConv.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 427 layout.verticalSpacing= fPixConv.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 428 composite.setLayout(layout); 429 return composite; 430 } 431 432 private void configurePreview(Composite composite, int numColumns) { 433 fJavaPreview= new CompilationUnitPreview(fProfileManager.getSelected().getSettings(), composite); 434 fJavaPreview.setPreviewText(PREVIEW); 435 436 final GridData gd = new GridData(GridData.FILL_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL); 437 gd.horizontalSpan = numColumns; 438 gd.verticalSpan= 7; 439 gd.widthHint = 0; 440 gd.heightHint = 0; 441 fJavaPreview.getControl().setLayoutData(gd); 442 } 443 444 protected IPreferenceStore doGetPreferenceStore() { 445 return JavaPlugin.getDefault().getPreferenceStore(); 446 } 447 448 public void performOk() { 449 fProfileManager.commitChanges(); 450 } 451 452 public void performDefaults() { 453 Profile profile= fProfileManager.getProfile(ProfileManager.JAVA_PROFILE); 454 if (profile != null) { 455 int defaultIndex= fProfileManager.getSortedProfiles().indexOf(profile); 456 if (defaultIndex != -1) { 457 fProfileCombo.select(defaultIndex); 458 fProfileManager.setSelected(profile); 459 } 460 } 461 } 462 463 464 } 465 | Popular Tags |