KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.samples;
12
13 import java.util.HashSet JavaDoc;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.jface.dialogs.IMessageProvider;
19 import org.eclipse.jface.wizard.WizardPage;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.pde.internal.ui.IHelpContextIds;
22 import org.eclipse.pde.internal.ui.PDEPlugin;
23 import org.eclipse.pde.internal.ui.PDEUIMessages;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.ModifyEvent;
26 import org.eclipse.swt.events.ModifyListener;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Text;
33 import org.eclipse.ui.PlatformUI;
34
35 /**
36  *
37  */

38 public class ProjectNamesPage extends WizardPage {
39     private SampleWizard wizard;
40     private Composite container;
41     /**
42      * @param pageName
43      */

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

156     public void createControl(Composite parent) {
157         container = new Composite(parent, SWT.NULL);
158         GridLayout layout = new GridLayout();
159         layout.numColumns = 2;
160         container.setLayout(layout);
161         setControl(container);
162         updateEntries();
163         
164         PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.PROJECT_NAMES);
165     }
166 }
167
Popular Tags