KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > ui > internal > samples > ProjectNamesPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.ui.internal.samples;
12
13 import java.util.HashSet JavaDoc;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.jface.wizard.WizardPage;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.pde.internal.ui.IHelpContextIds;
20 import org.eclipse.pde.internal.ui.PDEPlugin;
21 import org.eclipse.pde.internal.ui.PDEUIMessages;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.*;
24 import org.eclipse.swt.layout.*;
25 import org.eclipse.swt.widgets.*;
26 import org.eclipse.ui.PlatformUI;
27
28 /**
29  *
30  */

31 public class ProjectNamesPage extends WizardPage {
32     private SampleWizard wizard;
33     private Composite container;
34     /**
35      * @param pageName
36      */

37     public ProjectNamesPage(SampleWizard wizard) {
38         super("projects"); //$NON-NLS-1$
39
this.wizard = wizard;
40         setTitle(PDEUIMessages.ProjectNamesPage_title); //$NON-NLS-1$
41
setDescription(PDEUIMessages.ProjectNamesPage_desc); //$NON-NLS-1$
42
}
43     public void setVisible(boolean visible) {
44         setPageComplete(wizard.getSelection()!=null);
45         if (container!=null) updateEntries();
46         super.setVisible(visible);
47     }
48     
49     private void updateEntries() {
50         IConfigurationElement selection = wizard.getSelection();
51         if (selection!=null) {
52             setMessage(null);
53             IConfigurationElement [] projects = selection.getChildren("project"); //$NON-NLS-1$
54
Control [] children = container.getChildren();
55             if (projects.length==1 && children.length==2) {
56                 Text text = (Text)children[1];
57                 text.setText(projects[0].getAttribute("name")); //$NON-NLS-1$
58
validateEntries();
59                 return;
60             }
61             // dispose all
62
for (int i=0; i<children.length; i++) {
63                 children[i].dispose();
64             }
65             // create entries
66
if (projects.length==1) {
67                 createEntry(PDEUIMessages.ProjectNamesPage_projectName, projects[0].getAttribute("name")); //$NON-NLS-1$ //$NON-NLS-2$
68
}
69             else {
70                 for (int i=0; i<projects.length; i++) {
71                     String JavaDoc label = NLS.bind(PDEUIMessages.ProjectNamesPage_multiProjectName, ""+(i+1)); //$NON-NLS-1$ //$NON-NLS-2$
72
createEntry(label, projects[i].getAttribute("name")); //$NON-NLS-1$
73
}
74             }
75             container.layout();
76             validateEntries();
77         }
78         else {
79             setMessage(PDEUIMessages.ProjectNamesPage_noSampleFound, WizardPage.WARNING); //$NON-NLS-1$
80
}
81     }
82     public String JavaDoc [] getProjectNames() {
83         Control [] children = container.getChildren();
84         String JavaDoc [] names = new String JavaDoc[children.length/2];
85
86         int index=0;
87         for (int i=0; i<children.length; i++) {
88             if (children[i] instanceof Text) {
89                 String JavaDoc name = ((Text)children[i]).getText();
90                 names[index++] = name;
91             }
92         }
93         return names;
94     }
95     private void createEntry(String JavaDoc labelName, String JavaDoc projectName) {
96         Label label = new Label(container, SWT.NULL);
97         label.setText(labelName);
98         label.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
99         final Text text = new Text(container, SWT.SINGLE|SWT.BORDER);
100         text.setText(projectName);
101         text.addModifyListener(new ModifyListener() {
102             public void modifyText(ModifyEvent e) {
103                 validateEntries();
104             }
105         });
106         text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
107     }
108     private void validateEntries() {
109         Control [] children = container.getChildren();
110         boolean empty=false;
111         
112         HashSet JavaDoc set = new HashSet JavaDoc();
113         for (int i=0; i<children.length; i++) {
114             if (children[i] instanceof Text) {
115                 String JavaDoc name = ((Text)children[i]).getText();
116                 if (name.length()==0) {
117                     empty=true;
118                     break;
119                 }
120                 IStatus nameStatus = PDEPlugin.getWorkspace().validateName(name, IResource.PROJECT);
121                 if (!nameStatus.isOK()) {
122                     setErrorMessage(nameStatus.getMessage());
123                     setPageComplete(false);
124                     return;
125                 }
126                 set.add(name);
127             }
128         }
129         if (empty) {
130             setErrorMessage(PDEUIMessages.ProjectNamesPage_emptyName); //$NON-NLS-1$
131
setPageComplete(false);
132         }
133         else {
134             int nnames = set.size();
135             int nfields = children.length/2;
136             if (nfields>nnames) {
137                 setErrorMessage(PDEUIMessages.ProjectNamesPage_duplicateNames); //$NON-NLS-1$
138
setPageComplete(false);
139             }
140             else {
141                 setPageComplete(true);
142                 setErrorMessage(null);
143             }
144         }
145     }
146     /* (non-Javadoc)
147      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
148      */

149     public void createControl(Composite parent) {
150         container = new Composite(parent, SWT.NULL);
151         GridLayout layout = new GridLayout();
152         layout.numColumns = 2;
153         container.setLayout(layout);
154         setControl(container);
155         updateEntries();
156         
157         PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.PROJECT_NAMES);
158     }
159 }
160
Popular Tags