Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2aa6441c5dc4c122c7ec33b897311a12f4308c45 (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
/**
 * Copyright (c) 2011 Mia-Software.
 *
 * 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:
 * 	Nicolas Guyomar (Mia-Software) - Bug 349546 - EMF Facet facetSet editor
 *  Gregoire Dupe (Mia-Software) - Bug 373248 - Clean PMD errors
 */
package org.eclipse.papyrus.emf.facet.efacet.ui.internal.composites;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;

@SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
// @SuppressWarnings("PMD.ConstructorCallsOverridableMethod") Temporary: this class must be deeply refactored.
// TODO remove the @SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
// cf. bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=373248
public class BrowseComposite extends Composite {

	private Text textField;
	private Button browseButton;
	private String browseButtonText = "..."; //$NON-NLS-1$

	public BrowseComposite(final Composite parent, final int style) {
		this(parent, style, null, true);
	}

	public BrowseComposite(final Composite parent, final int style, final String buttonName, final boolean canBeChanged) {
		super(parent, SWT.NONE);
		if (buttonName != null) {
			this.browseButtonText = buttonName;
		}
		setEnabled(canBeChanged);
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		final int horizontalSpacing = 4;
		layout.horizontalSpacing = horizontalSpacing;
		layout.verticalSpacing = 0;
		layout.makeColumnsEqualWidth = false;
		layout.numColumns = 2;
		setLayout(layout);
		setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
		createWidgets(style);
		init();
	}

	public void addModifyListener(final ModifyListener modifyListener) {
		if (this.textField != null) {
			this.textField.addModifyListener(modifyListener);
		}
	}

	public void removeModifyListener(final ModifyListener modifyListener) {
		if (this.textField != null) {
			this.textField.removeModifyListener(modifyListener);
		}
	}

	protected void createWidgets(final int style) {
		this.textField = new Text(this, SWT.SINGLE | style);
		this.browseButton = new Button(this, SWT.PUSH);
	}

	private void init() {
		this.textField.setEditable(false);
		// We want the background white so that the user notices that there is something missing in
		// the textField, but he has to use the "..." button
		this.textField.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		this.textField.setBounds(getBounds());
		this.textField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		this.textField.addKeyListener(new KeyListener() {
			public void keyReleased(final KeyEvent e) {
				// Nothing on release
			}

			public void keyPressed(final KeyEvent event) {
				// Enter key pressed
				if ((event.keyCode == SWT.CR && event.stateMask == 0) || (event.keyCode == SWT.KEYPAD_CR && event.stateMask == 0)) {
					handleBrowse();
				}
			}
		});

		this.browseButton.setText(this.browseButtonText);
		this.browseButton.setEnabled(isEnabled());
		GridData data = new GridData(SWT.FILL, SWT.FILL, false, true);
		this.browseButton.setLayoutData(data);
		this.browseButton.addSelectionListener(new SelectionListener() {
			public void widgetSelected(final SelectionEvent e) {
				handleBrowse();
			}

			public void widgetDefaultSelected(final SelectionEvent e) {
				// Nothing
			}
		});
	}

	protected void handleBrowse() {
		// Meant to be overridden
	}

	public void updateTextFieldContent(final String content) {
		if (this.textField != null) {
			this.textField.setText(content);
			this.textField.setFocus();
		}
	}
}

Back to the top