KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > eclipse > wizards > NewXdocPage


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation or its licensors,
3  * as applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.forrest.eclipse.wizards;
18
19 import org.eclipse.jface.wizard.WizardPage;
20 import org.eclipse.swt.widgets.*;
21 import org.eclipse.swt.layout.*;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.core.resources.*;
24 import org.eclipse.core.runtime.Path;
25 import org.eclipse.swt.events.*;
26 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
27 import org.eclipse.jface.viewers.*;
28
29 /**
30  * The "New" wizard page allows setting the container for
31  * the new xdoc file as well as the file name. The page
32  * will only accept file name without the extension OR
33  * with the extension that matches the expected one (xml).
34  */

35
36 public class NewXdocPage extends WizardPage {
37     // FIXME: get these config settings from forrest.properties
38
public String JavaDoc projectXDocsDir = "/src/documentation/content/xdocs";
39     public String JavaDoc default_filename = "new_file.xml";
40     public String JavaDoc required_file_extension = "xml";
41     private Text containerText;
42     private Text fileText;
43     protected ISelection selection;
44
45     /**
46      * Create the new page.
47      * @param pageName
48      */

49     public NewXdocPage(ISelection selection) {
50         super("wizardPage");
51         setTitle("XDoc File");
52         setDescription("This wizard creates a new XDoc file with *.xml extension.");
53         this.selection = selection;
54     }
55
56     /**
57      * @see IDialogPage#createControl(Composite)
58      */

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

105     
106     private void initialize() {
107         if (selection!=null && selection.isEmpty()==false && selection instanceof IStructuredSelection) {
108             IStructuredSelection ssel = (IStructuredSelection)selection;
109             if (ssel.size()>1) return;
110             Object JavaDoc obj = ssel.getFirstElement();
111             if (obj instanceof IResource) {
112                 IContainer container;
113                 if (obj instanceof IContainer)
114                     container = (IContainer)obj;
115                 else
116                     container = ((IResource)obj).getParent();
117                 containerText.setText(container.getFullPath().toString());
118             }
119         }
120         fileText.setText(default_filename);
121     }
122     
123     /**
124      * Uses the standard container selection dialog to
125      * choose the new value for the container field.
126      */

127
128     private void handleBrowse() {
129         ContainerSelectionDialog dialog =
130             new ContainerSelectionDialog(
131                 getShell(),
132                 ResourcesPlugin.getWorkspace().getRoot(),
133                 false,
134                 "Select new file container");
135         if (dialog.open() == ContainerSelectionDialog.OK) {
136             Object JavaDoc[] result = dialog.getResult();
137             if (result.length == 1) {
138                 containerText.setText(((Path)result[0]).toOSString());
139             }
140         }
141     }
142     
143     /**
144      * Ensures that both text fields are set.
145      */

146
147     private void dialogChanged() {
148         String JavaDoc container = getContainerName();
149         String JavaDoc fileName = getFileName();
150
151         if (container.length() == 0) {
152             updateStatus("File container must be specified");
153             return;
154         }
155         if (fileName.length() == 0) {
156             updateStatus("File name must be specified");
157             return;
158         }
159         int dotLoc = fileName.lastIndexOf('.');
160         if (dotLoc != -1) {
161             String JavaDoc ext = fileName.substring(dotLoc + 1);
162             if (ext.equalsIgnoreCase(required_file_extension) == false) {
163                 updateStatus("File extension must be \"" + required_file_extension + "\"");
164                 return;
165             }
166         }
167         updateStatus(null);
168     }
169
170     private void updateStatus(String JavaDoc message) {
171         setErrorMessage(message);
172         setPageComplete(message == null);
173     }
174
175     public String JavaDoc getContainerName() {
176         return containerText.getText();
177     }
178     public String JavaDoc getFileName() {
179         return fileText.getText();
180     }
181 }
Popular Tags