Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3be03e3b47f1e1bc81577cb5bcf252a958fd76bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.core.ui.internal.wizards;

import java.util.HashMap;
import java.util.Map;

import org.apache.maven.model.Model;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.window.Window;

import org.eclipse.m2e.core.internal.index.IndexedArtifactFile;
import org.eclipse.m2e.core.project.ProjectImportConfiguration;
import org.eclipse.m2e.core.ui.internal.Messages;
import org.eclipse.m2e.core.ui.internal.dialogs.MavenRepositorySearchDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;


/**
 * Wizard page responsible for gathering information about the Maven2 artifact and the directories to create. This
 * wizard page gathers Maven2 specific information. The user must specify the necessary information about the Maven2
 * artifact and she can also decide which directories of the default Maven2 directory structure should be created. Input
 * validation is performed in order to make sure that all the provided information is valid before letting the wizard
 * continue.
 */
public class MavenProjectWizardArtifactPage extends AbstractMavenWizardPage {

  private static final ProjectFolder JAVA = new ProjectFolder("src/main/java", "target/classes"); //$NON-NLS-1$ //$NON-NLS-2$

  private static final ProjectFolder JAVA_TEST = new ProjectFolder("src/test/java", "target/test-classes"); //$NON-NLS-1$ //$NON-NLS-2$

  private static final ProjectFolder RESOURCES = new ProjectFolder("src/main/resources", "target/classes"); //$NON-NLS-1$ //$NON-NLS-2$

  private static final ProjectFolder RESOURCES_TEST = new ProjectFolder("src/test/resources", "target/test-classes"); //$NON-NLS-1$ //$NON-NLS-2$

  // private static final ProjectFolder FILTERS = new ProjectFolder("src/main/filters", null, false);

  // private static final ProjectFolder FILTERS_TEST = new ProjectFolder("src/test/filters", null, false);

  // private static final ProjectFolder ASSEMBLY = new ProjectFolder("src/main/assembly", null, false);

  // private static final ProjectFolder CONFIG = new ProjectFolder("src/main/config", null, false);

  private static final ProjectFolder WEBAPP = new ProjectFolder("src/main/webapp", null); //$NON-NLS-1$

  private static final ProjectFolder EAR = new ProjectFolder("src/main/application", null); //$NON-NLS-1$

  private static final ProjectFolder SITE = new ProjectFolder("src/site", null); //$NON-NLS-1$

  private static final ProjectFolder[] JAR_DIRS = {JAVA, JAVA_TEST, RESOURCES, RESOURCES_TEST};

  private static final ProjectFolder[] WAR_DIRS = {JAVA, JAVA_TEST, RESOURCES, RESOURCES_TEST, WEBAPP};

  private static final ProjectFolder[] EAR_DIRS = {EAR}; // MNGECLIPSE-688 add EAR Support

  private static final ProjectFolder[] POM_DIRS = {SITE};

  /** special directory sets, default is JAR_DIRS */
  private static final Map<String, ProjectFolder[]> directorySets = new HashMap<String, ProjectFolder[]>();
  static {
    directorySets.put(MavenArtifactComponent.WAR, WAR_DIRS);
    directorySets.put(MavenArtifactComponent.POM, POM_DIRS);
    directorySets.put(MavenArtifactComponent.EAR, EAR_DIRS); // MNGECLIPSE-688 add EAR Support
  }

  /** parent property panel */
  protected MavenParentComponent parentComponent;

  /** artifact property panel */
  protected MavenArtifactComponent artifactComponent;

  /** the parent readonly state */
  private boolean readonlyParent = false;

  private boolean isUsed;

  /**
   * Sets the title and description of this wizard page and marks it as not being complete as user input is required for
   * continuing.
   * @wbp.parser.constructor
   */
  public MavenProjectWizardArtifactPage(ProjectImportConfiguration projectImportConfiguration) {
    this("MavenProjectWizardArtifactPage", projectImportConfiguration); //$NON-NLS-1$
  }

  /**
   * Sets the title and description of this wizard page and marks it as not being complete as user input is required for
   * continuing.
   */
  protected MavenProjectWizardArtifactPage(String name, ProjectImportConfiguration projectImportConfiguration) {
    super(name, projectImportConfiguration);

    setTitle(Messages.wizardProjectPageMaven2Title);
    setDescription(Messages.wizardProjectPageMaven2Description);
    setPageComplete(false);
  }

  /**
   * {@inheritDoc} This wizard page contains a <code>MavenArtifactComponent</code> to gather information about the Maven
   * artifact and a <code>Maven2DirectoriesComponent</code> which allows to choose which directories of the default
   * Maven directory structure to create.
   */
  public void createControl(Composite parent) {
    GridLayout layout = new GridLayout();
    layout.marginWidth = 0;
    layout.marginHeight = 0;

    Composite container = new Composite(parent, SWT.NULL);
    container.setLayout(layout);

    WidthGroup widthGroup = new WidthGroup();
    container.addControlListener(widthGroup);

    ModifyListener modifyingListener = new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        validate();
      }
    };

    artifactComponent = new MavenArtifactComponent(container, SWT.NONE);
    artifactComponent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    artifactComponent.setWidthGroup(widthGroup);
    artifactComponent.setModifyingListener(modifyingListener);
    artifactComponent.setArtifactIdEditable(!readonlyParent);

    parentComponent = new MavenParentComponent(container, readonlyParent ? SWT.READ_ONLY : SWT.NONE);
    parentComponent.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
    parentComponent.setWidthGroup(widthGroup);

    parentComponent.addModifyListener(modifyingListener);
    parentComponent.addBrowseButtonListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        MavenRepositorySearchDialog dialog = MavenRepositorySearchDialog.createSearchParentDialog(getShell(), 
            org.eclipse.m2e.core.ui.internal.Messages.MavenProjectWizardArtifactPage_searchDialog_title, null, null); 
        
        if(dialog.open() == Window.OK) {
          IndexedArtifactFile indexedArtifactFile = (IndexedArtifactFile) dialog.getFirstResult();
          if(indexedArtifactFile != null) {
            parentComponent.setValues(indexedArtifactFile.group, indexedArtifactFile.artifact,
                indexedArtifactFile.version);
          }
        }
      }
    });

    createAdvancedSettings(container, new GridData(SWT.FILL, SWT.TOP, false, false, 2, 1));
    resolverConfigurationComponent.setModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        validate();
      }
    });

    addFieldWithHistory("groupId", artifactComponent.getGroupIdCombo()); //$NON-NLS-1$
    addFieldWithHistory("artifactId", artifactComponent.getArtifactIdCombo()); //$NON-NLS-1$
    addFieldWithHistory("version", artifactComponent.getVersionCombo()); //$NON-NLS-1$
    addFieldWithHistory("name", artifactComponent.getNameCombo()); //$NON-NLS-1$

    addFieldWithHistory("groupId", parentComponent.getGroupIdCombo()); //$NON-NLS-1$
    addFieldWithHistory("artifactId", parentComponent.getArtifactIdCombo()); //$NON-NLS-1$
    addFieldWithHistory("version", parentComponent.getVersionCombo()); //$NON-NLS-1$

    container.layout();

    validate();

    setControl(container);
  }

  public void setVisible(boolean visible) {
    super.setVisible(visible);
    artifactComponent.getGroupIdCombo().setFocus();
  }

  /**
   * Returns the Maven2 model containing the artifact information provided by the user.
   * 
   * @return The Maven2 model containing the provided artifact information. Is never <code>null</code>.
   */
  public Model getModel() {
    Model model = artifactComponent.getModel();

    parentComponent.updateModel(model);

    return model;
  }

  /** Returns the list of directories for the currently selected packaging. */
  private ProjectFolder[] getProjectFolders() {
    ProjectFolder[] folders = directorySets.get(artifactComponent.getPackaging());
    return folders == null ? JAR_DIRS : folders;
  }

  /**
   * Returns the directories of the default Maven2 directory structure selected by the user. These directories should be
   * created along with the new project.
   * 
   * @return The Maven2 directories selected by the user. Neither the array nor any of its elements is <code>null</code>
   *         .
   */
  public String[] getFolders() {
    ProjectFolder[] mavenDirectories = getProjectFolders();

    String[] directories = new String[mavenDirectories.length];
    for(int i = 0; i < directories.length; i++ ) {
      directories[i] = mavenDirectories[i].getPath();
    }

    return directories;
  }

  /**
   * Validates the contents of this wizard page.
   * <p>
   * Feedback about the validation is given to the user by displaying error messages or informative messages on the
   * wizard page. Depending on the provided user input, the wizard page is marked as being complete or not.
   * <p>
   * If some error or missing input is detected in the user input, an error message or informative message,
   * respectively, is displayed to the user. If the user input is complete and correct, the wizard page is marked as
   * begin complete to allow the wizard to proceed. To that end, the following conditions must be met:
   * <ul>
   * <li>The user must have provided a group ID.</li>
   * <li>The user must have provided an artifact ID.</li>
   * <li>The user must have provided a version for the artifact.</li>
   * <li>The user must have provided the packaging type for the artifact.</li>
   * </ul>
   * </p>
   * 
   * @see org.eclipse.jface.dialogs.DialogPage#setMessage(java.lang.String)
   * @see org.eclipse.jface.wizard.WizardPage#setErrorMessage(java.lang.String)
   * @see org.eclipse.jface.wizard.WizardPage#setPageComplete(boolean)
   */
  void validate() {
    String error = validateInput();
    setErrorMessage(error);
    setPageComplete(error == null);
  }

  private String validateInput() {
    String error = validateGroupIdInput(artifactComponent.getGroupId().trim());
    if(error != null) {
      return error;
    }

    error = validateArtifactIdInput(artifactComponent.getArtifactId().trim()); 
    if(error != null) {
      return error;
    }

    if(artifactComponent.getVersion().trim().length() == 0) {
      return Messages.wizardProjectPageMaven2ValidatorVersion;
    }

    if(artifactComponent.getPackaging().trim().length() == 0) {
      return Messages.wizardProjectPageMaven2ValidatorPackaging;
    }

    // if the parent project is specified, all three fields must be present
    if(!parentComponent.validate()) {
      return Messages.wizardProjectPageMaven2ValidatorParent;
    }

    // validate project name
    IStatus nameStatus = getImportConfiguration().validateProjectName(artifactComponent.getModel());
    if(!nameStatus.isOK()) {
      return nameStatus.getMessage();
    }

    return null;
  }

  /**
   * Updates the properties when a project name is set on the first page of the wizard.
   */
  public void setProjectName(String projectName) {
    if(artifactComponent.getArtifactId().equals(artifactComponent.getGroupId())) {
      artifactComponent.setGroupId(projectName);
    }
    artifactComponent.setArtifactId(projectName);
    validate();
  }

  /** Sets the readonly parent flag. */
  public void setParentReadonly(boolean b) {
    readonlyParent = b;
  }

  /**
   * Updates the properties when a project name is set on the first page of the wizard.
   */
  public void setParentProject(String groupId, String artifactId, String version) {
    parentComponent.setValues(groupId, artifactId, version);
    artifactComponent.setGroupId(groupId);
    artifactComponent.setVersion(version);
    validate();
  }

  public void setUsed(boolean isUsed) {
    this.isUsed = isUsed;
  }

  public boolean isPageComplete() {
    return !isUsed || super.isPageComplete();
  }

  /**
   * A placeholder representing a directory in the project structure.
   */
  final static class ProjectFolder {

    /** Folder path */
    private String path = null;

    /** Output path */
    private String outputPath = null;

    ProjectFolder(String path, String outputPath) {
      this.path = path;
      this.outputPath = outputPath;
    }

    /**
     * Returns folder path
     */
    String getPath() {
      return path;
    }

    /**
     * Returns folder output path
     */
    String getOutputPath() {
      return outputPath;
    }

    /**
     * Returns true for source folder
     */
    boolean isSourceEntry() {
      return this.getOutputPath() != null;
    }

  }

}

Back to the top