1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths; 12 13 import java.util.List ; 14 15 import org.eclipse.core.runtime.IPath; 16 import org.eclipse.core.runtime.IStatus; 17 18 import org.eclipse.core.resources.IContainer; 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.resources.IWorkspace; 22 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.layout.GridData; 25 import org.eclipse.swt.layout.GridLayout; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.swt.widgets.Control; 28 import org.eclipse.swt.widgets.Shell; 29 import org.eclipse.swt.widgets.Text; 30 31 import org.eclipse.jface.dialogs.StatusDialog; 32 33 import org.eclipse.ui.PlatformUI; 34 35 import org.eclipse.jdt.internal.corext.util.Messages; 36 37 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 38 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 39 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler; 40 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 41 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 42 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 43 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; 44 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField; 45 46 public class NewSourceFolderDialog extends StatusDialog { 47 48 private SelectionButtonDialogField fUseProjectButton; 49 private SelectionButtonDialogField fUseFolderButton; 50 51 private StringDialogField fContainerDialogField; 52 private StatusInfo fContainerFieldStatus; 53 54 private IContainer fFolder; 55 private List fExistingFolders; 56 private IProject fCurrProject; 57 58 public NewSourceFolderDialog(Shell parent, String title, IProject project, List existingFolders, CPListElement entryToEdit) { 59 super(parent); 60 setTitle(title); 61 62 fContainerFieldStatus= new StatusInfo(); 63 64 SourceContainerAdapter adapter= new SourceContainerAdapter(); 65 66 fUseProjectButton= new SelectionButtonDialogField(SWT.RADIO); 67 fUseProjectButton.setLabelText(NewWizardMessages.NewSourceFolderDialog_useproject_button); 68 fUseProjectButton.setDialogFieldListener(adapter); 69 70 fUseFolderButton= new SelectionButtonDialogField(SWT.RADIO); 71 fUseFolderButton.setLabelText(NewWizardMessages.NewSourceFolderDialog_usefolder_button); 72 fUseFolderButton.setDialogFieldListener(adapter); 73 74 fContainerDialogField= new StringDialogField(); 75 fContainerDialogField.setDialogFieldListener(adapter); 76 fContainerDialogField.setLabelText(NewWizardMessages.NewSourceFolderDialog_sourcefolder_label); 77 78 fUseFolderButton.attachDialogField(fContainerDialogField); 79 80 fFolder= null; 81 fExistingFolders= existingFolders; 82 fCurrProject= project; 83 84 boolean useFolders= true; 85 if (entryToEdit == null) { 86 fContainerDialogField.setText(""); } else { 88 IPath editPath= entryToEdit.getPath().removeFirstSegments(1); 89 fContainerDialogField.setText(editPath.toString()); 90 useFolders= !editPath.isEmpty(); 91 } 92 fUseFolderButton.setSelection(useFolders); 93 fUseProjectButton.setSelection(!useFolders); 94 } 95 96 public void setMessage(String message) { 97 fContainerDialogField.setLabelText(message); 98 } 99 100 protected Control createDialogArea(Composite parent) { 101 Composite composite= (Composite)super.createDialogArea(parent); 102 103 Composite inner= new Composite(composite, SWT.NONE); 104 GridLayout layout= new GridLayout(); 105 layout.marginHeight= 0; 106 layout.marginWidth= 0; 107 layout.numColumns= 1; 108 inner.setLayout(layout); 109 110 int widthHint= convertWidthInCharsToPixels(50); 111 112 113 GridData data= new GridData(GridData.FILL_HORIZONTAL); 114 data.widthHint= widthHint; 115 116 if (fExistingFolders.contains(fCurrProject)) { 117 fContainerDialogField.doFillIntoGrid(inner, 2); 118 } else { 119 fUseProjectButton.doFillIntoGrid(inner, 1); 120 fUseFolderButton.doFillIntoGrid(inner, 1); 121 fContainerDialogField.getTextControl(inner); 122 123 int horizontalIndent= convertWidthInCharsToPixels(3); 124 data.horizontalIndent= horizontalIndent; 125 } 126 Text text= fContainerDialogField.getTextControl(null); 127 text.setLayoutData(data); 128 TextFieldNavigationHandler.install(text); 129 130 fContainerDialogField.postSetFocusOnDialogField(parent.getDisplay()); 131 applyDialogFont(composite); 132 return composite; 133 } 134 135 136 138 private class SourceContainerAdapter implements IDialogFieldListener { 139 140 142 public void dialogFieldChanged(DialogField field) { 143 doStatusLineUpdate(); 144 } 145 } 146 147 protected void doStatusLineUpdate() { 148 checkIfPathValid(); 149 updateStatus(fContainerFieldStatus); 150 } 151 152 protected void checkIfPathValid() { 153 fFolder= null; 154 IContainer folder= null; 155 if (fUseFolderButton.isSelected()) { 156 String pathStr= fContainerDialogField.getText(); 157 if (pathStr.length() == 0) { 158 fContainerFieldStatus.setError(NewWizardMessages.NewSourceFolderDialog_error_enterpath); 159 return; 160 } 161 IPath path= fCurrProject.getFullPath().append(pathStr); 162 IWorkspace workspace= fCurrProject.getWorkspace(); 163 164 IStatus pathValidation= workspace.validatePath(path.toString(), IResource.FOLDER); 165 if (!pathValidation.isOK()) { 166 fContainerFieldStatus.setError(Messages.format(NewWizardMessages.NewSourceFolderDialog_error_invalidpath, pathValidation.getMessage())); 167 return; 168 } 169 folder= fCurrProject.getFolder(pathStr); 170 } else { 171 folder= fCurrProject; 172 } 173 if (isExisting(folder)) { 174 fContainerFieldStatus.setError(NewWizardMessages.NewSourceFolderDialog_error_pathexists); 175 return; 176 } 177 fContainerFieldStatus.setOK(); 178 fFolder= folder; 179 } 180 181 private boolean isExisting(IContainer folder) { 182 return fExistingFolders.contains(folder); 183 } 184 185 186 187 public IContainer getSourceFolder() { 188 return fFolder; 189 } 190 191 194 protected void configureShell(Shell newShell) { 195 super.configureShell(newShell); 196 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.NEW_CONTAINER_DIALOG); 197 } 198 199 200 } 201 | Popular Tags |