Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 00c347582248ef833cadc3a85f8790946f30c946 (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
/*******************************************************************************
 * Copyright (c) 2013 Igor Fedorenko
 * 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:
 *      Igor Fedorenko - initial API and implementation
 *******************************************************************************/

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

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.TitleAreaDialog;
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.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.ui.internal.Messages;
import org.eclipse.m2e.core.ui.internal.WorkingSets;
import org.eclipse.m2e.core.ui.internal.components.NestedProjectsComposite;


/**
 * @since 1.5
 */
@SuppressWarnings("restriction")
public class AssignWorkingSetDialog extends TitleAreaDialog {

  private final IProject[] initialSelection;

  NestedProjectsComposite selectedProjects;

  Set<IProject> allWorkingSetProjects = new HashSet<IProject>(WorkingSets.getProjects());

  Combo workingSetCombo;

  String workingSetName;

  public AssignWorkingSetDialog(Shell parentShell, IProject[] initialSelection) {
    super(parentShell);
    this.initialSelection = initialSelection;
  }

  protected Control createDialogArea(Composite parent) {
    setTitle(Messages.AssignWorkingSetDialog_title);

    Composite area = (Composite) super.createDialogArea(parent);

    Composite composite = new Composite(area, SWT.NONE);
    composite.setLayout(new GridLayout(3, false));
    composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    Composite filtersComposite = new Composite(composite, SWT.NONE);
    filtersComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 3, 1));
    GridLayout gl_filtersComposite = new GridLayout(4, false);
    gl_filtersComposite.verticalSpacing = 0;
    gl_filtersComposite.marginWidth = 0;
    gl_filtersComposite.marginHeight = 0;
    filtersComposite.setLayout(gl_filtersComposite);

    final Button btnFilterAssignedProjects = new Button(filtersComposite, SWT.CHECK);
    btnFilterAssignedProjects.setText(Messages.AssignWorkingSetDialog_btnFilterAssignedProjects_text);
    btnFilterAssignedProjects.setSelection(true);
    btnFilterAssignedProjects.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        selectedProjects.refresh();
      }
    });

    final Button btnFilterClosedProjects = new Button(filtersComposite, SWT.CHECK);
    btnFilterClosedProjects.setText(Messages.AssignWorkingSetDialog_btnFilterClosedProjects_text);
    btnFilterClosedProjects.setSelection(true);
    btnFilterClosedProjects.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        selectedProjects.refresh();
      }
    });

    this.selectedProjects = new NestedProjectsComposite(composite, SWT.NONE, initialSelection, false) {
      @Override
      protected boolean isInteresting(IProject project) throws CoreException {
        if(btnFilterClosedProjects.getSelection() && !project.isAccessible()) {
          return false;
        }
        if(project.isAccessible() && !project.hasNature(IMavenConstants.NATURE_ID)) {
          // project.hasNature throws an exception for inaccessible projects
          return false;
        }
        return !btnFilterAssignedProjects.getSelection() || !allWorkingSetProjects.contains(project);
      }
    };
    selectedProjects.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));

    Composite workingSetComposite = new Composite(composite, SWT.NONE);
    workingSetComposite.setLayout(new GridLayout(3, false));
    workingSetComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 3, 1));

    Label lblNewLabel = new Label(workingSetComposite, SWT.NONE);
    lblNewLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
    lblNewLabel.setText(Messages.AssignWorkingSetDialog_lblWorkingSet);

    workingSetCombo = new Combo(workingSetComposite, SWT.BORDER);
    workingSetCombo.addModifyListener(e -> workingSetName = workingSetCombo.getText());
    GridData gd_workingSetName = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
    gd_workingSetName.horizontalIndent = 10;
    workingSetCombo.setLayoutData(gd_workingSetName);
    workingSetCombo.setItems(WorkingSets.getWorkingSets());

    selectedProjects.addSelectionChangeListener(event -> {
      IProject selection = selectedProjects.getSelection();
      if(selection != null && workingSetCombo.getSelectionIndex() < 0) {
        workingSetCombo.setText(selection.getName());
      }
    });

    Button btnAssign = new Button(workingSetComposite, SWT.NONE);
    btnAssign.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
    btnAssign.setText(Messages.AssignWorkingSetDialog_btnAssign_text);
    btnAssign.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        assignWorkingSets();
        selectedProjects.reset();
      }
    });

    return area;
  }

  public void assignWorkingSets() {
    IProject[] projects = selectedProjects.getSelectedProjects();
    if(projects != null && projects.length > 0 && workingSetName != null && !workingSetName.isEmpty()) {
      WorkingSets.addToWorkingSet(projects, workingSetName);
      allWorkingSetProjects.addAll(Arrays.asList(projects));
    }
  }
}

Back to the top