]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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.jdt.internal.ui.refactoring; | |
12 | ||
13 | import java.util.ArrayList; | |
14 | import java.util.Arrays; | |
15 | import java.util.Collections; | |
16 | import java.util.Iterator; | |
17 | import java.util.List; | |
18 | ||
19 | import org.eclipse.swt.SWT; | |
20 | import org.eclipse.swt.events.KeyAdapter; | |
21 | import org.eclipse.swt.events.KeyEvent; | |
22 | import org.eclipse.swt.events.SelectionAdapter; | |
23 | import org.eclipse.swt.events.SelectionEvent; | |
24 | import org.eclipse.swt.events.TraverseEvent; | |
25 | import org.eclipse.swt.events.TraverseListener; | |
26 | import org.eclipse.swt.graphics.Font; | |
27 | import org.eclipse.swt.graphics.Image; | |
28 | import org.eclipse.swt.layout.GridData; | |
29 | import org.eclipse.swt.layout.GridLayout; | |
30 | import org.eclipse.swt.widgets.Button; | |
31 | import org.eclipse.swt.widgets.Composite; | |
32 | import org.eclipse.swt.widgets.Label; | |
33 | import org.eclipse.swt.widgets.Table; | |
34 | import org.eclipse.swt.widgets.TableItem; | |
35 | import org.eclipse.swt.widgets.Text; | |
36 | ||
37 | import org.eclipse.core.runtime.Assert; | |
38 | ||
39 | import org.eclipse.jface.contentassist.SubjectControlContentAssistant; | |
40 | import org.eclipse.jface.resource.JFaceResources; | |
41 | import org.eclipse.jface.viewers.ICellModifier; | |
42 | import org.eclipse.jface.viewers.ISelection; | |
43 | import org.eclipse.jface.viewers.ISelectionChangedListener; | |
44 | import org.eclipse.jface.viewers.IStructuredContentProvider; | |
45 | import org.eclipse.jface.viewers.IStructuredSelection; | |
46 | import org.eclipse.jface.viewers.ITableFontProvider; | |
47 | import org.eclipse.jface.viewers.ITableLabelProvider; | |
48 | import org.eclipse.jface.viewers.LabelProvider; | |
49 | import org.eclipse.jface.viewers.SelectionChangedEvent; | |
50 | import org.eclipse.jface.viewers.StructuredSelection; | |
51 | import org.eclipse.jface.viewers.TableViewer; | |
52 | import org.eclipse.jface.viewers.Viewer; | |
53 | ||
54 | import org.eclipse.ui.contentassist.ContentAssistHandler; | |
55 | ||
56 | import org.eclipse.jdt.core.IJavaProject; | |
57 | ||
58 | import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; | |
59 | import org.eclipse.jdt.internal.corext.refactoring.ParameterInfo; | |
60 | import org.eclipse.jdt.internal.corext.refactoring.StubTypeContext; | |
61 | ||
62 | import org.eclipse.jdt.internal.ui.dialogs.TableTextCellEditor; | |
63 | import org.eclipse.jdt.internal.ui.refactoring.ChangeSignatureWizard.ChangeSignatureInputPage; | |
64 | import org.eclipse.jdt.internal.ui.refactoring.IntroduceParameterWizard.IntroduceParameterInputPage; | |
65 | import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper; | |
66 | import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaTypeCompletionProcessor; | |
67 | import org.eclipse.jdt.internal.ui.refactoring.contentassist.VariableNamesProcessor; | |
68 | import org.eclipse.jdt.internal.ui.util.SWTUtil; | |
69 | import org.eclipse.jdt.internal.ui.util.TableLayoutComposite; | |
70 | ||
71 | /** | |
72 | * A special control to edit and reorder method parameters. | |
73 | */ | |
74 | public class ChangeParametersControl extends Composite { | |
75 | ||
76 | public static class Mode { | |
77 | private final String fName; | |
78 | private Mode(String name) { | |
79 | fName= name; | |
80 | } | |
81 | public static final Mode EXTRACT_METHOD= new Mode("EXTRACT_METHOD"); //$NON-NLS-1$ | |
82 | public static final Mode CHANGE_METHOD_SIGNATURE= new Mode("CHANGE_METHOD_SIGNATURE"); //$NON-NLS-1$ | |
83 | public static final Mode INTRODUCE_PARAMETER= new Mode("INTRODUCE_PARAMETER"); //$NON-NLS-1$ | |
84 | @Override | |
85 | public String toString() { | |
86 | return fName; | |
87 | } | |
88 | public boolean canChangeTypes() { | |
89 | return this == CHANGE_METHOD_SIGNATURE; | |
90 | } | |
91 | public boolean canAddParameters() { | |
92 | return this == Mode.CHANGE_METHOD_SIGNATURE; | |
93 | } | |
94 | public boolean canChangeDefault() { | |
95 | return this == Mode.CHANGE_METHOD_SIGNATURE; | |
96 | } | |
97 | public void generated_4440898246077263045(ChangeParametersControl changeparameterscontrol, Composite buttonComposite) { | |
98 | GridLayout gl= new GridLayout(); | |
99 | gl.marginHeight= 0; | |
100 | gl.marginWidth= 0; | |
101 | buttonComposite.setLayout(gl); | |
102 | ||
103 | if (canAddParameters()) | |
104 | changeparameterscontrol.fAddButton= changeparameterscontrol.createAddButton(buttonComposite); | |
105 | ||
106 | changeparameterscontrol.fEditButton= changeparameterscontrol.createEditButton(buttonComposite); | |
107 | ||
108 | if (canAddParameters()) | |
109 | changeparameterscontrol.fRemoveButton= changeparameterscontrol.createRemoveButton(buttonComposite); | |
110 | ||
111 | if (buttonComposite.getChildren().length != 0) | |
112 | changeparameterscontrol.addSpacer(buttonComposite); | |
113 | ||
114 | changeparameterscontrol.fUpButton= changeparameterscontrol.createButton(buttonComposite, RefactoringMessages.ChangeParametersControl_buttons_move_up, true); | |
115 | } | |
116 | public void generated_8586556560671749549(final ChangeParametersControl changeparameterscontrol, Button button) { | |
117 | SWTUtil.setButtonDimensionHint(button); | |
118 | button.addSelectionListener(new SelectionAdapter() { | |
119 | @Override | |
120 | public void widgetSelected(SelectionEvent e) { | |
121 | try { | |
122 | ParameterInfo[] selected= changeparameterscontrol.getSelectedElements(); | |
123 | Assert.isTrue(selected.length == 1); | |
124 | ParameterInfo parameterInfo= selected[0]; | |
125 | ParameterEditDialog dialog= new ParameterEditDialog(changeparameterscontrol.getShell(), parameterInfo, canChangeTypes(), canChangeDefault(), changeparameterscontrol.fTypeContext); | |
126 | dialog.open(); | |
127 | changeparameterscontrol.fListener.parameterChanged(parameterInfo); | |
128 | changeparameterscontrol.fTableViewer.update(parameterInfo, ChangeParametersControl.PROPERTIES); | |
129 | } finally { | |
130 | changeparameterscontrol.fTableViewer.getControl().setFocus(); | |
131 | } | |
132 | } | |
133 | }); | |
134 | } | |
135 | } | |
136 | ||
137 | private static class ParameterInfoContentProvider implements IStructuredContentProvider { | |
138 | @SuppressWarnings("unchecked") | |
139 | public Object[] getElements(Object inputElement) { | |
140 | return removeMarkedAsDeleted((List<ParameterInfo>) inputElement); | |
141 | } | |
142 | private ParameterInfo[] removeMarkedAsDeleted(List<ParameterInfo> paramInfos){ | |
143 | List<ParameterInfo> result= new ArrayList<ParameterInfo>(paramInfos.size()); | |
144 | for (Iterator<ParameterInfo> iter= paramInfos.iterator(); iter.hasNext();) { | |
145 | ParameterInfo info= iter.next(); | |
146 | info.generated_3372266110055682377(result); | |
147 | } | |
148 | return result.toArray(new ParameterInfo[result.size()]); | |
149 | } | |
150 | public void dispose() { | |
151 | // do nothing | |
152 | } | |
153 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { | |
154 | // do nothing | |
155 | } | |
156 | } | |
157 | ||
158 | private static class ParameterInfoLabelProvider extends LabelProvider implements ITableLabelProvider, ITableFontProvider { | |
159 | public Image getColumnImage(Object element, int columnIndex) { | |
160 | return null; | |
161 | } | |
162 | public String getColumnText(Object element, int columnIndex) { | |
163 | ParameterInfo info= (ParameterInfo) element; | |
164 | switch (columnIndex) { | |
165 | case TYPE_PROP: | |
166 | return info.getNewTypeName(); | |
167 | case NEWNAME_PROP: | |
168 | return info.getNewName(); | |
169 | case DEFAULT_PROP: | |
170 | return info.generated_2610381451030487894(); //$NON-NLS-1$ | |
171 | default: | |
172 | throw new IllegalArgumentException(columnIndex + ": " + element); //$NON-NLS-1$ | |
173 | } | |
174 | } | |
175 | public Font getFont(Object element, int columnIndex) { | |
176 | ParameterInfo info= (ParameterInfo) element; | |
177 | if (info.isAdded()) | |
178 | return JFaceResources.getFontRegistry().getBold(JFaceResources.DIALOG_FONT); | |
179 | else | |
180 | return null; | |
181 | } | |
182 | } | |
183 | ||
184 | private class ParametersCellModifier implements ICellModifier { | |
185 | public boolean canModify(Object element, String property) { | |
186 | Assert.isTrue(element instanceof ParameterInfo); | |
187 | if (property.equals(PROPERTIES[TYPE_PROP])) | |
188 | return fMode.canChangeTypes(); | |
189 | else if (property.equals(PROPERTIES[NEWNAME_PROP])) | |
190 | return true; | |
191 | else if (property.equals(PROPERTIES[DEFAULT_PROP])) | |
192 | return (((ParameterInfo)element).isAdded()); | |
193 | Assert.isTrue(false); | |
194 | return false; | |
195 | } | |
196 | public Object getValue(Object element, String property) { | |
197 | Assert.isTrue(element instanceof ParameterInfo); | |
198 | if (property.equals(PROPERTIES[TYPE_PROP])) | |
199 | return ((ParameterInfo) element).getNewTypeName(); | |
200 | else if (property.equals(PROPERTIES[NEWNAME_PROP])) | |
201 | return ((ParameterInfo) element).getNewName(); | |
202 | else if (property.equals(PROPERTIES[DEFAULT_PROP])) | |
203 | return ((ParameterInfo) element).getDefaultValue(); | |
204 | Assert.isTrue(false); | |
205 | return null; | |
206 | } | |
207 | public void modify(Object element, String property, Object value) { | |
208 | if (element instanceof TableItem) | |
209 | element= ((TableItem) element).getData(); | |
210 | if (!(element instanceof ParameterInfo)) | |
211 | return; | |
212 | boolean unchanged; | |
213 | ParameterInfo parameterInfo= (ParameterInfo) element; | |
214 | unchanged= parameterInfo.generated_814774772450749138(property, value, unchanged); | |
215 | if (! unchanged) { | |
216 | ChangeParametersControl.this.fListener.parameterChanged(parameterInfo); | |
217 | ChangeParametersControl.this.fTableViewer.update(parameterInfo, new String[] { property }); | |
218 | } | |
219 | } | |
220 | } | |
221 | ||
222 | public static final String[] PROPERTIES= { "type", "new", "default" }; //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-3$ | |
223 | public static final int TYPE_PROP= 0; | |
224 | public static final int NEWNAME_PROP= 1; | |
225 | public static final int DEFAULT_PROP= 2; | |
226 | ||
227 | public static final int ROW_COUNT= 7; | |
228 | ||
229 | public final Mode fMode; | |
230 | private final IParameterListChangeListener fListener; | |
231 | private List<ParameterInfo> fParameterInfos; | |
232 | public final StubTypeContext fTypeContext; | |
233 | private final String[] fParamNameProposals; | |
234 | private ContentAssistHandler fNameContentAssistHandler; | |
235 | ||
236 | public TableViewer fTableViewer; | |
237 | private Button fUpButton; | |
238 | private Button fDownButton; | |
239 | private Button fEditButton; | |
240 | private Button fAddButton; | |
241 | private Button fRemoveButton; | |
242 | ||
243 | public ChangeParametersControl(Composite parent, int style, String label, IParameterListChangeListener listener, Mode mode, StubTypeContext typeContext) { | |
244 | this(parent, style, label, listener, mode, typeContext, new String[0]); | |
245 | } | |
246 | ||
247 | public ChangeParametersControl(Composite parent, int style, String label, IParameterListChangeListener listener, Mode mode) { | |
248 | this(parent, style, label, listener, mode, null, new String[0]); | |
249 | } | |
250 | ||
251 | public ChangeParametersControl(Composite parent, int style, String label, IParameterListChangeListener listener, Mode mode, String[] paramNameProposals) { | |
252 | this(parent, style, label, listener, mode, null, paramNameProposals); | |
253 | } | |
254 | ||
255 | /** | |
256 | * @param label the label before the table or <code>null</code> | |
257 | * @param typeContext the package in which to complete types | |
258 | */ | |
259 | private ChangeParametersControl(Composite parent, int style, String label, IParameterListChangeListener listener, Mode mode, StubTypeContext typeContext, String[] paramNameProposals) { | |
260 | super(parent, style); | |
261 | Assert.isNotNull(listener); | |
262 | fListener= listener; | |
263 | fMode= mode; | |
264 | fTypeContext= typeContext; | |
265 | fParamNameProposals= paramNameProposals; | |
266 | ||
267 | GridLayout layout= new GridLayout(); | |
268 | layout.numColumns= 2; | |
269 | layout.marginWidth= 0; | |
270 | layout.marginHeight= 0; | |
271 | setLayout(layout); | |
272 | ||
273 | if (label != null) { | |
274 | Label tableLabel= new Label(this, SWT.NONE); | |
275 | GridData labelGd= new GridData(); | |
276 | labelGd.horizontalSpan= 2; | |
277 | tableLabel.setLayoutData(labelGd); | |
278 | tableLabel.setText(label); | |
279 | } | |
280 | ||
281 | createParameterList(this); | |
282 | createButtonComposite(this); | |
283 | } | |
284 | ||
285 | ||
286 | public void setInput(List<ParameterInfo> parameterInfos) { | |
287 | Assert.isNotNull(parameterInfos); | |
288 | fParameterInfos= parameterInfos; | |
289 | fTableViewer.setInput(fParameterInfos); | |
290 | if (fParameterInfos.size() > 0) | |
291 | fTableViewer.setSelection(new StructuredSelection(fParameterInfos.get(0))); | |
292 | } | |
293 | ||
294 | public void editParameter(ParameterInfo info) { | |
295 | info.generated_4087966562466953086(this); | |
296 | } | |
297 | ||
298 | ||
299 | ||
300 | // ---- Parameter table ----------------------------------------------------------------------------------- | |
301 | ||
302 | private void createParameterList(Composite parent) { | |
303 | TableLayoutComposite layouter= new TableLayoutComposite(parent, SWT.NONE); | |
304 | final Table table= layouter.generated_9197196252329882176(this); | |
305 | ||
306 | fTableViewer= new TableViewer(table); | |
307 | fTableViewer.setUseHashlookup(true); | |
308 | fTableViewer.setContentProvider(new ParameterInfoContentProvider()); | |
309 | fTableViewer.setLabelProvider(new ParameterInfoLabelProvider()); | |
310 | fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() { | |
311 | public void selectionChanged(SelectionChangedEvent event) { | |
312 | updateButtonsEnabledState(); | |
313 | } | |
314 | }); | |
315 | ||
316 | table.addTraverseListener(new TraverseListener() { | |
317 | public void keyTraversed(TraverseEvent e) { | |
318 | if (e.detail == SWT.TRAVERSE_RETURN && e.stateMask == SWT.NONE) { | |
319 | editColumnOrNextPossible(0); | |
320 | e.detail= SWT.TRAVERSE_NONE; | |
321 | } | |
322 | } | |
323 | }); | |
324 | table.addKeyListener(new KeyAdapter() { | |
325 | @Override | |
326 | public void keyPressed(KeyEvent e) { | |
327 | if (e.keyCode == SWT.F2 && e.stateMask == SWT.NONE) { | |
328 | editColumnOrNextPossible(0); | |
329 | e.doit= false; | |
330 | } | |
331 | } | |
332 | }); | |
333 | ||
334 | addCellEditors(); | |
335 | } | |
336 | ||
337 | public void editColumnOrNextPossible(int column){ | |
338 | ParameterInfo[] selected= getSelectedElements(); | |
339 | if (selected.length != 1) | |
340 | return; | |
341 | int nextColumn= column; | |
342 | do { | |
343 | fTableViewer.editElement(selected[0], nextColumn); | |
344 | if (fTableViewer.isCellEditorActive()) | |
345 | return; | |
346 | nextColumn= nextColumn(nextColumn); | |
347 | } while (nextColumn != column); | |
348 | } | |
349 | ||
350 | public void editColumnOrPrevPossible(int column){ | |
351 | ParameterInfo[] selected= getSelectedElements(); | |
352 | if (selected.length != 1) | |
353 | return; | |
354 | int prevColumn= column; | |
355 | do { | |
356 | fTableViewer.editElement(selected[0], prevColumn); | |
357 | if (fTableViewer.isCellEditorActive()) | |
358 | return; | |
359 | prevColumn= prevColumn(prevColumn); | |
360 | } while (prevColumn != column); | |
361 | } | |
362 | ||
363 | public int nextColumn(int column) { | |
364 | return (column >= getTable().getColumnCount() - 1) ? 0 : column + 1; | |
365 | } | |
366 | ||
367 | public int prevColumn(int column) { | |
368 | return (column <= 0) ? getTable().getColumnCount() - 1 : column - 1; | |
369 | } | |
370 | ||
371 | public void addColumnLayoutData(TableLayoutComposite layouter) { | |
372 | layouter.generated_4716778872934486415(this); | |
373 | } | |
374 | ||
375 | private ParameterInfo[] getSelectedElements() { | |
376 | ISelection selection= fTableViewer.getSelection(); | |
377 | if (selection == null) | |
378 | return new ParameterInfo[0]; | |
379 | ||
380 | if (!(selection instanceof IStructuredSelection)) | |
381 | return new ParameterInfo[0]; | |
382 | ||
383 | List<?> selected= ((IStructuredSelection) selection).toList(); | |
384 | return selected.toArray(new ParameterInfo[selected.size()]); | |
385 | } | |
386 | ||
387 | // ---- Button bar -------------------------------------------------------------------------------------- | |
388 | ||
389 | private void createButtonComposite(Composite parent) { | |
390 | Composite buttonComposite= new Composite(parent, SWT.NONE); | |
391 | buttonComposite.setLayoutData(new GridData(GridData.FILL_VERTICAL)); | |
392 | fMode.generated_4440898246077263045(this, buttonComposite); | |
393 | fDownButton= createButton(buttonComposite, RefactoringMessages.ChangeParametersControl_buttons_move_down, false); | |
394 | ||
395 | updateButtonsEnabledState(); | |
396 | } | |
397 | ||
398 | private void addSpacer(Composite parent) { | |
399 | Label label= new Label(parent, SWT.NONE); | |
400 | GridData gd= new GridData(GridData.FILL_HORIZONTAL); | |
401 | gd.heightHint= 5; | |
402 | label.setLayoutData(gd); | |
403 | } | |
404 | ||
405 | public void updateButtonsEnabledState() { | |
406 | fUpButton.setEnabled(canMove(true)); | |
407 | fDownButton.setEnabled(canMove(false)); | |
408 | if (fEditButton != null) | |
409 | fEditButton.setEnabled(getTableSelectionCount() == 1); | |
410 | if (fAddButton != null) | |
411 | fAddButton.setEnabled(true); | |
412 | if (fRemoveButton != null) | |
413 | fRemoveButton.setEnabled(getTableSelectionCount() != 0); | |
414 | } | |
415 | ||
416 | private int getTableSelectionCount() { | |
417 | return getTable().getSelectionCount(); | |
418 | } | |
419 | ||
420 | private int getTableItemCount() { | |
421 | return getTable().getItemCount(); | |
422 | } | |
423 | ||
424 | private Table getTable() { | |
425 | return fTableViewer.getTable(); | |
426 | } | |
427 | ||
428 | private Button createEditButton(Composite buttonComposite) { | |
429 | Button button= new Button(buttonComposite, SWT.PUSH); | |
430 | button.setText(RefactoringMessages.ChangeParametersControl_buttons_edit); | |
431 | button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
432 | fMode.generated_8586556560671749549(this, button); | |
433 | return button; | |
434 | } | |
435 | ||
436 | private Button createAddButton(Composite buttonComposite) { | |
437 | Button button= new Button(buttonComposite, SWT.PUSH); | |
438 | button.setText(RefactoringMessages.ChangeParametersControl_buttons_add); | |
439 | button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
440 | SWTUtil.setButtonDimensionHint(button); | |
441 | button.addSelectionListener(new SelectionAdapter() { | |
442 | @Override | |
443 | public void widgetSelected(SelectionEvent e) { | |
444 | String[] excludedParamNames= new String[fParameterInfos.size()]; | |
445 | for (int i= 0; i < fParameterInfos.size(); i++) { | |
446 | ParameterInfo info= fParameterInfos.get(i); | |
447 | excludedParamNames[i]= info.getNewName(); | |
448 | } | |
449 | IJavaProject javaProject= fTypeContext.getCuHandle().getJavaProject(); | |
450 | String newParamName= StubUtility.suggestArgumentName(javaProject, RefactoringMessages.ChangeParametersControl_new_parameter_default_name, excludedParamNames); | |
451 | ParameterInfo newInfo= ParameterInfo.createInfoForAddedParameter("Object", newParamName, "null"); //$NON-NLS-1$ //$NON-NLS-2$ | |
452 | int insertIndex= fParameterInfos.size(); | |
453 | for (int i= fParameterInfos.size() - 1; i >= 0; i--) { | |
454 | ParameterInfo info= fParameterInfos.get(i); | |
455 | if (info.isNewVarargs()) { | |
456 | insertIndex= i; | |
457 | break; | |
458 | } | |
459 | } | |
460 | fParameterInfos.add(insertIndex, newInfo); | |
461 | fListener.parameterAdded(newInfo); | |
462 | fTableViewer.refresh(); | |
463 | fTableViewer.getControl().setFocus(); | |
464 | fTableViewer.setSelection(new StructuredSelection(newInfo), true); | |
465 | updateButtonsEnabledState(); | |
466 | editColumnOrNextPossible(0); | |
467 | } | |
468 | }); | |
469 | return button; | |
470 | } | |
471 | ||
472 | private Button createRemoveButton(Composite buttonComposite) { | |
473 | final Button button= new Button(buttonComposite, SWT.PUSH); | |
474 | button.setText(RefactoringMessages.ChangeParametersControl_buttons_remove); | |
475 | button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
476 | SWTUtil.setButtonDimensionHint(button); | |
477 | button.addSelectionListener(new SelectionAdapter() { | |
478 | @Override | |
479 | public void widgetSelected(SelectionEvent e) { | |
480 | int index= getTable().getSelectionIndices()[0]; | |
481 | ParameterInfo[] selected= getSelectedElements(); | |
482 | for (int i= 0; i < selected.length; i++) { | |
483 | if (selected[i].isAdded()) | |
484 | fParameterInfos.remove(selected[i]); | |
485 | else | |
486 | selected[i].markAsDeleted(); | |
487 | } | |
488 | restoreSelection(index); | |
489 | } | |
490 | private void restoreSelection(int index) { | |
491 | fTableViewer.refresh(); | |
492 | fTableViewer.getControl().setFocus(); | |
493 | int itemCount= getTableItemCount(); | |
494 | if (itemCount != 0) { | |
495 | if (index >= itemCount) | |
496 | index= itemCount - 1; | |
497 | getTable().setSelection(index); | |
498 | } | |
499 | fListener.parameterListChanged(); | |
500 | updateButtonsEnabledState(); | |
501 | } | |
502 | }); | |
503 | return button; | |
504 | } | |
505 | ||
506 | private Button createButton(Composite buttonComposite, String text, final boolean up) { | |
507 | Button button= new Button(buttonComposite, SWT.PUSH); | |
508 | button.setText(text); | |
509 | button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
510 | SWTUtil.setButtonDimensionHint(button); | |
511 | button.addSelectionListener(new SelectionAdapter() { | |
512 | @Override | |
513 | public void widgetSelected(SelectionEvent e) { | |
514 | ISelection savedSelection= fTableViewer.getSelection(); | |
515 | if (savedSelection == null) | |
516 | return; | |
517 | ParameterInfo[] selection= getSelectedElements(); | |
518 | if (selection.length == 0) | |
519 | return; | |
520 | ||
521 | if (up) { | |
522 | moveUp(selection); | |
523 | } else { | |
524 | moveDown(selection); | |
525 | } | |
526 | fTableViewer.refresh(); | |
527 | fTableViewer.setSelection(savedSelection); | |
528 | fListener.parameterListChanged(); | |
529 | fTableViewer.getControl().setFocus(); | |
530 | } | |
531 | }); | |
532 | return button; | |
533 | } | |
534 | ||
535 | //---- editing ----------------------------------------------------------------------------------------------- | |
536 | ||
537 | private void addCellEditors() { | |
538 | fTableViewer.setColumnProperties(PROPERTIES); | |
539 | ||
540 | final TableTextCellEditor editors[]= new TableTextCellEditor[PROPERTIES.length]; | |
541 | ||
542 | editors[TYPE_PROP]= new TableTextCellEditor(fTableViewer, TYPE_PROP); | |
543 | editors[NEWNAME_PROP]= new TableTextCellEditor(fTableViewer, NEWNAME_PROP); | |
544 | editors[DEFAULT_PROP]= new TableTextCellEditor(fTableViewer, DEFAULT_PROP); | |
545 | ||
546 | if (fMode.canChangeTypes()) { | |
547 | SubjectControlContentAssistant assistant= installParameterTypeContentAssist(editors[TYPE_PROP].getText()); | |
548 | editors[TYPE_PROP].setContentAssistant(assistant); | |
549 | } | |
550 | if (fParamNameProposals.length > 0) { | |
551 | SubjectControlContentAssistant assistant= installParameterNameContentAssist(editors[NEWNAME_PROP].getText()); | |
552 | editors[NEWNAME_PROP].setContentAssistant(assistant); | |
553 | } | |
554 | ||
555 | for (int i = 0; i < editors.length; i++) { | |
556 | final int editorColumn= i; | |
557 | final TableTextCellEditor editor = editors[i]; | |
558 | // support tabbing between columns while editing: | |
559 | editor.generated_3875397303026142273(editorColumn, this); | |
560 | } | |
561 | ||
562 | editors[NEWNAME_PROP].setActivationListener(new TableTextCellEditor.IActivationListener(){ | |
563 | public void activate() { | |
564 | ParameterInfo[] selected= getSelectedElements(); | |
565 | if (selected.length == 1 && fNameContentAssistHandler != null) { | |
566 | fNameContentAssistHandler.setEnabled(selected[0].isAdded()); | |
567 | } | |
568 | } | |
569 | }); | |
570 | ||
571 | fTableViewer.setCellEditors(editors); | |
572 | fTableViewer.setCellModifier(new ParametersCellModifier()); | |
573 | } | |
574 | ||
575 | private SubjectControlContentAssistant installParameterTypeContentAssist(Text text) { | |
576 | JavaTypeCompletionProcessor processor= new JavaTypeCompletionProcessor(true, false); | |
577 | if (fTypeContext == null) | |
578 | processor.setCompletionContext(null, null, null); | |
579 | else | |
580 | fTypeContext.generated_3466710071222815815(processor); | |
581 | SubjectControlContentAssistant contentAssistant= ControlContentAssistHelper.createJavaContentAssistant(processor); | |
582 | ContentAssistHandler.createHandlerForText(text, contentAssistant); | |
583 | return contentAssistant; | |
584 | } | |
585 | ||
586 | private SubjectControlContentAssistant installParameterNameContentAssist(Text text) { | |
587 | VariableNamesProcessor processor= new VariableNamesProcessor(fParamNameProposals); | |
588 | SubjectControlContentAssistant contentAssistant= ControlContentAssistHelper.createJavaContentAssistant(processor); | |
589 | fNameContentAssistHandler= ContentAssistHandler.createHandlerForText(text, contentAssistant); | |
590 | return contentAssistant; | |
591 | } | |
592 | ||
593 | //---- change order ---------------------------------------------------------------------------------------- | |
594 | ||
595 | private void moveUp(ParameterInfo[] selection) { | |
596 | moveUp(fParameterInfos, Arrays.asList(selection)); | |
597 | } | |
598 | ||
599 | private void moveDown(ParameterInfo[] selection) { | |
600 | Collections.reverse(fParameterInfos); | |
601 | moveUp(fParameterInfos, Arrays.asList(selection)); | |
602 | Collections.reverse(fParameterInfos); | |
603 | } | |
604 | ||
605 | private static void moveUp(List<ParameterInfo> elements, List<ParameterInfo> move) { | |
606 | List<ParameterInfo> res= new ArrayList<ParameterInfo>(elements.size()); | |
607 | List<ParameterInfo> deleted= new ArrayList<ParameterInfo>(); | |
608 | ParameterInfo floating= null; | |
609 | for (Iterator<ParameterInfo> iter= elements.iterator(); iter.hasNext();) { | |
610 | ParameterInfo curr= iter.next(); | |
611 | if (move.contains(curr)) { | |
612 | res.add(curr); | |
613 | } else if (curr.isDeleted()) { | |
614 | deleted.add(curr); | |
615 | } else { | |
616 | if (floating != null) | |
617 | res.add(floating); | |
618 | floating= curr; | |
619 | } | |
620 | } | |
621 | if (floating != null) { | |
622 | res.add(floating); | |
623 | } | |
624 | res.addAll(deleted); | |
625 | elements.clear(); | |
626 | for (Iterator<ParameterInfo> iter= res.iterator(); iter.hasNext();) { | |
627 | elements.add(iter.next()); | |
628 | } | |
629 | } | |
630 | ||
631 | private boolean canMove(boolean up) { | |
632 | int notDeletedInfosCount= getNotDeletedInfosCount(); | |
633 | if (notDeletedInfosCount == 0) | |
634 | return false; | |
635 | int[] indc= getTable().getSelectionIndices(); | |
636 | if (indc.length == 0) | |
637 | return false; | |
638 | int invalid= up ? 0 : notDeletedInfosCount - 1; | |
639 | for (int i= 0; i < indc.length; i++) { | |
640 | if (indc[i] == invalid) | |
641 | return false; | |
642 | } | |
643 | return true; | |
644 | } | |
645 | ||
646 | private int getNotDeletedInfosCount(){ | |
647 | if (fParameterInfos == null) // during initialization | |
648 | return 0; | |
649 | int result= 0; | |
650 | for (Iterator<ParameterInfo> iter= fParameterInfos.iterator(); iter.hasNext();) { | |
651 | ParameterInfo info= iter.next(); | |
652 | if (! info.isDeleted()) | |
653 | result++; | |
654 | } | |
655 | return result; | |
656 | } | |
657 | ||
658 | public ChangeParametersControl generated_6445393995122872922(IntroduceParameterInputPage introduceparameterinputpage) { | |
659 | setInput(introduceparameterinputpage.getIntroduceParameterRefactoring().getParameterInfos()); | |
660 | editParameter(introduceparameterinputpage.getIntroduceParameterRefactoring().getAddedParameterInfo()); | |
661 | return this; | |
662 | } | |
663 | ||
664 | public void generated_1563505850580473698(ChangeSignatureInputPage changesignatureinputpage) { | |
665 | setLayoutData(new GridData(GridData.FILL_BOTH)); | |
666 | setInput(changesignatureinputpage.getChangeMethodSignatureProcessor().getParameterInfos()); | |
667 | } | |
668 | } |