Search in sources :

Example 1 with EnumerationsDialog

use of org.eclipse.wst.xsd.ui.internal.widgets.EnumerationsDialog in project webtools.sourceediting by eclipse.

the class EnumerationsSection method widgetSelected.

public void widgetSelected(SelectionEvent e) {
    XSDSimpleTypeDefinition st = (XSDSimpleTypeDefinition) input;
    if (e.widget == addButton) {
        List enumList = st.getEnumerationFacets();
        // $NON-NLS-1$
        String newName = XSDCommonUIUtils.createUniqueEnumerationValue("value", enumList);
        AddEnumerationsCommand command = new AddEnumerationsCommand(Messages._UI_ACTION_ADD_ENUMERATION, (XSDSimpleTypeDefinition) input);
        command.setValue(newName);
        getCommandStack().execute(command);
        enumerationsTable.refresh();
        int newItemIndex = enumerationsTable.getTable().getItemCount() - 1;
        enumerationsTable.editElement(enumerationsTable.getElementAt(newItemIndex), 0);
    } else if (e.widget == addManyButton) {
        Display display = Display.getCurrent();
        // if it is null, get the default one
        display = display == null ? Display.getDefault() : display;
        Shell parentShell = display.getActiveShell();
        EnumerationsDialog dialog = new EnumerationsDialog(parentShell);
        dialog.setBlockOnOpen(true);
        int result = dialog.open();
        if (result == Window.OK) {
            String text = dialog.getText();
            String delimiter = dialog.getDelimiter();
            StringTokenizer tokenizer = new StringTokenizer(text, delimiter);
            CompoundCommand compoundCommand = new CompoundCommand(Messages._UI_ACTION_ADD_ENUMERATIONS);
            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken();
                if (dialog.isPreserveWhitespace() == false) {
                    token = token.trim();
                }
                AddEnumerationsCommand command = new AddEnumerationsCommand(Messages._UI_ACTION_ADD_ENUMERATIONS, (XSDSimpleTypeDefinition) input);
                command.setValue(token);
                compoundCommand.add(command);
            }
            getCommandStack().execute(compoundCommand);
        }
        enumerationsTable.refresh();
    } else if (e.widget == deleteButton) {
        StructuredSelection selection = (StructuredSelection) enumerationsTable.getSelection();
        if (selection != null) {
            Iterator i = selection.iterator();
            CompoundCommand compoundCommand = new CompoundCommand(Messages._UI_ACTION_DELETE_ENUMERATION);
            while (i.hasNext()) {
                Object obj = i.next();
                if (obj != null) {
                    if (obj instanceof XSDEnumerationFacet) {
                        XSDEnumerationFacet enumFacet = (XSDEnumerationFacet) obj;
                        DeleteCommand deleteCommand = new DeleteCommand(Messages._UI_ACTION_DELETE_ENUMERATION, enumFacet);
                        compoundCommand.add(deleteCommand);
                    }
                }
            }
            getCommandStack().execute(compoundCommand);
            enumerationsTable.refresh();
        }
    } else if (e.widget == enumerationsTable.getTable()) {
        StructuredSelection selection = (StructuredSelection) enumerationsTable.getSelection();
        if (selection.getFirstElement() != null) {
            deleteButton.setEnabled(true);
        } else {
            deleteButton.setEnabled(false);
        }
    }
}
Also used : XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) AddEnumerationsCommand(org.eclipse.wst.xsd.ui.internal.common.commands.AddEnumerationsCommand) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) DeleteCommand(org.eclipse.wst.xsd.ui.internal.common.commands.DeleteCommand) Shell(org.eclipse.swt.widgets.Shell) StringTokenizer(com.ibm.icu.util.StringTokenizer) XSDEnumerationFacet(org.eclipse.xsd.XSDEnumerationFacet) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) EnumerationsDialog(org.eclipse.wst.xsd.ui.internal.widgets.EnumerationsDialog) Display(org.eclipse.swt.widgets.Display)

Example 2 with EnumerationsDialog

use of org.eclipse.wst.xsd.ui.internal.widgets.EnumerationsDialog in project webtools.sourceediting by eclipse.

the class SpecificConstraintsWidget method widgetSelected.

public void widgetSelected(SelectionEvent e) {
    XSDSimpleTypeDefinition st = input;
    if (e.widget == addButton) {
        List enumList = st.getEnumerationFacets();
        // $NON-NLS-1$
        String newName = XSDCommonUIUtils.createUniqueEnumerationValue("value", enumList);
        if (kind == ENUMERATION) {
            CompoundCommand compoundCommand = new CompoundCommand();
            XSDSimpleTypeDefinition targetSimpleType = null;
            if (feature != null) {
                XSDSimpleTypeDefinition anonymousSimpleType = XSDCommonUIUtils.getAnonymousSimpleType(feature, input);
                if (anonymousSimpleType == null) {
                    anonymousSimpleType = XSDFactory.eINSTANCE.createXSDSimpleTypeDefinition();
                    anonymousSimpleType.setBaseTypeDefinition(input);
                    ChangeToLocalSimpleTypeCommand changeToAnonymousCommand = new ChangeToLocalSimpleTypeCommand(Messages._UI_ACTION_CHANGE_PATTERN, feature);
                    changeToAnonymousCommand.setAnonymousSimpleType(anonymousSimpleType);
                    compoundCommand.add(changeToAnonymousCommand);
                    input = anonymousSimpleType;
                }
                targetSimpleType = anonymousSimpleType;
            } else {
                targetSimpleType = input;
            }
            AddEnumerationsCommand command = new AddEnumerationsCommand(Messages._UI_ACTION_ADD_ENUMERATION, targetSimpleType);
            command.setValue(newName);
            compoundCommand.add(command);
            commandStack.execute(compoundCommand);
            setInput(input);
            constraintsTableViewer.refresh();
            int newItemIndex = constraintsTableViewer.getTable().getItemCount() - 1;
            constraintsTableViewer.editElement(constraintsTableViewer.getElementAt(newItemIndex), 0);
        }
    } else if (e.widget == addUsingDialogButton) {
        Display display = Display.getCurrent();
        // if it is null, get the default one
        display = display == null ? Display.getDefault() : display;
        Shell shell = display.getActiveShell();
        if (kind == PATTERN) {
            // $NON-NLS-1$
            String initialValue = "";
            RegexWizard wizard = new RegexWizard(initialValue);
            WizardDialog wizardDialog = new WizardDialog(shell, wizard);
            wizardDialog.setBlockOnOpen(true);
            wizardDialog.create();
            int result = wizardDialog.open();
            if (result == Window.OK) {
                String newPattern = wizard.getPattern();
                CompoundCommand compoundCommand = new CompoundCommand();
                XSDSimpleTypeDefinition targetSimpleType = null;
                if (feature != null) {
                    XSDSimpleTypeDefinition anonymousSimpleType = XSDCommonUIUtils.getAnonymousSimpleType(feature, input);
                    if (anonymousSimpleType == null) {
                        anonymousSimpleType = XSDFactory.eINSTANCE.createXSDSimpleTypeDefinition();
                        anonymousSimpleType.setBaseTypeDefinition(input);
                        ChangeToLocalSimpleTypeCommand changeToAnonymousCommand = new ChangeToLocalSimpleTypeCommand(Messages._UI_ACTION_CHANGE_PATTERN, feature);
                        changeToAnonymousCommand.setAnonymousSimpleType(anonymousSimpleType);
                        compoundCommand.add(changeToAnonymousCommand);
                        input = anonymousSimpleType;
                    }
                    targetSimpleType = anonymousSimpleType;
                } else {
                    targetSimpleType = input;
                }
                UpdateXSDPatternFacetCommand command = new UpdateXSDPatternFacetCommand(Messages._UI_ACTION_ADD_PATTERN, targetSimpleType, UpdateXSDPatternFacetCommand.ADD);
                command.setValue(newPattern);
                setInput(input);
                compoundCommand.add(command);
                commandStack.execute(compoundCommand);
                facetSection.doSetInput();
            }
            constraintsTableViewer.refresh();
        } else {
            EnumerationsDialog dialog = new EnumerationsDialog(shell);
            dialog.setBlockOnOpen(true);
            int result = dialog.open();
            if (result == Window.OK) {
                String text = dialog.getText();
                String delimiter = dialog.getDelimiter();
                StringTokenizer tokenizer = new StringTokenizer(text, delimiter);
                CompoundCommand compoundCommand = new CompoundCommand(Messages._UI_ACTION_ADD_ENUMERATIONS);
                XSDSimpleTypeDefinition targetSimpleType = null;
                if (feature != null) {
                    XSDSimpleTypeDefinition anonymousSimpleType = XSDCommonUIUtils.getAnonymousSimpleType(feature, input);
                    if (anonymousSimpleType == null) {
                        anonymousSimpleType = XSDFactory.eINSTANCE.createXSDSimpleTypeDefinition();
                        anonymousSimpleType.setBaseTypeDefinition(input);
                        // $NON-NLS-1$
                        ChangeToLocalSimpleTypeCommand changeToAnonymousCommand = new ChangeToLocalSimpleTypeCommand("", feature);
                        changeToAnonymousCommand.setAnonymousSimpleType(anonymousSimpleType);
                        compoundCommand.add(changeToAnonymousCommand);
                        input = anonymousSimpleType;
                    }
                    targetSimpleType = anonymousSimpleType;
                } else {
                    targetSimpleType = input;
                }
                while (tokenizer.hasMoreTokens()) {
                    String token = tokenizer.nextToken();
                    if (dialog.isPreserveWhitespace() == false) {
                        token = token.trim();
                    }
                    AddEnumerationsCommand command = new AddEnumerationsCommand(Messages._UI_ACTION_ADD_ENUMERATIONS, targetSimpleType);
                    command.setValue(token);
                    compoundCommand.add(command);
                }
                commandStack.execute(compoundCommand);
            }
            // setInput(input);
            facetSection.doSetInput();
            constraintsTableViewer.refresh();
        }
    } else if (e.widget == deleteButton) {
        StructuredSelection selection = (StructuredSelection) constraintsTableViewer.getSelection();
        CompoundCommand compoundCommand = new CompoundCommand();
        if (selection != null) {
            Iterator i = selection.iterator();
            if (selection.size() > 0) {
                compoundCommand.setLabel(Messages._UI_ACTION_DELETE_CONSTRAINTS);
            } else {
                compoundCommand.setLabel(Messages._UI_ACTION_DELETE_PATTERN);
            }
            while (i.hasNext()) {
                Object obj = i.next();
                if (obj != null) {
                    if (obj instanceof XSDPatternFacet) {
                        // $NON-NLS-1$
                        UpdateXSDPatternFacetCommand command = new UpdateXSDPatternFacetCommand("", input, UpdateXSDPatternFacetCommand.DELETE);
                        command.setPatternToEdit((XSDPatternFacet) obj);
                        compoundCommand.add(command);
                    } else if (obj instanceof XSDEnumerationFacet) {
                        XSDEnumerationFacet enumFacet = (XSDEnumerationFacet) obj;
                        DeleteCommand deleteCommand = new DeleteCommand(Messages._UI_ACTION_DELETE_ENUMERATION, enumFacet);
                        compoundCommand.add(deleteCommand);
                    }
                }
            }
            commandStack.execute(compoundCommand);
            constraintsTableViewer.refresh();
        }
    } else if (e.widget == editButton) {
        StructuredSelection selection = (StructuredSelection) constraintsTableViewer.getSelection();
        if (selection != null) {
            Object obj = selection.getFirstElement();
            if (obj instanceof XSDPatternFacet) {
                XSDPatternFacet pattern = (XSDPatternFacet) obj;
                String initialValue = pattern.getLexicalValue();
                if (initialValue == null) {
                    // $NON-NLS-1$
                    initialValue = "";
                }
                Shell shell = Display.getCurrent().getActiveShell();
                RegexWizard wizard = new RegexWizard(initialValue);
                WizardDialog wizardDialog = new WizardDialog(shell, wizard);
                wizardDialog.setBlockOnOpen(true);
                wizardDialog.create();
                int result = wizardDialog.open();
                if (result == Window.OK) {
                    String newPattern = wizard.getPattern();
                    pattern.setLexicalValue(newPattern);
                    constraintsTableViewer.refresh();
                }
            }
        }
    }
    setButtonStates(this.kind);
}
Also used : XSDPatternFacet(org.eclipse.xsd.XSDPatternFacet) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) UpdateXSDPatternFacetCommand(org.eclipse.wst.xsd.ui.internal.common.commands.UpdateXSDPatternFacetCommand) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) RegexWizard(org.eclipse.wst.xsd.ui.internal.wizards.RegexWizard) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) AddEnumerationsCommand(org.eclipse.wst.xsd.ui.internal.common.commands.AddEnumerationsCommand) DeleteCommand(org.eclipse.wst.xsd.ui.internal.common.commands.DeleteCommand) Shell(org.eclipse.swt.widgets.Shell) StringTokenizer(com.ibm.icu.util.StringTokenizer) XSDEnumerationFacet(org.eclipse.xsd.XSDEnumerationFacet) ChangeToLocalSimpleTypeCommand(org.eclipse.wst.xsd.ui.internal.common.commands.ChangeToLocalSimpleTypeCommand) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) EnumerationsDialog(org.eclipse.wst.xsd.ui.internal.widgets.EnumerationsDialog) WizardDialog(org.eclipse.jface.wizard.WizardDialog) Display(org.eclipse.swt.widgets.Display)

Aggregations

StringTokenizer (com.ibm.icu.util.StringTokenizer)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)2 CompoundCommand (org.eclipse.gef.commands.CompoundCommand)2 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)2 Display (org.eclipse.swt.widgets.Display)2 Shell (org.eclipse.swt.widgets.Shell)2 AddEnumerationsCommand (org.eclipse.wst.xsd.ui.internal.common.commands.AddEnumerationsCommand)2 DeleteCommand (org.eclipse.wst.xsd.ui.internal.common.commands.DeleteCommand)2 EnumerationsDialog (org.eclipse.wst.xsd.ui.internal.widgets.EnumerationsDialog)2 XSDEnumerationFacet (org.eclipse.xsd.XSDEnumerationFacet)2 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)2 WizardDialog (org.eclipse.jface.wizard.WizardDialog)1 ChangeToLocalSimpleTypeCommand (org.eclipse.wst.xsd.ui.internal.common.commands.ChangeToLocalSimpleTypeCommand)1 UpdateXSDPatternFacetCommand (org.eclipse.wst.xsd.ui.internal.common.commands.UpdateXSDPatternFacetCommand)1 RegexWizard (org.eclipse.wst.xsd.ui.internal.wizards.RegexWizard)1 XSDPatternFacet (org.eclipse.xsd.XSDPatternFacet)1