|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2008 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.php.internal.ui.preferences.includepath; |
| 12 |
|
| 13 |
import java.io.File; |
| 14 |
import java.util.*; |
| 15 |
|
| 16 |
import org.eclipse.core.runtime.IPath; |
| 17 |
import org.eclipse.core.runtime.IStatus; |
| 18 |
import org.eclipse.core.runtime.Path; |
| 19 |
import org.eclipse.dltk.core.DLTKCore; |
| 20 |
import org.eclipse.dltk.core.IScriptProject; |
| 21 |
import org.eclipse.dltk.core.environment.EnvironmentManager; |
| 22 |
import org.eclipse.dltk.core.environment.IEnvironment; |
| 23 |
import org.eclipse.dltk.internal.ui.wizards.NewWizardMessages; |
| 24 |
import org.eclipse.dltk.internal.ui.wizards.buildpath.BPVariableElement; |
| 25 |
import org.eclipse.dltk.internal.ui.wizards.buildpath.BPVariableElementLabelProvider; |
| 26 |
import org.eclipse.dltk.ui.preferences.BuildpathVariablesPreferencePage; |
| 27 |
import org.eclipse.jface.dialogs.Dialog; |
| 28 |
import org.eclipse.jface.dialogs.IDialogConstants; |
| 29 |
import org.eclipse.jface.dialogs.IDialogSettings; |
| 30 |
import org.eclipse.jface.dialogs.StatusDialog; |
| 31 |
import org.eclipse.jface.resource.JFaceResources; |
| 32 |
import org.eclipse.jface.viewers.StructuredSelection; |
| 33 |
import org.eclipse.jface.viewers.Viewer; |
| 34 |
import org.eclipse.jface.viewers.ViewerComparator; |
| 35 |
import org.eclipse.jface.window.Window; |
| 36 |
import org.eclipse.php.internal.ui.PHPUiPlugin; |
| 37 |
import org.eclipse.php.internal.ui.util.Messages; |
| 38 |
import org.eclipse.php.internal.ui.util.StatusInfo; |
| 39 |
import org.eclipse.php.internal.ui.wizards.fields.*; |
| 40 |
import org.eclipse.swt.SWT; |
| 41 |
import org.eclipse.swt.custom.CLabel; |
| 42 |
import org.eclipse.swt.layout.GridData; |
| 43 |
import org.eclipse.swt.layout.GridLayout; |
| 44 |
import org.eclipse.swt.widgets.Button; |
| 45 |
import org.eclipse.swt.widgets.Composite; |
| 46 |
import org.eclipse.swt.widgets.Control; |
| 47 |
import org.eclipse.swt.widgets.Shell; |
| 48 |
import org.eclipse.ui.PlatformUI; |
| 49 |
import org.eclipse.ui.dialogs.PreferencesUtil; |
| 50 |
import org.eclipse.wst.jsdt.internal.ui.IJavaHelpContextIds; |
| 51 |
import org.eclipse.wst.jsdt.internal.ui.wizards.buildpaths.JARFileSelectionDialog; |
| 52 |
|
| 53 |
public class NewVariableEntryDialog extends StatusDialog { |
| 54 |
|
| 55 |
private class VariablesAdapter implements IDialogFieldListener, |
| 56 |
IListAdapter { |
| 57 |
|
| 58 |
// -------- IListAdapter -------- |
| 59 |
|
| 60 |
public void customButtonPressed(ListDialogField field, int index) { |
| 61 |
switch (index) { |
| 62 |
case IDX_EXTEND: /* extend */ |
| 63 |
extendButtonPressed(); |
| 64 |
break; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public void selectionChanged(ListDialogField field) { |
| 69 |
doSelectionChanged(); |
| 70 |
} |
| 71 |
|
| 72 |
public void doubleClicked(ListDialogField field) { |
| 73 |
doDoubleClick(); |
| 74 |
} |
| 75 |
|
| 76 |
// ---------- IDialogFieldListener -------- |
| 77 |
|
| 78 |
public void dialogFieldChanged(DialogField field) { |
| 79 |
if (field == fConfigButton) { |
| 80 |
configButtonPressed(); |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
private final int IDX_EXTEND = 0; |
| 88 |
|
| 89 |
private ListDialogField fVariablesList; |
| 90 |
private boolean fCanExtend; |
| 91 |
private boolean fIsValidSelection; |
| 92 |
|
| 93 |
private IPath[] fResultPaths; |
| 94 |
|
| 95 |
private SelectionButtonDialogField fConfigButton; |
| 96 |
|
| 97 |
private CLabel fWarning; |
| 98 |
IEnvironment environment; |
| 99 |
|
| 100 |
public NewVariableEntryDialog(Shell parent, IEnvironment environment) { |
| 101 |
super(parent); |
| 102 |
setTitle(NewWizardMessages.NewVariableEntryDialog_title); |
| 103 |
this.environment = environment; |
| 104 |
updateStatus(new StatusInfo(IStatus.ERROR, "")); //$NON-NLS-1$ |
| 105 |
|
| 106 |
String[] buttonLabels = new String[] { NewWizardMessages.NewVariableEntryDialog_vars_extend, }; |
| 107 |
|
| 108 |
VariablesAdapter adapter = new VariablesAdapter(); |
| 109 |
|
| 110 |
BPVariableElementLabelProvider labelProvider = new BPVariableElementLabelProvider( |
| 111 |
false); |
| 112 |
|
| 113 |
fVariablesList = new ListDialogField(adapter, buttonLabels, |
| 114 |
labelProvider); |
| 115 |
fVariablesList.setDialogFieldListener(adapter); |
| 116 |
fVariablesList |
| 117 |
.setLabelText(NewWizardMessages.NewVariableEntryDialog_vars_label); |
| 118 |
|
| 119 |
fVariablesList.enableButton(IDX_EXTEND, false); |
| 120 |
|
| 121 |
// fVariablesList.setViewerComparator(new ViewerComparator() { |
| 122 |
// public int compare(Viewer viewer, Object e1, Object e2) { |
| 123 |
// if (e1 instanceof BPVariableElement && e2 instanceof |
| 124 |
// BPVariableElement) { |
| 125 |
// return getComparator().compare(((BPVariableElement)e1).getName(), |
| 126 |
// ((BPVariableElement)e2).getName()); |
| 127 |
// } |
| 128 |
// return super.compare(viewer, e1, e2); |
| 129 |
// } |
| 130 |
// }); |
| 131 |
|
| 132 |
fConfigButton = new SelectionButtonDialogField(SWT.PUSH); |
| 133 |
fConfigButton |
| 134 |
.setLabelText(NewWizardMessages.NewVariableEntryDialog_configbutton_label); |
| 135 |
fConfigButton.setDialogFieldListener(adapter); |
| 136 |
|
| 137 |
initializeElements(); |
| 138 |
|
| 139 |
fCanExtend = false; |
| 140 |
fIsValidSelection = false; |
| 141 |
fResultPaths = null; |
| 142 |
|
| 143 |
fVariablesList.selectFirstElement(); |
| 144 |
} |
| 145 |
|
| 146 |
/* |
| 147 |
* @see org.eclipse.jface.dialogs.Dialog#isResizable() |
| 148 |
* |
| 149 |
* @since 3.4 |
| 150 |
*/ |
| 151 |
protected boolean isResizable() { |
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
private void initializeElements() { |
| 156 |
String[] entries = DLTKCore.getBuildpathVariableNames(); |
| 157 |
ArrayList elements = new ArrayList(entries.length); |
| 158 |
for (int i = 0; i < entries.length; i++) { |
| 159 |
String name = entries[i]; |
| 160 |
IPath entryPath = DLTKCore.getBuildpathVariable(name); |
| 161 |
if (entryPath != null) { |
| 162 |
elements.add(new BPVariableElement(name, entryPath)); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
fVariablesList.setElements(elements); |
| 167 |
} |
| 168 |
|
| 169 |
/* |
| 170 |
* (non-Javadoc) |
| 171 |
* |
| 172 |
* @see Window#configureShell(Shell) |
| 173 |
*/ |
| 174 |
protected void configureShell(Shell shell) { |
| 175 |
super.configureShell(shell); |
| 176 |
PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, |
| 177 |
IJavaHelpContextIds.NEW_VARIABLE_ENTRY_DIALOG); |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
* (non-Javadoc) |
| 182 |
* |
| 183 |
* @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings() |
| 184 |
*/ |
| 185 |
protected IDialogSettings getDialogBoundsSettings() { |
| 186 |
return PHPUiPlugin.getDefault().getDialogSettings()/* |
| 187 |
* getDialogSettingsSection( |
| 188 |
* getClass |
| 189 |
* ().getName()) |
| 190 |
*/; |
| 191 |
} |
| 192 |
|
| 193 |
/* |
| 194 |
* (non-Javadoc) |
| 195 |
* |
| 196 |
* @see |
| 197 |
* org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets |
| 198 |
* .Composite) |
| 199 |
*/ |
| 200 |
protected Control createDialogArea(Composite parent) { |
| 201 |
initializeDialogUnits(parent); |
| 202 |
|
| 203 |
Composite composite = (Composite) super.createDialogArea(parent); |
| 204 |
GridLayout layout = (GridLayout) composite.getLayout(); |
| 205 |
layout.numColumns = 2; |
| 206 |
|
| 207 |
fVariablesList.doFillIntoGrid(composite, 3); |
| 208 |
|
| 209 |
LayoutUtil.setHorizontalSpan(fVariablesList.getLabelControl(null), 2); |
| 210 |
|
| 211 |
GridData listData = (GridData) fVariablesList.getListControl(null) |
| 212 |
.getLayoutData(); |
| 213 |
listData.grabExcessHorizontalSpace = true; |
| 214 |
listData.heightHint = convertHeightInCharsToPixels(10); |
| 215 |
listData.widthHint = convertWidthInCharsToPixels(70); |
| 216 |
|
| 217 |
fWarning = new CLabel(composite, SWT.NONE); |
| 218 |
fWarning.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, |
| 219 |
fVariablesList.getNumberOfControls() - 1, 1)); |
| 220 |
|
| 221 |
Composite lowerComposite = new Composite(composite, SWT.NONE); |
| 222 |
lowerComposite.setLayoutData(new GridData( |
| 223 |
GridData.HORIZONTAL_ALIGN_FILL)); |
| 224 |
|
| 225 |
layout = new GridLayout(); |
| 226 |
layout.marginHeight = 0; |
| 227 |
layout.marginWidth = 0; |
| 228 |
lowerComposite.setLayout(layout); |
| 229 |
|
| 230 |
fConfigButton.doFillIntoGrid(lowerComposite, 1); |
| 231 |
|
| 232 |
applyDialogFont(composite); |
| 233 |
return composite; |
| 234 |
} |
| 235 |
|
| 236 |
public IPath[] getResult() { |
| 237 |
return fResultPaths; |
| 238 |
} |
| 239 |
|
| 240 |
/* |
| 241 |
* @see IDoubleClickListener#doubleClick(DoubleClickEvent) |
| 242 |
*/ |
| 243 |
private void doDoubleClick() { |
| 244 |
if (fIsValidSelection) { |
| 245 |
okPressed(); |
| 246 |
} else if (fCanExtend) { |
| 247 |
extendButtonPressed(); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
private void doSelectionChanged() { |
| 252 |
boolean isValidSelection = true; |
| 253 |
boolean canExtend = false; |
| 254 |
StatusInfo status = new StatusInfo(); |
| 255 |
|
| 256 |
List selected = fVariablesList.getSelectedElements(); |
| 257 |
int nSelected = selected.size(); |
| 258 |
|
| 259 |
if (nSelected > 0) { |
| 260 |
fResultPaths = new Path[nSelected]; |
| 261 |
for (int i = 0; i < nSelected; i++) { |
| 262 |
BPVariableElement curr = (BPVariableElement) selected.get(i); |
| 263 |
fResultPaths[i] = new Path(curr.getName()); |
| 264 |
File file = curr.getPath().toFile(); |
| 265 |
if (!file.exists()) { |
| 266 |
status |
| 267 |
.setError(NewWizardMessages.NewVariableEntryDialog_info_notexists); |
| 268 |
isValidSelection = false; |
| 269 |
break; |
| 270 |
} |
| 271 |
if (file.isDirectory()) { |
| 272 |
canExtend = true; |
| 273 |
} |
| 274 |
} |
| 275 |
} else { |
| 276 |
isValidSelection = false; |
| 277 |
status |
| 278 |
.setInfo(NewWizardMessages.NewVariableEntryDialog_info_noselection); |
| 279 |
} |
| 280 |
if (isValidSelection && nSelected > 1) { |
| 281 |
String str = Messages.format( |
| 282 |
NewWizardMessages.NewVariableEntryDialog_info_selected, |
| 283 |
String.valueOf(nSelected)); |
| 284 |
status.setInfo(str); |
| 285 |
} |
| 286 |
fCanExtend = nSelected == 1 && canExtend; |
| 287 |
fVariablesList.enableButton(0, fCanExtend); |
| 288 |
|
| 289 |
updateStatus(status); |
| 290 |
fIsValidSelection = isValidSelection; |
| 291 |
Button okButton = getButton(IDialogConstants.OK_ID); |
| 292 |
if (okButton != null && !okButton.isDisposed()) { |
| 293 |
okButton.setEnabled(isValidSelection); |
| 294 |
} |
| 295 |
updateDeprecationWarning(); |
| 296 |
} |
| 297 |
|
| 298 |
private void updateDeprecationWarning() { |
| 299 |
// TODO |
| 300 |
// if (fWarning == null || fWarning.isDisposed()) |
| 301 |
// return; |
| 302 |
// |
| 303 |
// for (Iterator iter= fVariablesList.getSelectedElements().iterator(); |
| 304 |
// iter.hasNext();) { |
| 305 |
// BPVariableElement element= (BPVariableElement) iter.next(); |
| 306 |
// String deprecationMessage= element.getDeprecationMessage(); |
| 307 |
// if (deprecationMessage != null) { |
| 308 |
// fWarning.setText(deprecationMessage); |
| 309 |
// fWarning.setImage(JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING)); |
| 310 |
// return; |
| 311 |
// } |
| 312 |
// } |
| 313 |
// fWarning.setText(null); |
| 314 |
// fWarning.setImage(null); |
| 315 |
} |
| 316 |
|
| 317 |
private IPath[] chooseExtensions(BPVariableElement elem) { |
| 318 |
File file = elem.getPath().toFile(); |
| 319 |
IPath[] selected = PHPBuildpathDialogAccess |
| 320 |
.chooseExternalArchiveEntries(getShell(), environment); |
| 321 |
// JARFileSelectionDialog dialog= new JARFileSelectionDialog(getShell(), |
| 322 |
// true, true, true); |
| 323 |
// dialog.setTitle(NewWizardMessages.NewVariableEntryDialog_ExtensionDialog_title); |
| 324 |
// dialog.setMessage(Messages.format(NewWizardMessages.NewVariableEntryDialog_ExtensionDialog_description, |
| 325 |
// elem.getName())); |
| 326 |
// dialog.setInput(file); |
| 327 |
// if (dialog.open() == Window.OK) { |
| 328 |
// Object[] selected= dialog.getResult(); |
| 329 |
IPath[] paths = new IPath[selected.length]; |
| 330 |
for (int i = 0; i < selected.length; i++) { |
| 331 |
IPath filePath = Path.fromOSString(((File) selected[i]).getPath()); |
| 332 |
IPath resPath = new Path(elem.getName()); |
| 333 |
for (int k = elem.getPath().segmentCount(); k < filePath |
| 334 |
.segmentCount(); k++) { |
| 335 |
resPath = resPath.append(filePath.segment(k)); |
| 336 |
} |
| 337 |
paths[i] = resPath; |
| 338 |
} |
| 339 |
return paths; |
| 340 |
// } |
| 341 |
// return null; |
| 342 |
} |
| 343 |
|
| 344 |
protected final void extendButtonPressed() { |
| 345 |
List selected = fVariablesList.getSelectedElements(); |
| 346 |
if (selected.size() == 1) { |
| 347 |
IPath[] extendedPaths = chooseExtensions((BPVariableElement) selected |
| 348 |
.get(0)); |
| 349 |
if (extendedPaths != null) { |
| 350 |
fResultPaths = extendedPaths; |
| 351 |
super.buttonPressed(IDialogConstants.OK_ID); |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
protected final void configButtonPressed() { |
| 357 |
String id = BuildpathVariablesPreferencePage.ID; |
| 358 |
Map options = new HashMap(); |
| 359 |
List selected = fVariablesList.getSelectedElements(); |
| 360 |
if (!selected.isEmpty()) { |
| 361 |
String varName = ((BPVariableElement) selected.get(0)).getName(); |
| 362 |
options.put(BuildpathVariablesPreferencePage.DATA_SELECT_VARIABLE, |
| 363 |
varName); |
| 364 |
} |
| 365 |
PreferencesUtil.createPreferenceDialogOn(getShell(), id, |
| 366 |
new String[] { id }, options).open(); |
| 367 |
|
| 368 |
List oldElements = fVariablesList.getElements(); |
| 369 |
initializeElements(); |
| 370 |
List newElements = fVariablesList.getElements(); |
| 371 |
newElements.removeAll(oldElements); |
| 372 |
if (!newElements.isEmpty()) { |
| 373 |
fVariablesList.selectElements(new StructuredSelection(newElements)); |
| 374 |
} else if (fVariablesList.getSelectedElements().isEmpty()) { |
| 375 |
fVariablesList.selectFirstElement(); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
} |