KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > ChangeParametersControl


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.KeyAdapter;
23 import org.eclipse.swt.events.KeyEvent;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.TraverseEvent;
27 import org.eclipse.swt.events.TraverseListener;
28 import org.eclipse.swt.graphics.Font;
29 import org.eclipse.swt.graphics.Image;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Table;
36 import org.eclipse.swt.widgets.TableColumn;
37 import org.eclipse.swt.widgets.TableItem;
38 import org.eclipse.swt.widgets.Text;
39
40 import org.eclipse.jface.contentassist.SubjectControlContentAssistant;
41 import org.eclipse.jface.resource.JFaceResources;
42 import org.eclipse.jface.viewers.ColumnWeightData;
43 import org.eclipse.jface.viewers.ICellModifier;
44 import org.eclipse.jface.viewers.ISelection;
45 import org.eclipse.jface.viewers.ISelectionChangedListener;
46 import org.eclipse.jface.viewers.IStructuredContentProvider;
47 import org.eclipse.jface.viewers.IStructuredSelection;
48 import org.eclipse.jface.viewers.ITableFontProvider;
49 import org.eclipse.jface.viewers.ITableLabelProvider;
50 import org.eclipse.jface.viewers.LabelProvider;
51 import org.eclipse.jface.viewers.SelectionChangedEvent;
52 import org.eclipse.jface.viewers.StructuredSelection;
53 import org.eclipse.jface.viewers.TableViewer;
54 import org.eclipse.jface.viewers.Viewer;
55
56 import org.eclipse.ui.contentassist.ContentAssistHandler;
57
58 import org.eclipse.jdt.core.IJavaProject;
59
60 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
61 import org.eclipse.jdt.internal.corext.refactoring.ParameterInfo;
62 import org.eclipse.jdt.internal.corext.refactoring.StubTypeContext;
63
64 import org.eclipse.jdt.internal.ui.dialogs.TableTextCellEditor;
65 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
66 import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
67 import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaTypeCompletionProcessor;
68 import org.eclipse.jdt.internal.ui.refactoring.contentassist.VariableNamesProcessor;
69 import org.eclipse.jdt.internal.ui.util.SWTUtil;
70 import org.eclipse.jdt.internal.ui.util.TableLayoutComposite;
71
72 /**
73  * A special control to edit and reorder method parameters.
74  */

75 public class ChangeParametersControl extends Composite {
76     
77     public static class Mode {
78         private final String JavaDoc fName;
79         private Mode(String JavaDoc name) {
80             fName= name;
81         }
82         public static final Mode EXTRACT_METHOD= new Mode("EXTRACT_METHOD"); //$NON-NLS-1$
83
public static final Mode CHANGE_METHOD_SIGNATURE= new Mode("CHANGE_METHOD_SIGNATURE"); //$NON-NLS-1$
84
public static final Mode INTRODUCE_PARAMETER= new Mode("INTRODUCE_PARAMETER"); //$NON-NLS-1$
85
public String JavaDoc 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     }
98     
99     private static class ParameterInfoContentProvider implements IStructuredContentProvider {
100         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
101             return removeMarkedAsDeleted((List JavaDoc) inputElement);
102         }
103         private ParameterInfo[] removeMarkedAsDeleted(List JavaDoc paramInfos){
104             List JavaDoc result= new ArrayList JavaDoc(paramInfos.size());
105             for (Iterator JavaDoc iter= paramInfos.iterator(); iter.hasNext();) {
106                 ParameterInfo info= (ParameterInfo) iter.next();
107                 if (! info.isDeleted())
108                     result.add(info);
109             }
110             return (ParameterInfo[]) result.toArray(new ParameterInfo[result.size()]);
111         }
112         public void dispose() {
113             // do nothing
114
}
115         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
116             // do nothing
117
}
118     }
119
120     private static class ParameterInfoLabelProvider extends LabelProvider implements ITableLabelProvider, ITableFontProvider {
121         public Image getColumnImage(Object JavaDoc element, int columnIndex) {
122             return null;
123         }
124         public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
125             ParameterInfo info= (ParameterInfo) element;
126             switch (columnIndex) {
127                 case TYPE_PROP:
128                     return info.getNewTypeName();
129                 case NEWNAME_PROP:
130                     return info.getNewName();
131                 case DEFAULT_PROP:
132                     if (info.isAdded())
133                         return info.getDefaultValue();
134                     else
135                         return "-"; //$NON-NLS-1$
136
default:
137                     throw new IllegalArgumentException JavaDoc(columnIndex + ": " + element); //$NON-NLS-1$
138
}
139         }
140         public Font getFont(Object JavaDoc element, int columnIndex) {
141             ParameterInfo info= (ParameterInfo) element;
142             if (info.isAdded())
143                 return JFaceResources.getFontRegistry().getBold(JFaceResources.DIALOG_FONT);
144             else
145                 return null;
146         }
147     }
148
149     private class ParametersCellModifier implements ICellModifier {
150         public boolean canModify(Object JavaDoc element, String JavaDoc property) {
151             Assert.isTrue(element instanceof ParameterInfo);
152             if (property.equals(PROPERTIES[TYPE_PROP]))
153                 return fMode.canChangeTypes();
154             else if (property.equals(PROPERTIES[NEWNAME_PROP]))
155                 return true;
156             else if (property.equals(PROPERTIES[DEFAULT_PROP]))
157                 return (((ParameterInfo)element).isAdded());
158             Assert.isTrue(false);
159             return false;
160         }
161         public Object JavaDoc getValue(Object JavaDoc element, String JavaDoc property) {
162             Assert.isTrue(element instanceof ParameterInfo);
163             if (property.equals(PROPERTIES[TYPE_PROP]))
164                 return ((ParameterInfo) element).getNewTypeName();
165             else if (property.equals(PROPERTIES[NEWNAME_PROP]))
166                 return ((ParameterInfo) element).getNewName();
167             else if (property.equals(PROPERTIES[DEFAULT_PROP]))
168                 return ((ParameterInfo) element).getDefaultValue();
169             Assert.isTrue(false);
170             return null;
171         }
172         public void modify(Object JavaDoc element, String JavaDoc property, Object JavaDoc value) {
173             if (element instanceof TableItem)
174                 element= ((TableItem) element).getData();
175             if (!(element instanceof ParameterInfo))
176                 return;
177             boolean unchanged;
178             ParameterInfo parameterInfo= (ParameterInfo) element;
179             if (property.equals(PROPERTIES[NEWNAME_PROP])) {
180                 unchanged= parameterInfo.getNewName().equals(value);
181                 parameterInfo.setNewName((String JavaDoc) value);
182             } else if (property.equals(PROPERTIES[DEFAULT_PROP])) {
183                 unchanged= parameterInfo.getDefaultValue().equals(value);
184                 parameterInfo.setDefaultValue((String JavaDoc) value);
185             } else if (property.equals(PROPERTIES[TYPE_PROP])) {
186                 unchanged= parameterInfo.getNewTypeName().equals(value);
187                 parameterInfo.setNewTypeName((String JavaDoc) value);
188             } else {
189                 throw new IllegalStateException JavaDoc();
190             }
191             if (! unchanged) {
192                 ChangeParametersControl.this.fListener.parameterChanged(parameterInfo);
193                 ChangeParametersControl.this.fTableViewer.update(parameterInfo, new String JavaDoc[] { property });
194             }
195         }
196     }
197
198     private static final String JavaDoc[] PROPERTIES= { "type", "new", "default" }; //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-3$
199
private static final int TYPE_PROP= 0;
200     private static final int NEWNAME_PROP= 1;
201     private static final int DEFAULT_PROP= 2;
202
203     private static final int ROW_COUNT= 7;
204
205     private final Mode fMode;
206     private final IParameterListChangeListener fListener;
207     private List JavaDoc fParameterInfos;
208     private final StubTypeContext fTypeContext;
209     private final String JavaDoc[] fParamNameProposals;
210     private ContentAssistHandler fNameContentAssistHandler;
211
212     private TableViewer fTableViewer;
213     private Button fUpButton;
214     private Button fDownButton;
215     private Button fEditButton;
216     private Button fAddButton;
217     private Button fRemoveButton;
218
219     public ChangeParametersControl(Composite parent, int style, String JavaDoc label, IParameterListChangeListener listener, Mode mode, StubTypeContext typeContext) {
220         this(parent, style, label, listener, mode, typeContext, new String JavaDoc[0]);
221     }
222     
223     public ChangeParametersControl(Composite parent, int style, String JavaDoc label, IParameterListChangeListener listener, Mode mode) {
224         this(parent, style, label, listener, mode, null, new String JavaDoc[0]);
225     }
226     
227     public ChangeParametersControl(Composite parent, int style, String JavaDoc label, IParameterListChangeListener listener, Mode mode, String JavaDoc[] paramNameProposals) {
228         this(parent, style, label, listener, mode, null, paramNameProposals);
229     }
230     
231     /**
232      * @param label the label before the table or <code>null</code>
233      * @param typeContext the package in which to complete types
234      */

235     private ChangeParametersControl(Composite parent, int style, String JavaDoc label, IParameterListChangeListener listener, Mode mode, StubTypeContext typeContext, String JavaDoc[] paramNameProposals) {
236         super(parent, style);
237         Assert.isNotNull(listener);
238         fListener= listener;
239         fMode= mode;
240         fTypeContext= typeContext;
241         fParamNameProposals= paramNameProposals;
242
243         GridLayout layout= new GridLayout();
244         layout.numColumns= 2;
245         layout.marginWidth= 0;
246         layout.marginHeight= 0;
247         setLayout(layout);
248
249         if (label != null) {
250             Label tableLabel= new Label(this, SWT.NONE);
251             GridData labelGd= new GridData();
252             labelGd.horizontalSpan= 2;
253             tableLabel.setLayoutData(labelGd);
254             tableLabel.setText(label);
255         }
256
257         createParameterList(this);
258         createButtonComposite(this);
259     }
260
261
262     public void setInput(List JavaDoc parameterInfos) {
263         Assert.isNotNull(parameterInfos);
264         fParameterInfos= parameterInfos;
265         fTableViewer.setInput(fParameterInfos);
266         if (fParameterInfos.size() > 0)
267             fTableViewer.setSelection(new StructuredSelection(fParameterInfos.get(0)));
268     }
269     
270     public void editParameter(ParameterInfo info) {
271         fTableViewer.getControl().setFocus();
272         if (! info.isDeleted()) {
273             fTableViewer.setSelection(new StructuredSelection(info), true);
274             updateButtonsEnabledState();
275             editColumnOrNextPossible(NEWNAME_PROP);
276             return;
277         }
278     }
279
280     // ---- Parameter table -----------------------------------------------------------------------------------
281

282     private void createParameterList(Composite parent) {
283         TableLayoutComposite layouter= new TableLayoutComposite(parent, SWT.NONE);
284         addColumnLayoutData(layouter);
285         
286         final Table table= new Table(layouter, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
287         table.setHeaderVisible(true);
288         table.setLinesVisible(true);
289         TableColumn tc;
290         tc= new TableColumn(table, SWT.NONE, TYPE_PROP);
291         tc.setResizable(true);
292         tc.setText(RefactoringMessages.ChangeParametersControl_table_type);
293         
294         tc= new TableColumn(table, SWT.NONE, NEWNAME_PROP);
295         tc.setResizable(true);
296         tc.setText(RefactoringMessages.ChangeParametersControl_table_name);
297
298         if (fMode.canChangeDefault()){
299             tc= new TableColumn(table, SWT.NONE, DEFAULT_PROP);
300             tc.setResizable(true);
301             tc.setText(RefactoringMessages.ChangeParametersControl_table_defaultValue);
302         }
303         
304         GridData gd= new GridData(GridData.FILL_BOTH);
305         gd.heightHint= SWTUtil.getTableHeightHint(table, ROW_COUNT);
306         gd.widthHint= 40;
307         layouter.setLayoutData(gd);
308
309         fTableViewer= new TableViewer(table);
310         fTableViewer.setUseHashlookup(true);
311         fTableViewer.setContentProvider(new ParameterInfoContentProvider());
312         fTableViewer.setLabelProvider(new ParameterInfoLabelProvider());
313         fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
314             public void selectionChanged(SelectionChangedEvent event) {
315                 updateButtonsEnabledState();
316             }
317         });
318
319         table.addTraverseListener(new TraverseListener() {
320             public void keyTraversed(TraverseEvent e) {
321                 if (e.detail == SWT.TRAVERSE_RETURN && e.stateMask == SWT.NONE) {
322                     editColumnOrNextPossible(0);
323                     e.detail= SWT.TRAVERSE_NONE;
324                 }
325             }
326         });
327         table.addKeyListener(new KeyAdapter() {
328             public void keyPressed(KeyEvent e) {
329                 if (e.keyCode == SWT.F2 && e.stateMask == SWT.NONE) {
330                     editColumnOrNextPossible(0);
331                     e.doit= false;
332                 }
333             }
334         });
335
336         addCellEditors();
337     }
338
339     private void editColumnOrNextPossible(int column){
340         ParameterInfo[] selected= getSelectedElements();
341         if (selected.length != 1)
342             return;
343         int nextColumn= column;
344         do {
345             fTableViewer.editElement(selected[0], nextColumn);
346             if (fTableViewer.isCellEditorActive())
347                 return;
348             nextColumn= nextColumn(nextColumn);
349         } while (nextColumn != column);
350     }
351     
352     private void editColumnOrPrevPossible(int column){
353         ParameterInfo[] selected= getSelectedElements();
354         if (selected.length != 1)
355             return;
356         int prevColumn= column;
357         do {
358             fTableViewer.editElement(selected[0], prevColumn);
359             if (fTableViewer.isCellEditorActive())
360                 return;
361             prevColumn= prevColumn(prevColumn);
362         } while (prevColumn != column);
363     }
364     
365     private int nextColumn(int column) {
366         return (column >= getTable().getColumnCount() - 1) ? 0 : column + 1;
367     }
368     
369     private int prevColumn(int column) {
370         return (column <= 0) ? getTable().getColumnCount() - 1 : column - 1;
371     }
372     
373     private void addColumnLayoutData(TableLayoutComposite layouter) {
374         if (fMode.canChangeDefault()){
375             layouter.addColumnData(new ColumnWeightData(33, true));
376             layouter.addColumnData(new ColumnWeightData(33, true));
377             layouter.addColumnData(new ColumnWeightData(34, true));
378         } else {
379             layouter.addColumnData(new ColumnWeightData(50, true));
380             layouter.addColumnData(new ColumnWeightData(50, true));
381         }
382     }
383
384     private ParameterInfo[] getSelectedElements() {
385         ISelection selection= fTableViewer.getSelection();
386         if (selection == null)
387             return new ParameterInfo[0];
388
389         if (!(selection instanceof IStructuredSelection))
390             return new ParameterInfo[0];
391
392         List JavaDoc selected= ((IStructuredSelection) selection).toList();
393         return (ParameterInfo[]) selected.toArray(new ParameterInfo[selected.size()]);
394     }
395
396     // ---- Button bar --------------------------------------------------------------------------------------
397

398     private void createButtonComposite(Composite parent) {
399         Composite buttonComposite= new Composite(parent, SWT.NONE);
400         buttonComposite.setLayoutData(new GridData(GridData.FILL_VERTICAL));
401         GridLayout gl= new GridLayout();
402         gl.marginHeight= 0;
403         gl.marginWidth= 0;
404         buttonComposite.setLayout(gl);
405
406         if (fMode.canAddParameters())
407             fAddButton= createAddButton(buttonComposite);
408
409         fEditButton= createEditButton(buttonComposite);
410         
411         if (fMode.canAddParameters())
412             fRemoveButton= createRemoveButton(buttonComposite);
413         
414         if (buttonComposite.getChildren().length != 0)
415             addSpacer(buttonComposite);
416
417         fUpButton= createButton(buttonComposite, RefactoringMessages.ChangeParametersControl_buttons_move_up, true);
418         fDownButton= createButton(buttonComposite, RefactoringMessages.ChangeParametersControl_buttons_move_down, false);
419
420         updateButtonsEnabledState();
421     }
422
423     private void addSpacer(Composite parent) {
424         Label label= new Label(parent, SWT.NONE);
425         GridData gd= new GridData(GridData.FILL_HORIZONTAL);
426         gd.heightHint= 5;
427         label.setLayoutData(gd);
428     }
429
430     private void updateButtonsEnabledState() {
431         fUpButton.setEnabled(canMove(true));
432         fDownButton.setEnabled(canMove(false));
433         if (fEditButton != null)
434             fEditButton.setEnabled(getTableSelectionCount() == 1);
435         if (fAddButton != null)
436             fAddButton.setEnabled(true);
437         if (fRemoveButton != null)
438             fRemoveButton.setEnabled(getTableSelectionCount() != 0);
439     }
440
441     private int getTableSelectionCount() {
442         return getTable().getSelectionCount();
443     }
444
445     private int getTableItemCount() {
446         return getTable().getItemCount();
447     }
448
449     private Table getTable() {
450         return fTableViewer.getTable();
451     }
452     
453     private Button createEditButton(Composite buttonComposite) {
454         Button button= new Button(buttonComposite, SWT.PUSH);
455         button.setText(RefactoringMessages.ChangeParametersControl_buttons_edit);
456         button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
457         SWTUtil.setButtonDimensionHint(button);
458         button.addSelectionListener(new SelectionAdapter() {
459             public void widgetSelected(SelectionEvent e) {
460                 try {
461                     ParameterInfo[] selected= getSelectedElements();
462                     Assert.isTrue(selected.length == 1);
463                     ParameterInfo parameterInfo= selected[0];
464                     ParameterEditDialog dialog= new ParameterEditDialog(getShell(), parameterInfo, fMode.canChangeTypes(), fMode.canChangeDefault(), fTypeContext);
465                     dialog.open();
466                     fListener.parameterChanged(parameterInfo);
467                     fTableViewer.update(parameterInfo, PROPERTIES);
468                 } finally {
469                     fTableViewer.getControl().setFocus();
470                 }
471             }
472         });
473         return button;
474     }
475     
476     private Button createAddButton(Composite buttonComposite) {
477         Button button= new Button(buttonComposite, SWT.PUSH);
478         button.setText(RefactoringMessages.ChangeParametersControl_buttons_add);
479         button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
480         SWTUtil.setButtonDimensionHint(button);
481         button.addSelectionListener(new SelectionAdapter() {
482             public void widgetSelected(SelectionEvent e) {
483                 String JavaDoc[] excludedParamNames= new String JavaDoc[fParameterInfos.size()];
484                 for (int i= 0; i < fParameterInfos.size(); i++) {
485                     ParameterInfo info= (ParameterInfo) fParameterInfos.get(i);
486                     excludedParamNames[i]= info.getNewName();
487                 }
488                 IJavaProject javaProject= fTypeContext.getCuHandle().getJavaProject();
489                 String JavaDoc newParamName= StubUtility.suggestArgumentName(javaProject, RefactoringMessages.ChangeParametersControl_new_parameter_default_name, excludedParamNames);
490                 ParameterInfo newInfo= ParameterInfo.createInfoForAddedParameter("Object", newParamName, "null"); //$NON-NLS-1$ //$NON-NLS-2$
491
int insertIndex= fParameterInfos.size();
492                 for (int i= fParameterInfos.size() - 1; i >= 0; i--) {
493                     ParameterInfo info= (ParameterInfo) fParameterInfos.get(i);
494                     if (info.isNewVarargs()) {
495                         insertIndex= i;
496                         break;
497                     }
498                 }
499                 fParameterInfos.add(insertIndex, newInfo);
500                 fListener.parameterAdded(newInfo);
501                 fTableViewer.refresh();
502                 fTableViewer.getControl().setFocus();
503                 fTableViewer.setSelection(new StructuredSelection(newInfo), true);
504                 updateButtonsEnabledState();
505                 editColumnOrNextPossible(0);
506             }
507         });
508         return button;
509     }
510
511     private Button createRemoveButton(Composite buttonComposite) {
512         final Button button= new Button(buttonComposite, SWT.PUSH);
513         button.setText(RefactoringMessages.ChangeParametersControl_buttons_remove);
514         button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
515         SWTUtil.setButtonDimensionHint(button);
516         button.addSelectionListener(new SelectionAdapter() {
517             public void widgetSelected(SelectionEvent e) {
518                 int index= getTable().getSelectionIndices()[0];
519                 ParameterInfo[] selected= getSelectedElements();
520                 for (int i= 0; i < selected.length; i++) {
521                     if (selected[i].isAdded())
522                         fParameterInfos.remove(selected[i]);
523                     else
524                         selected[i].markAsDeleted();
525                 }
526                 restoreSelection(index);
527             }
528             private void restoreSelection(int index) {
529                 fTableViewer.refresh();
530                 fTableViewer.getControl().setFocus();
531                 int itemCount= getTableItemCount();
532                 if (itemCount != 0 && index >= itemCount) {
533                     index= itemCount - 1;
534                     getTable().setSelection(index);
535                 }
536                 fListener.parameterListChanged();
537                 updateButtonsEnabledState();
538             }
539         });
540         return button;
541     }
542
543     private Button createButton(Composite buttonComposite, String JavaDoc text, final boolean up) {
544         Button button= new Button(buttonComposite, SWT.PUSH);
545         button.setText(text);
546         button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
547         SWTUtil.setButtonDimensionHint(button);
548         button.addSelectionListener(new SelectionAdapter() {
549             public void widgetSelected(SelectionEvent e) {
550                 ISelection savedSelection= fTableViewer.getSelection();
551                 if (savedSelection == null)
552                     return;
553                 ParameterInfo[] selection= getSelectedElements();
554                 if (selection.length == 0)
555                     return;
556                     
557                 if (up) {
558                     moveUp(selection);
559                 } else {
560                     moveDown(selection);
561                 }
562                 fTableViewer.refresh();
563                 fTableViewer.setSelection(savedSelection);
564                 fListener.parameterListChanged();
565                 fTableViewer.getControl().setFocus();
566             }
567         });
568         return button;
569     }
570     
571     //---- editing -----------------------------------------------------------------------------------------------
572

573     private void addCellEditors() {
574         fTableViewer.setColumnProperties(PROPERTIES);
575         
576         final TableTextCellEditor editors[]= new TableTextCellEditor[PROPERTIES.length];
577
578         editors[TYPE_PROP]= new TableTextCellEditor(fTableViewer, TYPE_PROP);
579         editors[NEWNAME_PROP]= new TableTextCellEditor(fTableViewer, NEWNAME_PROP);
580         editors[DEFAULT_PROP]= new TableTextCellEditor(fTableViewer, DEFAULT_PROP);
581         
582         if (fMode.canChangeTypes()) {
583             SubjectControlContentAssistant assistant= installParameterTypeContentAssist(editors[TYPE_PROP].getText());
584             editors[TYPE_PROP].setContentAssistant(assistant);
585         }
586         if (fParamNameProposals.length > 0) {
587             SubjectControlContentAssistant assistant= installParameterNameContentAssist(editors[NEWNAME_PROP].getText());
588             editors[NEWNAME_PROP].setContentAssistant(assistant);
589         }
590         
591         for (int i = 0; i < editors.length; i++) {
592             final int editorColumn= i;
593             final TableTextCellEditor editor = editors[i];
594             // support tabbing between columns while editing:
595
editor.getText().addTraverseListener(new TraverseListener() {
596                 public void keyTraversed(TraverseEvent e) {
597                     switch (e.detail) {
598                         case SWT.TRAVERSE_TAB_NEXT :
599                             editColumnOrNextPossible(nextColumn(editorColumn));
600                             e.detail= SWT.TRAVERSE_NONE;
601                             break;
602
603                         case SWT.TRAVERSE_TAB_PREVIOUS :
604                             editColumnOrPrevPossible(prevColumn(editorColumn));
605                             e.detail= SWT.TRAVERSE_NONE;
606                             break;
607                         
608                         default :
609                             break;
610                     }
611                 }
612             });
613             TextFieldNavigationHandler.install(editor.getText());
614         }
615         
616         editors[NEWNAME_PROP].setActivationListener(new TableTextCellEditor.IActivationListener(){
617             public void activate() {
618                 ParameterInfo[] selected= getSelectedElements();
619                 if (selected.length == 1 && fNameContentAssistHandler != null) {
620                     fNameContentAssistHandler.setEnabled(selected[0].isAdded());
621                 }
622             }
623         });
624         
625         fTableViewer.setCellEditors(editors);
626         fTableViewer.setCellModifier(new ParametersCellModifier());
627     }
628
629     private SubjectControlContentAssistant installParameterTypeContentAssist(Text text) {
630         JavaTypeCompletionProcessor processor= new JavaTypeCompletionProcessor(true, false);
631         if (fTypeContext == null)
632             processor.setCompletionContext(null, null, null);
633         else
634             processor.setCompletionContext(fTypeContext.getCuHandle(), fTypeContext.getBeforeString(), fTypeContext.getAfterString());
635         SubjectControlContentAssistant contentAssistant= ControlContentAssistHelper.createJavaContentAssistant(processor);
636         ContentAssistHandler.createHandlerForText(text, contentAssistant);
637         return contentAssistant;
638     }
639
640     private SubjectControlContentAssistant installParameterNameContentAssist(Text text) {
641         VariableNamesProcessor processor= new VariableNamesProcessor(fParamNameProposals);
642         SubjectControlContentAssistant contentAssistant= ControlContentAssistHelper.createJavaContentAssistant(processor);
643         fNameContentAssistHandler= ContentAssistHandler.createHandlerForText(text, contentAssistant);
644         return contentAssistant;
645     }
646
647     //---- change order ----------------------------------------------------------------------------------------
648

649     private void moveUp(ParameterInfo[] selection) {
650         moveUp(fParameterInfos, Arrays.asList(selection));
651     }
652
653     private void moveDown(ParameterInfo[] selection) {
654         Collections.reverse(fParameterInfos);
655         moveUp(fParameterInfos, Arrays.asList(selection));
656         Collections.reverse(fParameterInfos);
657     }
658
659     private static void moveUp(List JavaDoc elements, List JavaDoc move) {
660         List JavaDoc res= new ArrayList JavaDoc(elements.size());
661         List JavaDoc deleted= new ArrayList JavaDoc();
662         Object JavaDoc floating= null;
663         for (Iterator JavaDoc iter= elements.iterator(); iter.hasNext();) {
664             Object JavaDoc curr= iter.next();
665             if (move.contains(curr)) {
666                 res.add(curr);
667             } else if (((ParameterInfo) curr).isDeleted()) {
668                 deleted.add(curr);
669             } else {
670                 if (floating != null)
671                     res.add(floating);
672                 floating= curr;
673             }
674         }
675         if (floating != null) {
676             res.add(floating);
677         }
678         res.addAll(deleted);
679         elements.clear();
680         for (Iterator JavaDoc iter= res.iterator(); iter.hasNext();) {
681             elements.add(iter.next());
682         }
683     }
684
685     private boolean canMove(boolean up) {
686         int notDeletedInfosCount= getNotDeletedInfosCount();
687         if (notDeletedInfosCount == 0)
688             return false;
689         int[] indc= getTable().getSelectionIndices();
690         if (indc.length == 0)
691             return false;
692         int invalid= up ? 0 : notDeletedInfosCount - 1;
693         for (int i= 0; i < indc.length; i++) {
694             if (indc[i] == invalid)
695                 return false;
696         }
697         return true;
698     }
699     
700     private int getNotDeletedInfosCount(){
701         if (fParameterInfos == null) // during initialization
702
return 0;
703         int result= 0;
704         for (Iterator JavaDoc iter= fParameterInfos.iterator(); iter.hasNext();) {
705             ParameterInfo info= (ParameterInfo) iter.next();
706             if (! info.isDeleted())
707                 result++;
708         }
709         return result;
710     }
711 }
712
Popular Tags