KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > $packageName$ > $wizardPageClassName$


1 package $packageName$;
2
3 import org.eclipse.jface.wizard.WizardPage;
4 import org.eclipse.swt.widgets.*;
5 import org.eclipse.swt.layout.*;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.core.resources.*;
8 import org.eclipse.core.runtime.Path;
9 import org.eclipse.swt.events.*;
10 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
11 import org.eclipse.jface.viewers.*;
12
13 /**
14  * The "New" wizard page allows setting the container for
15  * the new file as well as the file name. The page
16  * will only accept file name without the extension OR
17  * with the extension that matches the expected one ($extension$).
18  */

19
20 public class $wizardPageClassName$ extends WizardPage {
21     private Text containerText;
22     private Text fileText;
23     private ISelection selection;
24
25     /**
26      * Constructor for SampleNewWizardPage.
27      * @param pageName
28      */

29     public $wizardPageClassName$(ISelection selection) {
30         super("wizardPage");
31         setTitle("Multi-page Editor File");
32         setDescription("This wizard creates a new file with *.$extension$ extension that can be opened by a multi-page editor.");
33         this.selection = selection;
34     }
35
36     /**
37      * @see IDialogPage#createControl(Composite)
38      */

39     public void createControl(Composite parent) {
40         Composite container = new Composite(parent, SWT.NULL);
41         GridLayout layout = new GridLayout();
42         container.setLayout(layout);
43         layout.numColumns = 3;
44         layout.verticalSpacing = 9;
45         Label label = new Label(container, SWT.NULL);
46         label.setText("&Container:");
47
48         containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
49         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
50         containerText.setLayoutData(gd);
51         containerText.addModifyListener(new ModifyListener() {
52             public void modifyText(ModifyEvent e) {
53                 dialogChanged();
54             }
55         });
56
57         Button button = new Button(container, SWT.PUSH);
58         button.setText("Browse...");
59         button.addSelectionListener(new SelectionAdapter() {
60             public void widgetSelected(SelectionEvent e) {
61                 handleBrowse();
62             }
63         });
64         label = new Label(container, SWT.NULL);
65         label.setText("&File name:");
66
67         fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
68         gd = new GridData(GridData.FILL_HORIZONTAL);
69         fileText.setLayoutData(gd);
70         fileText.addModifyListener(new ModifyListener() {
71             public void modifyText(ModifyEvent e) {
72                 dialogChanged();
73             }
74         });
75         initialize();
76         dialogChanged();
77         setControl(container);
78     }
79     
80     /**
81      * Tests if the current workbench selection is a suitable
82      * container to use.
83      */

84     
85     private void initialize() {
86         if (selection!=null && selection.isEmpty()==false && selection instanceof IStructuredSelection) {
87             IStructuredSelection ssel = (IStructuredSelection)selection;
88             if (ssel.size()>1) return;
89             Object JavaDoc obj = ssel.getFirstElement();
90             if (obj instanceof IResource) {
91                 IContainer container;
92                 if (obj instanceof IContainer)
93                     container = (IContainer)obj;
94                 else
95                     container = ((IResource)obj).getParent();
96                 containerText.setText(container.getFullPath().toString());
97             }
98         }
99         fileText.setText("$initialFileName$");
100     }
101     
102     /**
103      * Uses the standard container selection dialog to
104      * choose the new value for the container field.
105      */

106
107     private void handleBrowse() {
108         ContainerSelectionDialog dialog =
109             new ContainerSelectionDialog(
110                 getShell(),
111                 ResourcesPlugin.getWorkspace().getRoot(),
112                 false,
113                 "Select new file container");
114         if (dialog.open() == ContainerSelectionDialog.OK) {
115             Object JavaDoc[] result = dialog.getResult();
116             if (result.length == 1) {
117                 containerText.setText(((Path)result[0]).toOSString());
118             }
119         }
120     }
121     
122     /**
123      * Ensures that both text fields are set.
124      */

125
126     private void dialogChanged() {
127         String JavaDoc container = getContainerName();
128         String JavaDoc fileName = getFileName();
129
130         if (container.length() == 0) {
131             updateStatus("File container must be specified");
132             return;
133         }
134         if (fileName.length() == 0) {
135             updateStatus("File name must be specified");
136             return;
137         }
138         int dotLoc = fileName.lastIndexOf('.');
139         if (dotLoc != -1) {
140             String JavaDoc ext = fileName.substring(dotLoc + 1);
141             if (ext.equalsIgnoreCase("$extension$") == false) {
142                 updateStatus("File extension must be \"$extension$\"");
143                 return;
144             }
145         }
146         updateStatus(null);
147     }
148
149     private void updateStatus(String JavaDoc message) {
150         setErrorMessage(message);
151         setPageComplete(message == null);
152     }
153
154     public String JavaDoc getContainerName() {
155         return containerText.getText();
156     }
157     public String JavaDoc getFileName() {
158         return fileText.getText();
159     }
160 }
Popular Tags