Skip to content

Commit 74b9731

Browse files
ptzieglerazoitl
authored andcommitted
Use WizardNewFileCreationPage for "New Document Example" wizard page
The Eclipse IDE already provides a perfectly fine wizard page for creating new files. There is no need to re-implement existing functionality.
1 parent c3716d0 commit 74b9731

File tree

3 files changed

+19
-176
lines changed

3 files changed

+19
-176
lines changed

org.eclipse.gef.examples.text/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %Plugin.name
44
Bundle-SymbolicName: org.eclipse.gef.examples.text; singleton:=true
5-
Bundle-Version: 3.15.700.qualifier
5+
Bundle-Version: 3.16.0.qualifier
66
Bundle-Vendor: %Plugin.providerName
77
Bundle-Localization: plugin
88
Export-Package: org.eclipse.gef.examples.text,
Lines changed: 8 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2005, 2023 IBM Corporation and others.
2+
* Copyright (c) 2005, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -12,175 +12,39 @@
1212
*******************************************************************************/
1313
package org.eclipse.gef.examples.text.wizard;
1414

15-
import org.eclipse.swt.SWT;
16-
import org.eclipse.swt.events.SelectionAdapter;
17-
import org.eclipse.swt.events.SelectionEvent;
18-
import org.eclipse.swt.layout.GridData;
19-
import org.eclipse.swt.layout.GridLayout;
20-
import org.eclipse.swt.widgets.Button;
2115
import org.eclipse.swt.widgets.Composite;
22-
import org.eclipse.swt.widgets.Label;
23-
import org.eclipse.swt.widgets.Text;
2416

25-
import org.eclipse.core.resources.IContainer;
26-
import org.eclipse.core.resources.IResource;
27-
import org.eclipse.core.resources.ResourcesPlugin;
28-
import org.eclipse.core.runtime.Path;
2917
import org.eclipse.jface.dialogs.IDialogPage;
30-
import org.eclipse.jface.viewers.ISelection;
3118
import org.eclipse.jface.viewers.IStructuredSelection;
32-
import org.eclipse.jface.wizard.WizardPage;
33-
import org.eclipse.ui.dialogs.ContainerSelectionDialog;
19+
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
3420

3521
/**
3622
* The "New" wizard page allows setting the container for the new file as well
3723
* as the file name. The page will only accept file name without the extension
3824
* OR with the extension that matches the expected one (text).
3925
*/
40-
public class NewFileWizardPage extends WizardPage {
41-
private Text containerText;
42-
43-
private Text fileText;
44-
45-
private final ISelection selection;
26+
public class NewFileWizardPage extends WizardNewFileCreationPage {
4627

4728
/**
4829
* Constructor for SampleNewWizardPage.
4930
*
5031
*/
51-
public NewFileWizardPage(ISelection selection) {
52-
super("wizardPage"); //$NON-NLS-1$
32+
public NewFileWizardPage(IStructuredSelection selection) {
33+
super("wizardPage", selection); //$NON-NLS-1$
5334
setTitle("GEF WYSIWYG Text Document"); //$NON-NLS-1$
5435
setDescription("""
5536
This wizard creates a GEF-based WYSIWYG text document with \
5637
a *.text. extension. Choose a container and file name for the new\
5738
resource."""); //$NON-NLS-1$
58-
this.selection = selection;
5939
}
6040

6141
/**
6242
* @see IDialogPage#createControl(Composite)
6343
*/
6444
@Override
6545
public void createControl(Composite parent) {
66-
Composite container = new Composite(parent, SWT.NULL);
67-
GridLayout layout = new GridLayout();
68-
container.setLayout(layout);
69-
layout.numColumns = 3;
70-
layout.verticalSpacing = 9;
71-
Label label = new Label(container, SWT.NULL);
72-
label.setText("&Container:"); //$NON-NLS-1$
73-
74-
containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
75-
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
76-
containerText.setLayoutData(gd);
77-
containerText.addModifyListener(e -> dialogChanged());
78-
79-
Button button = new Button(container, SWT.PUSH);
80-
button.setText("Browse..."); //$NON-NLS-1$
81-
button.addSelectionListener(new SelectionAdapter() {
82-
@Override
83-
public void widgetSelected(SelectionEvent e) {
84-
handleBrowse();
85-
}
86-
});
87-
label = new Label(container, SWT.NULL);
88-
label.setText("&File name:"); //$NON-NLS-1$
89-
90-
fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
91-
gd = new GridData(GridData.FILL_HORIZONTAL);
92-
fileText.setLayoutData(gd);
93-
fileText.addModifyListener(e -> dialogChanged());
94-
initialize();
95-
dialogChanged();
96-
setControl(container);
97-
}
98-
99-
/**
100-
* Tests if the current workbench selection is a suitable container to use.
101-
*/
102-
private void initialize() {
103-
if (selection != null && selection.isEmpty() == false && selection instanceof IStructuredSelection ssel) {
104-
if (ssel.size() > 1) {
105-
return;
106-
}
107-
Object obj = ssel.getFirstElement();
108-
if (obj instanceof IResource res) {
109-
IContainer container;
110-
if (obj instanceof IContainer cont) {
111-
container = cont;
112-
} else {
113-
container = res.getParent();
114-
}
115-
containerText.setText(container.getFullPath().toString());
116-
}
117-
}
118-
fileText.setText("new_file.text"); //$NON-NLS-1$
119-
}
120-
121-
/**
122-
* Uses the standard container selection dialog to choose the new value for the
123-
* container field.
124-
*/
125-
private void handleBrowse() {
126-
ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(),
127-
ResourcesPlugin.getWorkspace().getRoot(), false, "Select new file container"); //$NON-NLS-1$
128-
if (dialog.open() == ContainerSelectionDialog.OK) {
129-
Object[] result = dialog.getResult();
130-
if (result.length == 1) {
131-
containerText.setText(((Path) result[0]).toString());
132-
}
133-
}
134-
}
135-
136-
/**
137-
* Ensures that both text fields are set.
138-
*/
139-
private void dialogChanged() {
140-
IResource container = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getContainerName()));
141-
String fileName = getFileName();
142-
143-
if (getContainerName().length() == 0) {
144-
updateStatus("File container must be specified"); //$NON-NLS-1$
145-
return;
146-
}
147-
if (container == null || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
148-
updateStatus("File container must exist"); //$NON-NLS-1$
149-
return;
150-
}
151-
if (!container.isAccessible()) {
152-
updateStatus("Project must be writable"); //$NON-NLS-1$
153-
return;
154-
}
155-
if (fileName.length() == 0) {
156-
updateStatus("File name must be specified"); //$NON-NLS-1$
157-
return;
158-
}
159-
if (fileName.replace('\\', '/').indexOf('/', 1) > 0) {
160-
updateStatus("File name must be valid"); //$NON-NLS-1$
161-
return;
162-
}
163-
int dotLoc = fileName.lastIndexOf('.');
164-
if (dotLoc != -1) {
165-
String ext = fileName.substring(dotLoc + 1);
166-
if (ext.equalsIgnoreCase("text") == false) { //$NON-NLS-1$
167-
updateStatus("File extension must be \"text\""); //$NON-NLS-1$
168-
return;
169-
}
170-
}
171-
updateStatus(null);
172-
}
173-
174-
private void updateStatus(String message) {
175-
setErrorMessage(message);
176-
setPageComplete(message == null);
177-
}
178-
179-
public String getContainerName() {
180-
return containerText.getText();
181-
}
182-
183-
public String getFileName() {
184-
return fileText.getText();
46+
super.createControl(parent);
47+
setFileName("new_file.text"); //$NON-NLS-1$
48+
setPageComplete(validatePage());
18549
}
18650
}

org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/wizard/TextEditorWizard.java

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,12 @@
1717
import java.io.InputStream;
1818
import java.lang.reflect.InvocationTargetException;
1919

20-
import org.eclipse.core.resources.IContainer;
2120
import org.eclipse.core.resources.IFile;
22-
import org.eclipse.core.resources.IResource;
23-
import org.eclipse.core.resources.IWorkspaceRoot;
24-
import org.eclipse.core.resources.ResourcesPlugin;
2521
import org.eclipse.core.runtime.CoreException;
2622
import org.eclipse.core.runtime.IProgressMonitor;
27-
import org.eclipse.core.runtime.IStatus;
28-
import org.eclipse.core.runtime.Path;
29-
import org.eclipse.core.runtime.Status;
23+
import org.eclipse.core.runtime.SubMonitor;
3024
import org.eclipse.jface.dialogs.MessageDialog;
3125
import org.eclipse.jface.operation.IRunnableWithProgress;
32-
import org.eclipse.jface.viewers.ISelection;
3326
import org.eclipse.jface.viewers.IStructuredSelection;
3427
import org.eclipse.jface.wizard.Wizard;
3528
import org.eclipse.ui.INewWizard;
@@ -50,7 +43,7 @@
5043
*/
5144
public class TextEditorWizard extends Wizard implements INewWizard {
5245
private NewFileWizardPage page;
53-
private ISelection selection;
46+
private IStructuredSelection selection;
5447

5548
/**
5649
* Constructor for TextEditorWizard.
@@ -74,11 +67,10 @@ public void addPages() {
7467
*/
7568
@Override
7669
public boolean performFinish() {
77-
final String containerName = page.getContainerName();
78-
final String fileName = page.getFileName();
70+
final IFile file = page.createNewFile();
7971
IRunnableWithProgress op = monitor -> {
8072
try {
81-
doFinish(containerName, fileName, monitor);
73+
doFinish(file, monitor);
8274
} catch (CoreException e) {
8375
throw new InvocationTargetException(e);
8476
} finally {
@@ -101,45 +93,32 @@ public boolean performFinish() {
10193
* The worker method. It will find the container, create the file if missing or
10294
* just replace its contents, and open the editor on the newly created file.
10395
*/
104-
private void doFinish(String containerName, String fileName, IProgressMonitor monitor) throws CoreException {
96+
private void doFinish(IFile file, IProgressMonitor monitor) throws CoreException {
10597
// create a sample file
106-
monitor.beginTask("Creating " + fileName, 2); //$NON-NLS-1$
107-
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
108-
IResource resource = root.findMember(new Path(containerName));
109-
if (!resource.exists() || !(resource instanceof IContainer)) {
110-
throwCoreException("Container \"" + containerName + "\" does not exist."); //$NON-NLS-1$ //$NON-NLS-2$
111-
}
112-
IContainer container = (IContainer) resource;
113-
final IFile file = container.getFile(new Path(fileName));
98+
SubMonitor subMonitor = SubMonitor.convert(monitor, "Creating " + file.getName(), 2); //$NON-NLS-1$
11499
try (InputStream stream = openContentStream()) {
115100
if (file.exists()) {
116-
file.setContents(stream, true, true, monitor);
101+
file.setContents(stream, true, true, subMonitor.split(1));
117102
} else {
118-
file.create(stream, true, monitor);
103+
file.create(stream, true, subMonitor.split(1));
119104
}
120105
} catch (IOException e) {
121106
}
122-
monitor.worked(1);
123-
monitor.setTaskName("Opening file for editing..."); //$NON-NLS-1$
107+
subMonitor.subTask("Opening file for editing..."); //$NON-NLS-1$
124108
getShell().getDisplay().asyncExec(() -> {
125109
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
126110
try {
127111
IDE.openEditor(page, file, true);
128112
} catch (PartInitException e) {
129113
}
130114
});
131-
monitor.worked(1);
115+
monitor.done();
132116
}
133117

134118
private static InputStream openContentStream() {
135119
return new ByteArrayInputStream(new byte[0]);
136120
}
137121

138-
private static void throwCoreException(String message) throws CoreException {
139-
IStatus status = new Status(IStatus.ERROR, "org.eclipse.gef.examples.text", IStatus.OK, message, null); //$NON-NLS-1$
140-
throw new CoreException(status);
141-
}
142-
143122
/**
144123
* We will accept the selection in the workbench to see if we can initialize
145124
* from it.

0 commit comments

Comments
 (0)