]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/internal/SaveAnnotationDialog.java
513030431f21a34d0b533b6cebfa1fffd722bc55
[simantics/platform.git] / bundles / org.simantics.annotation.ui / src / org / simantics / annotation / ui / internal / SaveAnnotationDialog.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.annotation.ui.internal;
13
14 import java.util.Map;
15
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;
37
38 /**
39  * @author Jani Simomaa <jani.simomaa@semantum.fi>
40  */
41 public class SaveAnnotationDialog extends ResourceSelectionDialog3<Resource> {
42
43         private String name;
44
45         public SaveAnnotationDialog(Shell shell,
46                         Map<Resource, Pair<String, ImageDescriptor>> parameter, String title) {
47                 super(shell, parameter, title);
48                 this.name = "";
49         }
50
51         @Override
52         protected IDialogSettings getBaseDialogSettings() {
53                 return Activator.getDefault().getDialogSettings();
54         }
55
56         @Override
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() {
63                         @Override
64                         public void modifyText(ModifyEvent e) {
65                                 name = t.getText();
66                                 validatePage();
67                         }
68                 });
69                 GridDataFactory.fillDefaults().grab(true, false).applyTo(t);
70                 validatePage();
71                 return super.createExtendedContentArea(parent);
72         }
73
74         @Override
75         protected void handleSelected(StructuredSelection selection) {
76                 super.handleSelected(selection);
77                 validatePage(); 
78         }
79
80         protected void validatePage() {
81                 StructuredSelection selection = getSelectedItems();
82                 String error = validateName(name);
83                 if (error != null) {
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"));
87                 } else {
88                         // Validate that the name is not in use.
89                         Resource library = (Resource) selection.getFirstElement();
90                         try {
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."));
94                                 } else {
95                                         updateStatus(new Status(Status.OK, Activator.PLUGIN_ID, ""));
96                                 }
97                         } catch (DatabaseException e) {
98                                 updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "Failed to check validity of name. See error log.", e));
99                         }
100                 }
101         }
102
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";
108                 return null;
109         }
110
111         public String getName() {
112                 return name;
113         }
114
115 }