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