/**
* Copyright 2009 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.safehaus.penrose.studio.source.editor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.safehaus.penrose.source.FieldConfig;
import org.safehaus.penrose.studio.PenroseImage;
import org.safehaus.penrose.studio.PenroseStudio;
import org.safehaus.penrose.studio.dialog.ErrorDialog;
import java.util.Collection;
public class SourceFieldsPage extends SourceEditorPage {
Table fieldsTable;
public SourceFieldsPage(SourceEditor editor) {
super(editor, "FIELDS", "Fields");
}
public void createFormContent(IManagedForm managedForm) {
super.createFormContent(managedForm);
ScrolledForm form = managedForm.getForm();
Composite body = form.getBody();
body.setLayout(new GridLayout());
Section fieldsSection = toolkit.createSection(body, Section.TITLE_BAR | Section.EXPANDED);
fieldsSection.setText("Fields");
fieldsSection.setLayoutData(new GridData(GridData.FILL_BOTH));
Control fieldsControl = createFieldsControl(fieldsSection);
fieldsSection.setClient(fieldsControl);
refresh();
}
public Composite createFieldsControl(final Composite parent) {
Composite composite = toolkit.createComposite(parent);
composite.setLayout(new GridLayout(2, false));
Composite leftControl = createFieldsLeftControl(composite);
leftControl.setLayoutData(new GridData(GridData.FILL_BOTH));
Composite rightControl = createPropertiesRightControl(composite);
GridData gd = new GridData(GridData.FILL_VERTICAL);
gd.widthHint = 100;
rightControl.setLayoutData(gd);
return composite;
}
public Composite createFieldsLeftControl(final Composite parent) {
fieldsTable = toolkit.createTable(parent, SWT.BORDER | SWT.FULL_SELECTION | SWT.CHECK | SWT.MULTI);
fieldsTable.setHeaderVisible(true);
fieldsTable.setLinesVisible(true);
fieldsTable.setLayoutData(new GridData(GridData.FILL_BOTH));
TableColumn tc = new TableColumn(fieldsTable, SWT.NONE);
tc.setText("Name");
tc.setWidth(250);
tc = new TableColumn(fieldsTable, SWT.NONE);
tc.setText("Alias");
tc.setWidth(200);
tc = new TableColumn(fieldsTable, SWT.NONE);
tc.setText("Type");
tc.setWidth(100);
return fieldsTable;
}
public Composite createPropertiesRightControl(final Composite parent) {
Composite composite = toolkit.createComposite(parent);
GridLayout layout = new GridLayout();
layout.marginWidth = 0;
layout.marginHeight = 0;
composite.setLayout(layout);
Button editButton = new Button(composite, SWT.PUSH);
editButton.setText("Edit");
editButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
editButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
try {
edit();
} catch (Exception e) {
log.error(e.getMessage(), e);
ErrorDialog.open(e);
}
}
});
return composite;
}
public void edit() throws Exception {
}
public void refresh() {
fieldsTable.removeAll();
Collection<FieldConfig> fieldConfigs = sourceConfig.getFieldConfigs();
for (FieldConfig fieldConfig : fieldConfigs) {
TableItem item = new TableItem(fieldsTable, SWT.CHECK);
item.setChecked(fieldConfig.isPrimaryKey());
item.setImage(PenroseStudio.getImage(fieldConfig.isPrimaryKey() ? PenroseImage.KEY : PenroseImage.NOKEY));
item.setText(0, fieldConfig.getName().equals(fieldConfig.getOriginalName()) ? fieldConfig.getName() : fieldConfig.getOriginalName());
item.setText(1, fieldConfig.getName().equals(fieldConfig.getOriginalName()) ? "" : fieldConfig.getName());
item.setText(2, fieldConfig.getType());
item.setData(fieldConfig);
}
}
public void checkDirty() {
}
}