Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8faa05cc86418e4a22ab4e8635f0cdef40c17e3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*******************************************************************************
 * Copyright (c) 2008, 2019 IBM Corporation and others.
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.itemcreation.customizer;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jst.jsf.common.dom.TagIdentifier;
import org.eclipse.jst.jsf.context.resolver.structureddocument.internal.ResolverUtil;
import org.eclipse.jst.jsf.core.internal.tld.TagIdentifierFactory;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.commands.ICustomizableCommand;
import org.eclipse.jst.pagedesigner.dom.IDOMPosition;
import org.eclipse.jst.pagedesigner.editors.palette.IDropSourceData;
import org.eclipse.jst.pagedesigner.editors.palette.ITagDropSourceData;
import org.eclipse.jst.pagedesigner.elementedit.ElementEditFactoryRegistry;
import org.eclipse.jst.pagedesigner.elementedit.IElementEdit;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;

/**
 * Not API -- Do not use
 * 
 * @author cbateman
 *
 */
public final class DropCustomizationController
{
    private final ICustomizableCommand _command;
    private final IDropSourceData  _dropSourceData;
    private final IDOMDocument  _domDocument;
    private final IDOMPosition _domPosition;

    /**
     * @param command
     * @param dropSourceData 
     * @param domDocument 
     * @param domPosition 
     */
    public DropCustomizationController(final ICustomizableCommand command,
            final IDropSourceData dropSourceData, final IDOMDocument domDocument, final IDOMPosition domPosition)
    {
        _command = command;
        _dropSourceData = dropSourceData;
        _domDocument = domDocument;
        _domPosition = domPosition;
    }

    /**
     * @return the result of the customization
     */
    public IStatus performCustomization()
    {
        String tagName = _dropSourceData.getId();
        if (_dropSourceData instanceof ITagDropSourceData)
        {
            tagName = ((ITagDropSourceData)_dropSourceData).getTagName();
        }
        final TagIdentifier tagId = TagIdentifierFactory.createJSPTagWrapper(
                _dropSourceData.getNamespace(), tagName);
        final IElementEdit elementEdit = ElementEditFactoryRegistry.getInstance()
                .createElementEdit(tagId);

        IStatus status = Status.OK_STATUS;
        if (elementEdit != null)
        {
            final IDropCustomizer customizer = elementEdit.getDropCustomizer(_dropSourceData);

            if (customizer != null)
            {
                final IFile file = getFile(_domDocument);
                if (file != null)
                {
                    status = customizer.runCustomizer(file, _domPosition);
                }
                else 
                {
                    PDPlugin.log("Could not find file.", new Exception("Not a real exception")); //$NON-NLS-1$ //$NON-NLS-2$
                    status = Status.CANCEL_STATUS;
                }

                if (status.getSeverity() == IStatus.OK)
                {
                    _command.setCustomizationData(customizer
                            .getDropCustomizationData());
                }
            }
        }
        return status;
    }

    private static IFile getFile(IDOMDocument domDoc)
    {
        final IStructuredDocument sdoc = domDoc.getStructuredDocument();
        if (sdoc != null)
        {
            return ResolverUtil.getFileForDocument(sdoc);
        }
        return null;
    }
}

Back to the top