/* This file is part of Green. * * Copyright (C) 2005 The Research Foundation of State University of New York * All Rights Under Copyright Reserved, The Research Foundation of S.U.N.Y. * * Green is free software, licensed under the terms of the Eclipse * Public License, version 1.0. The license is available at * http://www.eclipse.org/legal/epl-v10.html */ package edu.buffalo.cse.green.dialogs.wizards; import static edu.buffalo.cse.green.preferences.VariableAffix.FieldPrefix; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; import java.util.Stack; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IWorkspaceRunnable; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.core.runtime.Status; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaConventions; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.dom.PrimitiveType; import org.eclipse.jdt.internal.ui.dialogs.StatusUtil; import org.eclipse.jdt.ui.JavaUI; import org.eclipse.jdt.ui.wizards.NewElementWizardPage; import org.eclipse.jdt.ui.wizards.NewTypeWizardPage; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IMessageProvider; import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.TableEditor; import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.INewWizard; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard; import edu.buffalo.cse.green.GreenException; import edu.buffalo.cse.green.PlugIn; import edu.buffalo.cse.green.dialogs.ChooseTypeDialog; import edu.buffalo.cse.green.editor.DiagramEditor; import edu.buffalo.cse.green.editor.model.CompartmentModel; import edu.buffalo.cse.green.editor.model.MemberModel; import edu.buffalo.cse.green.editor.model.RootModel; import edu.buffalo.cse.green.preferences.VariableAffix; /** * Opens a dialog box that prompts the user for a new element that will be * displayed in the diagram. * * @author zgwang */ public abstract class NewElementWizard extends GreenWizard implements INewWizard { /** * The current workbench */ private IWorkbench _workbench; /** * The selected element upon which this wizard is acting */ private IStructuredSelection _selection; /** * Page for adding new types */ protected NewTypeWizardPage _fPage; /** * Error string for when attempting to create a type in the default pacakge. * Error: "Cannot use default package to create type in editor" */ private static final String DEFAULT_PACKAGE_ERROR = "Cannot use default package to create type in editor"; /** * Model for the new element */ private MemberModel<CompartmentModel, RootModel, IType> _model; /** * Default constructor */ public NewElementWizard() { setNeedsProgressMonitor(true); } /** * @param model - The model automatically generated by the * <code>CreateCommand</code>. */ public void setModel(MemberModel<CompartmentModel, RootModel, IType> model) { _model = model; } /** * @return The model created by this dialog. */ public MemberModel<CompartmentModel, RootModel, IType> getModel() { return _model; } /** * Subclasses should override to perform the actions of the wizard. This * method is run in the wizard container's context as a workspace runnable. * * @param monitor - The progress monitor to use. * @throws InterruptedException * @throws CoreException */ protected void finishPage(IProgressMonitor monitor) throws InterruptedException, CoreException { if (_fPage != null && _fPage.getPackageFragment().isDefaultPackage()) { GreenException.illegalOperation(DEFAULT_PACKAGE_ERROR); } } /** * @return True if the wizard can be run on a forked thread, false * otherwise. */ protected abstract boolean canRunForked(); /** * @see edu.buffalo.cse.green.dialogs.wizards.GreenWizard#doFinish() */ public boolean doFinish() { final IWorkspaceRunnable op = new IWorkspaceRunnable() { /** * @see org.eclipse.core.resources.IWorkspaceRunnable#run(org.eclipse.core.runtime.IProgressMonitor) */ public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException { try { finishPage(monitor); } catch (InterruptedException e) { throw new OperationCanceledException(e.getMessage()); } } }; try { getContainer().run(canRunForked(), true, new IRunnableWithProgress() { /** * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor) */ public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { try { op.run(monitor); } catch (CoreException e) { e.printStackTrace(); } } }); } catch (Exception e) { handleFinishException(e); return false; } return true; } /** * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection) */ public void init(IWorkbench workbench, IStructuredSelection currentSelection) { _workbench = workbench; _selection = currentSelection; } /** * @return The selection. */ public IStructuredSelection getSelection() { return _selection; } /** * @return The workbench. */ public IWorkbench getWorkbench() { return _workbench; } /** * Selects and reveals the given resource in the workbench. * * @param newResource - The resource. */ protected void selectAndReveal(IResource newResource) { BasicNewResourceWizard.selectAndReveal(newResource, _workbench .getActiveWorkbenchWindow()); } /** * This is the parent class of the wizards used to create fields and methods * using Green's editor. It creates the controls common to both wizards and * provides common functionality. * * @author bcmartin * @author zgwang */ abstract class NewMemberSignatureWizardPage extends NewElementWizardPage { // /** // * // */ // private VisibilityComposite _visibility; /** * Name of the element to be created */ private Text _name; /** * JavaDoc comment for the element to be created */ private Text _comment; /** * Text field for the variable type (if invoked through a new field * wizard) or method return type (if invoked through a new method wizard) */ private Text _typeNameText; /** * Table for optional parameters in creating a new method */ private Table _parameterTable; /** * Indicator for whether the selection invoking this wizard is an interface */ private boolean _isInterface; /** * List of modifiers on the variable to be generated */ private List<String> _modifiers; /** * Lowest layer of the composites on this page */ private Composite _pageComposite; /** * Label for the type associated with the element. */ private Label _typeLabel; /** * Checkbox to determine if imports will be automatically added. */ private Button _forceImports; /** * Collection of status for the page. */ private ArrayList<IStatus> _stati; /** * Error codes and texts for use with the page IStatus list */ //TODO Turn these into enums? protected final int ERROR_IDENTIFIER = 0; //Invalid identifier syntax protected final int ERROR_TYPE = 1; //No [return] type specified protected final int ERROR_PARAM_TYPE_NAME = 2; //Invalid type name syntax protected final int ERROR_PARAM_TYPE = 3; //Unresolved type name protected final int ERROR_PARAM_MULTI_ID = 4; //Multiple parameters with same identifier protected final String[] ERR_TEXT = { "\' is not a valid identifier.", "No type specified.", "\' is not a valid type name.", "\' cannot be resolved to a type or a valid primitive.", "Multiple parameters have the same identifier." }; /** * Boolean indicator of the completeness of the page */ private boolean _completed; /** * Constructor * @param name title of page */ public NewMemberSignatureWizardPage(String name) { super(name); _stati = new ArrayList<IStatus>(); _completed = false; } /* (non-Javadoc) * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite) */ public void createControl(Composite parent) { initializeDialogUnits(parent); _pageComposite = new Composite(parent, SWT.NONE); _pageComposite.setLayout(new GridLayout(1, false)); createCommentControls(); createNameControls(); //Type and Modifiers are grouped together Composite typeModifierComposite = new Composite(_pageComposite, SWT.NONE); typeModifierComposite.setLayout(new GridLayout(2, true)); typeModifierComposite.setLayoutData( new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); createModifierControls(typeModifierComposite); createTypeControls(typeModifierComposite); new Label(_pageComposite, SWT.SEPARATOR | SWT.HORIZONTAL) .setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); _forceImports = new Button(_pageComposite, SWT.CHECK); _forceImports.setText("Automatically import fully qualified types and use simple names."); setControl(_pageComposite); Dialog.applyDialogFont(_pageComposite); PlugIn.getWorkbenchHelp().setHelp(_pageComposite, JavaUI.ID_PLUGIN + "." + "new_class_wizard_page_context"); } /** * Creates the controls for the comment section of the page. * * @param composite - The parent of the modifier controls group. * * @author radygert */ private void createCommentControls() { Composite commentComposite = new Composite(_pageComposite, SWT.NONE); commentComposite.setLayout(new GridLayout(1, false)); commentComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); Label commentLabel = new Label(commentComposite, SWT.LEFT); commentLabel.setText("JavaDoc Comment:"); _comment = new Text(commentComposite, SWT.LEFT | SWT.BORDER | SWT.MULTI | SWT.V_SCROLL); GridData layoutData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); layoutData.heightHint = _comment.getLineHeight()*3; _comment.setLayoutData(layoutData); _comment.setSelection(0, _comment.getText().length()); } /** * Creates the controls for the name section of the page. * * @param composite - The parent of the modifier controls group. * * @author zgwang */ private void createNameControls() { Composite nameComposite = new Composite(_pageComposite, SWT.NONE); nameComposite.setLayout(new GridLayout(2, false)); nameComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); Label nameLabel = new Label(nameComposite, SWT.LEFT); nameLabel.setText("Name:"); _name = new Text(nameComposite, SWT.LEFT | SWT.BORDER | SWT.SINGLE); _name.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); if(isField()) _name.setText(VariableAffix.getAffixString(FieldPrefix)); else _name.setText(""); _name.setSelection(0, _name.getText().length()); _name.addKeyListener(new KeyListener() { /** * @see org.eclipse.swt.events.KeyListener#keyPressed(org.eclipse.swt.events.KeyEvent) */ public void keyPressed(KeyEvent e) {} /** * @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent) */ public void keyReleased(KeyEvent e) { validatePage(); } }); } /** * Creates the controls for the modifier section of the page. * * @param parent - The parent of the modifier controls group. */ private void createModifierControls(Composite parent) { final NewElementWizardSettings settings = getSettings(); _modifiers = new ArrayList<String>(); Composite modifierComposite = new Composite(parent, SWT.NONE); modifierComposite.setLayout(new GridLayout(1, false)); modifierComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); Label modifiersLabel = new Label(modifierComposite, SWT.LEFT); modifiersLabel.setText("Modifiers:"); Composite modifierSubcomposite = new Composite(modifierComposite, SWT.NONE); modifierSubcomposite.setLayout(new GridLayout(2, false)); modifierSubcomposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL)); Group ACMGroup = new Group(modifierSubcomposite, SWT.NONE); ACMGroup.setLayout(new GridLayout(1, false)); ACMGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); final Button defaultACM = new Button(ACMGroup, SWT.RADIO); defaultACM.setText("default"); defaultACM.setSelection(true); final Button publicACM = new Button(ACMGroup, SWT.RADIO); publicACM.setText("public"); final Button protectedACM = new Button(ACMGroup, SWT.RADIO); protectedACM.setText("protected"); final Button privateACM = new Button(ACMGroup, SWT.RADIO); privateACM.setText("private"); defaultACM.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) {} public void widgetSelected(SelectionEvent e) { if(defaultACM.getSelection()) { _modifiers.remove("public"); _modifiers.remove("protected"); _modifiers.remove("private"); } } }); publicACM.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) {} public void widgetSelected(SelectionEvent e) { if(publicACM.getSelection()) { _modifiers.add("public"); _modifiers.remove("protected"); _modifiers.remove("private"); } } }); protectedACM.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) {} public void widgetSelected(SelectionEvent e) { if(protectedACM.getSelection()){ _modifiers.remove("public"); _modifiers.add("protected"); _modifiers.remove("private"); } } }); privateACM.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) {} public void widgetSelected(SelectionEvent e) { if(privateACM.getSelection()){ _modifiers.remove("public"); _modifiers.remove("protected"); _modifiers.add("private"); } } }); Group modifierGroup = new Group(modifierSubcomposite, SWT.NONE); modifierGroup.setLayout(new GridLayout(1, false)); modifierGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); final Button abstractButton = new Button(modifierGroup, SWT.CHECK); abstractButton.setText("abstract"); final Button finalButton = new Button(modifierGroup, SWT.CHECK); finalButton.setText("final"); final Button staticButton = new Button(modifierGroup, SWT.CHECK); staticButton.setText("static"); final Button dummyButton = new Button(modifierGroup, SWT.CHECK); dummyButton.setVisible(false); abstractButton.setEnabled(settings.isAbstractAvailable()); finalButton.setEnabled(settings.isFinalAvailable()); staticButton.setEnabled(settings.isStaticAvailable()); abstractButton.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) {} public void widgetSelected(SelectionEvent e) { if(abstractButton.getSelection()) _modifiers.add("abstract"); else _modifiers.remove("abstract"); } }); finalButton.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) {} public void widgetSelected(SelectionEvent e) { if(finalButton.getSelection()) _modifiers.add("final"); else _modifiers.remove("final"); } }); staticButton.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) {} public void widgetSelected(SelectionEvent e) { if(staticButton.getSelection()) _modifiers.add("static"); else _modifiers.remove("static"); } }); } /** * Creates the controls for the type section of the page * @param parent - The parent of the type group. * @param columns - The number of columns to create. */ private void createTypeControls(Composite parent) { Composite typeComposite = new Composite(parent, SWT.NONE); typeComposite.setLayout(new GridLayout(1, false)); typeComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL |GridData.VERTICAL_ALIGN_FILL)); _typeLabel = new Label(typeComposite, SWT.LEFT); _typeLabel.setText(""); Composite typeSubcomposite = new Composite(typeComposite, SWT.NONE); typeSubcomposite.setLayout(new GridLayout(2, false)); typeSubcomposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); Combo typeCombo = new Combo(typeSubcomposite, SWT.READ_ONLY); Button nonPrimButton = new Button(typeSubcomposite, SWT.NONE); Label selectedTypeLabel = new Label(typeSubcomposite, SWT.LEFT); selectedTypeLabel.setText("Selected Type:"); _typeNameText = new Text(typeSubcomposite, SWT.LEFT | SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY); _typeNameText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); _typeNameText.addListener(SWT.Modify, new Listener() { public void handleEvent(Event event) { validatePage(); } }); nonPrimButton.setText("Browse Types..."); nonPrimButton.setEnabled(false); nonPrimButton.addSelectionListener(new SelectionListener() { /** * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent) */ public void widgetSelected(SelectionEvent e) { List<IType> types = new ChooseTypeDialog(false).open(); if (!types.isEmpty()) { String selectedName = types.get(0).getFullyQualifiedName(); _typeNameText.setText(selectedName); } validatePage(); } /** * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) */ public void widgetDefaultSelected(SelectionEvent e) {} }); if(allowVoidType()) { typeCombo.add("void"); } typeCombo.add("boolean"); typeCombo.add("byte"); typeCombo.add("char"); typeCombo.add("double"); typeCombo.add("float"); typeCombo.add("int"); typeCombo.add("long"); typeCombo.add("short"); typeCombo.add("<Non-primitive>"); typeCombo.setVisibleItemCount(typeCombo.getItemCount()); new ComboSelectionListener(typeCombo, nonPrimButton); } /** * Creates the controls for the method parameter section of the page * Should only be invoked by the new method wizard * @param composite The parent of the method parameters group */ protected void createMethodParameterControls() { Label methodLabel = new Label(_pageComposite, SWT.LEFT); methodLabel.setText("Method Parameters"); Composite paramComposite = new Composite(_pageComposite, SWT.NONE); paramComposite.setLayout(new GridLayout(2, false)); paramComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_VERTICAL)); _parameterTable = new Table(paramComposite, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION); _parameterTable.setLinesVisible(true); _parameterTable.setHeaderVisible(true); _parameterTable.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_VERTICAL)); TableColumn typeCol = new TableColumn(_parameterTable, SWT.LEFT); typeCol.setText("Type"); typeCol.setWidth(100); TableColumn nameCol = new TableColumn(_parameterTable, SWT.LEFT); nameCol.setText("Parameter Name"); nameCol.setWidth(100); createTableEditor(_parameterTable); Composite buttonSubcomposite = new Composite(paramComposite, SWT.NONE); buttonSubcomposite.setLayout(new GridLayout(1, false)); buttonSubcomposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); final Button addButton = new Button(buttonSubcomposite, SWT.NONE); final Button removeButton = new Button(buttonSubcomposite, SWT.NONE); final Button moveUpButton = new Button(buttonSubcomposite, SWT.NONE); final Button moveDownButton = new Button(buttonSubcomposite, SWT.NONE); addButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); removeButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); moveUpButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); moveDownButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); addButton.setText("Add"); removeButton.setText("Delete"); moveUpButton.setText("Move Up"); moveDownButton.setText("Move Down"); removeButton.setEnabled(false); moveUpButton.setEnabled(false); moveDownButton.setEnabled(false); new TableSelectionListener(_parameterTable, removeButton, moveUpButton, moveDownButton); addButton.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) {} public void widgetSelected(SelectionEvent e) { TableItem item = new TableItem(_parameterTable, SWT.NONE); item.setText(new String[] {"type", "name"}); validatePage(); } }); removeButton.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) {} public void widgetSelected(SelectionEvent e) { int index = _parameterTable.getSelectionIndex(); _parameterTable.remove(index); if(_parameterTable.getItemCount() > 0) {//there are items left if(index > 0) { _parameterTable.setSelection(index - 1); } else { _parameterTable.setSelection(index); } } else { removeButton.setEnabled(false); moveUpButton.setEnabled(false); moveDownButton.setEnabled(false); } validatePage(); } }); moveUpButton.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) {} public void widgetSelected(SelectionEvent e) { //Assert index > 0 int index = _parameterTable.getSelectionIndex(); int numItems = _parameterTable.getItemCount(); String[] itemToMove = new String[] {_parameterTable.getItem(index).getText(0), _parameterTable.getItem(index).getText(1)}; Stack<String[]> tempStack = new Stack<String[]>(); for(int a = numItems - 1; a > index; a--) { tempStack.push(new String[] {new String(_parameterTable.getItem(a).getText(0)), new String(_parameterTable.getItem(a).getText(1))}); _parameterTable.remove(a); } _parameterTable.remove(index); tempStack.push(new String[] {new String(_parameterTable.getItem(index - 1).getText(0)), new String(_parameterTable.getItem(index - 1).getText(1))}); _parameterTable.remove(index - 1); TableItem newItem = new TableItem(_parameterTable, SWT.NONE); newItem.setText(itemToMove); while(!tempStack.isEmpty()) { newItem = new TableItem(_parameterTable, SWT.NONE); newItem.setText(tempStack.pop()); } _parameterTable.setSelection(index - 1); if(index - 1 == 0) { moveUpButton.setEnabled(false); } moveDownButton.setEnabled(true); } }); moveDownButton.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent e) {} public void widgetSelected(SelectionEvent e) { //Assert index < last index entry int index = _parameterTable.getSelectionIndex(); int numItems = _parameterTable.getItemCount(); String[] itemBelow = new String[] {_parameterTable.getItem(index + 1).getText(0), _parameterTable.getItem(index + 1).getText(1)}; Stack<String[]> tempStack = new Stack<String[]>(); for(int a = numItems - 1; a > index + 1; a--) { tempStack.push(new String[] {new String(_parameterTable.getItem(a).getText(0)), new String(_parameterTable.getItem(a).getText(1))}); _parameterTable.remove(a); } _parameterTable.remove(index + 1); tempStack.push(new String[] {new String(_parameterTable.getItem(index).getText(0)), new String(_parameterTable.getItem(index).getText(1))}); _parameterTable.remove(index); TableItem newItem = new TableItem(_parameterTable, SWT.NONE); newItem.setText(itemBelow); while(!tempStack.isEmpty()) { newItem = new TableItem(_parameterTable, SWT.NONE); newItem.setText(tempStack.pop()); } _parameterTable.setSelection(index + 1); if(index + 1 == _parameterTable.getItemCount() - 1) { moveDownButton.setEnabled(false); } moveUpButton.setEnabled(true); } }); } public void setTypeLabel(String text) { _typeLabel.setText(text); } /** * @return The modifiers for the pending variable. */ public List<String> getModifiers() { return _modifiers; } /** * @see org.eclipse.jface.wizard.IWizardPage#getName() */ public String getName() { return _name.getText(); } /** * Gets the JavaDoc comment for this element. * @return javadoc comment text */ public String getComment() { return _comment.getText(); } /** * @return The user specified data type for the pending variable. */ public String getTypeName() { return _typeNameText.getText(); } /** * @return the parameters for the method */ public String getParameters() { if(_parameterTable.getItemCount() == 0) { return ""; } String parameters = new String(); for(int a = 0; a < _parameterTable.getItemCount(); a++) { parameters = parameters + _parameterTable.getItem(a).getText(0) + " " + _parameterTable.getItem(a).getText(1) + ", "; } return parameters.substring(0, parameters.length() - 2); } /** * * @return true if the force imports button is checked */ public boolean forceImports() { if(_forceImports == null) { return false; } return _forceImports.getSelection(); } /** * Sets whether or not we are creating the element in an interface. * * @param isInterface - The value to use. */ protected void setInterface(boolean isInterface) { _isInterface = isInterface; } /** * @return Provides settings used to fill out the dialog. */ protected abstract NewElementWizardSettings getSettings(); /** * @return true if void is a valid data type, false otherwise. */ protected abstract boolean allowVoidType(); /** * @return true if this element wizard is being used to add a field, * false otherwise. * @author zgwang */ protected abstract boolean isField(); /** * @return true if this element wizard is being used to add an element to an * interface, false otherwise. */ protected boolean isInterface() { return _isInterface; } /** * Attach in place editor to table * @param table given table */ private void createTableEditor(final Table table) { /* * Taken from * TableEditor example snippet: edit a cell in a table (in place, fancy) * Minor modifications were made for use. * * http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/editacellinaSWTtableinplacefancy.htm * * For a list of all SWT example snippets see * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets */ final TableEditor editor = new TableEditor(table); editor.horizontalAlignment = SWT.LEFT; editor.grabHorizontal = true; table.addListener(SWT.MouseDown, new Listener() { public void handleEvent(Event event) { Rectangle clientArea = table.getClientArea(); Point pt = new Point(event.x, event.y); int index = table.getTopIndex(); while (index < table.getItemCount()) { boolean visible = false; final TableItem item = table.getItem(index); for (int i = 0; i < table.getColumnCount(); i++) { Rectangle rect = item.getBounds(i); if (rect.contains(pt)) { final int column = i; final Text text = new Text(table, SWT.NONE); // text.addKeyListener(new KeyListener() { /** * @see org.eclipse.swt.events.KeyListener#keyPressed(org.eclipse.swt.events.KeyEvent) */ public void keyPressed(KeyEvent e) {} /** * @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent) */ public void keyReleased(KeyEvent e) { item.setText(column, text.getText()); validatePage(); } }); // Listener textListener = new Listener() { public void handleEvent(final Event e) { switch (e.type) { case SWT.FocusOut: item.setText(column, text.getText()); text.dispose(); break; case SWT.Traverse: switch (e.detail) { case SWT.TRAVERSE_RETURN: item.setText(column, text.getText()); // FALL THROUGH case SWT.TRAVERSE_ESCAPE: text.dispose(); e.doit = false; } break; } } }; text.addListener(SWT.FocusOut, textListener); text.addListener(SWT.Traverse, textListener); editor.setEditor(text, item, i); text.setText(item.getText(i)); text.selectAll(); text.setFocus(); return; } if (!visible && rect.intersects(clientArea)) { visible = true; } } if (!visible) return; index++; } } }); } /** * Validates relevant fields on the current page and sets the appropriate * error message. * @return true if page has no errors, false otherwise */ protected void validatePage() { _stati.clear(); _completed = true; if(_parameterTable != null) { validateTable(); } //Validate type name field. if(_name != null) { IStatus identifierStatus = JavaConventions.validateIdentifier(_name.getText(), "5.0", "5.0"); if(identifierStatus.getSeverity() >= Status.WARNING) { _stati.add(new Status(identifierStatus.getSeverity(), "green", ERROR_IDENTIFIER, "\'" + _name.getText(), null)); } } else { _stati.add(new Status(Status.ERROR, "green", ERROR_IDENTIFIER, "\'", null)); } //Validate return type field. if(_typeNameText != null) { if(_typeNameText.getText().equals("")) {//No text _stati.add(new Status(Status.ERROR, "green", ERROR_TYPE, "", null)); } } if(_stati.size() == 0) { //No validation issues setErrorMessage(null); setMessage(null); } else { Status[] temp = new Status[_stati.size()]; for(int a = 0; a < _stati.size(); a++) { temp[a] = (Status)_stati.get(a); } IStatus s = StatusUtil.getMostSevere(temp); if(s.getSeverity() == Status.OK) setErrorMessage(null); else if(s.getSeverity() == IMessageProvider.WARNING){ setErrorMessage(null); setMessage(s.getMessage() + ERR_TEXT[s.getCode()], IMessageProvider.WARNING); } else { setErrorMessage(s.getMessage() + ERR_TEXT[s.getCode()]); _completed = false; } } getWizard().getContainer().updateMessage(); getWizard().getContainer().updateButtons(); } /** * * @return true if the page is completed without errors, false otherwise. */ public boolean isCompleted() { return _completed; } /** * Checks parameters table for any errors/warnings and updates the * status collection. */ private void validateTable() { IJavaProject jp = DiagramEditor.getActiveEditor().getProject(); ArrayList<String> paramId = new ArrayList<String>(); for(int a = 0; a < _parameterTable.getItemCount(); a++) { String tempType = _parameterTable.getItem(a).getText(0); try { IStatus nameStatus = JavaConventions.validateJavaTypeName(tempType, "5.0", "5.0"); if(nameStatus.getSeverity() >= Status.WARNING && PrimitiveType.toCode(tempType) == null) { _stati.add(new Status(nameStatus.getSeverity(), "green", ERROR_PARAM_TYPE_NAME, "\'" + tempType, null)); } else if((jp.findType(tempType) == null && PrimitiveType.toCode(tempType) == null && !tempType.equals("String")) || tempType.equals("void")) { _stati.add(new Status(Status.WARNING, "green", ERROR_PARAM_TYPE, "\'" + tempType, null)); } } catch(JavaModelException jme) { jme.printStackTrace(); } String tempName = _parameterTable.getItem(a).getText(1); paramId.add(tempName); IStatus nameStatus = JavaConventions.validateIdentifier(tempName, "5.0", "5.0"); if(nameStatus.getSeverity() >= Status.WARNING) { _stati.add(new Status(nameStatus.getSeverity(), "green", ERROR_IDENTIFIER, "\'" + tempName, null)); } } if(hasDupes(paramId)) { _stati.add(new Status(Status.WARNING, "green", ERROR_PARAM_MULTI_ID, "", null)); } } /** * Checks for duplicates in an ArrayList * @param list the list to be checked * @return true if there are duplicates in the given ArrayList, * false otherwise */ private boolean hasDupes(ArrayList<String> list) { for(int a = 0; a < list.size(); a++) { for(int b = 0; b < list.size(); b++) { if(list.get(a).equals(list.get(b)) && a != b) { return true; } } } return false; } /** * Customized SelectionListener for the type combo * @author zgwang * */ class ComboSelectionListener implements SelectionListener { /** * The combo box for which this listener is listening for selections. */ private Combo _combo; /** * Button for non-primitives that is to be activated if the selection is non-primitive type. */ private Button _nonPrim; /** * Constructor * @param parent * @param nonPrim */ public ComboSelectionListener(Combo combo, Button nonPrim) { _combo = combo; _nonPrim = nonPrim; _combo.addSelectionListener(this); } /* (non-Javadoc) * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) */ public void widgetDefaultSelected(SelectionEvent e) { } /* (non-Javadoc) * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent) */ public void widgetSelected(SelectionEvent e) { String selectionText = _combo.getItem(_combo.getSelectionIndex()); _nonPrim.setEnabled(selectionText.equals("<Non-primitive>")); if(!_nonPrim.isEnabled()) { _typeNameText.setText(selectionText); } else { _typeNameText.setText(""); } } } /** * Customized SelectionListener for the parameters table * @author zgwang * */ class TableSelectionListener implements SelectionListener { /** * The table for which this listener is listening for selections. */ private Table _table; /** * Button for removing an item */ private Button _remove; /** * Button for moving an item up in the list */ private Button _up; /** * Button for moving an item down in the list */ private Button _down; /** * Constructor * @param parent * @param nonPrim */ public TableSelectionListener(final Table table, Button remove, Button up, Button down) { _table = table; _remove = remove; _up = up; _down = down; _table.addSelectionListener(this); } /* (non-Javadoc) * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) */ public void widgetDefaultSelected(SelectionEvent e) { } /* (non-Javadoc) * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent) */ public void widgetSelected(SelectionEvent e) { TableItem item = _table.getSelection()[0]; if(item != null) { _remove.setEnabled(true); _up.setEnabled(true); _down.setEnabled(true); //First item in list, cannot move up if(item.equals(_table.getItem(0))) { _up.setEnabled(false); } //Last item, cannot move down if(item.equals(_table.getItem(_table.getItemCount() - 1))) { _down.setEnabled(false); } } else { _remove.setEnabled(false); _up.setEnabled(false); _down.setEnabled(false); } } } } }