]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/dialogs/UrlDetailDialog.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / dialogs / UrlDetailDialog.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in 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  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.document.ui.dialogs;\r
13 \r
14 import org.eclipse.jface.dialogs.IDialogConstants;\r
15 import org.eclipse.jface.dialogs.IInputValidator;\r
16 import org.eclipse.jface.layout.GridDataFactory;\r
17 import org.eclipse.swt.SWT;\r
18 import org.eclipse.swt.events.ModifyEvent;\r
19 import org.eclipse.swt.events.ModifyListener;\r
20 import org.eclipse.swt.layout.FillLayout;\r
21 import org.eclipse.swt.layout.GridLayout;\r
22 import org.eclipse.swt.widgets.Composite;\r
23 import org.eclipse.swt.widgets.Control;\r
24 import org.eclipse.swt.widgets.Label;\r
25 import org.eclipse.swt.widgets.Shell;\r
26 import org.eclipse.swt.widgets.Text;\r
27 import org.simantics.Simantics;\r
28 import org.simantics.db.Resource;\r
29 import org.simantics.db.exception.DatabaseException;\r
30 import org.simantics.document.DocumentResource;\r
31 import org.simantics.utils.ui.validators.URLValidator;\r
32 \r
33 /**\r
34  * Dialog for adding URL links with additional information\r
35  * \r
36  * Note: AnnotationConfigurator must be disposed manually.\r
37  * \r
38  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
39  *\r
40  */\r
41 public class UrlDetailDialog extends TextInputDialog{\r
42 \r
43         private String url;\r
44         private String name;\r
45         \r
46         Text urlText;\r
47         Text nameText;\r
48         \r
49         IInputValidator urlValidator;\r
50         IInputValidator nameValidator;\r
51         \r
52         Composite annotationComposite;\r
53         \r
54         AnnotationConfigurator annotationConfigurator;\r
55 \r
56         //PropertyTable annotationTable;\r
57         \r
58 \r
59         \r
60         public UrlDetailDialog(Shell parentShell, Resource lib) {\r
61                 super(parentShell);\r
62                 try {\r
63                         annotationConfigurator = new AnnotationConfigurator(DocumentResource.getInstance(Simantics.getSession()).UrlDocument,lib);\r
64                 } catch (DatabaseException e) {\r
65                         \r
66                 }\r
67         }\r
68         \r
69         public String getUrl() {\r
70                 return url;\r
71         }\r
72         \r
73         public String getName() {\r
74                 return name;\r
75         }\r
76         \r
77         @Override\r
78         protected Control createDialogArea(Composite parent) {\r
79                 Composite composite = (Composite) super.createDialogArea(parent);\r
80                 GridLayout layout = new GridLayout(2,false);\r
81                 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);\r
82                 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);\r
83                 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);\r
84                 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);\r
85                 composite.setLayout(layout);\r
86                 GridDataFactory.fillDefaults().hint(500, 500).applyTo(composite);\r
87                 \r
88                 Label label = new Label(composite, SWT.NONE);\r
89                 label.setText("URL:");\r
90                 urlText = new Text(composite, SWT.SINGLE|SWT.BORDER);\r
91                 label = new Label(composite, SWT.NONE);\r
92                 label.setText("Name:");\r
93                 nameText = new Text(composite, SWT.SINGLE|SWT.BORDER);\r
94                 label = new Label(composite, SWT.NONE);\r
95                 label.setText("Annotations:");\r
96                 annotationComposite = new Composite(composite, SWT.BORDER);\r
97                 annotationComposite.setLayout(new FillLayout());\r
98 //              \r
99                 \r
100                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(urlText);\r
101                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(nameText);\r
102                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(annotationComposite);\r
103                 \r
104                 urlValidator = new URLValidator();\r
105                 \r
106                 \r
107                 urlText.addModifyListener(new ModifyListener() {\r
108                         @Override\r
109                         public void modifyText(ModifyEvent e) {\r
110                                 if (validate(urlText, urlValidator)) {\r
111                                         url = urlText.getText();\r
112                                         updateName();\r
113                                 }\r
114                                 \r
115                         }\r
116                 });\r
117                 \r
118                 nameText.addModifyListener(new ModifyListener() {\r
119                         \r
120                         @Override\r
121                         public void modifyText(ModifyEvent e) {\r
122                                 if (validate(nameText,nameValidator)) {\r
123                                         name = nameText.getText();\r
124                                 }\r
125                                 \r
126                         }\r
127                 });\r
128                 \r
129                 annotationConfigurator.createComposite(annotationComposite);\r
130                 \r
131                 //annotationHolder = createAnnotationHolder();\r
132                 \r
133                 \r
134                 \r
135                 \r
136 \r
137                 \r
138                 return composite;\r
139         }\r
140         \r
141         private void updateName() {\r
142                 if (url.length() > 0 ) {\r
143                         String newName = updateName(url, name);\r
144                         if (newName != null) {\r
145                                 name = newName;\r
146                                 nameText.setText(name);\r
147                         }\r
148                 }\r
149         }\r
150         \r
151         public void setUrlValidator(IInputValidator urlValidator) {\r
152                 this.urlValidator = urlValidator;\r
153         }\r
154         \r
155         public void setNameValidator(IInputValidator nameValidator) {\r
156                 this.nameValidator = nameValidator;\r
157         }\r
158         \r
159         public AnnotationConfigurator getAnnotationConfigurator() {\r
160                 return annotationConfigurator;\r
161         }\r
162         \r
163 \r
164         \r
165 }\r