001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.samples;
011:
012: import java.util.HashSet;
013:
014: import org.eclipse.core.resources.IResource;
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.IStatus;
017: import org.eclipse.jface.dialogs.IMessageProvider;
018: import org.eclipse.jface.wizard.WizardPage;
019: import org.eclipse.osgi.util.NLS;
020: import org.eclipse.pde.internal.ui.IHelpContextIds;
021: import org.eclipse.pde.internal.ui.PDEPlugin;
022: import org.eclipse.pde.internal.ui.PDEUIMessages;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.events.ModifyEvent;
025: import org.eclipse.swt.events.ModifyListener;
026: import org.eclipse.swt.layout.GridData;
027: import org.eclipse.swt.layout.GridLayout;
028: import org.eclipse.swt.widgets.Composite;
029: import org.eclipse.swt.widgets.Control;
030: import org.eclipse.swt.widgets.Label;
031: import org.eclipse.swt.widgets.Text;
032: import org.eclipse.ui.PlatformUI;
033:
034: /**
035: *
036: */
037: public class ProjectNamesPage extends WizardPage {
038: private SampleWizard wizard;
039: private Composite container;
040:
041: /**
042: * @param pageName
043: */
044: public ProjectNamesPage(SampleWizard wizard) {
045: super ("projects"); //$NON-NLS-1$
046: this .wizard = wizard;
047: setTitle(PDEUIMessages.ProjectNamesPage_title);
048: setDescription(PDEUIMessages.ProjectNamesPage_desc);
049: }
050:
051: public void setVisible(boolean visible) {
052: setPageComplete(wizard.getSelection() != null);
053: if (container != null)
054: updateEntries();
055: super .setVisible(visible);
056: }
057:
058: private void updateEntries() {
059: IConfigurationElement selection = wizard.getSelection();
060: if (selection != null) {
061: setMessage(null);
062: IConfigurationElement[] projects = selection
063: .getChildren("project"); //$NON-NLS-1$
064: Control[] children = container.getChildren();
065: if (projects.length == 1 && children.length == 2) {
066: Text text = (Text) children[1];
067: text.setText(projects[0].getAttribute("name")); //$NON-NLS-1$
068: validateEntries();
069: return;
070: }
071: // dispose all
072: for (int i = 0; i < children.length; i++) {
073: children[i].dispose();
074: }
075: // create entries
076: if (projects.length == 1) {
077: createEntry(PDEUIMessages.ProjectNamesPage_projectName,
078: projects[0].getAttribute("name")); //$NON-NLS-1$
079: } else {
080: for (int i = 0; i < projects.length; i++) {
081: String label = NLS
082: .bind(
083: PDEUIMessages.ProjectNamesPage_multiProjectName,
084: "" + (i + 1)); //$NON-NLS-1$
085: createEntry(label, projects[i].getAttribute("name")); //$NON-NLS-1$
086: }
087: }
088: container.layout();
089: validateEntries();
090: } else {
091: setMessage(PDEUIMessages.ProjectNamesPage_noSampleFound,
092: IMessageProvider.WARNING);
093: }
094: }
095:
096: public String[] getProjectNames() {
097: Control[] children = container.getChildren();
098: String[] names = new String[children.length / 2];
099:
100: int index = 0;
101: for (int i = 0; i < children.length; i++) {
102: if (children[i] instanceof Text) {
103: String name = ((Text) children[i]).getText();
104: names[index++] = name;
105: }
106: }
107: return names;
108: }
109:
110: private void createEntry(String labelName, String projectName) {
111: Label label = new Label(container, SWT.NULL);
112: label.setText(labelName);
113: label
114: .setLayoutData(new GridData(
115: GridData.VERTICAL_ALIGN_CENTER));
116: final Text text = new Text(container, SWT.SINGLE | SWT.BORDER);
117: text.setText(projectName);
118: text.addModifyListener(new ModifyListener() {
119: public void modifyText(ModifyEvent e) {
120: validateEntries();
121: }
122: });
123: text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
124: }
125:
126: private void validateEntries() {
127: Control[] children = container.getChildren();
128: boolean empty = false;
129:
130: HashSet set = new HashSet();
131: for (int i = 0; i < children.length; i++) {
132: if (children[i] instanceof Text) {
133: String name = ((Text) children[i]).getText();
134: if (name.length() == 0) {
135: empty = true;
136: break;
137: }
138: IStatus nameStatus = PDEPlugin.getWorkspace()
139: .validateName(name, IResource.PROJECT);
140: if (!nameStatus.isOK()) {
141: setErrorMessage(nameStatus.getMessage());
142: setPageComplete(false);
143: return;
144: }
145: set.add(name);
146: }
147: }
148: if (empty) {
149: setErrorMessage(PDEUIMessages.ProjectNamesPage_emptyName);
150: setPageComplete(false);
151: } else {
152: int nnames = set.size();
153: int nfields = children.length / 2;
154: if (nfields > nnames) {
155: setErrorMessage(PDEUIMessages.ProjectNamesPage_duplicateNames);
156: setPageComplete(false);
157: } else {
158: setPageComplete(true);
159: setErrorMessage(null);
160: }
161: }
162: }
163:
164: /* (non-Javadoc)
165: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
166: */
167: public void createControl(Composite parent) {
168: container = new Composite(parent, SWT.NULL);
169: GridLayout layout = new GridLayout();
170: layout.numColumns = 2;
171: container.setLayout(layout);
172: setControl(container);
173: updateEntries();
174:
175: PlatformUI.getWorkbench().getHelpSystem().setHelp(container,
176: IHelpContextIds.PROJECT_NAMES);
177: }
178: }
|