KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > plugin > NewProjectCreationPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.wizards.plugin;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.resources.IWorkspace;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.jdt.ui.PreferenceConstants;
19 import org.eclipse.jface.dialogs.Dialog;
20 import org.eclipse.jface.dialogs.IDialogSettings;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.pde.internal.core.ICoreConstants;
23 import org.eclipse.pde.internal.core.PDECore;
24 import org.eclipse.pde.internal.core.TargetPlatformHelper;
25 import org.eclipse.pde.internal.ui.IHelpContextIds;
26 import org.eclipse.pde.internal.ui.PDEUIMessages;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.ModifyEvent;
29 import org.eclipse.swt.events.ModifyListener;
30 import org.eclipse.swt.events.SelectionAdapter;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Combo;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Group;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Text;
40 import org.eclipse.ui.PlatformUI;
41 import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
42
43
44 public class NewProjectCreationPage extends WizardNewProjectCreationPage {
45     protected Button fJavaButton;
46     protected boolean fFragment;
47     private Label fSourceLabel;
48     private Text fSourceText;
49     private Label fOutputlabel;
50     private Text fOutputText;
51     private AbstractFieldData fData;
52     private Button fEclipseButton;
53     private Combo fTargetCombo;
54     private Combo fOSGiCombo;
55     private Button fOSGIButton;
56     
57     private static final String JavaDoc S_OSGI_PROJECT = "osgiProject"; //$NON-NLS-1$
58
private static final String JavaDoc S_TARGET_NAME = "targetName"; //$NON-NLS-1$
59

60     public NewProjectCreationPage(String JavaDoc pageName, AbstractFieldData data, boolean fragment){
61         super(pageName);
62         fFragment = fragment;
63         fData = data;
64     }
65     
66     public void createControl(Composite parent) {
67         super.createControl(parent);
68         Composite control = (Composite)getControl();
69         GridLayout layout = new GridLayout();
70         control.setLayout(layout);
71
72         createProjectTypeGroup(control);
73         createFormatGroup(control);
74         
75         updateRuntimeDependency();
76
77         Dialog.applyDialogFont(control);
78         PlatformUI.getWorkbench().getHelpSystem().setHelp(control,
79                 fFragment ? IHelpContextIds.NEW_FRAGMENT_STRUCTURE_PAGE
80                             : IHelpContextIds.NEW_PROJECT_STRUCTURE_PAGE);
81         setControl(control);
82     }
83     
84     private void createProjectTypeGroup(Composite container) {
85         Group group = new Group(container, SWT.NONE);
86         group.setText(PDEUIMessages.ProjectStructurePage_settings);
87         GridLayout layout = new GridLayout();
88         layout.numColumns = 2;
89         group.setLayout(layout);
90         group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
91     
92         fJavaButton = createButton(group, SWT.CHECK, 2, 0);
93         fJavaButton.setText(PDEUIMessages.ProjectStructurePage_java);
94         fJavaButton.setSelection(true);
95         fJavaButton.addSelectionListener(new SelectionAdapter(){
96             public void widgetSelected(SelectionEvent e) {
97                 boolean enabled = fJavaButton.getSelection();
98                 fSourceLabel.setEnabled(enabled);
99                 fSourceText.setEnabled(enabled);
100                 fOutputlabel.setEnabled(enabled);
101                 fOutputText.setEnabled(enabled);
102                 setPageComplete(validatePage());
103             }
104         });
105         
106         fSourceLabel = createLabel(group, PDEUIMessages.ProjectStructurePage_source);
107         fSourceText = createText(group);
108         IPreferenceStore store = PreferenceConstants.getPreferenceStore();
109         fSourceText.setText(store.getString(PreferenceConstants.SRCBIN_SRCNAME));
110         
111         fOutputlabel = createLabel(group, PDEUIMessages.ProjectStructurePage_output);
112         fOutputText = createText(group);
113         fOutputText.setText(store.getString(PreferenceConstants.SRCBIN_BINNAME));
114     }
115     
116     private void createFormatGroup(Composite container) {
117         Group group = new Group(container, SWT.NONE);
118         group.setText(PDEUIMessages.NewProjectCreationPage_target);
119         group.setLayout(new GridLayout(2, false));
120         group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
121         
122         Label label = new Label(group, SWT.NONE);
123         if (fFragment)
124             label.setText(PDEUIMessages.NewProjectCreationPage_ftarget);
125         else
126             label.setText(PDEUIMessages.NewProjectCreationPage_ptarget);
127         GridData gd = new GridData();
128         gd.horizontalSpan = 2;
129         label.setLayoutData(gd);
130         
131         IDialogSettings settings = getDialogSettings();
132         boolean osgiProject = (settings == null) ? false : settings.getBoolean(S_OSGI_PROJECT);
133             
134         fEclipseButton = createButton(group, SWT.RADIO, 1, 30);
135         fEclipseButton.setText(PDEUIMessages.NewProjectCreationPage_pDependsOnRuntime);
136         fEclipseButton.setSelection(!osgiProject);
137         fEclipseButton.addSelectionListener(new SelectionAdapter(){
138             public void widgetSelected(SelectionEvent e) {
139                 updateRuntimeDependency();
140             }
141         });
142         
143         fTargetCombo = new Combo(group, SWT.READ_ONLY|SWT.SINGLE);
144         fTargetCombo.setItems(new String JavaDoc[] {ICoreConstants.TARGET33, ICoreConstants.TARGET32, ICoreConstants.TARGET31, ICoreConstants.TARGET30});
145         boolean comboInitialized = false;
146         if (settings != null && !osgiProject) {
147             String JavaDoc text = settings.get(S_TARGET_NAME);
148             comboInitialized = (text != null && fTargetCombo.indexOf(text) >= 0);
149             if (comboInitialized)
150                 fTargetCombo.setText(text);
151         }
152         if (!comboInitialized) {
153             if (PDECore.getDefault().areModelsInitialized())
154                 fTargetCombo.setText(TargetPlatformHelper.getTargetVersionString());
155             else
156                 fTargetCombo.setText(ICoreConstants.TARGET33);
157         }
158         
159         fOSGIButton = createButton(group, SWT.RADIO, 1, 30);
160         fOSGIButton.setText(PDEUIMessages.NewProjectCreationPage_pPureOSGi);
161         fOSGIButton.setSelection(osgiProject);
162         
163         fOSGiCombo = new Combo(group, SWT.READ_ONLY|SWT.SINGLE);
164         fOSGiCombo.setItems(new String JavaDoc[] {ICoreConstants.EQUINOX, PDEUIMessages.NewProjectCreationPage_standard});
165         comboInitialized = false;
166         if (settings != null && osgiProject) {
167             String JavaDoc text = settings.get(S_TARGET_NAME);
168             comboInitialized = (text != null && fOSGiCombo.indexOf(text) >= 0);
169             if (comboInitialized)
170                 fOSGiCombo.setText(text);
171         }
172         if (!comboInitialized)
173             fOSGiCombo.setText(ICoreConstants.EQUINOX);
174         
175     }
176     
177     private void updateRuntimeDependency() {
178         boolean depends = fEclipseButton.getSelection();
179         fTargetCombo.setEnabled(depends);
180         fOSGiCombo.setEnabled(!depends);
181     }
182     
183     private Button createButton(Composite container, int style, int span, int indent) {
184         Button button = new Button(container, style);
185         GridData gd = new GridData();
186         gd.horizontalSpan = span;
187         gd.horizontalIndent = indent;
188         button.setLayoutData(gd);
189         return button;
190     }
191     
192     private Label createLabel(Composite container, String JavaDoc text) {
193         Label label = new Label(container, SWT.NONE);
194         label.setText(text);
195         GridData gd = new GridData();
196         gd.horizontalIndent = 30;
197         label.setLayoutData(gd);
198         return label;
199     }
200     
201     private Text createText(Composite container) {
202         Text text = new Text(container, SWT.BORDER | SWT.SINGLE);
203         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
204         gd.widthHint = 300;
205         text.setLayoutData(gd);
206         text.addModifyListener(new ModifyListener() {
207             public void modifyText(ModifyEvent e) {
208                 setPageComplete(validatePage());
209             }
210         });
211         return text;
212     }
213     
214     public void updateData() {
215         fData.setSimple(!fJavaButton.getSelection());
216         fData.setSourceFolderName(fSourceText.getText().trim());
217         fData.setOutputFolderName(fOutputText.getText().trim());
218         fData.setLegacy(false); //$NON-NLS-1$
219
fData.setTargetVersion(fTargetCombo.getText());
220         fData.setHasBundleStructure(fOSGIButton.getSelection() || Double.parseDouble(fTargetCombo.getText()) >= 3.1);
221         fData.setOSGiFramework(fOSGIButton.getSelection() ? fOSGiCombo.getText() : null);
222     }
223     
224     protected boolean validatePage() {
225         if (!super.validatePage())
226             return false;
227         
228         String JavaDoc name = getProjectName();
229         if (name.indexOf('%') > 0) {
230             setErrorMessage(PDEUIMessages.NewProjectCreationPage_invalidProjectName);
231             return false;
232         }
233         
234         String JavaDoc location = getLocationPath().toString();
235         if (location.indexOf('%') > 0) {
236             setErrorMessage(PDEUIMessages.NewProjectCreationPage_invalidLocationPath);
237             return false;
238         }
239         
240         if (fJavaButton.getSelection()) {
241             IWorkspace workspace = ResourcesPlugin.getWorkspace();
242             IProject dmy = workspace.getRoot().getProject("project"); //$NON-NLS-1$
243
IStatus status;
244             if(fSourceText != null && fSourceText.getText().length() != 0) {
245                 status = workspace.validatePath(dmy.getFullPath().append(fSourceText.getText()).toString(), IResource.FOLDER);
246                 if (!status.isOK()) {
247                     setErrorMessage(status.getMessage());
248                     return false;
249                 }
250             }
251             if(fOutputText != null && fOutputText.getText().length() != 0) {
252                 status = workspace.validatePath(dmy.getFullPath().append(fOutputText.getText()).toString(), IResource.FOLDER);
253                 if (!status.isOK()) {
254                     setErrorMessage(status.getMessage());
255                     return false;
256                 }
257             }
258         }
259         setErrorMessage(null);
260         setMessage(null);
261         return true;
262     }
263     
264     protected void saveSettings(IDialogSettings settings) {
265         boolean eclipseSelected = fEclipseButton.getSelection();
266         String JavaDoc targetName = eclipseSelected ? fTargetCombo.getText() : fOSGiCombo.getText();
267         settings.put(S_TARGET_NAME, (eclipseSelected && TargetPlatformHelper.getTargetVersionString().equals(targetName)) ? null : targetName);
268         settings.put(S_OSGI_PROJECT, !eclipseSelected);
269     }
270     
271 }
272
Popular Tags