1 11 package org.eclipse.pde.internal.ui.wizards.imports; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.core.runtime.Path; 18 import org.eclipse.core.runtime.Preferences; 19 import org.eclipse.jface.dialogs.Dialog; 20 import org.eclipse.jface.dialogs.IDialogSettings; 21 import org.eclipse.jface.wizard.WizardPage; 22 import org.eclipse.pde.internal.core.ICoreConstants; 23 import org.eclipse.pde.internal.core.PDECore; 24 import org.eclipse.pde.internal.ui.IHelpContextIds; 25 import org.eclipse.pde.internal.ui.PDEUIMessages; 26 import org.eclipse.pde.internal.ui.util.SWTUtil; 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.events.ModifyEvent; 29 import org.eclipse.swt.events.ModifyListener; 30 import org.eclipse.swt.events.SelectionAdapter; 31 import org.eclipse.swt.events.SelectionEvent; 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.Control; 38 import org.eclipse.swt.widgets.DirectoryDialog; 39 import org.eclipse.swt.widgets.Label; 40 import org.eclipse.ui.PlatformUI; 41 42 public class FeatureImportWizardFirstPage extends WizardPage { 43 44 private static final String SETTINGS_DROPLOCATION = "droplocation"; private static final String SETTINGS_DOOTHER = "doother"; private static final String SETTINGS_NOT_BINARY = "notbinary"; 48 private Label fOtherLocationLabel; 49 private Button fRuntimeLocationButton; 50 private Button fBrowseButton; 51 private Combo fDropLocation; 52 private Button fBinaryButton; 53 54 public FeatureImportWizardFirstPage() { 55 super("FeatureImportWizardPage"); setTitle(PDEUIMessages.FeatureImportWizard_FirstPage_title); setDescription(PDEUIMessages.FeatureImportWizard_FirstPage_desc); } 59 60 63 public void createControl(Composite parent) { 64 initializeDialogUnits(parent); 65 66 Composite composite = new Composite(parent, SWT.NONE); 67 68 GridLayout layout = new GridLayout(); 69 layout.numColumns = 3; 70 composite.setLayout(layout); 71 72 fRuntimeLocationButton = new Button(composite, SWT.CHECK); 73 fillHorizontal(fRuntimeLocationButton, 3, false); 74 fRuntimeLocationButton.setText( 75 PDEUIMessages.FeatureImportWizard_FirstPage_runtimeLocation); 77 fOtherLocationLabel = new Label(composite, SWT.NULL); 78 fOtherLocationLabel.setText( 79 PDEUIMessages.FeatureImportWizard_FirstPage_otherFolder); 81 fDropLocation = new Combo(composite, SWT.DROP_DOWN); 82 fillHorizontal(fDropLocation, 1, true); 83 84 fBrowseButton = new Button(composite, SWT.PUSH); 85 fBrowseButton.setText(PDEUIMessages.FeatureImportWizard_FirstPage_browse); fBrowseButton.addSelectionListener(new SelectionAdapter() { 87 public void widgetSelected(SelectionEvent e) { 88 IPath chosen = chooseDropLocation(); 89 if (chosen != null) 90 fDropLocation.setText(chosen.toOSString()); 91 } 92 }); 93 fBrowseButton.setLayoutData(new GridData()); 94 SWTUtil.setButtonDimensionHint(fBrowseButton); 95 96 fBinaryButton = new Button(composite, SWT.CHECK); 97 fillHorizontal(fBinaryButton, 3, false); 98 fBinaryButton.setText( 99 PDEUIMessages.FeatureImportWizard_FirstPage_binaryImport); 101 initializeFields(getDialogSettings()); 102 hookListeners(); 103 104 setControl(composite); 105 Dialog.applyDialogFont(composite); 106 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IHelpContextIds.FEATURE_IMPORT_FIRST_PAGE); 107 } 108 109 private String getTargetHome() { 110 Preferences preferences = PDECore.getDefault().getPluginPreferences(); 111 return preferences.getString(ICoreConstants.PLATFORM_PATH); 112 } 113 114 private void hookListeners() { 115 fRuntimeLocationButton.addSelectionListener(new SelectionAdapter() { 116 public void widgetSelected(SelectionEvent e) { 117 setOtherEnabled(!fRuntimeLocationButton.getSelection()); 118 validateDropLocation(); 119 if (fRuntimeLocationButton.getSelection()) { 120 fDropLocation.setText(getTargetHome()); 121 } 122 } 123 }); 124 125 fDropLocation.addSelectionListener(new SelectionAdapter() { 126 public void widgetSelected(SelectionEvent e) { 127 validateDropLocation(); 128 } 129 }); 130 fDropLocation.addModifyListener(new ModifyListener() { 131 public void modifyText(ModifyEvent e) { 132 validateDropLocation(); 133 } 134 }); 135 } 136 137 private GridData fillHorizontal(Control control, int span, boolean grab) { 138 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 139 gd.horizontalSpan = span; 140 gd.grabExcessHorizontalSpace = grab; 141 control.setLayoutData(gd); 142 return gd; 143 } 144 145 private void initializeFields(IDialogSettings initialSettings) { 146 String [] dropItems = new String [0]; 147 boolean doOther = false; 148 boolean binary = true; 149 150 if (initialSettings != null) { 151 doOther = initialSettings.getBoolean(SETTINGS_DOOTHER); 152 binary = !initialSettings.getBoolean(SETTINGS_NOT_BINARY); 153 154 ArrayList items = new ArrayList (); 155 for (int i = 0; i < 6; i++) { 156 String curr = 157 initialSettings.get( 158 SETTINGS_DROPLOCATION + String.valueOf(i)); 159 if (curr != null && !items.contains(curr)) { 160 items.add(curr); 161 } 162 } 163 dropItems = (String []) items.toArray(new String [items.size()]); 164 } 165 fDropLocation.setItems(dropItems); 166 fRuntimeLocationButton.setSelection(!doOther); 167 setOtherEnabled(doOther); 168 if (doOther) { 169 if (dropItems.length > 0) 170 fDropLocation.setText(dropItems[0]); 171 } else { 172 fDropLocation.setText(getTargetHome()); 173 } 174 fBinaryButton.setSelection(binary); 175 176 validateDropLocation(); 177 } 178 179 private void setOtherEnabled(boolean enabled) { 180 fOtherLocationLabel.setEnabled(enabled); 181 fDropLocation.setEnabled(enabled); 182 fBrowseButton.setEnabled(enabled); 183 } 184 185 public void storeSettings(boolean finishPressed) { 186 IDialogSettings settings = getDialogSettings(); 187 boolean other = !fRuntimeLocationButton.getSelection(); 188 boolean binary = fBinaryButton.getSelection(); 189 if (finishPressed || fDropLocation.getText().length() > 0 && other) { 190 settings.put( 191 SETTINGS_DROPLOCATION + String.valueOf(0), 192 fDropLocation.getText()); 193 String [] items = fDropLocation.getItems(); 194 int nEntries = Math.min(items.length, 5); 195 for (int i = 0; i < nEntries; i++) { 196 settings.put( 197 SETTINGS_DROPLOCATION + String.valueOf(i + 1), 198 items[i]); 199 } 200 } 201 if (finishPressed) { 202 settings.put(SETTINGS_DOOTHER, other); 203 settings.put(SETTINGS_NOT_BINARY, !binary); 204 } 205 } 206 207 210 private IPath chooseDropLocation() { 211 DirectoryDialog dialog = new DirectoryDialog(getShell()); 212 dialog.setFilterPath(fDropLocation.getText()); 213 dialog.setText(PDEUIMessages.FeatureImportWizard_messages_folder_title); dialog.setMessage(PDEUIMessages.FeatureImportWizard_messages_folder_message); String res = dialog.open(); 216 if (res != null) { 217 return new Path(res); 218 } 219 return null; 220 } 221 222 private void validateDropLocation() { 223 String errorMessage = null; 224 if (isOtherLocation()) { 225 IPath curr = getDropLocation(); 226 if (curr.segmentCount() == 0) { 227 errorMessage = 228 PDEUIMessages.FeatureImportWizard_errors_locationMissing; } else if (!Path.ROOT.isValidPath(fDropLocation.getText())) { 230 errorMessage = 231 PDEUIMessages.FeatureImportWizard_errors_buildFolderInvalid; } else { 233 234 File file = curr.toFile(); 235 if (!file.exists() || !file.isDirectory()) { 236 errorMessage = 237 PDEUIMessages.FeatureImportWizard_errors_buildFolderMissing; } 239 } 240 } 241 setErrorMessage(errorMessage); 242 setPageComplete(errorMessage == null); 243 } 244 245 public boolean isBinary(){ 246 return fBinaryButton.getSelection(); 247 } 248 249 252 public IPath getDropLocation() { 253 return new Path(fDropLocation.getText()); 254 } 255 256 public boolean isOtherLocation() { 257 return !fRuntimeLocationButton.getSelection(); 258 } 259 260 263 public boolean isPageComplete() { 264 return getErrorMessage() == null; 265 } 266 } 267 | Popular Tags |