Initial commit
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 983 B |
@@ -0,0 +1,149 @@
|
||||
package $packageName$;
|
||||
|
||||
import org.eclipse.jface.viewers.IStructuredSelection;
|
||||
import org.eclipse.jface.wizard.Wizard;
|
||||
import org.eclipse.ui.INewWizard;
|
||||
import org.eclipse.ui.IWorkbench;
|
||||
import org.eclipse.core.runtime.*;
|
||||
import org.eclipse.jface.operation.*;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import org.eclipse.jface.dialogs.MessageDialog;
|
||||
import org.eclipse.jface.viewers.ISelection;
|
||||
import org.eclipse.core.resources.*;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import java.io.*;
|
||||
import org.eclipse.ui.*;
|
||||
|
||||
/**
|
||||
* This is a sample new wizard. Its role is to create a new file
|
||||
* resource in the provided container. If the container resource
|
||||
* (a folder or a project) is selected in the workspace
|
||||
* when the wizard is opened, it will accept it as the target
|
||||
* container. The wizard creates one file with the extension
|
||||
* "$extension$". If a sample multi-page editor (also available
|
||||
* as a template) is registered for the same extension, it will
|
||||
* be able to open it.
|
||||
*/
|
||||
|
||||
public class $wizardClassName$ extends Wizard implements INewWizard {
|
||||
private $wizardPageClassName$ page;
|
||||
private ISelection selection;
|
||||
|
||||
/**
|
||||
* Constructor for $wizardClassName$.
|
||||
*/
|
||||
public $wizardClassName$() {
|
||||
super();
|
||||
setNeedsProgressMonitor(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adding the page to the wizard.
|
||||
*/
|
||||
|
||||
public void addPages() {
|
||||
page = new $wizardPageClassName$(selection);
|
||||
addPage(page);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when 'Finish' button is pressed in
|
||||
* the wizard. We will create an operation and run it
|
||||
* using wizard as execution context.
|
||||
*/
|
||||
public boolean performFinish() {
|
||||
final String containerName = page.getContainerName();
|
||||
final String fileName = page.getFileName();
|
||||
IRunnableWithProgress op = new IRunnableWithProgress() {
|
||||
public void run(IProgressMonitor monitor) throws InvocationTargetException {
|
||||
try {
|
||||
doFinish(containerName, fileName, monitor);
|
||||
} catch (CoreException e) {
|
||||
throw new InvocationTargetException(e);
|
||||
} finally {
|
||||
monitor.done();
|
||||
}
|
||||
}
|
||||
};
|
||||
try {
|
||||
getContainer().run(true, false, op);
|
||||
} catch (InterruptedException e) {
|
||||
return false;
|
||||
} catch (InvocationTargetException e) {
|
||||
Throwable realException = e.getTargetException();
|
||||
MessageDialog.openError(getShell(), "Error", realException.getMessage());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The worker method. It will find the container, create the
|
||||
* file if missing or just replace its contents, and open
|
||||
* the editor on the newly created file.
|
||||
*/
|
||||
|
||||
private void doFinish(
|
||||
String containerName,
|
||||
String fileName,
|
||||
IProgressMonitor monitor)
|
||||
throws CoreException {
|
||||
// create a sample file
|
||||
monitor.beginTask("Creating " + fileName, 2);
|
||||
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
|
||||
IResource resource = root.findMember(new Path(containerName));
|
||||
if (!resource.exists() || !(resource instanceof IContainer)) {
|
||||
throwCoreException("Container \"" + containerName + "\" does not exist.");
|
||||
}
|
||||
IContainer container = (IContainer) resource;
|
||||
final IFile file = container.getFile(new Path(fileName));
|
||||
try {
|
||||
InputStream stream = openContentStream();
|
||||
if (file.exists()) {
|
||||
file.setContents(stream, true, true, monitor);
|
||||
} else {
|
||||
file.create(stream, true, monitor);
|
||||
}
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
monitor.worked(1);
|
||||
monitor.setTaskName("Opening file for editing...");
|
||||
getShell().getDisplay().asyncExec(new Runnable() {
|
||||
public void run() {
|
||||
IWorkbenchPage page =
|
||||
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
|
||||
try {
|
||||
page.openEditor(file);
|
||||
} catch (PartInitException e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
monitor.worked(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* We will initialize file contents with a sample text.
|
||||
*/
|
||||
|
||||
private InputStream openContentStream() {
|
||||
String contents =
|
||||
"This is the initial file contents for *.$extension$ file that should be word-sorted in the Preview page of the multi-page editor";
|
||||
return new ByteArrayInputStream(contents.getBytes());
|
||||
}
|
||||
|
||||
private void throwCoreException(String message) throws CoreException {
|
||||
IStatus status =
|
||||
new Status(IStatus.ERROR, "$pluginId$", IStatus.OK, message, null);
|
||||
throw new CoreException(status);
|
||||
}
|
||||
|
||||
/**
|
||||
* We will accept the selection in the workbench to see if
|
||||
* we can initialize from it.
|
||||
* @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
|
||||
*/
|
||||
public void init(IWorkbench workbench, IStructuredSelection selection) {
|
||||
this.selection = selection;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
package $packageName$;
|
||||
|
||||
import org.eclipse.jface.wizard.WizardPage;
|
||||
import org.eclipse.swt.widgets.*;
|
||||
import org.eclipse.swt.layout.*;
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.core.resources.*;
|
||||
import org.eclipse.core.runtime.Path;
|
||||
import org.eclipse.swt.events.*;
|
||||
import org.eclipse.ui.dialogs.ContainerSelectionDialog;
|
||||
import org.eclipse.jface.viewers.*;
|
||||
|
||||
/**
|
||||
* The "New" wizard page allows setting the container for
|
||||
* the new file as well as the file name. The page
|
||||
* will only accept file name without the extension OR
|
||||
* with the extension that matches the expected one ($extension$).
|
||||
*/
|
||||
|
||||
public class $wizardPageClassName$ extends WizardPage {
|
||||
private Text containerText;
|
||||
private Text fileText;
|
||||
private ISelection selection;
|
||||
|
||||
/**
|
||||
* Constructor for SampleNewWizardPage.
|
||||
* @param pageName
|
||||
*/
|
||||
public $wizardPageClassName$(ISelection selection) {
|
||||
super("wizardPage");
|
||||
setTitle("Multi-page Editor File");
|
||||
setDescription("This wizard creates a new file with *.$extension$ extension that can be opened by a multi-page editor.");
|
||||
this.selection = selection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see IDialogPage#createControl(Composite)
|
||||
*/
|
||||
public void createControl(Composite parent) {
|
||||
Composite container = new Composite(parent, SWT.NULL);
|
||||
GridLayout layout = new GridLayout();
|
||||
container.setLayout(layout);
|
||||
layout.numColumns = 3;
|
||||
layout.verticalSpacing = 9;
|
||||
Label label = new Label(container, SWT.NULL);
|
||||
label.setText("&Container:");
|
||||
|
||||
containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
|
||||
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
|
||||
containerText.setLayoutData(gd);
|
||||
containerText.addModifyListener(new ModifyListener() {
|
||||
public void modifyText(ModifyEvent e) {
|
||||
dialogChanged();
|
||||
}
|
||||
});
|
||||
|
||||
Button button = new Button(container, SWT.PUSH);
|
||||
button.setText("Browse...");
|
||||
button.addSelectionListener(new SelectionAdapter() {
|
||||
public void widgetSelected(SelectionEvent e) {
|
||||
handleBrowse();
|
||||
}
|
||||
});
|
||||
label = new Label(container, SWT.NULL);
|
||||
label.setText("&File name:");
|
||||
|
||||
fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
|
||||
gd = new GridData(GridData.FILL_HORIZONTAL);
|
||||
fileText.setLayoutData(gd);
|
||||
fileText.addModifyListener(new ModifyListener() {
|
||||
public void modifyText(ModifyEvent e) {
|
||||
dialogChanged();
|
||||
}
|
||||
});
|
||||
initialize();
|
||||
dialogChanged();
|
||||
setControl(container);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if the current workbench selection is a suitable
|
||||
* container to use.
|
||||
*/
|
||||
|
||||
private void initialize() {
|
||||
if (selection!=null && selection.isEmpty()==false && selection instanceof IStructuredSelection) {
|
||||
IStructuredSelection ssel = (IStructuredSelection)selection;
|
||||
if (ssel.size()>1) return;
|
||||
Object obj = ssel.getFirstElement();
|
||||
if (obj instanceof IResource) {
|
||||
IContainer container;
|
||||
if (obj instanceof IContainer)
|
||||
container = (IContainer)obj;
|
||||
else
|
||||
container = ((IResource)obj).getParent();
|
||||
containerText.setText(container.getFullPath().toString());
|
||||
}
|
||||
}
|
||||
fileText.setText("$initialFileName$");
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the standard container selection dialog to
|
||||
* choose the new value for the container field.
|
||||
*/
|
||||
|
||||
private void handleBrowse() {
|
||||
ContainerSelectionDialog dialog =
|
||||
new ContainerSelectionDialog(
|
||||
getShell(),
|
||||
ResourcesPlugin.getWorkspace().getRoot(),
|
||||
false,
|
||||
"Select new file container");
|
||||
if (dialog.open() == ContainerSelectionDialog.OK) {
|
||||
Object[] result = dialog.getResult();
|
||||
if (result.length == 1) {
|
||||
containerText.setText(((Path)result[0]).toOSString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that both text fields are set.
|
||||
*/
|
||||
|
||||
private void dialogChanged() {
|
||||
String container = getContainerName();
|
||||
String fileName = getFileName();
|
||||
|
||||
if (container.length() == 0) {
|
||||
updateStatus("File container must be specified");
|
||||
return;
|
||||
}
|
||||
if (fileName.length() == 0) {
|
||||
updateStatus("File name must be specified");
|
||||
return;
|
||||
}
|
||||
int dotLoc = fileName.lastIndexOf('.');
|
||||
if (dotLoc != -1) {
|
||||
String ext = fileName.substring(dotLoc + 1);
|
||||
if (ext.equalsIgnoreCase("$extension$") == false) {
|
||||
updateStatus("File extension must be \"$extension$\"");
|
||||
return;
|
||||
}
|
||||
}
|
||||
updateStatus(null);
|
||||
}
|
||||
|
||||
private void updateStatus(String message) {
|
||||
setErrorMessage(message);
|
||||
setPageComplete(message == null);
|
||||
}
|
||||
|
||||
public String getContainerName() {
|
||||
return containerText.getText();
|
||||
}
|
||||
public String getFileName() {
|
||||
return fileText.getText();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user