Skip to main content
summaryrefslogtreecommitdiffstats
blob: 46ec8646e635662840e3a29143022897d9638efd (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
/*******************************************************************************
 * 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 org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * 
 * @author cbateman
 * 
 */
public abstract class AbstractMasterForm
{

    private final FormToolkit         _toolkit;
    private ISelectionChangedListener _listener;
    private ToolBarManager            _toolBarManager;

    /**
     * @param toolkit
     */
    protected AbstractMasterForm(FormToolkit toolkit)
    {
        super();
        _toolkit = toolkit;
    }

    /**
     * @param listener
     *            the selection listener that is signalled to indicate the
     *            selection in the master has changed and the detail should be
     *            updated.
     */
    public final void initialize(final ISelectionChangedListener listener)
    {
        _listener = listener;
        doInitialize();
    }

    /**
     * It is safe to call getListener() and get getToolkit() in this method. All
     * other methods should be considered unavailable.
     */
    protected void doInitialize()
    {
        // do nothing by default; override to do customize init
    }

    /**
     * @param parent
     * @return the client area for the master form, using parent is the parent
     * control.
     */
    public abstract Control createClientArea(final Composite parent);

    /**
     * @param form
     */
    public final void createHead(final Form form)
    {
        final Composite head = form.getHead();
        final Composite container = getToolkit().createComposite(head);
        container.setLayout(new RowLayout());

        // sub-class contribution
        contributeToHeadArea(getToolkit(), container);

        _toolBarManager = new ToolBarManager(SWT.FLAT);
        ToolBar toolbar = _toolBarManager.createControl(container);
        // _toolkit.adapt(toolbar, false, false);

        toolbar.setBackground(form.getHead().getBackground());
        toolbar.setForeground(form.getHead().getForeground());
        //toolbar.setCursor(FormsResources.getHandCursor());
        container.addDisposeListener(new DisposeListener()
        {
            public void widgetDisposed(DisposeEvent e)
            {
                if (_toolBarManager != null)
                {
                    _toolBarManager.dispose();
                    _toolBarManager = null;
                }
            }
        });

        form.setHeadClient(container);

    }

    /**
     * Override to add client area before the toolbar.
     * 
     * @param toolkit
     * @param container
     */
    protected void contributeToHeadArea(FormToolkit toolkit, Composite container)
    {
        // do nothing by default
    }

    /**
     * @param formManager
     */
    public final void contributeActions(final IToolBarManager formManager)
    {
        contributeActions(formManager, _toolBarManager);
    }

    /**
     * @param formManager
     *            adds to the toolkit's toolbar
     * @param localManager
     *            adds to AbstractMasterForm's toolbar.
     */
    protected void contributeActions(final IToolBarManager formManager,
            final IToolBarManager localManager)
    {
        // do nothing by default. Override to add actions.
    }

    /**
     * Should be called at any time after initialize and createContents when the
     * owner is finished with the master-detail form block.
     */
    public void dispose()
    {
        // do nothing by default;
    }

    /**
     * @return the tool kit in use.
     */
    protected final FormToolkit getToolkit()
    {
        return _toolkit;
    }

    /**
     * @return the selection listener
     */
    protected final ISelectionChangedListener getListener()
    {
        return _listener;
    }
}

Back to the top