|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2003 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Common Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/cpl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.jdt.ui.actions; |
| 12 |
|
| 13 |
import java.lang.reflect.InvocationTargetException; |
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.List; |
| 16 |
|
| 17 |
import org.eclipse.core.runtime.CoreException; |
| 18 |
import org.eclipse.core.runtime.IStatus; |
| 19 |
|
| 20 |
import org.eclipse.swt.layout.GridLayout; |
| 21 |
import org.eclipse.swt.widgets.Composite; |
| 22 |
import org.eclipse.swt.widgets.Shell; |
| 23 |
|
| 24 |
import org.eclipse.jface.dialogs.IDialogConstants; |
| 25 |
import org.eclipse.jface.dialogs.MessageDialog; |
| 26 |
import org.eclipse.jface.operation.IRunnableContext; |
| 27 |
import org.eclipse.jface.text.IRewriteTarget; |
| 28 |
import org.eclipse.jface.text.ITextSelection; |
| 29 |
import org.eclipse.jface.viewers.CheckboxTreeViewer; |
| 30 |
import org.eclipse.jface.viewers.ILabelProvider; |
| 31 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 32 |
import org.eclipse.jface.viewers.ITreeContentProvider; |
| 33 |
import org.eclipse.jface.viewers.Viewer; |
| 34 |
import org.eclipse.jface.window.Window; |
| 35 |
|
| 36 |
import org.eclipse.ui.IEditorPart; |
| 37 |
import org.eclipse.ui.IWorkbenchSite; |
| 38 |
import org.eclipse.ui.dialogs.ISelectionStatusValidator; |
| 39 |
import org.eclipse.ui.help.WorkbenchHelp; |
| 40 |
|
| 41 |
import org.eclipse.jdt.core.ICompilationUnit; |
| 42 |
import org.eclipse.jdt.core.IField; |
| 43 |
import org.eclipse.jdt.core.IJavaElement; |
| 44 |
import org.eclipse.jdt.core.IMethod; |
| 45 |
import org.eclipse.jdt.core.IType; |
| 46 |
import org.eclipse.jdt.core.JavaModelException; |
| 47 |
|
| 48 |
import org.eclipse.jdt.ui.JavaElementLabelProvider; |
| 49 |
|
| 50 |
import org.eclipse.jdt.internal.corext.codemanipulation.AddCustomConstructorOperation; |
| 51 |
import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; |
| 52 |
import org.eclipse.jdt.internal.corext.util.JavaModelUtil; |
| 53 |
import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; |
| 54 |
import org.eclipse.jdt.internal.ui.JavaPlugin; |
| 55 |
import org.eclipse.jdt.internal.ui.actions.ActionMessages; |
| 56 |
import org.eclipse.jdt.internal.ui.actions.ActionUtil; |
| 57 |
import org.eclipse.jdt.internal.ui.actions.SelectionConverter; |
| 58 |
import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter; |
| 59 |
import org.eclipse.jdt.internal.ui.dialogs.SourceActionDialog; |
| 60 |
import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; |
| 61 |
import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor; |
| 62 |
import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; |
| 63 |
import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings; |
| 64 |
import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext; |
| 65 |
import org.eclipse.jdt.internal.ui.util.ElementValidator; |
| 66 |
import org.eclipse.jdt.internal.ui.util.ExceptionHandler; |
| 67 |
|
| 68 |
|
| 69 |
public class CreateNewConstructorAction extends SelectionDispatchAction { |
| 70 |
|
| 71 |
private CompilationUnitEditor fEditor; |
| 72 |
private static final String fDialogTitle= ActionMessages.getString("CreateNewConstructorAction.error.title"); //$NON-NLS-1$ |
| 73 |
|
| 74 |
/** |
| 75 |
* Creates a new <code>CreateNewConstructorAction</code>. The action requires |
| 76 |
* that the selection provided by the site's selection provider is of type <code> |
| 77 |
* org.eclipse.jface.viewers.IStructuredSelection</code>. |
| 78 |
* |
| 79 |
* @param site the site providing context information for this action |
| 80 |
*/ |
| 81 |
public CreateNewConstructorAction(IWorkbenchSite site) { |
| 82 |
super(site); |
| 83 |
setText(ActionMessages.getString("CreateNewConstructorAction.label")); //$NON-NLS-1$ |
| 84 |
setDescription(ActionMessages.getString("CreateNewConstructorAction.description")); //$NON-NLS-1$ |
| 85 |
setToolTipText(ActionMessages.getString("CreateNewConstructorAction.tooltip")); //$NON-NLS-1$ |
| 86 |
|
| 87 |
WorkbenchHelp.setHelp(this, IJavaHelpContextIds.CREATE_NEW_CONSTRUCTOR_ACTION); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Note: This constructor is for internal use only. Clients should not call this constructor. |
| 92 |
*/ |
| 93 |
public CreateNewConstructorAction(CompilationUnitEditor editor) { |
| 94 |
this(editor.getEditorSite()); |
| 95 |
fEditor= editor; |
| 96 |
setEnabled(checkEnabledEditor()); |
| 97 |
} |
| 98 |
|
| 99 |
//---- Structured Viewer ----------------------------------------------------------- |
| 100 |
|
| 101 |
/* (non-Javadoc) |
| 102 |
* Method declared on SelectionDispatchAction |
| 103 |
*/ |
| 104 |
public void selectionChanged(IStructuredSelection selection) { |
| 105 |
try { |
| 106 |
setEnabled(canEnable(selection)); |
| 107 |
} catch (JavaModelException e) { |
| 108 |
// http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253 |
| 109 |
if (JavaModelUtil.filterNotPresentException(e)) |
| 110 |
JavaPlugin.log(e); |
| 111 |
setEnabled(false); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
private boolean canEnable(IStructuredSelection selection) throws JavaModelException { |
| 116 |
if (getSelectedFields(selection) != null) |
| 117 |
return true; |
| 118 |
|
| 119 |
if ((selection.size() == 1) && (selection.getFirstElement() instanceof IType)) { |
| 120 |
IType type= (IType) selection.getFirstElement(); |
| 121 |
return type.getCompilationUnit() != null && type.isClass(); // look if class: not cheap but done by all source generation actions |
| 122 |
} |
| 123 |
|
| 124 |
if ((selection.size() == 1) && (selection.getFirstElement() instanceof ICompilationUnit)) |
| 125 |
return true; |
| 126 |
|
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
/* (non-Javadoc) |
| 131 |
* Method declared on SelectionDispatchAction |
| 132 |
*/ |
| 133 |
public void run(IStructuredSelection selection) { |
| 134 |
try { |
| 135 |
IField[] selectedFields= getSelectedFields(selection); |
| 136 |
// open an editor and work on a working copy |
| 137 |
IEditorPart editor= EditorUtility.openInEditor(getSelectedType(selection)); |
| 138 |
|
| 139 |
if (canRunOn(selectedFields)){ |
| 140 |
run((IType)EditorUtility.getWorkingCopy(selectedFields[0].getDeclaringType()), selectedFields, editor, false); |
| 141 |
return; |
| 142 |
} |
| 143 |
Object firstElement= selection.getFirstElement(); |
| 144 |
|
| 145 |
if (firstElement instanceof IType) |
| 146 |
run((IType)EditorUtility.getWorkingCopy((IType)firstElement), new IField[0], editor, false); |
| 147 |
else if (firstElement instanceof ICompilationUnit) { |
| 148 |
IType type= ((ICompilationUnit) firstElement).findPrimaryType(); |
| 149 |
if (type.isInterface()) { |
| 150 |
MessageDialog.openInformation(getShell(), fDialogTitle, ActionMessages.getString("CreateNewConstructorAction.interface_not_applicable")); //$NON-NLS-1$ |
| 151 |
return; |
| 152 |
} |
| 153 |
else |
| 154 |
run((IType)EditorUtility.getWorkingCopy(((ICompilationUnit) firstElement).findPrimaryType()), new IField[0], editor, false); |
| 155 |
} |
| 156 |
} catch (CoreException e) { |
| 157 |
ExceptionHandler.handle(e, getShell(), fDialogTitle, ActionMessages.getString("CreateNewConstructorAction.error.actionfailed")); //$NON-NLS-1$ |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
private IType getSelectedType(IStructuredSelection selection) throws JavaModelException { |
| 162 |
Object[] elements= selection.toArray(); |
| 163 |
if (elements.length == 1 && (elements[0] instanceof IType)) { |
| 164 |
IType type= (IType) elements[0]; |
| 165 |
if (type.getCompilationUnit() != null && type.isClass()) { |
| 166 |
return type; |
| 167 |
} |
| 168 |
} |
| 169 |
else if (elements[0] instanceof ICompilationUnit) { |
| 170 |
ICompilationUnit cu= (ICompilationUnit) elements[0]; |
| 171 |
IType type= cu.findPrimaryType(); |
| 172 |
if (!type.isInterface()) |
| 173 |
return type; |
| 174 |
} |
| 175 |
return null; |
| 176 |
} |
| 177 |
|
| 178 |
private static boolean canRunOn(IField[] fields) throws JavaModelException { |
| 179 |
return fields != null && fields.length > 0; |
| 180 |
} |
| 181 |
|
| 182 |
/* |
| 183 |
* Returns fields in the selection or <code>null</code> if the selection is |
| 184 |
* empty or not valid. |
| 185 |
*/ |
| 186 |
private IField[] getSelectedFields(IStructuredSelection selection) { |
| 187 |
List elements= selection.toList(); |
| 188 |
int nElements= elements.size(); |
| 189 |
if (nElements > 0) { |
| 190 |
IField[] res= new IField[nElements]; |
| 191 |
ICompilationUnit cu= null; |
| 192 |
for (int i= 0; i < nElements; i++) { |
| 193 |
Object curr= elements.get(i); |
| 194 |
if (curr instanceof IField) { |
| 195 |
IField fld= (IField)curr; |
| 196 |
|
| 197 |
if (i == 0) { |
| 198 |
// remember the cu of the first element |
| 199 |
cu= fld.getCompilationUnit(); |
| 200 |
if (cu == null) { |
| 201 |
return null; |
| 202 |
} |
| 203 |
} else if (!cu.equals(fld.getCompilationUnit())) { |
| 204 |
// all fields must be in the same CU |
| 205 |
return null; |
| 206 |
} |
| 207 |
try { |
| 208 |
if (fld.getDeclaringType().isInterface()) { |
| 209 |
// no constructors for interfaces |
| 210 |
return null; |
| 211 |
} |
| 212 |
} catch (JavaModelException e) { |
| 213 |
JavaPlugin.log(e); |
| 214 |
return null; |
| 215 |
} |
| 216 |
|
| 217 |
res[i]= fld; |
| 218 |
} else { |
| 219 |
return null; |
| 220 |
} |
| 221 |
} |
| 222 |
return res; |
| 223 |
} |
| 224 |
return null; |
| 225 |
} |
| 226 |
|
| 227 |
//---- Java Editior -------------------------------------------------------------- |
| 228 |
|
| 229 |
/* (non-Javadoc) |
| 230 |
* Method declared on SelectionDispatchAction |
| 231 |
*/ |
| 232 |
public void selectionChanged(ITextSelection selection) { |
| 233 |
} |
| 234 |
|
| 235 |
/* (non-Javadoc) |
| 236 |
* Method declared on SelectionDispatchAction |
| 237 |
*/ |
| 238 |
public void run(ITextSelection selection) { |
| 239 |
|
| 240 |
try { |
| 241 |
IJavaElement[] elements= SelectionConverter.codeResolve(fEditor); |
| 242 |
if (elements.length == 1 && (elements[0] instanceof IField)) { |
| 243 |
IField field= (IField)elements[0]; |
| 244 |
run(field.getDeclaringType(), new IField[] {field}, fEditor, false); |
| 245 |
return; |
| 246 |
} |
| 247 |
IJavaElement element= SelectionConverter.getElementAtOffset(fEditor); |
| 248 |
|
| 249 |
if (element != null){ |
| 250 |
IType type= (IType)element.getAncestor(IJavaElement.TYPE); |
| 251 |
if (type != null) { |
| 252 |
if (type.getFields().length > 0) { |
| 253 |
run(type, new IField[0], fEditor, true); |
| 254 |
return; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
MessageDialog.openInformation(getShell(), fDialogTitle, |
| 259 |
ActionMessages.getString("CreateNewConstructorAction.not_applicable")); //$NON-NLS-1$ |
| 260 |
} catch (CoreException e) { |
| 261 |
ExceptionHandler.handle(e, getShell(), getDialogTitle(), null); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
private boolean checkEnabledEditor() { |
| 266 |
return fEditor != null && SelectionConverter.canOperateOn(fEditor); |
| 267 |
} |
| 268 |
|
| 269 |
//---- Helpers ------------------------------------------------------------------- |
| 270 |
|
| 271 |
private void run(IType type, IField[] preselected, IEditorPart editor, boolean activatedFromEditor) throws CoreException { |
| 272 |
if (!ElementValidator.check(type, getShell(), getDialogTitle(), activatedFromEditor)) { |
| 273 |
return; |
| 274 |
} |
| 275 |
if (!ActionUtil.isProcessable(getShell(), type)) { |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
IField[] constructorFields= type.getFields(); |
| 280 |
ArrayList constructorFieldsList= new ArrayList(); |
| 281 |
for (int i= 0; i < constructorFields.length; i++) { |
| 282 |
constructorFieldsList.add(constructorFields[i]); |
| 283 |
} |
| 284 |
if (constructorFieldsList.isEmpty()){ |
| 285 |
MessageDialog.openInformation(getShell(), fDialogTitle, ActionMessages.getString("CreateNewConstructorAction.typeContainsNoFields.message")); //$NON-NLS-1$ |
| 286 |
return; |
| 287 |
} |
| 288 |
|
| 289 |
JavaElementLabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT); |
| 290 |
CreateNewConstructorContentProvider contentProvider = new CreateNewConstructorContentProvider(constructorFieldsList); |
| 291 |
CreateNewConstructorSelectionDialog dialog= new CreateNewConstructorSelectionDialog(getShell(), labelProvider, contentProvider, fEditor, type); |
| 292 |
dialog.setCommentString(ActionMessages.getString("SourceActionDialog.createConstructorComment")); //$NON-NLS-1$ |
| 293 |
dialog.setTitle(getDialogTitle()); |
| 294 |
dialog.setInitialSelections(preselected); |
| 295 |
dialog.setContainerMode(true); |
| 296 |
dialog.setSize(60, 18); |
| 297 |
dialog.setInput(new Object()); |
| 298 |
dialog.setMessage(ActionMessages.getString("CreateNewConstructorAction.dialog.label")); //$NON-NLS-1$ |
| 299 |
dialog.setValidator(createValidator(constructorFields.length)); |
| 300 |
|
| 301 |
IField[] selected= null; |
| 302 |
int dialogResult = dialog.open(); |
| 303 |
if (dialogResult == Window.OK) { |
| 304 |
Object[] checkedElements = dialog.getResult(); |
| 305 |
if (checkedElements == null) |
| 306 |
return; |
| 307 |
ArrayList result= new ArrayList(checkedElements.length); |
| 308 |
for (int i= 0; i < checkedElements.length; i++) { |
| 309 |
Object curr= checkedElements[i]; |
| 310 |
if (curr instanceof IField) { |
| 311 |
result.add(curr); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
selected= (IField[]) result.toArray(new IField[result.size()]); |
| 316 |
|
| 317 |
CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(); |
| 318 |
settings.createComments= dialog.getGenerateComment(); |
| 319 |
|
| 320 |
IJavaElement elementPosition= dialog.getElementPosition(); |
| 321 |
AddCustomConstructorOperation op= new AddCustomConstructorOperation(type, settings, selected, false, elementPosition); |
| 322 |
|
| 323 |
IRewriteTarget target= editor != null ? (IRewriteTarget) editor.getAdapter(IRewriteTarget.class) : null; |
| 324 |
if (target != null) { |
| 325 |
target.beginCompoundChange(); |
| 326 |
} |
| 327 |
try { |
| 328 |
IRunnableContext context= JavaPlugin.getActiveWorkbenchWindow(); |
| 329 |
if (context == null) { |
| 330 |
context= new BusyIndicatorRunnableContext(); |
| 331 |
} |
| 332 |
context.run(false, true, new WorkbenchRunnableAdapter(op)); |
| 333 |
IMethod res= op.getCreatedConstructor(); |
| 334 |
|
| 335 |
if (res.getCompilationUnit().isWorkingCopy()) { |
| 336 |
synchronized(res.getCompilationUnit()) { |
| 337 |
res.getCompilationUnit().reconcile(); |
| 338 |
} |
| 339 |
} |
| 340 |
EditorUtility.revealInEditor(editor, res); |
| 341 |
|
| 342 |
} catch (InvocationTargetException e) { |
| 343 |
ExceptionHandler.handle(e, getShell(), getDialogTitle(), null); |
| 344 |
} catch (InterruptedException e) { |
| 345 |
// Do nothing. Operation has been canceled by user. |
| 346 |
} finally { |
| 347 |
if (target != null) { |
| 348 |
target.endCompoundChange(); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
private static ISelectionStatusValidator createValidator(int entries) { |
| 355 |
CreateNewConstructorValidator validator= new CreateNewConstructorValidator(entries); |
| 356 |
return validator; |
| 357 |
} |
| 358 |
|
| 359 |
private String getDialogTitle() { |
| 360 |
return fDialogTitle; |
| 361 |
} |
| 362 |
|
| 363 |
private static class CreateNewConstructorContentProvider implements ITreeContentProvider { |
| 364 |
|
| 365 |
private List fFieldsList; |
| 366 |
private static final Object[] EMPTY= new Object[0]; |
| 367 |
|
| 368 |
public CreateNewConstructorContentProvider(List fieldList) { |
| 369 |
fFieldsList= fieldList; |
| 370 |
} |
| 371 |
|
| 372 |
/* |
| 373 |
* @see ITreeContentProvider#getChildren(Object) |
| 374 |
*/ |
| 375 |
public Object[] getChildren(Object parentElement) { |
| 376 |
return EMPTY; |
| 377 |
} |
| 378 |
|
| 379 |
/* |
| 380 |
* @see ITreeContentProvider#getParent(Object) |
| 381 |
*/ |
| 382 |
public Object getParent(Object element) { |
| 383 |
return null; |
| 384 |
} |
| 385 |
|
| 386 |
/* |
| 387 |
* @see ITreeContentProvider#hasChildren(Object) |
| 388 |
*/ |
| 389 |
public boolean hasChildren(Object element) { |
| 390 |
return getChildren(element).length > 0; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
/* |
| 395 |
* @see IStructuredContentProvider#getElements(Object) |
| 396 |
*/ |
| 397 |
public Object[] getElements(Object inputElement) { |
| 398 |
return fFieldsList.toArray(); |
| 399 |
} |
| 400 |
|
| 401 |
/* |
| 402 |
* @see IContentProvider#dispose() |
| 403 |
*/ |
| 404 |
public void dispose() { |
| 405 |
} |
| 406 |
|
| 407 |
/* |
| 408 |
* @see IContentProvider#inputChanged(Viewer, Object, Object) |
| 409 |
*/ |
| 410 |
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
| 411 |
} |
| 412 |
|
| 413 |
private List moveUp(List elements, List move) { |
| 414 |
int nElements= elements.size(); |
| 415 |
List res= new ArrayList(nElements); |
| 416 |
Object floating= null; |
| 417 |
for (int i= 0; i < nElements; i++) { |
| 418 |
Object curr= elements.get(i); |
| 419 |
if (move.contains(curr)) { |
| 420 |
res.add(curr); |
| 421 |
} else { |
| 422 |
if (floating != null) { |
| 423 |
res.add(floating); |
| 424 |
} |
| 425 |
floating= curr; |
| 426 |
} |
| 427 |
} |
| 428 |
if (floating != null) { |
| 429 |
res.add(floating); |
| 430 |
} |
| 431 |
return res; |
| 432 |
} |
| 433 |
|
| 434 |
private List reverse(List p) { |
| 435 |
List reverse= new ArrayList(p.size()); |
| 436 |
for (int i= p.size() - 1; i >= 0; i--) { |
| 437 |
reverse.add(p.get(i)); |
| 438 |
} |
| 439 |
return reverse; |
| 440 |
} |
| 441 |
|
| 442 |
public void setElements(List elements, CheckboxTreeViewer tree) { |
| 443 |
fFieldsList= new ArrayList(elements); |
| 444 |
if (tree != null) |
| 445 |
tree.refresh(); |
| 446 |
} |
| 447 |
|
| 448 |
public void up(List checkedElements, CheckboxTreeViewer tree) { |
| 449 |
if (checkedElements.size() > 0) { |
| 450 |
setElements(moveUp(fFieldsList, checkedElements), tree); |
| 451 |
tree.reveal(checkedElements.get(0)); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
public void down(List checkedElements, CheckboxTreeViewer tree) { |
| 456 |
if (checkedElements.size() > 0) { |
| 457 |
setElements(reverse(moveUp(reverse(fFieldsList), checkedElements)), tree); |
| 458 |
tree.reveal(checkedElements.get(checkedElements.size() - 1)); |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
private static class CreateNewConstructorValidator implements ISelectionStatusValidator { |
| 464 |
private static int fEntries; |
| 465 |
|
| 466 |
CreateNewConstructorValidator(int entries) { |
| 467 |
super(); |
| 468 |
fEntries= entries; |
| 469 |
} |
| 470 |
|
| 471 |
public IStatus validate(Object[] selection) { |
| 472 |
int count= countSelectedFields(selection); |
| 473 |
|
| 474 |
String message= ActionMessages.getFormattedString("CreateNewConstructorAction.fields_selected", //$NON-NLS-1$ |
| 475 |
new Object[] { String.valueOf(count), String.valueOf(fEntries)} ); |
| 476 |
return new StatusInfo(IStatus.INFO, message); |
| 477 |
} |
| 478 |
|
| 479 |
private int countSelectedFields(Object[] selection){ |
| 480 |
int count= 0; |
| 481 |
for (int i = 0; i < selection.length; i++) { |
| 482 |
if (selection[i] instanceof IField) |
| 483 |
count++; |
| 484 |
} |
| 485 |
return count; |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
private static class CreateNewConstructorSelectionDialog extends SourceActionDialog { |
| 490 |
private CreateNewConstructorContentProvider fContentProvider; |
| 491 |
private static final int UP_BUTTON= IDialogConstants.CLIENT_ID + 1; |
| 492 |
private static final int DOWN_BUTTON= IDialogConstants.CLIENT_ID + 2; |
| 493 |
|
| 494 |
public CreateNewConstructorSelectionDialog(Shell parent, ILabelProvider labelProvider, CreateNewConstructorContentProvider contentProvider, CompilationUnitEditor editor, IType type) { |
| 495 |
super(parent, labelProvider, contentProvider, editor, type); |
| 496 |
fContentProvider= contentProvider; |
| 497 |
} |
| 498 |
|
| 499 |
protected Composite createSelectionButtons(Composite composite) { |
| 500 |
Composite buttonComposite= super.createSelectionButtons(composite); |
| 501 |
|
| 502 |
GridLayout layout = new GridLayout(); |
| 503 |
buttonComposite.setLayout(layout); |
| 504 |
|
| 505 |
createUpDownButtons(buttonComposite); |
| 506 |
|
| 507 |
layout.marginHeight= 0; |
| 508 |
layout.marginWidth= 0; |
| 509 |
layout.numColumns= 1; |
| 510 |
|
| 511 |
return buttonComposite; |
| 512 |
} |
| 513 |
|
| 514 |
protected void createUpDownButtons(Composite buttonComposite) { |
| 515 |
createButton(buttonComposite, UP_BUTTON, ActionMessages.getString("CreateNewConstructorSelectionDialog.up_button"), false); //$NON-NLS-1$ |
| 516 |
createButton(buttonComposite, DOWN_BUTTON, ActionMessages.getString("CreateNewConstructorSelectionDialog.down_button"), false); //$NON-NLS-1$ |
| 517 |
} |
| 518 |
|
| 519 |
protected void buttonPressed(int buttonId) { |
| 520 |
super.buttonPressed(buttonId); |
| 521 |
switch(buttonId) { |
| 522 |
case UP_BUTTON: { |
| 523 |
fContentProvider.up(getElementList(), getTreeViewer()); |
| 524 |
updateOKStatus(); |
| 525 |
break; |
| 526 |
} |
| 527 |
case DOWN_BUTTON: { |
| 528 |
fContentProvider.down(getElementList(), getTreeViewer()); |
| 529 |
updateOKStatus(); |
| 530 |
break; |
| 531 |
} |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
private List getElementList() { |
| 536 |
Object elements[]= getTreeViewer().getCheckedElements(); |
| 537 |
ArrayList elementList= new ArrayList(); |
| 538 |
for(int i = 0; i < elements.length; i++) |
| 539 |
elementList.add(elements[i]); |
| 540 |
return elementList; |
| 541 |
} |
| 542 |
|
| 543 |
} |
| 544 |
|
| 545 |
} |