]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/internal/SaveAnnotationDialog.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.annotation.ui / src / org / simantics / annotation / ui / internal / SaveAnnotationDialog.java
1 /*******************************************************************************\r
2  * Copyright (c) 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.annotation.ui.internal;\r
13 \r
14 import java.util.Map;\r
15 \r
16 import org.eclipse.core.runtime.Status;\r
17 import org.eclipse.jface.dialogs.IDialogSettings;\r
18 import org.eclipse.jface.layout.GridDataFactory;\r
19 import org.eclipse.jface.resource.ImageDescriptor;\r
20 import org.eclipse.jface.viewers.StructuredSelection;\r
21 import org.eclipse.swt.SWT;\r
22 import org.eclipse.swt.events.ModifyEvent;\r
23 import org.eclipse.swt.events.ModifyListener;\r
24 import org.eclipse.swt.widgets.Composite;\r
25 import org.eclipse.swt.widgets.Control;\r
26 import org.eclipse.swt.widgets.Label;\r
27 import org.eclipse.swt.widgets.Shell;\r
28 import org.eclipse.swt.widgets.Text;\r
29 import org.eclipse.ui.PlatformUI;\r
30 import org.simantics.Simantics;\r
31 import org.simantics.annotation.ui.Activator;\r
32 import org.simantics.db.Resource;\r
33 import org.simantics.db.common.uri.UnescapedChildMapOfResource;\r
34 import org.simantics.db.exception.DatabaseException;\r
35 import org.simantics.ui.workbench.dialogs.ResourceSelectionDialog3;\r
36 import org.simantics.utils.datastructures.Pair;\r
37 \r
38 /**\r
39  * @author Jani Simomaa <jani.simomaa@semantum.fi>\r
40  */\r
41 public class SaveAnnotationDialog extends ResourceSelectionDialog3<Resource> {\r
42 \r
43         private String name;\r
44 \r
45         public SaveAnnotationDialog(Shell shell,\r
46                         Map<Resource, Pair<String, ImageDescriptor>> parameter, String title) {\r
47                 super(shell, parameter, title);\r
48                 this.name = "";\r
49         }\r
50 \r
51         @Override\r
52         protected IDialogSettings getBaseDialogSettings() {\r
53                 return Activator.getDefault().getDialogSettings();\r
54         }\r
55 \r
56         @Override\r
57         protected Control createExtendedContentArea(Composite parent) {\r
58                 Label l = new Label(parent, SWT.NONE); \r
59                 l.setText("Select a name:");\r
60                 GridDataFactory.fillDefaults().grab(true, false).applyTo(l);\r
61                 final Text t = new Text(parent,SWT.BORDER);\r
62                 t.addModifyListener(new ModifyListener() {\r
63                         @Override\r
64                         public void modifyText(ModifyEvent e) {\r
65                                 name = t.getText();\r
66                                 validatePage();\r
67                         }\r
68                 });\r
69                 GridDataFactory.fillDefaults().grab(true, false).applyTo(t);\r
70                 validatePage();\r
71                 return super.createExtendedContentArea(parent);\r
72         }\r
73 \r
74         @Override\r
75         protected void handleSelected(StructuredSelection selection) {\r
76                 super.handleSelected(selection);\r
77                 validatePage(); \r
78         }\r
79 \r
80         protected void validatePage() {\r
81                 StructuredSelection selection = getSelectedItems();\r
82                 String error = validateName(name);\r
83                 if (error != null) {\r
84                         updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, error));\r
85                 } else if (selection.isEmpty()) {\r
86                         updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "No Library selected"));\r
87                 } else {\r
88                         // Validate that the name is not in use.\r
89                         Resource library = (Resource) selection.getFirstElement();\r
90                         try {\r
91                                 Map<String, Resource> children = Simantics.sync(new UnescapedChildMapOfResource(library));\r
92                                 if (children.containsKey(name)) {\r
93                                         updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "Name is already in use."));\r
94                                 } else {\r
95                                         updateStatus(new Status(Status.OK, Activator.PLUGIN_ID, ""));\r
96                                 }\r
97                         } catch (DatabaseException e) {\r
98                                 updateStatus(new Status(Status.ERROR, PlatformUI.PLUGIN_ID, "Failed to check validity of name. See error log.", e));\r
99                         }\r
100                 }\r
101         }\r
102 \r
103         protected String validateName(String name) {\r
104                 if (name.trim().isEmpty())\r
105                         return "Name cannot be empty";\r
106                 if (name.startsWith("."))\r
107                         return "Name cannot begin with a dot";\r
108                 return null;\r
109         }\r
110 \r
111         public String getName() {\r
112                 return name;\r
113         }\r
114 \r
115 }