Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f5849046271fb70a1fdcbb9a31afd106018b06fe (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*****************************************************************************
 * 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:
 *  Tatiana Fesenko (CEA LIST) - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.wizards.pages;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.papyrus.uml.diagram.wizards.CreateModelWizard;
import org.eclipse.papyrus.uml.diagram.wizards.Messages;
import org.eclipse.papyrus.uml.diagram.wizards.SettingsHelper;
import org.eclipse.papyrus.uml.diagram.wizards.category.DiagramCategoryDescriptor;
import org.eclipse.papyrus.uml.diagram.wizards.category.DiagramCategoryRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
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.Group;


/**
 * The Class SelectDiagramCategoryPage.
 */
public class SelectDiagramCategoryPage extends WizardPage {

	/** The my diagram kind buttons. */
	final private List<Button> myDiagramKindButtons = new ArrayList<Button>();

	/** The diagram category. */
	private final List<String> mySelectedDiagramCategoryIds = new LinkedList<String>();

	/** The Constant PAGE_ID. */
	public static final String PAGE_ID = "SelectDiagramCategory"; //$NON-NLS-1$

	/** The Constant DEFAULT_EXTENSION. */
	public static final String DEFAULT_EXTENSION = "uml"; //$NON-NLS-1$

	/** The my allow several categories. */
	private final boolean myAllowSeveralCategories;

	/**
	 * Instantiates a new select diagram category page.
	 * 
	 */
	public SelectDiagramCategoryPage() {
		this(false);
	}

	/**
	 * Instantiates a new select diagram category page.
	 *
	 * @param allowSeveralCategories the allow several categories
	 */
	public SelectDiagramCategoryPage(boolean allowSeveralCategories) {
		super(PAGE_ID);
		myAllowSeveralCategories = allowSeveralCategories;
		setTitle(Messages.SelectDiagramCategoryPage_select_language);
	}

	/**
	 * Sets the wizard.
	 *
	 * @param newWizard the new wizard
	 * @see org.eclipse.jface.wizard.WizardPage#setWizard(org.eclipse.jface.wizard.IWizard)
	 */
	@Override
	public void setWizard(IWizard newWizard) {
		super.setWizard(newWizard);
		SettingsHelper settingsHelper = new SettingsHelper(getDialogSettings());
		String[] defaultDiagramCategory = settingsHelper.getDefaultDiagramCategories();
		if (defaultDiagramCategory != null && defaultDiagramCategory.length > 0) {
			if (myAllowSeveralCategories) {
				setDefaultDiagramCategories(defaultDiagramCategory);
			} else {
				setDefaultDiagramCategories(new String[]{defaultDiagramCategory[0]});
			}
		}
	}

	/**
	 * Creates the control.
	 *
	 * @param parent the parent
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {
		Composite plate = new Composite(parent, SWT.NONE);
		plate.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		GridLayout gridLayout = new GridLayout();
		gridLayout.marginWidth = 10;
		plate.setLayout(gridLayout);
		setControl(plate);

		createDiagramCategoryForm(plate);

		setPageComplete(validatePage());
	}

	@Override
	public void setVisible(boolean visible) {
		super.setVisible(visible);
		
		// re-validate in case the user changed the file name on previous page
		setPageComplete(validatePage());
	}
	
	/**
	 * 
	 * Gets the diagram category.
	 * 
	 * @return the diagram category
     * @deprecated Use getDiagramCategories() instead.
	 */
	public String getDiagramCategory() {
		if(!mySelectedDiagramCategoryIds.isEmpty()) {
			return mySelectedDiagramCategoryIds.get(0);
		}
		return null;
	}

	/**
	 * Gets the diagram categories.
	 *
	 * @return the diagram categories
	 */
	public String[] getDiagramCategories() {
		return mySelectedDiagramCategoryIds.toArray(new String[mySelectedDiagramCategoryIds.size()]);
	}

	/**
	 * Sets the default diagram categories.
	 *
	 * @param categories the new default diagram categories
	 */
	protected final void setDefaultDiagramCategories(String[] categories) {
		mySelectedDiagramCategoryIds.clear();
		mySelectedDiagramCategoryIds.addAll(Arrays.asList(categories));
	}


	/**
	 * Validate page.
	 * 
	 * @return true, if successful
	 */
	protected boolean validatePage() {
		setMessage(null);
		setErrorMessage(null);
		String[] categories = getDiagramCategories();
		if(categories == null || categories.length == 0) {
			setErrorMessage(Messages.SelectDiagramCategoryPage_select_one_category);
			return false;
		}
		for(String newCategory : categories) {
			if(!validateCategoryExists(newCategory)) {
				return false;
			}
		}
		if(!validateFileExtension(categories)) {
			return false;
		}
		return true;
	}

	/**
	 * Validate file extension.
	 *
	 * @param categories the categories
	 * @return true, if successful
	 */
	protected boolean validateFileExtension(String... categories) {
		IStatus status = ((CreateModelWizard)getWizard()).diagramCategoryChanged(categories);
		switch(status.getSeverity()) {
		case Status.ERROR:
			setErrorMessage(status.getMessage());
			return false;
		case Status.WARNING:
			setMessage(status.getMessage(), IMessageProvider.WARNING);
			break;
		case Status.INFO:
			setMessage(status.getMessage(), IMessageProvider.INFORMATION);
			break;
		}
		return true;
	}

	/**
	 * Validate category exists.
	 *
	 * @param newCategory the new category
	 * @return true, if successful
	 */
	protected boolean validateCategoryExists(String newCategory) {
		DiagramCategoryDescriptor selected = getDiagramCategoryMap().get(newCategory);
		if(selected == null) {
			setErrorMessage(Messages.bind(Messages.SelectDiagramCategoryPage_cannot_find_category,newCategory));
			return false;
		}
		return true;
	}

	/**
	 * Gets the diagram category map.
	 *
	 * @return the diagram category map
	 */
	protected Map<String, DiagramCategoryDescriptor> getDiagramCategoryMap() {
		return DiagramCategoryRegistry.getInstance().getDiagramCategoryMap();
	}

	/**
	 * Creates the diagram language form.
	 * 
	 * @param composite
	 *        the composite
	 */
	private void createDiagramCategoryForm(Composite composite) {
		Group group = createGroup(composite, Messages.SelectDiagramCategoryPage_diagram_language_group);

		SelectionListener listener = new SelectionListener() {

			private SelectionEvent prevEvent;

			public void widgetSelected(SelectionEvent e) {
				if(e == prevEvent) {
					return;
				}
				Button selected = ((Button)e.widget);
				diagramCategorySelected((String)selected.getData(), selected.getSelection());
				setPageComplete(validatePage());
				prevEvent = e;
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		};

		for(DiagramCategoryDescriptor diagramCategoryDescriptor : getDiagramCategoryMap().values()) {
			Button button = createCategoryButton(diagramCategoryDescriptor, group);
			button.addSelectionListener(listener);
			myDiagramKindButtons.add(button);
		}
		checkDiagramCategoryButtons();
	}

	/**
	 * Diagram category selected.
	 *
	 * @param category the category
	 * @param checked the checked
	 */
	protected void diagramCategorySelected(String category, boolean checked) {
		if(checked) {
			if (!mySelectedDiagramCategoryIds.contains(category))
				mySelectedDiagramCategoryIds.add(category);
		} else {
			mySelectedDiagramCategoryIds.remove(category);
		}
	}

	/**
	 * Check diagram category buttons.
	 */
	protected void checkDiagramCategoryButtons() {
		checkButtonsFor(mySelectedDiagramCategoryIds.toArray(new String[mySelectedDiagramCategoryIds.size()]));
	}

	/**
	 * Check buttons for.
	 *
	 * @param diagramCategories the diagram categories
	 */
	protected void checkButtonsFor(String... diagramCategories) {
		for(Button button : myDiagramKindButtons) {
			button.setSelection(false);
		}
		for(Button button : myDiagramKindButtons) {
			for(String diagramCategory : diagramCategories) {
				if(diagramCategory.equals(button.getData())) {
					button.setSelection(true);
				}
			}
		}
	}

	/**
	 * Creates the category button.
	 *
	 * @param diagramCategoryDescriptor the diagram category descriptor
	 * @param group the group
	 * @return the button
	 */
	private Button createCategoryButton(DiagramCategoryDescriptor diagramCategoryDescriptor, Group group) {
		Button button = new Button(group, myAllowSeveralCategories ? SWT.CHECK : SWT.RADIO);
		button.setText(diagramCategoryDescriptor.getLabel());
		button.setData(diagramCategoryDescriptor.getId());
		Image image = getImage(diagramCategoryDescriptor.getIcon());
		if(image != null) {
			button.setImage(image);
		}
		button.setToolTipText(diagramCategoryDescriptor.getDescription());
		return button;
	}

	/**
	 * Gets the image.
	 *
	 * @param imageDescriptor the image descriptor
	 * @return the image
	 */
	private static Image getImage(ImageDescriptor imageDescriptor) {
		if(imageDescriptor != null) {
			return new Image(null, imageDescriptor.getImageData());
		}
		return null;
	}

	/**
	 * Creates the group.
	 * 
	 * @param parent
	 *        the parent
	 * @param name
	 *        the name
	 * @return the group
	 */
	private static Group createGroup(Composite parent, String name) {
		Group group = new Group(parent, SWT.NONE);
		group.setText(name);
		GridLayout layout = new GridLayout(1, false);
		layout.marginHeight = 5;
		layout.marginWidth = 5;
		group.setLayout(layout);
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
		group.setLayoutData(data);
		return group;
	}


}

Back to the top