KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > wizard > JonasProjectWizardPage


1 package com.bull.eclipse.jonas.wizard;
2
3 import org.eclipse.core.resources.IContainer;
4 import org.eclipse.core.resources.IResource;
5 import org.eclipse.core.resources.ResourcesPlugin;
6 import org.eclipse.core.runtime.Path;
7 import org.eclipse.jface.dialogs.IDialogPage;
8 import org.eclipse.jface.viewers.ISelection;
9 import org.eclipse.jface.viewers.IStructuredSelection;
10 import org.eclipse.jface.wizard.WizardPage;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.events.ModifyEvent;
13 import org.eclipse.swt.events.ModifyListener;
14 import org.eclipse.swt.events.SelectionAdapter;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.Text;
22 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
23
24 /**
25  * The "New" wizard page allows setting the container for
26  * the new file as well as the file name. The page
27  * will only accept file name without the extension OR
28  * with the extension that matches the expected one (mpe).
29  */

30
31 public class JonasProjectWizardPage extends WizardPage {
32     private Text containerText;
33     private Text fileText;
34     private ISelection selection;
35
36     /**
37      * Constructor for SampleNewWizardPage.
38      * @param pageName
39      */

40     public JonasProjectWizardPage(ISelection selection) {
41         super("wizardPage");
42         setTitle("Multi-page Editor File");
43         setDescription("This wizard creates a new file with *.mpe extension that can be opened by a multi-page editor.");
44         this.selection = selection;
45     }
46
47     /**
48      * @see IDialogPage#createControl(Composite)
49      */

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

95     
96     private void initialize() {
97         if (selection!=null && selection.isEmpty()==false && selection instanceof IStructuredSelection) {
98             IStructuredSelection ssel = (IStructuredSelection)selection;
99             if (ssel.size()>1) return;
100             Object JavaDoc obj = ssel.getFirstElement();
101             if (obj instanceof IResource) {
102                 IContainer container;
103                 if (obj instanceof IContainer)
104                     container = (IContainer)obj;
105                 else
106                     container = ((IResource)obj).getParent();
107                 containerText.setText(container.getFullPath().toString());
108             }
109         }
110         fileText.setText("new_file.mpe");
111     }
112     
113     /**
114      * Uses the standard container selection dialog to
115      * choose the new value for the container field.
116      */

117
118     private void handleBrowse() {
119         ContainerSelectionDialog dialog =
120             new ContainerSelectionDialog(
121                 getShell(),
122                 ResourcesPlugin.getWorkspace().getRoot(),
123                 false,
124                 "Select new file container");
125         if (dialog.open() == ContainerSelectionDialog.OK) {
126             Object JavaDoc[] result = dialog.getResult();
127             if (result.length == 1) {
128                 containerText.setText(((Path)result[0]).toOSString());
129             }
130         }
131     }
132     
133     /**
134      * Ensures that both text fields are set.
135      */

136
137     private void dialogChanged() {
138         String JavaDoc container = getContainerName();
139         String JavaDoc fileName = getFileName();
140
141         if (container.length() == 0) {
142             updateStatus("File container must be specified");
143             return;
144         }
145         if (fileName.length() == 0) {
146             updateStatus("File name must be specified");
147             return;
148         }
149         int dotLoc = fileName.lastIndexOf('.');
150         if (dotLoc != -1) {
151             String JavaDoc ext = fileName.substring(dotLoc + 1);
152             if (ext.equalsIgnoreCase("mpe") == false) {
153                 updateStatus("File extension must be \"mpe\"");
154                 return;
155             }
156         }
157         updateStatus(null);
158     }
159
160     private void updateStatus(String JavaDoc message) {
161         setErrorMessage(message);
162         setPageComplete(message == null);
163     }
164
165     public String JavaDoc getContainerName() {
166         return containerText.getText();
167     }
168     public String JavaDoc getFileName() {
169         return fileText.getText();
170     }
171 }
Popular Tags