Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0bd5dd5e73587bea888c13e0ea8ba19201264e9b (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*******************************************************************************
 * 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.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.osgi.util.NLS;
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.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import org.apache.maven.project.MavenProject;

import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.ArtifactKey;
import org.eclipse.m2e.core.embedder.IMaven;
import org.eclipse.m2e.core.internal.index.IndexedArtifactFile;
import org.eclipse.m2e.core.ui.internal.Messages;
import org.eclipse.m2e.core.ui.internal.actions.SelectionUtil;


/**
 * Wizard page to enter parameters required for artifact installation.
 * 
 * @author Guillaume Sauthier
 * @author Mike Haller
 * @author Eugene Kuleshov
 */
public class MavenInstallFileArtifactWizardPage extends WizardPage {
  private static final Logger log = LoggerFactory.getLogger(MavenInstallFileArtifactWizardPage.class);

  Text artifactFileNameText;
  Text pomFileNameText;

  private Combo groupIdCombo;
  private Combo artifactIdCombo;
  private Combo versionCombo;
  private Combo packagingCombo;
  private Combo classifierCombo;

  Button createChecksumButton;
  Button generatePomButton;

  private final IFile file;

  public MavenInstallFileArtifactWizardPage(IFile file) {
    super("mavenInstallFileWizardPage");
    this.file = file;
    this.setTitle(Messages.MavenInstallFileArtifactWizardPage_title);
    this.setDescription(Messages.MavenInstallFileArtifactWizardPage_desc);
  }

  public void createControl(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    container.setLayout(new GridLayout(3, false));
    container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

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

    Label artifactFileNameLabel = new Label(container, SWT.NONE);
    artifactFileNameLabel.setText(Messages.MavenInstallFileArtifactWizardPage_lblFileName);
    
    artifactFileNameText = new Text(container, SWT.BORDER);
    artifactFileNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    artifactFileNameText.setData("name", "artifactFileNametext"); //$NON-NLS-1$ //$NON-NLS-2$
    artifactFileNameText.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        updateFileName(getArtifactFileName());
        pageChanged();
      }
    });
    
    final Button artifactFileNameButton = new Button(container, SWT.NONE);
    artifactFileNameButton.setLayoutData(new GridData());
    artifactFileNameButton.setData("name", "externalPomFileButton"); //$NON-NLS-1$ //$NON-NLS-2$
    artifactFileNameButton.setText(Messages.MavenInstallFileArtifactWizardPage_btnFilename);
    artifactFileNameButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        FileDialog fileDialog = new FileDialog(artifactFileNameButton.getShell());
        fileDialog.setText(Messages.MavenInstallFileArtifactWizardPage_file_title);
        fileDialog.setFileName(artifactFileNameText.getText());
        String name = fileDialog.open();
        if(name!=null) {
          updateFileName(name);
        }
      }
    });

    Label pomFileNameLabel = new Label(container, SWT.NONE);
    pomFileNameLabel.setText(Messages.MavenInstallFileArtifactWizardPage_lblPom);

    pomFileNameText = new Text(container, SWT.BORDER);
    pomFileNameText.setData("name", "pomFileNameText"); //$NON-NLS-1$ //$NON-NLS-2$
    pomFileNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    pomFileNameText.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        generatePomButton.setSelection(getPomFileName().length()==0);
        pageChanged();
      }
    });

    final Button pomFileNameButton = new Button(container, SWT.NONE);
    pomFileNameButton.setLayoutData(new GridData());
    pomFileNameButton.setData("name", "externalPomFileButton"); //$NON-NLS-1$ //$NON-NLS-2$
    pomFileNameButton.setText(Messages.MavenInstallFileArtifactWizardPage_btnPom);
    pomFileNameButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        FileDialog fileDialog = new FileDialog(pomFileNameButton.getShell());
        fileDialog.setText(Messages.MavenInstallFileArtifactWizardPage_file_title);
        fileDialog.setFileName(pomFileNameText.getText());
        String res = fileDialog.open();
        if(res!=null) {
          updatePOMFileName(res);
        }
      }
    });
    
    new Label(container, SWT.NONE);

    generatePomButton = new Button(container, SWT.CHECK);
    generatePomButton.setData("name", "generatePomButton"); //$NON-NLS-1$ //$NON-NLS-2$
    generatePomButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
    generatePomButton.setText(Messages.MavenInstallFileArtifactWizardPage_btnGenerate);
    generatePomButton.setSelection(true);
    new Label(container, SWT.NONE);

    createChecksumButton = new Button(container, SWT.CHECK);
    createChecksumButton.setData("name", "createChecksumButton"); //$NON-NLS-1$ //$NON-NLS-2$
    createChecksumButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
    createChecksumButton.setText(Messages.MavenInstallFileArtifactWizardPage_btnChecksum);
    createChecksumButton.setSelection(true);

    Label separator = new Label(container, SWT.HORIZONTAL | SWT.SEPARATOR);
    GridData separatorData = new GridData(SWT.FILL, SWT.CENTER, false, false, 3, 1);
    separatorData.verticalIndent = 5;
    separator.setLayoutData(separatorData);

    Label groupIdlabel = new Label(container, SWT.NONE);
    groupIdlabel.setText(Messages.MavenInstallFileArtifactWizardPage_lblgroupid);

    groupIdCombo = new Combo(container, SWT.NONE);
    groupIdCombo.setData("name", "groupIdCombo"); //$NON-NLS-1$ //$NON-NLS-2$
    groupIdCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    groupIdCombo.addModifyListener(modifyingListener);
    new Label(container, SWT.NONE);

    Label artifactIdLabel = new Label(container, SWT.NONE);
    artifactIdLabel.setText(Messages.MavenInstallFileArtifactWizardPage_lblArtifact);

    artifactIdCombo = new Combo(container, SWT.NONE);
    artifactIdCombo.setData("name", "artifactIdCombo"); //$NON-NLS-1$ //$NON-NLS-2$
    artifactIdCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
    artifactIdCombo.addModifyListener(modifyingListener);
    new Label(container, SWT.NONE);

    Label versionLabel = new Label(container, SWT.NONE);
    versionLabel.setText(Messages.MavenInstallFileArtifactWizardPage_lblVersion);

    versionCombo = new Combo(container, SWT.NONE);
    versionCombo.setData("name", "versionCombo"); //$NON-NLS-1$ //$NON-NLS-2$
    versionCombo.setText(MavenArtifactComponent.DEFAULT_VERSION);
    GridData versionComboData = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
    versionComboData.widthHint = 150;
    versionCombo.setLayoutData(versionComboData);
    versionCombo.addModifyListener(modifyingListener);

    Label packagingLabel = new Label(container, SWT.NONE);
    packagingLabel.setText(Messages.MavenInstallFileArtifactWizardPage_lblPackaging);

    packagingCombo = new Combo(container, SWT.NONE);
    packagingCombo.setData("name", "packagingCombo"); //$NON-NLS-1$ //$NON-NLS-2$
    packagingCombo.setText(MavenArtifactComponent.DEFAULT_PACKAGING);
    packagingCombo.setItems(MavenArtifactComponent.PACKAGING_OPTIONS);
    GridData packagingComboData = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
    packagingComboData.widthHint = 150;
    packagingCombo.setLayoutData(packagingComboData);
    packagingCombo.addModifyListener(modifyingListener);
    
    Label classifierLabel = new Label(container, SWT.NONE);
    classifierLabel.setText(Messages.MavenInstallFileArtifactWizardPage_lblClassifier);
    
    classifierCombo = new Combo(container, SWT.NONE);
    classifierCombo.setData("name", "classifierText"); //$NON-NLS-1$ //$NON-NLS-2$
    classifierCombo.setItems(new String[] {"sources", "javadoc"}); //$NON-NLS-1$ //$NON-NLS-2$
    GridData classifierTextData = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
    classifierTextData.widthHint = 150;
    classifierCombo.setLayoutData(classifierTextData);
    classifierCombo.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
        generatePomButton.setSelection(getClassifier().length()==0);  
      }
    });

    if(file != null) {
      updateFileName(file.getLocation().toOSString());
    }
    
    setControl(container);
  }

  void updateFileName(String fileName) {
    if(!getArtifactFileName().equals(fileName)) {
      artifactFileNameText.setText(fileName);
    }
    
    File file = new File(fileName);
    if(!file.exists() || !file.isFile()) {
      return;
    }

    if(fileName.endsWith(".jar") || fileName.endsWith(".war") || fileName.endsWith(".ear")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      packagingCombo.setText(fileName.substring(fileName.length()-3));
    }

    int n = fileName.lastIndexOf('.');
    if(n>-1) {
      String pomFileName = fileName.substring(0, n) + ".pom"; //$NON-NLS-1$
      if(new File(pomFileName).exists()) {
        pomFileNameText.setText(pomFileName);
      }
    } else {
      pomFileNameText.setText(""); //$NON-NLS-1$
    }
    
    try {
      IndexedArtifactFile iaf = MavenPlugin.getIndexManager().getAllIndexes().identify(file);
      if(iaf!=null) {
        groupIdCombo.setText(iaf.group);
        artifactIdCombo.setText(iaf.artifact);
        versionCombo.setText(iaf.version);
        if(iaf.classifier!=null) {
          classifierCombo.setText(iaf.classifier);
        }
        
        String name = iaf.group + ":" + iaf.artifact + "-" + iaf.version // //$NON-NLS-1$ //$NON-NLS-2$
            + (iaf.classifier == null ? "" : iaf.classifier); //$NON-NLS-1$
        setMessage(NLS.bind(Messages.MavenInstallFileArtifactWizardPage_message, name), WARNING);
        return;
      }
    } catch(CoreException ex) {
      log.error(ex.getMessage(), ex);
    }

    if(n>-1) {
      String pomFileName = fileName.substring(0, n) + ".pom"; //$NON-NLS-1$
      if(new File(pomFileName).exists()) {
        pomFileNameText.setText(pomFileName);
        readPOMFile(pomFileName);
      }
    }
    
    ArtifactKey artifactKey = SelectionUtil.getType(file, ArtifactKey.class);
    if(artifactKey!=null) {
      groupIdCombo.setText(artifactKey.getGroupId());
      artifactIdCombo.setText(artifactKey.getArtifactId());
      versionCombo.setText(artifactKey.getVersion());
      if(artifactKey.getClassifier()!=null) {
        classifierCombo.setText(artifactKey.getClassifier());
      }
    }
  }
  
  private void updatePOMFileName(String fileName){
    if(!getPomFileName().equals(fileName))
      pomFileNameText.setText(fileName);
      
    File file = new File(fileName);
    if(!file.exists() || !file.isFile() || !fileName.endsWith(".pom")) { //$NON-NLS-1$
      return;
    }
    
    readPOMFile(fileName);
  }
  
  private void readPOMFile(String fileName){
    try {
      IMaven maven = MavenPlugin.getMaven();
      MavenProject mavenProject = maven.readProject(new File(fileName), null);

      groupIdCombo.setText(mavenProject.getGroupId());
      artifactIdCombo.setText(mavenProject.getArtifactId());
      versionCombo.setText(mavenProject.getVersion());
      packagingCombo.setText(mavenProject.getPackaging());
      return;

    } catch(CoreException ex) {
      log.error(ex.getMessage(), ex);
    }
  }

  void pageChanged() {
    String artifactFileName = getArtifactFileName();
    if(artifactFileName.length() == 0) {
      updateStatus(Messages.MavenInstallFileArtifactWizardPage_error_no_name);
      return;
    }

    File file = new File(artifactFileName);
    if(!file.exists() || !file.isFile()) {
      updateStatus(Messages.MavenInstallFileArtifactWizardPage_error_missing);
      return;
    }
    
    String pomFileName = getPomFileName();
    if(pomFileName.length()>0) {
      if(!new File(pomFileName).exists()) {
        updateStatus(Messages.MavenInstallFileArtifactWizardPage_error_missingpom);
        return;
      }
    }

    if(getGroupId().length() == 0) {
      updateStatus(Messages.MavenInstallFileArtifactWizardPage_error_groupid);
      return;
    }

    if(getArtifactId().length() == 0) {
      updateStatus(Messages.MavenInstallFileArtifactWizardPage_error_artifactid);
      return;
    }

    if(getVersion().length() == 0) {
      updateStatus(Messages.MavenInstallFileArtifactWizardPage_error_version);
      return;
    }

    if(getPackaging().length() == 0) {
      updateStatus(Messages.MavenInstallFileArtifactWizardPage_error_packaging);
      return;
    }

    updateStatus(null);
  }

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

  public String getArtifactFileName() {
    return artifactFileNameText.getText().trim();
  }

  public String getPomFileName() {
    return pomFileNameText.getText().trim();
  }

  public String getGroupId() {
    return groupIdCombo.getText().trim();
  }

  public String getArtifactId() {
    return artifactIdCombo.getText().trim();
  }

  public String getVersion() {
    return versionCombo.getText().trim();
  }

  public String getPackaging() {
    return packagingCombo.getText().trim();
  }

  public String getClassifier() {
    return this.classifierCombo.getText().trim();
  }

  public boolean isGeneratePom() {
    return generatePomButton.getSelection();
  }

  public boolean isCreateChecksum() {
    return createChecksumButton.getSelection();
  }

}

Back to the top