1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 18:26:01 +02:00

Added persistence for the last used template and adjusted layout of the dialog (bug 238195).

This commit is contained in:
Sergey Prigogin 2008-07-20 00:20:44 +00:00
parent 2c26884385
commit b6b73f1b19
3 changed files with 82 additions and 17 deletions

View file

@ -9,6 +9,7 @@
* QNX Software Systems - initial API and implementation
* IBM Corporation
* Anton Leherbauer (Wind River Systems)
* Sergey Prigogin (Google)
*******************************************************************************/
package org.eclipse.cdt.internal.ui.wizards.filewizard;
@ -80,6 +81,7 @@ import org.eclipse.cdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
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;
@ -147,7 +149,9 @@ public abstract class AbstractFileCreationWizardPage extends NewElementWizardPag
createSourceFolderControls(composite, nColumns);
createFileControls(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);
@ -187,7 +191,7 @@ public abstract class AbstractFileCreationWizardPage extends NewElementWizardPag
/**
* Creates the controls for the file name field. Expects a <code>GridLayout</code> with at
* least 3 columns.
* least 2 columns.
*
* @param parent the parent composite
* @param nColumns number of columns to span
@ -206,6 +210,7 @@ public abstract class AbstractFileCreationWizardPage extends NewElementWizardPag
fTemplateDialogField.doFillIntoGrid(parent, columns - 1);
Button configureButton= new Button(parent, SWT.PUSH);
configureButton.setText(NewFileWizardMessages.AbstractFileCreationWizardPage_configure_label);
configureButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
configureButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
@ -237,30 +242,41 @@ public abstract class AbstractFileCreationWizardPage extends NewElementWizardPag
protected void updateTemplates() {
Template selected= getSelectedTemplate();
String name = selected != null ?
selected.getName() :
getLastUsedTemplateName();
fTemplates= getApplicableTemplates();
int idx= 1;
int idx= NO_TEMPLATE.equals(name) ? 0 : 1;
String[] names= new String[fTemplates.length + 1];
for (int i = 0; i < fTemplates.length; i++) {
names[i + 1]= fTemplates[i].getName();
if (selected != null && selected.getName().equals(names[i + 1])) {
idx= i;
if (name != null && name.equals(names[i + 1])) {
idx= i + 1;
}
}
names[0]= NewFileWizardMessages.AbstractFileCreationWizardPage_noTemplate;
fTemplateDialogField.setItems(names);
fTemplateDialogField.selectItem(idx + 1);
fTemplateDialogField.selectItem(idx);
}
/**
* Configure the set of selectable templates.
* Configure the set of templates to select from.
* @return the set of templates
*/
protected abstract Template[] getApplicableTemplates();
/**
* @return the selected template or <code>null</code> if none
* Returns the selected template and saves its name for future use.
*
* @return the selected template or <code>null</code> if none.
*/
protected Template getSelectedTemplate() {
protected Template getTemplate() {
Template template = getSelectedTemplate();
saveLastUsedTemplateName(template != null ? template.getName() : NO_TEMPLATE);
return template;
}
private Template getSelectedTemplate() {
if (fTemplateDialogField != null) {
int index= fTemplateDialogField.getSelectionIndex() - 1;
if (index >= 0 && index < fTemplates.length) {
@ -699,4 +715,16 @@ public abstract class AbstractFileCreationWizardPage extends NewElementWizardPag
* @see #createFile(IProgressMonitor)
*/
public abstract ITranslationUnit getCreatedFileTU();
/**
* @return the name of the template used in the previous dialog invocation.
*/
public abstract String getLastUsedTemplateName();
/**
* Saves the name of the last used template.
*
* @param name the name of a template, or an empty string for no template.
*/
public abstract void saveLastUsedTemplateName(String name);
}

View file

@ -8,7 +8,8 @@
* Contributors:
* QNX Software Systems - initial API and implementation
* Anton Leherbauer (Wind River Systems)
*******************************************************************************/
* Sergey Prigogin (Google)
******************************************************************************/
package org.eclipse.cdt.internal.ui.wizards.filewizard;
import org.eclipse.cdt.core.CConventions;
@ -38,6 +39,7 @@ import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
public class NewHeaderFileCreationWizardPage extends AbstractFileCreationWizardPage {
private final String KEY_LAST_USED_TEMPLATE = "LastUsedHeaderTemplate"; //$NON-NLS-1$
private ITranslationUnit fNewFileTU = null;
private StringDialogField fNewFileDialogField;
@ -65,7 +67,7 @@ public class NewHeaderFileCreationWizardPage extends AbstractFileCreationWizardP
/**
* Creates the controls for the file name field. Expects a <code>GridLayout</code> with at
* least 3 columns.
* least 2 columns.
*
* @param parent the parent composite
* @param nColumns number of columns to span
@ -153,7 +155,7 @@ public class NewHeaderFileCreationWizardPage extends AbstractFileCreationWizardP
fNewFileTU = (ITranslationUnit) CoreModel.getDefault().create(newFile);
if (fNewFileTU != null) {
String lineDelimiter= StubUtility.getLineDelimiterUsed(fNewFileTU);
String content= CodeGeneration.getHeaderFileContent(getSelectedTemplate(), fNewFileTU, null, null, lineDelimiter);
String content= CodeGeneration.getHeaderFileContent(getTemplate(), fNewFileTU, null, null, lineDelimiter);
if (content != null) {
fNewFileTU.getBuffer().setContents(content.toCharArray());
fNewFileTU.save(monitor, true);
@ -182,4 +184,20 @@ public class NewHeaderFileCreationWizardPage extends AbstractFileCreationWizardP
return StubUtility.getFileTemplatesForContentTypes(
new String[] { CCorePlugin.CONTENT_TYPE_CXXHEADER, CCorePlugin.CONTENT_TYPE_CHEADER }, null);
}
/*
* @see org.eclipse.cdt.internal.ui.wizards.filewizard.AbstractFileCreationWizardPage#getPreferredTemplateName()
*/
@Override
public String getLastUsedTemplateName() {
return getDialogSettings().get(KEY_LAST_USED_TEMPLATE);
}
/*
* @see org.eclipse.cdt.internal.ui.wizards.filewizard.AbstractFileCreationWizardPage#savePreferredTemplateName(String)
*/
@Override
public void saveLastUsedTemplateName(String name) {
getDialogSettings().put(KEY_LAST_USED_TEMPLATE, name);
}
}

View file

@ -8,6 +8,7 @@
* Contributors:
* QNX Software Systems - initial API and implementation
* Anton Leherbauer (Wind River Systems)
* Sergey Prigogin (Google)
*******************************************************************************/
package org.eclipse.cdt.internal.ui.wizards.filewizard;
@ -39,6 +40,8 @@ import org.eclipse.cdt.internal.ui.wizards.dialogfields.LayoutUtil;
import org.eclipse.cdt.internal.ui.wizards.dialogfields.StringDialogField;
public class NewSourceFileCreationWizardPage extends AbstractFileCreationWizardPage {
private final String KEY_LAST_USED_TEMPLATE = "LastUsedSourceTemplate"; //$NON-NLS-1$
private ITranslationUnit fNewFileTU = null;
private StringDialogField fNewFileDialogField;
@ -65,7 +68,7 @@ public class NewSourceFileCreationWizardPage extends AbstractFileCreationWizardP
/**
* Creates the controls for the file name field. Expects a <code>GridLayout</code> with at
* least 3 columns.
* least 2 columns.
*
* @param parent the parent composite
* @param nColumns number of columns to span
@ -153,7 +156,7 @@ public class NewSourceFileCreationWizardPage extends AbstractFileCreationWizardP
fNewFileTU = (ITranslationUnit) CoreModel.getDefault().create(newFile);
if (fNewFileTU != null) {
String lineDelimiter= StubUtility.getLineDelimiterUsed(fNewFileTU);
String content= CodeGeneration.getBodyFileContent(getSelectedTemplate(), fNewFileTU, null, null, lineDelimiter);
String content= CodeGeneration.getBodyFileContent(getTemplate(), fNewFileTU, null, null, lineDelimiter);
if (content != null) {
fNewFileTU.getBuffer().setContents(content.toCharArray());
fNewFileTU.save(monitor, true);
@ -182,4 +185,20 @@ public class NewSourceFileCreationWizardPage extends AbstractFileCreationWizardP
return StubUtility.getFileTemplatesForContentTypes(
new String[] { CCorePlugin.CONTENT_TYPE_CXXSOURCE, CCorePlugin.CONTENT_TYPE_CSOURCE }, null);
}
/*
* @see org.eclipse.cdt.internal.ui.wizards.filewizard.AbstractFileCreationWizardPage#getPreferredTemplateName()
*/
@Override
public String getLastUsedTemplateName() {
return getDialogSettings().get(KEY_LAST_USED_TEMPLATE);
}
/*
* @see org.eclipse.cdt.internal.ui.wizards.filewizard.AbstractFileCreationWizardPage#savePreferredTemplateName(String)
*/
@Override
public void saveLastUsedTemplateName(String name) {
getDialogSettings().put(KEY_LAST_USED_TEMPLATE, name);
}
}