/*******************************************************************************
* 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.editors.preferences;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import de.innovationgate.eclipse.editors.models.Snippet;
import de.innovationgate.eclipse.utils.ui.ValidatedDialog;
import de.innovationgate.eclipse.utils.ui.WidgetFactory;
public class SnippetDialog extends ValidatedDialog {
private Text _txtName;
private Text _txtDescription;
private Text _txtCode;
private Button _chkEnabled;
private Snippet _snippet;
private Combo _comboContext;
public SnippetDialog(Shell parentShell) {
super(parentShell);
init(null);
}
public SnippetDialog(Shell parentShell, Snippet snippet) {
super(parentShell);
init(snippet);
}
private void init(Snippet snippet){
setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.APPLICATION_MODAL);
setStatusLineAboveButtons(true);
setHelpAvailable(false);
_snippet = snippet;
}
@Override
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(2, false);
composite.setLayout(layout);
GridData txtStyle = new GridData(GridData.FILL_HORIZONTAL);
txtStyle.minimumWidth = convertWidthInCharsToPixels(80);
Label lblName = new Label(composite, SWT.None);
lblName.setText("Name:");
_txtName = new Text(composite, SWT.BORDER);
_txtName.setLayoutData(GridDataFactory.copyData(txtStyle));
if (_snippet != null) {
_txtName.setText(_snippet.getName());
}
_txtName.addModifyListener(this);
Label lblDescription = new Label(composite, SWT.None);
lblDescription.setText("Description:");
_txtDescription = new Text(composite, SWT.BORDER|SWT.MULTI);
GridData descLayout = new GridData(GridData.FILL_BOTH);
descLayout.minimumHeight = WidgetFactory.computeFontHeight(_txtDescription) * 3;
_txtDescription.setLayoutData(descLayout);
if (_snippet != null) {
_txtDescription.setText(_snippet.getDescription());
}
_txtDescription.addModifyListener(this);
Label lblContext = new Label(composite, SWT.None);
lblContext.setText("Context:");
_comboContext = new Combo(composite, SWT.BORDER|SWT.READ_ONLY);
_comboContext.setItems(new String[] {Snippet.CONTEXT_TML, Snippet.CONTEXT_TMLScript});
if (_snippet != null) {
_comboContext.setText(_snippet.getContext());
} else {
_comboContext.setText(Snippet.CONTEXT_TML);
}
_comboContext.addModifyListener(this);
Label lblEnabled = new Label(composite, SWT.None);
lblEnabled.setText("");
_chkEnabled = new Button(composite, SWT.CHECK);
_chkEnabled.setText("enabled");
if (_snippet != null) {
_chkEnabled.setSelection(_snippet.isEnabled());
} else {
_chkEnabled.setSelection(true);
}
Label lblCode = new Label(composite, SWT.None);
lblCode.setText("Code:");
_txtCode = new Text(composite, SWT.BORDER|SWT.MULTI|SWT.V_SCROLL);
GridData codeLayout = new GridData(GridData.FILL_BOTH);
codeLayout.minimumHeight = WidgetFactory.computeFontHeight(_txtCode) * 8;
_txtCode.setLayoutData(codeLayout);
if (_snippet != null) {
_txtCode.setText(_snippet.getCode());
}
_txtCode.addModifyListener(this);
Label lblHelp = new Label(composite, SWT.None);
lblHelp.setText("Possible variables in code block:");
lblHelp = new Label(composite, SWT.None);
lblHelp.setText("$CURSOR$ - cursor position after snippet insertion");
lblHelp = new Label(composite, SWT.None);
lblHelp.setText("");
lblHelp = new Label(composite, SWT.None);
lblHelp.setText("$SELECTION$ - will be replaced by the selected text");
return composite;
}
@Override
public List<String> validate() {
List<String> messages = new ArrayList<String>();
// validate name
String currentName = _txtName.getText();
if (currentName.trim().equals("")) {
messages.add("Please define a name for the snippet.");
}
// validate code
String currentCode = _txtCode.getText();
if (currentCode.trim().equals("")) {
messages.add("Please define the snippet code.");
}
return messages;
}
@Override
public void computeResponse() {
if (_snippet == null) {
_snippet = new Snippet();
}
_snippet.setName(_txtName.getText());
_snippet.setDescription(_txtDescription.getText());
_snippet.setCode(_txtCode.getText());
_snippet.setEnabled(_chkEnabled.getSelection());
_snippet.setContext(_comboContext.getText());
}
@Override
public void create() {
super.create();
}
public Snippet getSnippet() {
return _snippet;
}
}