/*******************************************************************************
* 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 org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.resource.ImageDescriptor;
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.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.models.Snippet;
import de.innovationgate.eclipse.editors.models.SnippetModel;
import de.innovationgate.eclipse.utils.ui.EditableTableControl;
public class CodeSnippets extends PreferencePage implements IWorkbenchPreferencePage {
private Composite _container;
private EditableTableControl<Snippet> _tableControl;
private SnippetModel _model;
private static final String BUTTON_EDIT = "BUTTON_EDIT";
public CodeSnippets() {
setDescription("This page allows the configuration of code snippets for TML and TMLScript.");
}
public CodeSnippets(String title) {
super(title);
// TODO Auto-generated constructor stub
}
public CodeSnippets(String title, ImageDescriptor image) {
super(title, image);
// TODO Auto-generated constructor stub
}
@Override
protected Control createContents(Composite parent) {
_container = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 1;
layout.marginWidth = 0;
layout.marginHeight = 0;
layout.horizontalSpacing = 0;
layout.verticalSpacing = 0;
_container.setLayout(layout);
_tableControl = new EditableTableControl<Snippet>(_container, SWT.NONE) {
@Override
protected void createButtonArea(Composite area) {
super.createButtonArea(area);
Button btnEdit = new Button(area, SWT.PUSH);
btnEdit.setText("edit");
btnEdit.setLayoutData(GridDataFactory.copyData(_btnLayout));
_buttons.put(BUTTON_EDIT, btnEdit);
}
};
//_tableControl.getTable().setData(SWTBotIDs.WIDGET_KEY, ResourceIDs.TABLE_PREFS_SNIPPETS);
_tableControl.setLayoutData(new GridData(GridData.FILL_BOTH));
// create columns
String[] columnNames = new String[] { "Snippet name", "Description" , "Context", "Enabled"};
// create model
Plugin.getDefault().getCodeSnippetStore().load();
_model = new SnippetModel(Plugin.getDefault().getCodeSnippetStore().getBeans());
_tableControl.init(columnNames, _model);
_tableControl.getButton(EditableTableControl.BUTTON_ADD).addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleAdd();
}
});
_tableControl.getButton(EditableTableControl.BUTTON_REMOVE).addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleRemove();
}
});
_tableControl.getButton(BUTTON_EDIT).addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleEdit();
}
});
return _container;
}
protected void handleEdit() {
if (_tableControl.getTable().getSelectionCount() > 0) {
Snippet selectedSnippet = (Snippet) _tableControl.getTable().getSelection()[0].getData();
SnippetDialog dialog = new SnippetDialog(getShell(), selectedSnippet);
dialog.setTitle("Edit code snippet");
dialog.open();
_model.update(dialog.getSnippet());
}
}
protected void handleRemove() {
if (_tableControl.getTable().getSelectionCount() > 0) {
Snippet selectedSnippet = (Snippet) _tableControl.getTable().getSelection()[0].getData();
boolean remove = MessageDialog.openConfirm(getShell(), "Remove code snippet?", "Are you sure you want permanently remove the code snippet '" + selectedSnippet.getName() + "'?");
if (remove) {
_model.remove(selectedSnippet);
}
}
}
protected void handleAdd() {
SnippetDialog dialog = new SnippetDialog(getShell());
dialog.setTitle("Add new code snippet");
dialog.open();
Snippet snippet = dialog.getSnippet();
if (snippet != null) {
_model.add(snippet);
}
}
public void init(IWorkbench workbench) {
}
@Override
public boolean performOk() {
Plugin.getDefault().getCodeSnippetStore().flush();
return true;
}
}