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
1212 *******************************************************************************/
1313package 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 ;
2115import 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 ;
2917import org .eclipse .jface .dialogs .IDialogPage ;
30- import org .eclipse .jface .viewers .ISelection ;
3118import 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}
0 commit comments