/*
* 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.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.actions.SelectionProviderAction;
import org.sonarlint.eclipse.core.internal.TriggerType;
import org.sonarlint.eclipse.core.internal.jobs.ServerUpdateJob;
import org.sonarlint.eclipse.core.internal.server.IServer;
import org.sonarlint.eclipse.ui.internal.Messages;
import org.sonarlint.eclipse.ui.internal.SonarLintImages;
public class ServerUpdateAction extends SelectionProviderAction {
private List<IServer> servers;
public ServerUpdateAction(ISelectionProvider selectionProvider) {
super(selectionProvider, Messages.actionUpdate);
setImageDescriptor(SonarLintImages.UPDATE_IMG);
}
@Override
public void selectionChanged(IStructuredSelection sel) {
if (sel.isEmpty()) {
setEnabled(false);
return;
}
servers = new ArrayList<>();
boolean enabled = false;
Iterator iterator = sel.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
if (obj instanceof IServer) {
IServer server = (IServer) obj;
servers.add(server);
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 (servers == null) {
IStructuredSelection sel = getStructuredSelection();
if (sel != null) {
selectionChanged(sel);
}
}
if (servers != null) {
for (final IServer server : servers) {
Job job = new ServerUpdateJob(server);
JobUtils.scheduleAnalysisOfOpenFilesInBoundProjects(job, server, TriggerType.BINDING_CHANGE);
job.schedule();
}
}
}
}