Skip to main content
summaryrefslogtreecommitdiffstats
blob: f8b4323c38433515023258df9476fe1e64318bc7 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*******************************************************************************
 * Copyright (c) 2001, 2008 Oracle Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Oracle Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.common.ui.internal.form;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormText;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

/**
 * A detail form that uses XML text sections.
 * 
 * @author cbateman
 * 
 */
public abstract class AbstractXMLSectionsDetailsForm extends
        AbstractDetailsForm
{
    private Map<Object, XMLTextSection> _textSections;
    private Composite                   _detailFormComposite;

    /**
     * @param parent
     */
    @Override
    public final void createContents(final Composite parent)
    {
        _detailFormComposite = getToolkit().createComposite(parent, SWT.NONE);
        final RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
        rowLayout.fill = true;
        _detailFormComposite.setLayout(rowLayout);
        // take a copy of what's returned so the sub-class can't control
        // the map reference
        _textSections = Collections
                .unmodifiableMap(new HashMap<Object, XMLTextSection>(
                        createXMLTextSections(_detailFormComposite)));

        final Set<XMLTextSection> expandedSections = getInitiallyExpanded(_textSections);
        for (final Map.Entry<? extends Object, XMLTextSection> entry : _textSections
                .entrySet())
        {
            final XMLTextSection section = entry.getValue();
            if (expandedSections.contains(section))
            {
                section._section.setExpanded(true);
            }
        }
    }

    /**
     * @param parent
     *            the parent that should be used for all XMLTextSections
     * @return a map keyed by an object type understood by the sub-class
     *         containing as values the XMLTextSections
     */
    protected abstract Map<? extends Object, XMLTextSection> createXMLTextSections(
            final Composite parent);

    /**
     * @param sections
     * @return the subset of XMLTextSections in the provided map that are to be
     *         expanded. NOTE: all elements in the returned set must be in
     *         sections.getValues.
     */
    protected abstract Set<XMLTextSection> getInitiallyExpanded(
            final Map<Object, XMLTextSection> sections);

    /**
     * @return the control for this form
     */
    @Override
    public Control getControl()
    {
        return _detailFormComposite;
    }

    /**
     * @param selection
     */
    public final void selectionChanged(final ISelection selection)
    {
        if (selection instanceof IStructuredSelection)
        {
            final Object selectionObj = ((IStructuredSelection) selection)
                    .getFirstElement();
            doUpdateSelection(selectionObj);
        }
    }

    @Override
    public void commit(final boolean onSave)
    {
        // do nothing
    }

    @Override
    public void dispose()
    {
        // do nothing
    }

    @Override
    public void setFocus()
    {
        // do nothing; sub-classes should override to pick an XMLTextSection
        // where they want focus.
    }

    /**
     * An XML text section
     * 
     */
    protected final static class XMLTextSection
    {
        private final Section _section;
        private FormText      _formText;

        /**
         * @param toolkit
         * @param parent
         * @param title
         */
        public XMLTextSection(final FormToolkit toolkit,
                final Composite parent, final String title)
        {
            _section = toolkit.createSection(parent,
                    ExpandableComposite.TREE_NODE
                            | ExpandableComposite.CLIENT_INDENT);
            _section.setLayoutData(new RowData());
            _section.setText(title);

            _formText = toolkit.createFormText(_section, true);
            _formText.setText("", false, false);

            _section.setClient(_formText);
        }

        /**
         * @param text
         * @param parseTags
         * @param expandURLs
         */
        public void setText(final String text, final boolean parseTags,
                final boolean expandURLs)
        {
            _formText.setText(text, parseTags, expandURLs);
        }

        /**
         * @return the parent control.
         */
        public Control getControl()
        {
            return _section;
        }

        /**
         * Force a visual relayout and update.
         */
        public void refresh()
        {
            _section.getParent().layout(true, true);
        }
    }

}

Back to the top