Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8e218495df89669b1b96ec6baa68d7c28bb20fce (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
/*******************************************************************************
 * Copyright (c) 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.ui.internal.launch;

import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
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.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.dialogs.PreferencesUtil;

import org.eclipse.m2e.actions.MavenLaunchConstants;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.MavenRuntime;
import org.eclipse.m2e.core.embedder.MavenRuntimeManager;


/**
 * @since 1.4
 */
public class MavenRuntimeSelector extends Composite {

  ComboViewer runtimeComboViewer;

  private MavenRuntimeManager runtimeManager;

  public MavenRuntimeSelector(final Composite mainComposite) {
    super(mainComposite, SWT.NONE);

    GridLayout gridLayout = new GridLayout(3, false);
    gridLayout.marginWidth = 0;
    gridLayout.marginHeight = 0;
    setLayout(gridLayout);

    Label mavenRuntimeLabel = new Label(this, SWT.NONE);
    mavenRuntimeLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
    mavenRuntimeLabel.setText(org.eclipse.m2e.internal.launch.Messages.MavenLaunchMainTab_lblRuntime);

    runtimeComboViewer = new ComboViewer(this, SWT.BORDER | SWT.READ_ONLY);
    runtimeComboViewer.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    runtimeComboViewer.setContentProvider(new IStructuredContentProvider() {

      public Object[] getElements(Object input) {
        return ((List<?>) input).toArray();
      }

      public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
      }

      public void dispose() {
      }

    });

    try {
      runtimeManager = MavenPlugin.getMavenRuntimeManager();
      runtimeComboViewer.setInput(runtimeManager.getMavenRuntimes());
      runtimeComboViewer.setSelection(new StructuredSelection(runtimeManager.getDefaultRuntime()));
    } catch(NullPointerException e) {
      // ignore, this only happens inside windowbuilder
    }

    Button configureRuntimesButton = new Button(this, SWT.NONE);
    configureRuntimesButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
    configureRuntimesButton.setText(org.eclipse.m2e.internal.launch.Messages.MavenLaunchMainTab_btnConfigure);
    configureRuntimesButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        PreferencesUtil.createPreferenceDialogOn(mainComposite.getShell(),
            "org.eclipse.m2e.core.preferences.MavenInstallationsPreferencePage", null, null).open(); //$NON-NLS-1$
        MavenRuntimeManager runtimeManager = MavenPlugin.getMavenRuntimeManager();
        runtimeComboViewer.setInput(runtimeManager.getMavenRuntimes());
        runtimeComboViewer.setSelection(new StructuredSelection(runtimeManager.getDefaultRuntime()));
      }
    });
  }

  public void setSelectRuntime(MavenRuntime runtime) {
    this.runtimeComboViewer.setSelection(new StructuredSelection(runtime));
  }

  public MavenRuntime getSelectedRuntime() {
    IStructuredSelection selection = (IStructuredSelection) runtimeComboViewer.getSelection();
    return (MavenRuntime) selection.getFirstElement();
  }

  public void addSelectionChangedListener(ISelectionChangedListener listener) {
    runtimeComboViewer.addSelectionChangedListener(listener);
  }

  public void initializeFrom(ILaunchConfiguration configuration) {
    String location = "";
    try {
      location = configuration.getAttribute(MavenLaunchConstants.ATTR_RUNTIME, ""); //$NON-NLS-1$
    } catch(CoreException ex) {
      // TODO log
    }
    MavenRuntime runtime = runtimeManager.getRuntime(location);
    if(runtime != null) {
      setSelectRuntime(runtime);
    }
  }

  public void performApply(ILaunchConfigurationWorkingCopy configuration) {
    MavenRuntime runtime = getSelectedRuntime();
    configuration.setAttribute(MavenLaunchConstants.ATTR_RUNTIME, runtime.getLocation());
  }
}

Back to the top