/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner.dialogs;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.ui.ValidatedDialog;
import de.innovationgate.eclipse.wgadesigner.models.DesignTemplate;
public class AddTemplateDialog extends ValidatedDialog {
// Shell _shell;
Text _tmplName;
Text _tmplFile;
Button _btnBrowse;
private DesignTemplate _design;
public AddTemplateDialog(Shell parent) {
super(parent);
setTitle("Add Design Template");
init();
}
public void init() {
setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.APPLICATION_MODAL);
setStatusLineAboveButtons(true);
setHelpAvailable(false);
}
protected Control createDialogArea(Composite parent) {
Composite composite = new Composite(parent, SWT.None);
composite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
GridLayout layout = new GridLayout(3, false);
composite.setLayout(layout);
GridData txtStyle = new GridData(GridData.FILL_HORIZONTAL);
txtStyle.minimumWidth = convertWidthInCharsToPixels(60);
Label lblTemplateName = new Label(composite, SWT.None);
lblTemplateName.setText("Template name:");
_tmplName = new Text(composite, SWT.BORDER);
_tmplName.setLayoutData(txtStyle);
_tmplName.addModifyListener(this);
Label filler = new Label(composite, SWT.None);
filler.setText("");
Label lblLocation = new Label(composite, SWT.None);
lblLocation.setText("Location:");
_tmplFile = new Text(composite, SWT.BORDER);
_tmplFile.setLayoutData(txtStyle);
_tmplFile.addModifyListener(this);
_btnBrowse = new Button(composite, SWT.PUSH);
_btnBrowse.setText("...");
_btnBrowse.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleBrowse();
}
});
return composite;
}
private void handleBrowse() {
FileDialog dialog = new FileDialog(getShell(), SWT.SINGLE);
dialog.setText("Select file");
dialog.setFilterExtensions(new String[] { "*.zip", "*.wgaplugin" });
String selectedFile = dialog.open();
if (selectedFile != null) {
String[] fileNames = dialog.getFileNames();
File dir = new File(dialog.getFilterPath());
if (fileNames.length > 0) {
_tmplFile.setText(new File(dir, fileNames[0]).getAbsolutePath());
}
}
}
public List<String> validate() {
List<String> messages = new ArrayList<String>();
// validate name
String currentName = _tmplName.getText();
if (currentName.trim().equals("")) {
messages.add("Please define a template name.");
}
if (!WorkbenchUtils.isValidResourceName(currentName)) {
messages.add("Template name contains invalid characters. Only alphanumeric ascii characters and '_', '-' are allowed.");
}
if (!isEmpty(_tmplFile.getText()) && !isEmpty(_tmplName.getText())) {
File template = new File(_tmplFile.getText());
if (!template.exists()) {
messages.add(template.getAbsolutePath() + " not found");
}
_design = new DesignTemplate(_tmplName.getText(), template.getAbsolutePath());
}
// validate location
String currentLocation = _tmplFile.getText();
if (currentLocation.trim().equals("")) {
messages.add("Please specify a location to an .zip or .wgaplugin.");
} else if (currentLocation.endsWith(".zip") || currentLocation.endsWith(".wgaplugin")){
// check if location exists
File tmpfile = new File(currentLocation);
if (!tmpfile.exists()) {
messages.add("The specified file does not exist.");
}
} else {
messages.add("Invalid file.");
}
return messages;
}
@Override
public void create() {
super.create();
getButton(OK).setText("Add");
}
public DesignTemplate getDesign() {
return _design;
}
private boolean isEmpty(String value) {
if (value != null) {
return value.length() == 0;
} else {
return true;
}
}
@Override
public void computeResponse() {
}
}