/**
* Copyright (c) 2008 Aptana, Inc.
*
* 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. If redistributing this code,
* this entire header must remain intact.
*
* This file is based on a JDT equivalent:
*******************************************************************************
* Copyright (c) 2000, 2005 IBM Corporation and others.
* 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:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.rubypeople.rdt.internal.ui.wizards.buildpaths;
import java.util.ArrayList;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.PlatformUI;
import org.rubypeople.rdt.core.ILoadpathEntry;
import org.rubypeople.rdt.core.IRubyProject;
import org.rubypeople.rdt.core.RubyCore;
import org.rubypeople.rdt.internal.ui.IRubyHelpContextIds;
import org.rubypeople.rdt.internal.ui.RubyPluginImages;
import org.rubypeople.rdt.internal.ui.dialogs.StatusInfo;
import org.rubypeople.rdt.internal.ui.wizards.NewWizardMessages;
import org.rubypeople.rdt.internal.ui.wizards.dialogfields.DialogField;
import org.rubypeople.rdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
import org.rubypeople.rdt.internal.ui.wizards.dialogfields.LayoutUtil;
import org.rubypeople.rdt.internal.ui.wizards.dialogfields.StringDialogField;
import org.rubypeople.rdt.ui.wizards.ILoadpathContainerPage;
import org.rubypeople.rdt.ui.wizards.ILoadpathContainerPageExtension;
import org.rubypeople.rdt.ui.wizards.NewElementWizardPage;
/**
*/
public class LoadpathContainerDefaultPage extends NewElementWizardPage implements ILoadpathContainerPage, ILoadpathContainerPageExtension {
private StringDialogField fEntryField;
private ArrayList fUsedPaths;
/**
* Constructor for LoadpathContainerDefaultPage.
*/
public LoadpathContainerDefaultPage() {
super("LoadpathContainerDefaultPage"); //$NON-NLS-1$
setTitle(NewWizardMessages.LoadpathContainerDefaultPage_title);
setDescription(NewWizardMessages.LoadpathContainerDefaultPage_description);
setImageDescriptor(RubyPluginImages.DESC_WIZBAN_ADD_LIBRARY);
fUsedPaths= new ArrayList();
fEntryField= new StringDialogField();
fEntryField.setLabelText(NewWizardMessages.LoadpathContainerDefaultPage_path_label);
fEntryField.setDialogFieldListener(new IDialogFieldListener() {
public void dialogFieldChanged(DialogField field) {
validatePath();
}
});
validatePath();
}
private void validatePath() {
StatusInfo status= new StatusInfo();
String str= fEntryField.getText();
if (str.length() == 0) {
status.setError(NewWizardMessages.LoadpathContainerDefaultPage_path_error_enterpath);
} else if (!Path.ROOT.isValidPath(str)) {
status.setError(NewWizardMessages.LoadpathContainerDefaultPage_path_error_invalidpath);
} else {
IPath path= new Path(str);
if (path.segmentCount() == 0) {
status.setError(NewWizardMessages.LoadpathContainerDefaultPage_path_error_needssegment);
} else if (fUsedPaths.contains(path)) {
status.setError(NewWizardMessages.LoadpathContainerDefaultPage_path_error_alreadyexists);
}
}
updateStatus(status);
}
/* (non-Javadoc)
* @see IDialogPage#createControl(Composite)
*/
public void createControl(Composite parent) {
Composite composite= new Composite(parent, SWT.NONE);
GridLayout layout= new GridLayout();
layout.numColumns= 1;
composite.setLayout(layout);
fEntryField.doFillIntoGrid(composite, 2);
LayoutUtil.setHorizontalGrabbing(fEntryField.getTextControl(null));
fEntryField.setFocus();
setControl(composite);
Dialog.applyDialogFont(composite);
PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IRubyHelpContextIds.CLASSPATH_CONTAINER_DEFAULT_PAGE);
}
/* (non-Javadoc)
* @see ILoadpathContainerPage#finish()
*/
public boolean finish() {
return true;
}
/* (non-Javadoc)
* @see ILoadpathContainerPage#getSelection()
*/
public ILoadpathEntry getSelection() {
return RubyCore.newContainerEntry(new Path(fEntryField.getText()));
}
/* (non-Javadoc)
* @see org.eclipse.jdt.ui.wizards.ILoadpathContainerPageExtension#initialize(org.eclipse.jdt.core.IRubyProject, org.eclipse.jdt.core.ILoadpathEntry)
*/
public void initialize(IRubyProject project, ILoadpathEntry[] currentEntries) {
for (int i= 0; i < currentEntries.length; i++) {
ILoadpathEntry curr= currentEntries[i];
if (curr.getEntryKind() == ILoadpathEntry.CPE_CONTAINER) {
fUsedPaths.add(curr.getPath());
}
}
}
/* (non-Javadoc)
* @see ILoadpathContainerPage#setSelection(ILoadpathEntry)
*/
public void setSelection(ILoadpathEntry containerEntry) {
if (containerEntry != null) {
fUsedPaths.remove(containerEntry.getPath());
fEntryField.setText(containerEntry.getPath().toString());
} else {
fEntryField.setText(""); //$NON-NLS-1$
}
}
}