1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.document.ui.dialogs;
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;
40 * Dialog for adding URL links with additional information
42 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
45 public class FileDetailDialog extends TextInputDialog{
47 private String fileName;
53 IInputValidator fileValidator;
54 IInputValidator nameValidator;
56 Composite annotationComposite;
58 AnnotationConfigurator annotationConfigurator;
60 public FileDetailDialog(Shell parentShell, Resource lib) {
63 annotationConfigurator = new AnnotationConfigurator(DocumentResource.getInstance(Simantics.getSession()).FileDocument,lib);
64 } catch (DatabaseException e) {
69 public String getFileName() {
73 public String getName() {
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);
88 Label label = new Label(composite, SWT.NONE);
89 label.setText(Messages.FileDetailDialog_File);
90 fileText = new Text(composite, SWT.SINGLE|SWT.BORDER);
91 Button browseButton = new Button(composite, SWT.PUSH);
92 browseButton.setText(Messages.FileDetailDialog_Browse);
93 label = new Label(composite, SWT.NONE);
94 label.setText(Messages.FileDetailDialog_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(Messages.FileDetailDialog_Annotations);
99 annotationComposite = new Composite(composite, SWT.NONE);
100 annotationComposite.setLayout(new FillLayout());
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);
106 fileValidator = new FileNameValidator();
108 fileText.addModifyListener(new ModifyListener() {
110 public void modifyText(ModifyEvent e) {
111 if (validate(fileText, fileValidator)) {
112 fileName = fileText.getText();
118 nameText.addModifyListener(new ModifyListener() {
121 public void modifyText(ModifyEvent e) {
122 if (validate(nameText,nameValidator)) {
123 name = nameText.getText();
129 browseButton.addSelectionListener(new SelectionAdapter() {
131 public void widgetSelected(SelectionEvent e) {
132 FileDialog dialog = new FileDialog(e.display.getActiveShell(),SWT.OPEN);
133 dialog.setFilterExtensions(new String[]{"*.*"}); //$NON-NLS-1$
134 String s = dialog.open();
142 annotationConfigurator.createComposite(annotationComposite);
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) {
153 nameText.setText(name);
158 public void setNameValidator(IInputValidator nameValidator) {
159 this.nameValidator = nameValidator;
162 public AnnotationConfigurator getAnnotationConfigurator() {
163 return annotationConfigurator;