Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3c14363ba1d3e8361ab7e014807c34b848c40ca5 (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
/*******************************************************************************
 * 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 org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;

import org.apache.maven.model.Model;

import org.eclipse.m2e.core.ui.internal.Messages;


/**
 * Wizard page used to specify basic POM parameters
 */
public class MavenPomWizardPage extends AbstractMavenWizardPage {
  private Text projectText;

  private ISelection selection;

  private MavenArtifactComponent pomComponent;

  public MavenPomWizardPage(ISelection selection) {
    super("wizardPage"); //$NON-NLS-1$
    setTitle(Messages.MavenPomWizardPage_title);
    setDescription(Messages.MavenPomWizardPage_desc);
    this.selection = selection;
  }

  public void createControl(Composite parent) {
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    layout.makeColumnsEqualWidth = false;

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

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

    Label label = new Label(container, SWT.NULL);
    label.setText(Messages.MavenPomWizardPage_lblProject);

    projectText = new Text(container, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
    projectText.setEditable(false);
    projectText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    projectText.addModifyListener(modifyingListener);

    pomComponent = new MavenArtifactComponent(container, SWT.NONE);
    pomComponent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
    pomComponent.setModifyingListener(modifyingListener);
    addFieldWithHistory("groupId", pomComponent.getGroupIdCombo()); //$NON-NLS-1$
    addFieldWithHistory("artifactId", pomComponent.getArtifactIdCombo()); //$NON-NLS-1$
    addFieldWithHistory("version", pomComponent.getVersionCombo()); //$NON-NLS-1$
    addFieldWithHistory("name", pomComponent.getNameCombo()); //$NON-NLS-1$

    initialize();
    dialogChanged();
    setControl(container);
  }

  /**
   * Tests if the current workbench selection is a suitable container to use.
   */
  private void initialize() {
    if(selection != null && !selection.isEmpty() && selection instanceof IStructuredSelection) {
      IStructuredSelection ssel = (IStructuredSelection) selection;
      if(ssel.size() > 1) {
        return;
      }
      Object obj = ssel.getFirstElement();
      if(obj instanceof IResource) {
        IContainer container;
        if(obj instanceof IContainer) {
          container = (IContainer) obj;
        } else {
          container = ((IResource) obj).getParent();
        }
        projectText.setText(container.getFullPath().toString());
        pomComponent.setArtifactId(container.getName());
        pomComponent.setGroupId(container.getName());
      }
    }

    pomComponent.setVersion(MavenArtifactComponent.DEFAULT_VERSION);
    pomComponent.setPackaging(MavenArtifactComponent.DEFAULT_PACKAGING);
    pomComponent.setFocus();
  }

  /**
   * Uses the standard container selection dialog to choose the new value for the container field.
   */
  void handleBrowse() {
    ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(),
        ResourcesPlugin.getWorkspace().getRoot(), false, Messages.MavenPomWizardPage_dialog_title);
    dialog.showClosedProjects(false);
    if(dialog.open() == Window.OK) {
      Object[] result = dialog.getResult();
      if(result.length == 1) {
        projectText.setText(((Path) result[0]).toString());
      }
    }

//    IJavaModel javaModel = JavaCore.create();
//
//    IJavaProject[] projects;
//    try {
//      projects = javaModel.getJavaProjects();
//    } catch(JavaModelException e) {
//      log.error(e.getMessage(), e);
//      projects = new IJavaProject[0];
//    }
//
//    ILabelProvider labelProvider = new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
//    ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), labelProvider);
//    dialog.setTitle("Select Project");
//    dialog.setMessage("Choose project where POM will be created");
//    dialog.setElements(projects);
//
//    String projectName = getProject();
//    if(projectName != null && projectName.length() > 0) {
//      IJavaProject javaProject = javaModel.getJavaProject(projectName);
//      if(javaProject != null) {
//        dialog.setInitialSelections(new Object[] {javaProject});
//      }
//    }
//
//    if(dialog.open() == Window.OK) {
//      projectText.setText(((IJavaProject) dialog.getFirstResult()).getProject().getFullPath().toString());
//    }
  }

  /**
   * Ensures that both text fields are set.
   */
  void dialogChanged() {
    IResource container = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getProject()));

    if(getProject().length() == 0) {
      updateStatus(Messages.MavenPomWizardPage_error_folder);
      return;
    }
    if(container == null || (container.getType() & IResource.FOLDER | IResource.PROJECT) == 0) {
      updateStatus(Messages.MavenPomWizardPage_error_folder2);
      return;
    }
    if(!container.isAccessible()) {
      updateStatus(Messages.MavenPomWizardPage_error_folder_write);
      return;
    }

    // TODO
    if(pomComponent.getGroupId().length() == 0) {
      updateStatus(Messages.MavenPomWizardPage_error_grid);
    }

    if(pomComponent.getArtifactId().length() == 0) {
      updateStatus(Messages.MavenPomWizardPage_error_artid);
    }

    if(pomComponent.getVersion().length() == 0) {
      updateStatus(Messages.MavenPomWizardPage_error_version);
    }

    if(pomComponent.getPackaging().length() == 0) {
      updateStatus(Messages.MavenPomWizardPage_error_pack);
    }

    updateStatus(null);
  }

  private void updateStatus(String message) {
    setErrorMessage(message);
    setPageComplete(message == null);
  }

  public String getProject() {
    return projectText.getText();
  }

  public Model getModel() {
    return pomComponent.getModel();
  }

}

Back to the top