1 11 package org.eclipse.pde.ui.internal.samples; 12 13 import java.util.HashSet ; 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 31 public class ProjectNamesPage extends WizardPage { 32 private SampleWizard wizard; 33 private Composite container; 34 37 public ProjectNamesPage(SampleWizard wizard) { 38 super("projects"); this.wizard = wizard; 40 setTitle(PDEUIMessages.ProjectNamesPage_title); setDescription(PDEUIMessages.ProjectNamesPage_desc); } 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"); 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")); validateEntries(); 59 return; 60 } 61 for (int i=0; i<children.length; i++) { 63 children[i].dispose(); 64 } 65 if (projects.length==1) { 67 createEntry(PDEUIMessages.ProjectNamesPage_projectName, projects[0].getAttribute("name")); } 69 else { 70 for (int i=0; i<projects.length; i++) { 71 String label = NLS.bind(PDEUIMessages.ProjectNamesPage_multiProjectName, ""+(i+1)); createEntry(label, projects[i].getAttribute("name")); } 74 } 75 container.layout(); 76 validateEntries(); 77 } 78 else { 79 setMessage(PDEUIMessages.ProjectNamesPage_noSampleFound, WizardPage.WARNING); } 81 } 82 public String [] getProjectNames() { 83 Control [] children = container.getChildren(); 84 String [] names = new String [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 name = ((Text)children[i]).getText(); 90 names[index++] = name; 91 } 92 } 93 return names; 94 } 95 private void createEntry(String labelName, String 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 set = new HashSet (); 113 for (int i=0; i<children.length; i++) { 114 if (children[i] instanceof Text) { 115 String 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); 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); setPageComplete(false); 139 } 140 else { 141 setPageComplete(true); 142 setErrorMessage(null); 143 } 144 } 145 } 146 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 |