1 11 package org.eclipse.jdt.internal.ui.workingsets; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.events.ModifyEvent; 17 import org.eclipse.swt.events.ModifyListener; 18 import org.eclipse.swt.layout.GridData; 19 import org.eclipse.swt.layout.GridLayout; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Label; 22 import org.eclipse.swt.widgets.Text; 23 24 import org.eclipse.jface.dialogs.Dialog; 25 import org.eclipse.jface.resource.ImageDescriptor; 26 import org.eclipse.jface.wizard.WizardPage; 27 28 import org.eclipse.ui.IWorkingSet; 29 import org.eclipse.ui.PlatformUI; 30 import org.eclipse.ui.dialogs.IWorkingSetPage; 31 32 35 public abstract class AbstractWorkingSetWizardPage extends WizardPage implements IWorkingSetPage { 36 37 private boolean fIsFirstValidation; 38 private Text fWorkingSetName; 39 private IWorkingSet fWorkingSet; 40 41 44 public AbstractWorkingSetWizardPage(String pageid, String title, ImageDescriptor image) { 45 super(pageid, title, image); 46 } 47 48 51 public void createControl(Composite parent) { 52 initializeDialogUnits(parent); 53 54 Composite composite= new Composite(parent, SWT.NONE); 55 composite.setLayout(new GridLayout()); 56 composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); 57 setControl(composite); 58 59 Label label= new Label(composite, SWT.WRAP); 60 label.setText(WorkingSetMessages.AbstractWorkingSetPage_workingSet_name); 61 GridData gd= new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER); 62 label.setLayoutData(gd); 63 64 fWorkingSetName= new Text(composite, SWT.SINGLE | SWT.BORDER); 65 fWorkingSetName.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL)); 66 fWorkingSetName.addModifyListener( 67 new ModifyListener() { 68 public void modifyText(ModifyEvent e) { 69 validateInput(); 70 } 71 } 72 ); 73 fWorkingSetName.setFocus(); 74 75 Dialog.applyDialogFont(composite); 76 } 77 78 81 public IWorkingSet getSelection() { 82 return fWorkingSet; 83 } 84 85 88 public void setSelection(IWorkingSet workingSet) { 89 Assert.isNotNull(workingSet, "Working set must not be null"); fWorkingSet= workingSet; 91 if (getContainer() != null && getShell() != null && fWorkingSetName != null) { 92 fWorkingSetName.setText(fWorkingSet.getName()); 93 validateInput(); 94 } 95 } 96 97 100 public void finish() { 101 String workingSetName= fWorkingSetName.getText(); 102 if (fWorkingSet == null) { 103 fWorkingSet= createWorkingSet(workingSetName); 104 } 105 fWorkingSet.setName(workingSetName); 106 } 107 108 protected abstract IWorkingSet createWorkingSet(String workingSetName); 109 110 private void validateInput() { 111 String errorMessage= null; 112 String newText= fWorkingSetName.getText(); 113 114 if (newText.equals(newText.trim()) == false) 115 errorMessage = WorkingSetMessages.AbstractWorkingSetPage_warning_nameWhitespace; 116 if (newText.equals("")) { if (fIsFirstValidation) { 118 setPageComplete(false); 119 fIsFirstValidation= false; 120 return; 121 } else { 122 errorMessage= WorkingSetMessages.AbstractWorkingSetPage_warning_nameMustNotBeEmpty; 123 } 124 } 125 126 fIsFirstValidation= false; 127 128 if (errorMessage == null && (fWorkingSet == null || newText.equals(fWorkingSet.getName()) == false)) { 129 IWorkingSet[] workingSets= PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets(); 130 for (int i= 0; i < workingSets.length; i++) { 131 if (newText.equals(workingSets[i].getName())) { 132 errorMessage= WorkingSetMessages.AbstractWorkingSetPage_warning_workingSetExists; 133 } 134 } 135 } 136 setErrorMessage(errorMessage); 137 setPageComplete(errorMessage == null); 138 } 139 } 140 | Popular Tags |