]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/dialogs/FileDetailDialog.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / dialogs / FileDetailDialog.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 java.io.File;\r
15 \r
16 import org.eclipse.jface.dialogs.IDialogConstants;\r
17 import org.eclipse.jface.dialogs.IInputValidator;\r
18 import org.eclipse.jface.layout.GridDataFactory;\r
19 import org.eclipse.swt.SWT;\r
20 import org.eclipse.swt.events.ModifyEvent;\r
21 import org.eclipse.swt.events.ModifyListener;\r
22 import org.eclipse.swt.events.SelectionAdapter;\r
23 import org.eclipse.swt.events.SelectionEvent;\r
24 import org.eclipse.swt.layout.FillLayout;\r
25 import org.eclipse.swt.layout.GridLayout;\r
26 import org.eclipse.swt.widgets.Button;\r
27 import org.eclipse.swt.widgets.Composite;\r
28 import org.eclipse.swt.widgets.Control;\r
29 import org.eclipse.swt.widgets.FileDialog;\r
30 import org.eclipse.swt.widgets.Label;\r
31 import org.eclipse.swt.widgets.Shell;\r
32 import org.eclipse.swt.widgets.Text;\r
33 import org.simantics.Simantics;\r
34 import org.simantics.db.Resource;\r
35 import org.simantics.db.exception.DatabaseException;\r
36 import org.simantics.document.DocumentResource;\r
37 import org.simantics.utils.ui.validators.FileNameValidator;\r
38 \r
39 /**\r
40  * Dialog for adding URL links with additional information\r
41  * \r
42  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
43  *\r
44  */\r
45 public class FileDetailDialog extends TextInputDialog{\r
46 \r
47         private String fileName;\r
48         private String name;\r
49         \r
50         Text fileText;\r
51         Text nameText;\r
52         \r
53         IInputValidator fileValidator;\r
54         IInputValidator nameValidator;\r
55         \r
56         Composite annotationComposite;\r
57         \r
58         AnnotationConfigurator annotationConfigurator;\r
59         \r
60         public FileDetailDialog(Shell parentShell, Resource lib) {\r
61                 super(parentShell);\r
62                 try {\r
63                         annotationConfigurator = new AnnotationConfigurator(DocumentResource.getInstance(Simantics.getSession()).FileDocument,lib);\r
64                 } catch (DatabaseException e) {\r
65                         \r
66                 }\r
67         }\r
68         \r
69         public String getFileName() {\r
70                 return fileName;\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(3,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("File:");\r
90                 fileText = new Text(composite, SWT.SINGLE|SWT.BORDER);\r
91                 Button browseButton = new Button(composite, SWT.PUSH);\r
92                 browseButton.setText("Browse");\r
93                 label = new Label(composite, SWT.NONE);\r
94                 label.setText("Name:");\r
95                 nameText = new Text(composite, SWT.SINGLE|SWT.BORDER);\r
96                 label = new Label(composite, SWT.NONE);\r
97                 label = new Label(composite, SWT.NONE);\r
98                 label.setText("Annotations:");\r
99                 annotationComposite = new Composite(composite, SWT.NONE);\r
100                 annotationComposite.setLayout(new FillLayout());\r
101                 \r
102                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(fileText);\r
103                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(nameText);\r
104                 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(annotationComposite);\r
105                 \r
106                 fileValidator = new FileNameValidator();\r
107                 \r
108                 fileText.addModifyListener(new ModifyListener() {\r
109                         @Override\r
110                         public void modifyText(ModifyEvent e) {\r
111                                 if (validate(fileText, fileValidator)) {\r
112                                         fileName = fileText.getText();\r
113                                         updateName();\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                 browseButton.addSelectionListener(new SelectionAdapter() {\r
130                         @Override\r
131                         public void widgetSelected(SelectionEvent e) {\r
132                                 FileDialog dialog = new FileDialog(e.display.getActiveShell(),SWT.OPEN);\r
133                                 dialog.setFilterExtensions(new String[]{"*.*"});\r
134                                 String s = dialog.open();\r
135                                 if (s != null) {\r
136                                         name = null;\r
137                                         fileText.setText(s);\r
138                                 }\r
139                         }\r
140                 });\r
141                 \r
142                 annotationConfigurator.createComposite(annotationComposite);\r
143                 \r
144                 return composite;\r
145         }\r
146         \r
147         private void updateName() {\r
148                 if (fileName.length() > 0) {\r
149                         String proposal = new File(fileName).getName();\r
150                         String newName = updateName(proposal, name);\r
151                         if (newName != null) {\r
152                                 name = newName;\r
153                                 nameText.setText(name);\r
154                         }\r
155                 }\r
156         }\r
157         \r
158         public void setNameValidator(IInputValidator nameValidator) {\r
159                 this.nameValidator = nameValidator;\r
160         }\r
161         \r
162         public AnnotationConfigurator getAnnotationConfigurator() {\r
163                 return annotationConfigurator;\r
164         }\r
165         \r
166 \r
167 }\r