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