Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 52684ea0e6342521b99af12fa480301b3fcb0c28 (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
/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * 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:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package org.eclipse.papyrus.extensionpoints.editors.ui;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.papyrus.extensionpoints.editors.configuration.IDirectEditorConfiguration;
import org.eclipse.papyrus.extensionpoints.editors.configuration.IDirectEditorConfiguration.Selection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.VerifyKeyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

/**
 * Dialog used in for direct edition, when an extension is provided
 */
public class ExtendedDirectEditionDialog extends LabelEditorDialog {

	/** Title of the Dialog */
	final private static String TITLE = "Edit Label";

	/** Edited object */
	protected Object editedObject;

	/** Document used by the dialog */
	protected Document document;

	/** initial text value */
	protected String value;

	/** extension configuration */
	protected IDirectEditorConfiguration configuration;

	/**
	 * Creates a new ExtendedDirectEditionDialog
	 *
	 * @param parentShell
	 *            the parent shell
	 * @param parameter
	 *            the editedObject
	 * @param initialValue
	 *            the initial text value
	 */
	public ExtendedDirectEditionDialog(Shell parentShell, Object object, String initialValue,
			IDirectEditorConfiguration configuration) {
		super(parentShell, TITLE, initialValue, configuration.getInputValidator());
		this.editedObject = object;
		this.value = initialValue;
		this.configuration = configuration;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Group composite = new Group(parent, SWT.RESIZE);
		composite.setText("Specification");
		GridLayout layout = new GridLayout();
		layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
		layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
		layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
		layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
		composite.setLayout(layout);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));

		Composite viewerGroup = new Composite(composite, SWT.RESIZE);
		FillLayout viewerLayout = new FillLayout();
		viewerGroup.setLayout(viewerLayout);
		GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL
				| GridData.VERTICAL_ALIGN_CENTER);
		data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
		viewerGroup.setLayoutData(data);
		viewer = new SourceViewer(viewerGroup, null, SWT.BORDER | SWT.FILL_EVEN_ODD);

		// configure source viewer
		document = new Document();
		document.set(value);

		// add completion processor key listener (ctrl+space keys)
		viewer.appendVerifyKeyListener(new LabelKeyListener(viewer));

		document.addDocumentListener(new DocumentListener());
		viewer.configure(configuration.getSourceViewerConfiguration());
		viewer.setDocument(document);

		Selection selection = configuration.getTextSelection(value, editedObject);
		viewer.setSelectedRange(selection.getStart(), selection.getLentgh());

		Composite extendedArea = configuration.createExtendedDialogArea(viewerGroup);
		if (extendedArea != null) {
			extendedArea.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
		}

		errorMessageText = new CLabel(composite, SWT.READ_ONLY | SWT.SHADOW_NONE);
		errorMessageText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
		errorMessageText.setBackground(errorMessageText.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		// Set the error message text
		// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=66292
		setErrorMessage(errorMessage);

		applyDialogFont(composite);
		return composite;
	}

	/**
	 *
	 */
	private class LabelKeyListener implements VerifyKeyListener {

		/**
		 *
		 */
		SourceViewer viewer;

		/**
		 * Default constructor.
		 *
		 * @param viewer
		 */
		public LabelKeyListener(SourceViewer viewer) {
			this.viewer = viewer;
		}

		/*
		 * (non-Javadoc)
		 *
		 * @see
		 * org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
		 */
		/**
		 *
		 *
		 * @param event
		 */
		public void verifyKey(VerifyEvent event) {
			if ((event.stateMask == SWT.CTRL) && (event.character == ' ')) {
				if (viewer.canDoOperation(ISourceViewer.CONTENTASSIST_PROPOSALS)) {
					viewer.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
				}
				event.doit = false;
			} else if (event.character == SWT.CR) {
				event.doit = false;
			}
		}
	}

	/**
	 *
	 */
	private class DocumentListener implements IDocumentListener {

		/**
		 *
		 *
		 * @param event
		 */
		public void documentAboutToBeChanged(DocumentEvent event) {
		}

		/**
		 *
		 *
		 * @param event
		 */
		public void documentChanged(DocumentEvent event) {
			validateInput();
		}

	}

}

Back to the top