<commit_before>package com.github.sergueik.swet;

/**
 * Copyright 2014 - 2017 <NAME>
 */

import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import org.apache.log4j.Category;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.io.IOUtils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Rectangle;
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.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import org.mihalis.opal.breadcrumb.Breadcrumb;
import org.mihalis.opal.breadcrumb.BreadcrumbItem;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import org.passer.ChoiceItem;
import org.passer.ChoicesDialog;

import com.github.sergueik.swet.ExceptionDialogEx;
import com.github.sergueik.swet.Utils;

/**
 * Main form for Selenium WebDriver Elementor Tool (SWET)
 * @author: <NAME> (<EMAIL>)
 */

public class SimpleToolBarEx {

	private Shell shell;
	private WebDriver driver;
	private WebDriverWait wait;
	private int flexibleWait = 5;
	private int implicitWait = 1;
	private long pollingInterval = 500;
	private String baseURL = "about:blank";
	// private final String getCommand = "return document.swdpr_command ===
	// undefined ? '' : document.swdpr_command;";
	private List<String> stepKeys = new ArrayList<>();
	private Map<String, Map<String, String>> testData = new HashMap<>();
	private Map<String, Image> iconCache = new HashMap<>();
	private static final int IMAGE_SIZE = 32;
	private Configuration config = null;
	private SimpleToolBarEx app;
	@SuppressWarnings("deprecation")
	static final Category logger = Category.getInstance(SimpleToolBarEx.class);
	private static StringBuilder loggingSb = new StringBuilder();
	private static Formatter formatter = new Formatter(loggingSb, Locale.US);

	private static Map<String, Boolean> browserStatus = new HashMap<>();
	private static String configFilePath; // TODO: rename
	static Map<String, String> browserDefaults = new HashMap<>();
	static {
		browserDefaults.put("windows", "Chrome");
		browserDefaults.put("linux", "Firefox");
		browserDefaults.put("mac", "Safari");
	}
	// "Chrome" or better
	static String browserDefault = Utils.getPropertyEnv("browser.default",
			browserDefaults.get(Pattern.compile(" +").split(OSUtils.getOsName())[0]));
	private static Map<String, String> configData = new HashMap<>();
	static {
		configData.put("Browser", browserDefault);
		configData.put("Template", "Core Selenium Java (embedded)");
	}
	private static final String defaultConfig = String.format(
			"{ \"Browser\": \"%s\", \"Template\": \"%s\", }",
			configData.get("Browser"), configData.get("Template"));

	private static final int shellWidth = 768;
	private static final int shellHeight = 324;
	private static int step_index = 0;
	private static String osName = OSUtils.getOsName();
	private static Display display;
	private String generatedScript = null;
	private Label statusMessage;
	private TemplateCache templateCache = TemplateCache.getInstance();
	private Utils utils = Utils.getInstance();
	private ToolItem launchTool = null;
	private ToolItem saveTool = null;
	private ToolItem pageExploreTool = null;
	private static String defaultTemplateResourcePath = "templates/core_selenium_java.twig";

	private Breadcrumb bc;

	@SuppressWarnings("unused")
	public SimpleToolBarEx() {
		app = this;
		utils.initializeLogger();
		logger.info("Initialized logger.");
	}

	public void open(Display display) {
		testData = new HashMap<>(); // infer
		shell = new Shell(display, SWT.CENTER | SWT.SHELL_TRIM); // (~SWT.RESIZE)));
		Rectangle boundRect = new Rectangle(0, 0, shellWidth, shellHeight);
		shell.setBounds(boundRect);
		shell.setImage(new Image(display,
				utils.getResourceStream("images/document_wrench_color.ico")));
		try {

			iconCache.put("launch icon",
					resize(getImage(String.format("images/%s", launchImage)), IMAGE_SIZE,
							IMAGE_SIZE));
			iconCache.put("find icon",
					getImage(String.format("images/%s", findImage)));
			iconCache.put("prefs icon",
					getImage(String.format("images/%s", gearImage)));
			iconCache.put("shutdown icon",
					getImage(String.format("images/%s", quitImage)));
			iconCache.put("step icon",
					getImage(String.format("images/%s", "document_wrench_bw.png")));
			iconCache.put("codeGen icon",
					resize(getImage(String.format("images/%s", codeGenImage)), 32, 32));
			iconCache.put("open icon",
					getImage(String.format("images/%s", openImage)));<commit_msg>intermediate revision - close the browser when application is closed<commit_after>package com.github.sergueik.swet;

/**
 * Copyright 2014 - 2017 <NAME>
 */

import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import org.apache.log4j.Category;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.io.IOUtils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Rectangle;
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.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import org.mihalis.opal.breadcrumb.Breadcrumb;
import org.mihalis.opal.breadcrumb.BreadcrumbItem;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import org.passer.ChoiceItem;
import org.passer.ChoicesDialog;

import com.github.sergueik.swet.ExceptionDialogEx;
import com.github.sergueik.swet.Utils;

/**
 * Main form for Selenium WebDriver Elementor Tool (SWET)
 * @author: <NAME> (<EMAIL>)
 */

public class SimpleToolBarEx {

	private Shell shell;
	private WebDriver driver;
	private WebDriverWait wait;
	private int flexibleWait = 5;
	private int implicitWait = 1;
	private long pollingInterval = 500;
	private String baseURL = "about:blank";
	// private final String getCommand = "return document.swdpr_command ===
	// undefined ? '' : document.swdpr_command;";
	private List<String> stepKeys = new ArrayList<>();
	private Map<String, Map<String, String>> testData = new HashMap<>();
	private Map<String, Image> iconCache = new HashMap<>();
	private static final int IMAGE_SIZE = 32;
	private Configuration config = null;
	private SimpleToolBarEx app;
	@SuppressWarnings("deprecation")
	static final Category logger = Category.getInstance(SimpleToolBarEx.class);
	private static StringBuilder loggingSb = new StringBuilder();
	private static Formatter formatter = new Formatter(loggingSb, Locale.US);

	private static Map<String, Boolean> browserStatus = new HashMap<>();
	private static String configFilePath; // TODO: rename
	static Map<String, String> browserDefaults = new HashMap<>();
	static {
		browserDefaults.put("windows", "Chrome");
		browserDefaults.put("linux", "Firefox");
		browserDefaults.put("mac", "Safari");
	}
	// "Chrome" or better
	static String browserDefault = Utils.getPropertyEnv("browser.default",
			browserDefaults.get(Pattern.compile(" +").split(OSUtils.getOsName())[0]));
	private static Map<String, String> configData = new HashMap<>();
	static {
		configData.put("Browser", browserDefault);
		configData.put("Template", "Core Selenium Java (embedded)");
	}
	private static final String defaultConfig = String.format(
			"{ \"Browser\": \"%s\", \"Template\": \"%s\", }",
			configData.get("Browser"), configData.get("Template"));

	private static final int shellWidth = 768;
	private static final int shellHeight = 324;
	private static int step_index = 0;
	private static String osName = OSUtils.getOsName();
	private static Display display;
	private String generatedScript = null;
	private Label statusMessage;
	private TemplateCache templateCache = TemplateCache.getInstance();
	private Utils utils = Utils.getInstance();
	private ToolItem launchTool = null;
	private ToolItem saveTool = null;
	private ToolItem pageExploreTool = null;
	private static String defaultTemplateResourcePath = "templates/core_selenium_java.twig";

	private Breadcrumb bc;

	@SuppressWarnings("unused")
	public SimpleToolBarEx() {
		app = this;
		utils.initializeLogger();
		logger.info("Initialized logger.");
	}

	public void open(Display display) {
		testData = new HashMap<>(); // infer
		shell = new Shell(display, SWT.CENTER | SWT.SHELL_TRIM); // (~SWT.RESIZE)));

		shell.addListener(SWT.Close, new Listener() {
			public void handleEvent(Event event) {
				// signal the possibly running background threads watching the browser,
				// to stop
				if (browserStatus.containsKey("closed")) {
					browserStatus.replace("closed", true);
				} else {
					browserStatus.put("closed", true);
				}
				closeBrowser();
				// event.doit = false;
				shell.setVisible(false);
			}
		});

		Rectangle boundRect = new Rectangle(0, 0, shellWidth, shellHeight);
		shell.setBounds(boundRect);
		shell.setImage(new Image(display,
				utils.getResourceStream("images/document_wrench_color.ico")));
		try {

			iconCache.put("launch icon",
					resize(getImage(String.format("images/%s", launchImage)), IMAGE_SIZE,
							IMAGE_SIZE));
			iconCache.put("find icon",
					getImage(String.format("images/%s", findImage)));
			iconCache.put("prefs icon",
					getImage(String.format("images/%s", gearImage)));
			iconCache.put("shutdown icon",
					getImage(String.format("images/%s", quitImage)));
			iconCache.put("step icon",
					getImage(String.format("images/%s", "document_wrench_bw.png")));
			iconCache.put("codeGen icon",
					resize(getImage(String.format("images/%s", codeGenImage)), 32, 32));
			iconCache.put("open icon",
					getImage(String.format("images/%s", openImage)));
{'fraction_non_alphanumeric': 0.12668800249785342, 'fraction_numerical': 0.004683475138552806, 'mean_word_length': 5.364629905613512, 'pattern_counts': {'":': 4, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2650735', 'n_tokens_mistral': 4186, 'n_tokens_neox': 3899, 'n_words': 880}