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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.document.ui.dialogs;
\r
14 import java.io.File;
\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
40 * Dialog for adding URL links with additional information
\r
42 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
45 public class FileDetailDialog extends TextInputDialog{
\r
47 private String fileName;
\r
48 private String name;
\r
53 IInputValidator fileValidator;
\r
54 IInputValidator nameValidator;
\r
56 Composite annotationComposite;
\r
58 AnnotationConfigurator annotationConfigurator;
\r
60 public FileDetailDialog(Shell parentShell, Resource lib) {
\r
63 annotationConfigurator = new AnnotationConfigurator(DocumentResource.getInstance(Simantics.getSession()).FileDocument,lib);
\r
64 } catch (DatabaseException e) {
\r
69 public String getFileName() {
\r
73 public String getName() {
\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
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
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
106 fileValidator = new FileNameValidator();
\r
108 fileText.addModifyListener(new ModifyListener() {
\r
110 public void modifyText(ModifyEvent e) {
\r
111 if (validate(fileText, fileValidator)) {
\r
112 fileName = fileText.getText();
\r
118 nameText.addModifyListener(new ModifyListener() {
\r
121 public void modifyText(ModifyEvent e) {
\r
122 if (validate(nameText,nameValidator)) {
\r
123 name = nameText.getText();
\r
129 browseButton.addSelectionListener(new SelectionAdapter() {
\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
137 fileText.setText(s);
\r
142 annotationConfigurator.createComposite(annotationComposite);
\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
153 nameText.setText(name);
\r
158 public void setNameValidator(IInputValidator nameValidator) {
\r
159 this.nameValidator = nameValidator;
\r
162 public AnnotationConfigurator getAnnotationConfigurator() {
\r
163 return annotationConfigurator;
\r