1 /*******************************************************************************
2 * Copyright (c) 2013 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.annotation.ui.internal;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.jface.dialogs.IDialogSettings;
18 import org.eclipse.jface.layout.GridDataFactory;
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.jface.viewers.StructuredSelection;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.swt.widgets.Text;
29 import org.eclipse.ui.PlatformUI;
30 import org.simantics.Simantics;
31 import org.simantics.annotation.ui.Activator;
32 import org.simantics.db.Resource;
33 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
34 import org.simantics.db.exception.DatabaseException;
35 import org.simantics.ui.workbench.dialogs.ResourceSelectionDialog3;
36 import org.simantics.utils.datastructures.Pair;
39 * @author Jani Simomaa <jani.simomaa@semantum.fi>
41 public class SaveAnnotationDialog extends ResourceSelectionDialog3<Resource> {
45 public SaveAnnotationDialog(Shell shell,
46 Map<Resource, Pair<String, ImageDescriptor>> parameter, String title) {
47 super(shell, parameter, title);
52 protected IDialogSettings getBaseDialogSettings() {
53 return Activator.getDefault().getDialogSettings();
57 protected Control createExtendedContentArea(Composite parent) {
58 Label l = new Label(parent, SWT.NONE);
59 l.setText("Select a name:");
60 GridDataFactory.fillDefaults().grab(true, false).applyTo(l);
61 final Text t = new Text(parent,SWT.BORDER);
62 t.addModifyListener(new ModifyListener() {
64 public void modifyText(ModifyEvent e) {
69 GridDataFactory.fillDefaults().grab(true, false).applyTo(t);
71 return super.createExtendedContentArea(parent);
75 protected void handleSelected(StructuredSelection selection) {
76 super.handleSelected(selection);
80 protected void validatePage() {
81 StructuredSelection selection = getSelectedItems();
82 String error = validateName(name);
84 updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, error));
85 } else if (selection.isEmpty()) {
86 updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "No Library selected"));
88 // Validate that the name is not in use.
89 Resource library = (Resource) selection.getFirstElement();
91 Map<String, Resource> children = Simantics.sync(new UnescapedChildMapOfResource(library));
92 if (children.containsKey(name)) {
93 updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "Name is already in use."));
95 updateStatus(new Status(Status.OK, Activator.PLUGIN_ID, ""));
97 } catch (DatabaseException e) {
98 updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "Failed to check validity of name. See error log.", e));
103 protected String validateName(String name) {
104 if (name.trim().isEmpty())
105 return "Name cannot be empty";
106 if (name.startsWith("."))
107 return "Name cannot begin with a dot";
111 public String getName() {