<commit_before>/*******************************************************************************
 * Copyright (c) 2013, 2014 Pivotal Software, Inc. and others.
 * All rights reserved. This program and the accompanying materials are made 
 * available under the terms of the Eclipse Public License v1.0 
 * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 
 *
 * Contributors:
 *     Pivotal Software, Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.flux.ui.integration.handlers;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.flux.core.CallbackIDAwareMessageHandler;
import org.eclipse.flux.core.IMessagingConnector;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author <NAME>
 */
public class SyncDownloadSelectionDialog extends ElementListSelectionDialog {

	private final IMessagingConnector messagingConnector;
	private final String username;

	public SyncDownloadSelectionDialog(final Shell parent, final ILabelProvider renderer, final IMessagingConnector messagingConnector, final String username) {
		super(parent, renderer);
		this.messagingConnector = messagingConnector;
		this.username = username;

		this.setMultipleSelection(true);
		this.setAllowDuplicates(false);
		this.setTitle("Import Synced Projects...");
	}
	
	@Override
	public int open() {
		try {
			int callbackID = this.hashCode();
			
			CallbackIDAwareMessageHandler responseHandler = new CallbackIDAwareMessageHandler("getProjectsResponse", callbackID) {
				@Override
				public void handleMessage(String messageType, JSONObject response) {
					try {
						List<String> projectsNames = new ArrayList<String>();
						JSONArray projects = response.getJSONArray("projects");
						for (int i = 0; i < projects.length(); i++) {
							JSONObject project = projects.getJSONObject(i);
							String projectName = project.getString("name");

							projectsNames.add(projectName);
						}
						setElements((String[]) projectsNames.toArray(new String[projectsNames.size()]));
					}
					catch (Exception e) {
						e.printStackTrace();
					}
					
					messagingConnector.removeMessageHandler(this);
				}
			};
			
			this.messagingConnector.addMessageHandler(responseHandler);
			
			JSONObject message = new JSONObject();
			message.put("callback_id", callbackID);
			message.put("username", username);
			this.messagingConnector.send("getProjectsRequest", message);
		} catch (JSONException e1) {
			e1.printStackTrace();
		}		
		
		return super.open();
	}

}<commit_msg>fixed issues with download dialog in Eclipse plugin, now deals with multiple concurrent messages correctly<commit_after>/*******************************************************************************
 * Copyright (c) 2013, 2014 Pivotal Software, Inc. and others.
 * All rights reserved. This program and the accompanying materials are made 
 * available under the terms of the Eclipse Public License v1.0 
 * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 
 *
 * Contributors:
 *     Pivotal Software, Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.flux.ui.integration.handlers;

import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;

import org.eclipse.flux.core.CallbackIDAwareMessageHandler;
import org.eclipse.flux.core.IMessagingConnector;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author <NAME>
 */
public class SyncDownloadSelectionDialog extends ElementListSelectionDialog {

	private final IMessagingConnector messagingConnector;
	private final String username;
	private CallbackIDAwareMessageHandler responseHandler;
	private Set<String> projects;

	public SyncDownloadSelectionDialog(final Shell parent, final ILabelProvider renderer, final IMessagingConnector messagingConnector, final String username) {
		super(parent, renderer);
		this.messagingConnector = messagingConnector;
		this.username = username;

		this.setMultipleSelection(true);
		this.setAllowDuplicates(false);
		this.setTitle("Import Synced Projects...");
		
		this.projects = new ConcurrentSkipListSet<>();
	}
	
	@Override
	public int open() {
		try {
			int callbackID = this.hashCode();
			
			projects.clear();
			
			responseHandler = new CallbackIDAwareMessageHandler("getProjectsResponse", callbackID) {
				@Override
				public void handleMessage(String messageType, JSONObject response) {
					try {
						boolean newProjects = false;

						JSONArray projectsList = response.getJSONArray("projects");
						for (int i = 0; i < projectsList.length(); i++) {
							JSONObject project = projectsList.getJSONObject(i);
							String projectName = project.getString("name");

							newProjects |= projects.add(projectName);
						}

						if (newProjects) {
							Display.getDefault().asyncExec(new Runnable() {
								@Override
								public void run() {
									setListElements((String[]) projects.toArray(new String[projects.size()]));
								}
							});
						}
					}
					catch (Exception e) {
						e.printStackTrace();
					}
				}
			};
			
			this.messagingConnector.addMessageHandler(responseHandler);
			
			JSONObject message = new JSONObject();
			message.put("callback_id", callbackID);
			message.put("username", username);
			this.messagingConnector.send("getProjectsRequest", message);
		} catch (JSONException e1) {
			e1.printStackTrace();
		}		
		
		return super.open();
	}
	
	@Override
	public boolean close() {
		messagingConnector.removeMessageHandler(responseHandler);
		return super.close();
	}

}
{'fraction_non_alphanumeric': 0.15069154068832422, 'fraction_numerical': 0.006111289803795433, 'mean_word_length': 4.4504820333041195, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3757045', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1642, 'n_words': 389}