package tool.builder.properties;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ListDialog;
import org.eclipse.ui.dialogs.PropertyPage;
import tool.ToolBuilderActivator;
import tool.builder.FScript;
import tool.builder.InvalidToolRepositoryException;
import tool.builder.ToolNature;
import tool.builder.ToolSystemException;
import org.eclipse.wb.swt.ResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
public class ProjectPropertyPage extends PropertyPage {
private static final String PATH_TITLE = "Path:";
private static final String OWNER_PROPERTY = "OWNER";
private static final String DEFAULT_FORTE_ROOT = "no FORTE_ROOT";
private static final int TEXT_FIELD_WIDTH = 50;
private Text ownerText;
private Text reposText;
private Text logFlagsText;
private Text workspaceText;
private Button lookupButton;
/**
* Constructor for ProjectPropertyPage.
*/
public ProjectPropertyPage() {
super();
setImageDescriptor(ResourceManager.getPluginImageDescriptor("ToolBuilder", "icons/notuds.gif"));
setDescription("Properties to define the Forte runtine and Repository location");
setTitle("Tool Properties");
}
/**
* @see PreferencePage#createContents(Composite)
*/
protected Control createContents(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
GridData data = new GridData(GridData.FILL);
data.grabExcessHorizontalSpace = true;
composite.setLayoutData(data);
composite.setLayout(new GridLayout(2, false));
//Label for log flags field
Label logFlagsLabel = new Label(composite, SWT.NONE);
logFlagsLabel.setText("Log Flags:");
logFlagsText = new Text(composite, SWT.BORDER);
GridData gd_logFlagsText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
gd_logFlagsText.widthHint = 300;
logFlagsText.setLayoutData(gd_logFlagsText);
try {
String logFlags =
((IResource) getElement()).getPersistentProperty(ToolNature.loggerQualifiedName);
logFlagsText.setText((logFlags != null) ? logFlags : ToolNature.DEFAULT_LOG_FLAGS);
} catch (CoreException e) {
logFlagsText.setText(ToolNature.DEFAULT_LOG_FLAGS);
}
// Label for Forte root field
Label forteRootLabel = new Label(composite, SWT.NONE);
forteRootLabel.setText("Forte Root:");
// Forte Root text field
ownerText = new Text(composite, SWT.SINGLE | SWT.BORDER);
GridData gd_ownerText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
gd_ownerText.widthHint = 300;
ownerText.setLayoutData(gd_ownerText);
// Populate forte root text field
try {
String forteRoot =
((IResource) getElement()).getPersistentProperty(ToolNature.forteRootQualifiedName);
ownerText.setText((forteRoot!=null)?forteRoot:DEFAULT_FORTE_ROOT);
} catch (CoreException e) {
ownerText.setText(DEFAULT_FORTE_ROOT);
}
Label lblRepository = new Label(composite, SWT.NONE);
lblRepository.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblRepository.setText("Repository:");
reposText = new Text(composite, SWT.BORDER);
reposText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
try {
String repos =
((IResource) getElement()).getPersistentProperty(ToolNature.reposQualifiedName);
reposText.setText((repos!=null)?repos:"no REPOS name");
} catch (CoreException e) {
reposText.setText("no REPOS name");
}
Label lblWorkspace = new Label(composite, SWT.NONE);
lblWorkspace.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblWorkspace.setText("Workspace:");
Composite workspaceComp = new Composite(composite, SWT.NONE);
GridData gd = new GridData(GridData.FILL);
gd.horizontalAlignment = SWT.FILL;
gd.grabExcessHorizontalSpace = true;
workspaceComp.setLayoutData(gd);
GridLayout gl_workspaceComp = new GridLayout(2, false);
gl_workspaceComp.marginWidth = 0;
gl_workspaceComp.verticalSpacing = 0;
gl_workspaceComp.marginHeight = 0;
workspaceComp.setLayout(gl_workspaceComp);
workspaceText = new Text(workspaceComp, SWT.BORDER);
workspaceText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
lookupButton = new Button(workspaceComp, SWT.NONE);
lookupButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (reposText.getText().trim().isEmpty())
return;
String[] workspaces = null;
try {
workspaces = FScript.getFScript((IResource) getElement()).ListWorkspaces();
} catch (ToolSystemException e1) {
ToolBuilderActivator.showError("Tool repository error", e1);
} catch (InvalidToolRepositoryException e1) {
ToolBuilderActivator.showError("Tool repository error", e1);
} catch (CoreException e1) {
ToolBuilderActivator.showError("Tool repository error", e1);
}
ListDialog wsListDialog = new ListDialog(getShell());
wsListDialog.setAddCancelButton(true);
wsListDialog.setContentProvider(new ArrayContentProvider());
wsListDialog.setLabelProvider(new LabelProvider());
if (workspaces != null)
wsListDialog.setInput(workspaces);
wsListDialog.setInitialSelections(workspaces);
wsListDialog.setTitle("Select workspace");
wsListDialog.open();
Object[] result = wsListDialog.getResult();
if (result != null && result.length > 0){
workspaceText.setText((String)result[0]);
}
}
});
lookupButton.setToolTipText("Look up Workspace");
lookupButton.setImage(ResourceManager.getPluginImage("ToolBuilder", "icons/look_up.gif"));
try {
String workspace =
((IResource) getElement()).getPersistentProperty(ToolNature.workspaceQualifiedName);
workspaceText.setText((workspace!=null)?workspace:"no workspace");
new Label(composite, SWT.NONE);
new Label(composite, SWT.NONE);
} catch (CoreException e) {
workspaceText.setText("no workspace");
}
return composite;
}
private Composite createDefaultComposite(Composite parent) {
Composite composite = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
composite.setLayout(layout);
GridData data = new GridData();
data.verticalAlignment = GridData.FILL;
data.horizontalAlignment = GridData.FILL;
composite.setLayoutData(data);
return composite;
}
protected void performDefaults() {
super.performDefaults();
String forteRoot = System.getenv("FORTE_ROOT");
ownerText.setText((forteRoot != null) ? forteRoot : DEFAULT_FORTE_ROOT);
String logFlags = ToolNature.DEFAULT_LOG_FLAGS;
logFlagsText.setText(logFlags);
String repos = System.getenv("FORTE_REPOSNAME");
reposText.setText(repos);
String workspace = System.getenv("FORTE_WORKSPACE");
workspaceText.setText(workspace);
// project.setPersistentProperty(worspacePasswordQualifiedName, workspacePassword);
}
public boolean performOk() {
try {
IProject project = (IProject) getElement();
String forteRoot = ownerText.getText();
project.setPersistentProperty(ToolNature.forteRootQualifiedName,
forteRoot);
String logFlags = logFlagsText.getText();
project.setPersistentProperty(ToolNature.loggerQualifiedName, logFlags);
String repos = reposText.getText();
project.setPersistentProperty(ToolNature.reposQualifiedName, repos);
String workspace = workspaceText.getText();
project.setPersistentProperty(ToolNature.workspaceQualifiedName, workspace);
// project.setPersistentProperty(worspacePasswordQualifiedName, workspacePassword);
} catch (CoreException e) {
return false;
}
return true;
}
}