public abstract class AbstractFileCreationWizardPage extends NewElementWizardPage {
private static final int MAX_FIELD_CHARS = 50;
private static final String NO_TEMPLATE = ""; //$NON-NLS-1$
private IWorkspaceRoot fWorkspaceRoot;
// field IDs
private static final int SOURCE_FOLDER_ID = 1;
protected static final int NEW_FILE_ID = 2;
private static final int ALL_FIELDS = SOURCE_FOLDER_ID | NEW_FILE_ID;
int fLastFocusedField = 0;
private StringButtonDialogField fSourceFolderDialogField;
private IStatus fSourceFolderStatus;
private IStatus fNewFileStatus;
private final IStatus STATUS_OK = new StatusInfo();
/**
* This flag isFirstTime is used to keep a note
* that the file creation wizard has just been
* created.
*/
private boolean isFirstTime = true;
public AbstractFileCreationWizardPage(String name) {
super(name);
setDescription(NewFileWizardMessages.AbstractFileCreationWizardPage_description);
fWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
SourceFolderFieldAdapter sourceFolderAdapter = new SourceFolderFieldAdapter();
fSourceFolderDialogField = new StringButtonDialogField(sourceFolderAdapter);
fSourceFolderDialogField.setDialogFieldListener(sourceFolderAdapter);
fSourceFolderDialogField.setLabelText(NewFileWizardMessages.AbstractFileCreationWizardPage_sourceFolder_label);
fSourceFolderDialogField.setButtonLabel(NewFileWizardMessages.AbstractFileCreationWizardPage_sourceFolder_button);
fTemplates= getApplicableTemplates();
if (fTemplates != null && fTemplates.length > 0) {
fTemplateDialogField= new ComboDialogField(SWT.READ_ONLY);
fTemplateDialogField.setLabelText(NewFileWizardMessages.AbstractFileCreationWizardPage_template_label);
}
fSourceFolderStatus = STATUS_OK;
fNewFileStatus = STATUS_OK;
fLastFocusedField = 0;
}
// -------- UI Creation ---------
public void createControl(Composite parent) {
initializeDialogUnits(parent);
Composite composite = new Composite(parent, SWT.NONE);
int nColumns = 3;
GridLayout layout = new GridLayout();
layout.numColumns = nColumns;
composite.setLayout(layout);
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
composite.setFont(parent.getFont());
createSourceFolderControls(composite, nColumns);
createFileControls(composite, nColumns - 1);
// Placeholder for the right column.
(new Composite(composite, SWT.NO_FOCUS)).setLayoutData(new GridData(1, 1));
createTemplateControls(composite, nColumns);
composite.layout();
setErrorMessage(null);
setMessage(null);
setControl(composite);
}
/**
* Creates the necessary controls (label, text field and browse button) to edit
* the source folder location. The method expects that the parent composite
* uses a <code>GridLayout</code> as its layout manager and that the
* grid layout has at least 3 columns.
*
* @param parent the parent composite
* @param nColumns the number of columns to span. This number must be
* greater or equal three
*/
protected void createSourceFolderControls(Composite parent, int nColumns) {
fSourceFolderDialogField.doFillIntoGrid(parent, nColumns);
Text textControl = fSourceFolderDialogField.getTextControl(null);
LayoutUtil.setWidthHint(textControl, getMaxFieldWidth());
textControl.addFocusListener(new StatusFocusListener(SOURCE_FOLDER_ID));
}
/**
* The wizard owning this page is responsible for calling this method with the
* current selection. The selection is used to initialize the fields of the wizard
* page(初始化selection).
*
* @param selection used to initialize the fields
*/
public void init(IStructuredSelection selection) {
ICElement celem = getInitialCElement(selection);
// 初始化 源文件夹, 并将其设入到 source folder name text ***************
initFields(celem);
doStatusUpdate();
}
/**
* Utility method to inspect a selection to find a C element.
*
* @param selection the selection to be inspected
* @return a C element to be used as the initial selection, or <code>null</code>,
* if no C element exists in the given selection
*/
protected ICElement getInitialCElement(IStructuredSelection selection) {
ICElement celem = null;
if (selection != null && !selection.isEmpty()) {
Object selectedElement = selection.getFirstElement();
if (selectedElement instanceof IAdaptable) {
IAdaptable adaptable = (IAdaptable) selectedElement;
celem = (ICElement) adaptable.getAdapter(ICElement.class);
if (celem == null) {
IResource resource = (IResource) adaptable.getAdapter(IResource.class);
if (resource != null && resource.getType() != IResource.ROOT) {
while (celem == null && resource.getType() != IResource.PROJECT) {
celem = (ICElement) resource.getAdapter(ICElement.class);
resource = resource.getParent();
}
if (celem == null) {
celem = CoreModel.getDefault().create(resource); // c project
}
}
}
}
}
if (celem == null) {
IWorkbenchPart part = CUIPlugin.getActivePage().getActivePart();
if (part instanceof ContentOutline) {
part = CUIPlugin.getActivePage().getActiveEditor();
}
if (part instanceof IViewPartInputProvider) {
Object elem = ((IViewPartInputProvider)part).getViewPartInput();
if (elem instanceof ICElement) {
celem = (ICElement) elem;
}
}
if (celem == null && part instanceof CEditor) {
IEditorInput input = ((IEditorPart)part).getEditorInput();
if (input != null) {
final IResource res = (IResource) input.getAdapter(IResource.class);
if (res != null && res instanceof IFile) {
celem = CoreModel.getDefault().create((IFile)res);
}
}
}
}
if (celem == null || celem.getElementType() == ICElement.C_MODEL) {
try {
ICProject[] projects = CoreModel.create(getWorkspaceRoot()).getCProjects();
if (projects.length == 1) {
celem = projects[0];
}
} catch (CModelException e) {
CUIPlugin.log(e);
}
}
return celem;
}
/**
* Initializes all fields provided by the page with a given selection.
*
* @param elem the selection used to initialize this page or <code>
* null</code> if no selection was available
*/
protected void initFields(ICElement elem) {
initSourceFolder(elem);
updateTemplates();
handleFieldChanged(ALL_FIELDS);
}
/**
* Initializes the source folder field.
*
* @param elem the C element used to compute the initial folder
*/
protected void initSourceFolder(ICElement elem) {
ICContainer folder = null;
if (elem != null) {
folder = CModelUtil.getSourceFolder(elem);
if (folder == null) {
ICProject cproject = elem.getCProject();
if (cproject != null) {
try {
if (cproject.exists()) {
ISourceRoot[] roots = cproject.getSourceRoots();
if (roots != null && roots.length > 0)
folder = roots[0];
}
} catch (CModelException e) {
CUIPlugin.log(e);
}
if (folder == null) {
folder = cproject.findSourceRoot(cproject.getResource());
}
}
}
}
setSourceFolderFullPath(folder != null ? folder.getResource().getFullPath() : null, false);
}
protected final class StatusFocusListener implements FocusListener {
private int fieldID;