Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7ef5a4378b4571afe87507530bdf8f5fb68e3d56 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*****************************************************************************
 * Copyright (c) 2010 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:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.properties.tabbed.profile.imagesection;

import java.io.File;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.core.utils.EditorUtils;
import org.eclipse.papyrus.uml.properties.tabbed.profile.AbstractViewSection;
import org.eclipse.papyrus.uml.properties.tabbed.profile.Activator;
import org.eclipse.papyrus.uml.tools.utils.ImageUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Image;


/**
 * this class display section to display serialize in XML the image
 */
public class ImageContentSection extends AbstractViewSection {

	/**
	 * 
	 */
	private Button browseButton, removeButton;

	/**
	 * 
	 */
	private MouseListener browseButtonListener, removeButtonListener;

	/**
	 * 
	 */
	private String sectionLabel = "Content:";

	/**
	 * 
	 */
	private final org.eclipse.swt.graphics.Image remove_img = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/delete.gif").createImage();

	/**
	 * 
	 */
	private final org.eclipse.swt.graphics.Image add_img_32 = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/Add_32x32.gif").createImage();

	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage
	 * @param parent
	 */
	@Override
	public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {

		super.createControls(parent, tabbedPropertySheetPage);
		final Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		FormData data;

		browseButton = getWidgetFactory().createButton(composite, "", SWT.PUSH);
		browseButton.setImage(add_img_32);
		removeButton = getWidgetFactory().createButton(composite, "", SWT.PUSH);
		removeButton.setImage(remove_img);
		CLabel iconLabel = getWidgetFactory().createCLabel(composite, sectionLabel);

		// browseButton layout
		data = new FormData();
		data.left = new FormAttachment(0, 85);
		data.top = new FormAttachment(0, ITabbedPropertyConstants.HSPACE);
		browseButton.setLayoutData(data);

		// removeButton layout
		data = new FormData();
		data.left = new FormAttachment(browseButton, ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(browseButton, 0, SWT.CENTER);
		removeButton.setLayoutData(data);

		// iconLabel layout
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.top = new FormAttachment(browseButton, 0, SWT.CENTER);
		iconLabel.setLayoutData(data);

		browseButton.addMouseListener(browseButtonListener = new MouseListener() {

			public void mouseDoubleClick(MouseEvent e) {
			}

			public void mouseDown(MouseEvent e) {
			}

			public void mouseUp(MouseEvent e) {
				FileDialog fd = new FileDialog(composite.getShell());
				String extensions[] = { "*.jpg;*.bmp;*.ico;*.gif;*.png;*.wmf;*.emf" };
				fd.setFilterExtensions(extensions);
				String iconSelected = fd.open();

				// No image selected
				if(iconSelected == null) {
					return;
				}

				if(getElement() instanceof Image) {

					final File imgFile = new File(iconSelected);

					TransactionalEditingDomain dom = EditorUtils.getTransactionalEditingDomain();
					AbstractTransactionalCommand operation = new AbstractTransactionalCommand(dom, "Set Image content", null) {

						/**
						 * {@inheritDoc}
						 */
						@Override
						protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
							try {
								ImageUtil.setContent((Image)getElement(), imgFile);
							} catch (Exception ex) {
								return CommandResult.newErrorCommandResult(ex);
							}
							return CommandResult.newOKCommandResult();
						}
					};

					dom.getCommandStack().execute(new GMFtoEMFCommandWrapper(operation));
					
					refresh();
				}
			}
		});

		removeButton.addMouseListener(removeButtonListener = new MouseListener() {

			public void mouseDoubleClick(MouseEvent e) {
			}

			public void mouseDown(MouseEvent e) {
			}

			public void mouseUp(MouseEvent e) {
				// Erase image content
				if(getElement() instanceof Image) {

					TransactionalEditingDomain dom = EditorUtils.getTransactionalEditingDomain();
					AbstractTransactionalCommand operation = new AbstractTransactionalCommand(dom, "Remove Image content", null) {

						/**
						 * {@inheritDoc}
						 */
						@Override
						protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
							try {
								ImageUtil.setContent((Image)getElement(), null);
							} catch (Exception ex) {
								return CommandResult.newErrorCommandResult(ex);
							}
							return CommandResult.newOKCommandResult();
						}
					};
					dom.getCommandStack().execute(new GMFtoEMFCommandWrapper(operation));

					refresh();
				}
			}
		});
	}

	/**
	 * 
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#refresh()
	 * 
	 */
	public void refresh() {
		if(!browseButton.isDisposed()) {

			/* initialization of buttons enabling */
			if(!(getElement() instanceof Image)) {
				return;
			}

			// Get Image content
			org.eclipse.swt.graphics.Image image = null;

			try {
				image = ImageUtil.getContent((Image)getElement());
			} catch (Exception e) {
				Activator.log.error(e);
			}

			// Refresh text
			if(image != null) {
				// Resize icon to 32x32
				org.eclipse.swt.graphics.Image resizedIcon = new org.eclipse.swt.graphics.Image(image.getDevice(), image.getImageData().scaledTo(32, 32));
				browseButton.setImage(resizedIcon);
				removeButton.setEnabled(true);

			} else {
				browseButton.setImage(add_img_32);
				removeButton.setEnabled(false);
			}
		}
	}

	/**
	 * 
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#dispose()
	 * 
	 */
	public void dispose() {
		super.dispose();
		if(browseButton != null && !browseButton.isDisposed())
			browseButton.removeMouseListener(browseButtonListener);
		if(removeButton != null && !removeButton.isDisposed())
			removeButton.removeMouseListener(removeButtonListener);
	}

}

Back to the top