Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e0aa11f82d476d867514cf8c5ea43c346f14922a (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
/*****************************************************************************
 * Copyright (c) 2016 CEA LIST 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:
 *   Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.customization.nattableconfiguration.wizards;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.internal.resources.File;
import org.eclipse.core.runtime.Assert;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jface.dialogs.DialogSettings;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.customization.nattableconfiguration.Activator;
import org.eclipse.papyrus.customization.nattableconfiguration.helper.TableConfigurationHelper;
import org.eclipse.papyrus.customization.nattableconfiguration.messages.Messages;
import org.eclipse.papyrus.customization.nattableconfiguration.pages.ColumnConfigurationWizardPage;
import org.eclipse.papyrus.customization.nattableconfiguration.pages.EditGenericNattableConfigurationFieldsNattableWizardPage;
import org.eclipse.papyrus.customization.nattableconfiguration.pages.RowConfigurationWizardPage;
import org.eclipse.papyrus.customization.nattableconfiguration.pages.SlaveConfigurationWizardPage;
import org.eclipse.papyrus.customization.nattableconfiguration.utils.NattableConfigurationConstants;
import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableconfiguration.TableConfiguration;
import org.eclipse.papyrus.infra.nattable.utils.TableEditingDomainUtils;
import org.eclipse.papyrus.infra.nattable.wizard.AbstractTableWizard;
import org.eclipse.papyrus.infra.ui.util.WorkbenchPartHelper;
import org.eclipse.papyrus.internal.infra.nattable.model.resources.NattableConfigurationResource;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWizard;

/**
 * The wizard to edit an existing TableConfiguration file
 */
public class EditTableConfigurationWizard extends AbstractTableWizard implements IWorkbenchWizard {

	/**
	 * The generate plugin activator setting.
	 */
	private static final String GENERATE_PlUGIN_ACTIVATOR = "generatePluginActivator"; //$NON-NLS-1$

	/**
	 * The ui plugin setting.
	 */
	private static final String UI_PLUGIN = "uiPlugin"; //$NON-NLS-1$

	/**
	 * The edited table configuration
	 */
	protected TableConfiguration configuration;

	/**
	 * The initial resource selected.
	 */
	protected Resource initialResource;

	/**
	 * the edited Table Configuration Helper;
	 */
	protected TableConfigurationHelper helper;


	/**
	 * 
	 * Constructor.
	 * 
	 * Thi sconstructor is used when we create a new Papyrus Table Configuration Poject from the wizard Dialog
	 *
	 */
	public EditTableConfigurationWizard() {
		final ImageDescriptor desc = org.eclipse.papyrus.infra.widgets.Activator.getDefault().getImageDescriptor(Activator.PLUGIN_ID, NattableConfigurationConstants.ICON_WIZBAN_PATH);
		setDefaultPageImageDescriptor(desc);
		setWindowTitle(Messages.CreateNattableConfigurationWizard_WizardTitke);
		setForcePreviousAndNextButtons(true);
	}

	/**
	 * @see org.eclipse.papyrus.infra.nattable.wizard.AbstractTableWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
	 *
	 * @param workbench
	 * @param selection
	 */
	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		super.init(workbench, selection);
		this.initialResource = getSelectedResource(selection);
		this.configuration = getEditedTableConfiguration(this.initialResource);
		Assert.isNotNull(this.configuration);
		this.helper = new TableConfigurationHelper(configuration);
	}

	/**
	 * 
	 * @param currentSelection
	 *            the current selection
	 * @return
	 * 		the resource if it already exist or <code>null</code>;
	 */
	protected Resource getSelectedResource(final ISelection currentSelection) {
		Resource resource = null;

		if (currentSelection instanceof StructuredSelection && 1 == ((StructuredSelection) currentSelection).size()) {
			final Object selectedElement = ((StructuredSelection) currentSelection).getFirstElement();
			if (selectedElement instanceof File && NattableConfigurationResource.NATTABLE_CONFIGURATION_RESOURCE_FILE_EXTENSION.equals(((File) selectedElement).getFileExtension())) {

				INattableModelManager manager = null;
				// TODO required ?
				final IEditorPart currentPart = WorkbenchPartHelper.getCurrentActiveWorkbenchPart().getSite().getPage().getActiveEditor();
				if (null != currentPart) {
					manager = (INattableModelManager) currentPart.getAdapter(INattableModelManager.class);
				}

				if (null != manager) {
					resource = TableEditingDomainUtils.getTableEditingDomain(manager.getTable()).getResourceSet().getResource(URI.createFileURI(((File) selectedElement).getLocation().toString()), true);
				} else {
					final ResourceSet set = new ResourceSetImpl();
					resource = set.getResource(URI.createFileURI(((File) selectedElement).getLocation().toString()), true);
				}
			}
		}

		return resource;
	}

	/**
	 * Get the edited table configuration.
	 * 
	 * @return
	 * 		the edited table configuration.
	 */
	protected TableConfiguration getEditedTableConfiguration(final Resource resource) {
		TableConfiguration configuration = null;
		if (null != resource) {
			if (!resource.getContents().isEmpty() && resource.getContents().get(0) instanceof TableConfiguration) {
				configuration = EcoreUtil.copy((TableConfiguration) resource.getContents().get(0));
			}
		}
		return configuration;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.jface.wizard.Wizard#addPages()
	 */
	@Override
	public void addPages() {
		addEditNattableConfigurationPage();
		addPage(new RowConfigurationWizardPage(this.helper));
		addPage(new ColumnConfigurationWizardPage(this.helper));
		addPage(new SlaveConfigurationWizardPage(this.helper));
	}

	protected void addEditNattableConfigurationPage(){
		addPage(new EditGenericNattableConfigurationFieldsNattableWizardPage(this.helper));
	}
	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.jface.wizard.Wizard#getDialogSettings()
	 */
	@Override
	public IDialogSettings getDialogSettings() {
		DialogSettings dialogSettings = new DialogSettings(""); //$NON-NLS-1$
		dialogSettings.put(GENERATE_PlUGIN_ACTIVATOR, true);
		dialogSettings.put(UI_PLUGIN, false);
		return dialogSettings;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 */
	@Override
	public boolean performFinish() {
		
		boolean result = false;

		// The resource is modified directly without command (undo/redo impossible)
		// Save DiagramDialog at proper position
		if (null != this.initialResource) {
			this.initialResource.getContents().clear();
			this.initialResource.getContents().add(this.configuration);
			result = true;
		}
		if (result) {
			result = saveResource();
		}

		return result;
	}

	/**
	 * 
	 * @return
	 * 		<code>true</code> if the resource has been properly saved
	 */
	protected final boolean saveResource() {
		boolean result = true;
		final Map<Object, Object> saveOptions = new HashMap<Object, Object>();
		saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED, Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
		saveOptions.put(Resource.OPTION_LINE_DELIMITER, Resource.OPTION_LINE_DELIMITER_UNSPECIFIED);

		try {
			initialResource.save(saveOptions);
		} catch (final IOException e) {
			Activator.log.error(e);
			result = false;
		}
		return result;
	}
}

Back to the top