/* * SonarLint for Eclipse * Copyright (C) 2015-2017 SonarSource SA * sonarlint@sonarsource.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package org.sonarlint.eclipse.ui.internal.server.actions; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.actions.SelectionProviderAction; import org.sonarlint.eclipse.core.resource.ISonarLintProject; import org.sonarlint.eclipse.ui.internal.Messages; import org.sonarlint.eclipse.ui.internal.SonarLintImages; import org.sonarlint.eclipse.ui.internal.bind.BindProjectsWizard; public class ProjectChangeBindingAction extends SelectionProviderAction { private final Shell shell; private List<ISonarLintProject> selectedProjects; public ProjectChangeBindingAction(Shell shell, ISelectionProvider selectionProvider) { super(selectionProvider, Messages.actionChangeBinding); this.shell = shell; setImageDescriptor(SonarLintImages.SYNCED_IMG); } @Override public void selectionChanged(IStructuredSelection sel) { if (sel.isEmpty()) { setEnabled(false); return; } selectedProjects = new ArrayList<>(); boolean enabled = false; Iterator iterator = sel.iterator(); while (iterator.hasNext()) { Object obj = iterator.next(); if (obj instanceof ISonarLintProject) { ISonarLintProject project = (ISonarLintProject) obj; selectedProjects.add(project); enabled = true; } else { setEnabled(false); return; } } setEnabled(enabled); } @Override public void run() { // It is possible that the server is created and added to the server view on workbench // startup. As a result, when the user switches to the server view, the server is // selected, but the selectionChanged event is not called, which results in servers // being null. When servers is null the server will not be deleted and the error log // will have an IllegalArgumentException. // // To handle the case where servers is null, the selectionChanged method is called // to ensure servers will be populated. if (selectedProjects == null) { IStructuredSelection sel = getStructuredSelection(); if (sel != null) { selectionChanged(sel); } } if (selectedProjects != null) { BindProjectsWizard wizard = new BindProjectsWizard(selectedProjects); final WizardDialog dialog = new WizardDialog(shell, wizard); dialog.setHelpAvailable(true); dialog.open(); } } }