Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a0d9780973335f61247f49bacca6dc855090b57a (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
187
188
189
190
191
192
193
194
195
196
197
198
199
/*******************************************************************************
 * Copyright (c) 2012, 2013 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.ui.views.editor.controls;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.tcf.te.ui.controls.BaseControl;
import org.eclipse.tcf.te.ui.swt.SWTControlUtil;
import org.eclipse.tcf.te.ui.views.nls.Messages;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPropertyListener;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;

/**
 * A panel control holding a set of buttons.
 * <p>
 * The panel control is associated with a managed form. If the managed form is the
 * embedded within a form editor or a form page, <code>getEditor()</code> will return
 * the parent form editor instance.
 * <p>
 * If the panel control has the default "Apply" button, the apply button will be
 * enabled once the parent form editor is marked dirty. If the "Apply" button is
 * clicked the form editor will be saved.
 */
public class ButtonPanelControl extends BaseControl {
	// Reference to the parent managed form
	private final IManagedForm form;

	// The control sub-control
	private Composite panel;
	/* default */ Button applyButton;

	// Reference to the dirty action listener
	private IPropertyListener dirtyListener = null;

	/**
	 * Constructor
	 *
	 * @param form The parent managed form. Must not be <code>null</code>.
	 */
	public ButtonPanelControl(IManagedForm form) {
		super();

		Assert.isNotNull(form);
		this.form = form;
	}

	/**
	 * Returns the parent managed form instance.
	 *
	 * @return The parent managed form instance.
	 */
	protected final IManagedForm getManagedForm() {
		return form;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
	 */
	@Override
	public void dispose() {
		// Dispose the dirty action listener
		if (dirtyListener != null && getEditor() != null) {
			getEditor().removePropertyListener(dirtyListener);
			dirtyListener = null;
		}

	    super.dispose();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseControl#setupPanel(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void setupPanel(Composite parent) {
	    super.setupPanel(parent);

	    // Create the buttons panel
	    panel = doCreatePanel(parent);
	    Assert.isNotNull(panel);

	    // Create the buttons within the buttons panel
	    doCreateButtons(panel);
	}

	/**
	 * Create the panel that holds the buttons.
	 *
	 * @param parent The parent composite. Must not be <code>null</code>.
	 * @return The panel.
	 */
	protected Composite doCreatePanel(Composite parent) {
		Assert.isNotNull(parent);

		Composite panel = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0; layout.marginWidth = 0;
		panel.setLayout(layout);
		GridData layoutData = new GridData(SWT.TRAIL, SWT.BEGINNING, true, false);
		if (parent.getLayout() instanceof GridLayout) layoutData.horizontalSpan = ((GridLayout)parent.getLayout()).numColumns;
		panel.setLayoutData(layoutData);
		panel.setBackground(parent.getBackground());

		return panel;
	}

	/**
	 * Returns if or of not the control has an apply button added to the buttons
	 * panel by default.
	 * <p>
	 * <b>Note:</b> The default return by this method is <code>true</code>.
	 *
	 * @return <code>true</code> if there should be an apply button.
	 */
	protected boolean hasApplyButton() {
		return true;
	}

	/**
	 * Creates the buttons within the given parent composite.
	 *
	 * @param parent The parent composite. Must not be <code>null</code>.
	 */
	protected void doCreateButtons(Composite parent) {
		Assert.isNotNull(parent);

		// Create a "Apply" button if requested
		if (hasApplyButton() && getEditor() != null) {
			applyButton = new Button(parent, SWT.PUSH);
			applyButton.setText(Messages.ButtonPanelControl_applyButton_label);
			applyButton.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_ETOOL_SAVE_EDIT));
			applyButton.setBackground(parent.getBackground());

			GridData layoutData = new GridData(SWT.TRAIL, SWT.CENTER, false, false);
			layoutData.widthHint = SWTControlUtil.convertWidthInCharsToPixels(applyButton, 15);
			applyButton.setLayoutData(layoutData);

			applyButton.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					FormEditor editor = getEditor();
					Assert.isNotNull(editor);
					if (editor.isDirty()) {
						editor.doSave(new NullProgressMonitor());
					}
				}
			});

			dirtyListener = new IPropertyListener() {
				@Override
				public void propertyChanged(Object source, int propId) {
					if (propId == IEditorPart.PROP_DIRTY) {
						boolean dirty = getEditor().isDirty();
						applyButton.setEnabled(dirty);
					}
				}
			};
			getEditor().addPropertyListener(dirtyListener);
			applyButton.setEnabled(getEditor().isDirty());
		}
	}

	/**
	 * Returns the parent form editor if the managed form is embedded
	 * in such an editor.
	 *
	 * @return The parent form editor or <code>null</code>.
	 */
	protected final FormEditor getEditor() {
		FormEditor editor = null;

		Object container = getManagedForm() != null ? getManagedForm().getContainer() : null;
		if (container  instanceof FormEditor) {
			editor = (FormEditor)container;
		} else if (container instanceof FormPage) {
			editor = ((FormPage)container).getEditor();
		}

		return editor;
	}
}

Back to the top