<commit_before>package org.lunarproject.gw2route;

/**
 * Navi.java native OS browser wrapper frame for gw2timer.com website.
 * Based on SimpleWebBrowserExample.java and uses DJ Native Swing project by
 * <NAME>. Resizable frame from coderanch.com post by "<NAME>".
 * Various codes copied/adapted from Internet posting, mainly stackoverflow.com.
 * 
 * Libraries/APIs used:
 * DJNativeSwing for embedding browser in program.
 * Standard Widget Toolkit for displaying GUI with DJNativeSwing. 
 * ini4j for reading and writing user options from and to portable text files.
 * 
 * See variable declarations for program version.
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import org.ini4j.Ini;

import org.cef.CefApp;
import org.cef.CefApp.CefAppState;
import org.cef.CefClient;
import org.cef.CefSettings;
import org.cef.browser.CefBrowser;
import org.cef.handler.CefAppHandlerAdapter;


public class Navi extends JPanel {
	
	final static String PROGRAMNAME = "gw2route";
	final static String PROGRAMVERSION = "0.0.2b";
	final static String DIRECTORY_ICONS = "img/";
	final static String FILENAME_OPTIONS = "options.ini";
	final static String FILENAME_TRANSLATIONS = "translations.ini";
	final static String FILENAME_BOOKMARKS = "bookmarks.txt";
	final static String REPO_URI = "https://github.com/ffd114/gw2route";
	final static String UPDATE_URI = "https://github.com/ffd114/gw2route/releases";
	
	WindowPreset currentWindowPreset;
	int ICON_HEIGHT = 16;
	int ADD_VERTICAL_PIXELS;
	int ADD_HORIZONTAL_PIXELS;
	int BORDER_THICKNESS_TOTAL;
	int RESOLUTION_WIDTH;
	int RESOLUTION_HEIGHT;
	static Color COLOR_BAR_CURRENT;
	int OPACITY_LEVELS_10 = 10;
	float OPACITY_STEP = 0.10f;
	boolean isMiniaturized = false;
	boolean isGPSStarted = false;
	
	protected Translation TheTranslations;
	protected static Option TheOptions;
	protected Bookmark TheBookmarks;
	private final CefApp     _cefApp;
	private final CefClient  _client;
	private final CefBrowser _browser;
	private CefSettings _settings;
	
	private static ResizableFrame TheFrame; // Frame resizable edges
	private static JPanel TheContainer; // Frame's container
	private static JPanel TheBar; // The draggable top bar of the overlay that contains the menu
	private static CustomMenu TheMenu; // The menu and its submenus on the bar
	private static JPopupMenu ThePopup; // The context menu that pops up from the bar after right clicking
	private static ClassLoader TheClassLoader; // Helper class to load images
	private static GPS TheGPS; // GPS memory reading class
	
	public static Point mousePressedPoint;
	
	final static String TEXT_FILELOADWARNING = " was not found in program's directory!\n"
		+ "The file should've been packaged with the program when you downloaded it.";
	final static String TEXT_FILESAVEWARNING = " was unable to be saved!";
	final static String TEXT_ABOUT =
		PROGRAMNAME + " browser-overlay for Guild Wars 2 by GW2Timer.com\n"
		+ "Version: " + PROGRAMVERSION + " - Released: 2014.07.16 - Created: 2014.06.01\n"
		+ "Browser embed code part of SWT by Eclipse and DJ Native Swing by <NAME>.\n"
		+ "Uses icons from Crystal set by <NAME>.\n"
		+ "\n"
		+ "Shortcuts: Left click the \"G\" icon to miniaturize, right click it to minimize.\n"
		+ "Double click the overlay window bar to maximize. Move cursor over edges to resize.\n"
		+ "Press F5 in the browser to reload the website.\n"
		+ "\n"
		+ "Appearance: Right click the window bar to open the popup context menu.\n"
		+ "With the context menu visible, keypress (a letter then a number):\n"
		+ "S + # = load a Size preset.\n"
		+ "C + # = load a Color preset.\n"
		+ "# = load Opacity value.\n";

	public Navi() throws InterruptedException
	{
		super(new BorderLayout());
		// Load options and data first before doing anything
		loadStorage();

		// Initialize browser
		JPanel webBrowserPanel = new JPanel(new BorderLayout());
		webBrowserPanel.setBorder(BorderFactory.createEmptyBorder());

		// Add args to enable using flash
		String[] args = new String[] {"--enable-system-flash"};

		// Initialize cefbrowser
		CefApp.addAppHandler(new CefAppHandlerAdapter(args) {
			@Override
			public void stateHasChanged(org.cef.CefApp.CefAppState state) {
				// Shutdown the app if the native CEF part is terminated
				if (state == CefAppState.TERMINATED)
					System.exit(0);
			}
		});

		// Setup settings
		_settings = new CefSettings();

		// Enable cache
		_settings.cache_path = "cache";

		_cefApp = CefApp.getInstance(args, _settings);
		_client = _cefApp.createClient();
		_browser = _client.createBrowser(TheOptions.URL_HOMEPAGE, false, true);

		if (TheOptions.wantLastVisited)
		{
			_browser.loadURL(TheOptions.URL_LASTVISITED);
		}

		webBrowserPanel.add(_browser.getUIComponent(), BorderLayout.CENTER);
		add(webBrowserPanel, BorderLayout.CENTER);

		TheClassLoader = this.getClass().getClassLoader();

		// Browser frame initializations in separate functions to reduce clutter
		sumDimensions();
		createMenu();
		styleFrame();
		createPopup();
		bindFrameFocus();
		bindFrameClose();
		enableGPS(TheOptions.wantGPS);
	}
	

	/**
	 * Reads the options and other text files and initializes them.
	 * Must be in this order.
	 */
	private void loadStorage()
	{
		try
		{
			TheOptions = new Option(new Ini(new File(FILENAME_OPTIONS)));
		}
		catch (IOException ex)
		{
			JOptionPane.showMessageDialog(TheFrame,
				FILENAME_OPTIONS + TEXT_FILELOADWARNING,<commit_msg>Fix bugs loading last visited URL<commit_after>package org.lunarproject.gw2route;

/**
 * Navi.java native OS browser wrapper frame for gw2timer.com website.
 * Based on SimpleWebBrowserExample.java and uses DJ Native Swing project by
 * <NAME>. Resizable frame from coderanch.com post by "<NAME>".
 * Various codes copied/adapted from Internet posting, mainly stackoverflow.com.
 * 
 * Libraries/APIs used:
 * DJNativeSwing for embedding browser in program.
 * Standard Widget Toolkit for displaying GUI with DJNativeSwing. 
 * ini4j for reading and writing user options from and to portable text files.
 * 
 * See variable declarations for program version.
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import org.ini4j.Ini;

import org.cef.CefApp;
import org.cef.CefApp.CefAppState;
import org.cef.CefClient;
import org.cef.CefSettings;
import org.cef.browser.CefBrowser;
import org.cef.handler.CefAppHandlerAdapter;


public class Navi extends JPanel {
	
	final static String PROGRAMNAME = "gw2route";
	final static String PROGRAMVERSION = "0.0.2b";
	final static String DIRECTORY_ICONS = "img/";
	final static String FILENAME_OPTIONS = "options.ini";
	final static String FILENAME_TRANSLATIONS = "translations.ini";
	final static String FILENAME_BOOKMARKS = "bookmarks.txt";
	final static String REPO_URI = "https://github.com/ffd114/gw2route";
	final static String UPDATE_URI = "https://github.com/ffd114/gw2route/releases";
	
	WindowPreset currentWindowPreset;
	int ICON_HEIGHT = 16;
	int ADD_VERTICAL_PIXELS;
	int ADD_HORIZONTAL_PIXELS;
	int BORDER_THICKNESS_TOTAL;
	int RESOLUTION_WIDTH;
	int RESOLUTION_HEIGHT;
	static Color COLOR_BAR_CURRENT;
	int OPACITY_LEVELS_10 = 10;
	float OPACITY_STEP = 0.10f;
	boolean isMiniaturized = false;
	boolean isGPSStarted = false;
	
	protected Translation TheTranslations;
	protected static Option TheOptions;
	protected Bookmark TheBookmarks;
	private final CefApp     _cefApp;
	private final CefClient  _client;
	private final CefBrowser _browser;
	private CefSettings _settings;
	
	private static ResizableFrame TheFrame; // Frame resizable edges
	private static JPanel TheContainer; // Frame's container
	private static JPanel TheBar; // The draggable top bar of the overlay that contains the menu
	private static CustomMenu TheMenu; // The menu and its submenus on the bar
	private static JPopupMenu ThePopup; // The context menu that pops up from the bar after right clicking
	private static ClassLoader TheClassLoader; // Helper class to load images
	private static GPS TheGPS; // GPS memory reading class
	
	public static Point mousePressedPoint;
	
	final static String TEXT_FILELOADWARNING = " was not found in program's directory!\n"
		+ "The file should've been packaged with the program when you downloaded it.";
	final static String TEXT_FILESAVEWARNING = " was unable to be saved!";
	final static String TEXT_ABOUT =
		PROGRAMNAME + " browser-overlay for Guild Wars 2 by GW2Timer.com\n"
		+ "Version: " + PROGRAMVERSION + " - Released: 2014.07.16 - Created: 2014.06.01\n"
		+ "Browser embed code part of SWT by Eclipse and DJ Native Swing by <NAME>.\n"
		+ "Uses icons from Crystal set by <NAME>.\n"
		+ "\n"
		+ "Shortcuts: Left click the \"G\" icon to miniaturize, right click it to minimize.\n"
		+ "Double click the overlay window bar to maximize. Move cursor over edges to resize.\n"
		+ "Press F5 in the browser to reload the website.\n"
		+ "\n"
		+ "Appearance: Right click the window bar to open the popup context menu.\n"
		+ "With the context menu visible, keypress (a letter then a number):\n"
		+ "S + # = load a Size preset.\n"
		+ "C + # = load a Color preset.\n"
		+ "# = load Opacity value.\n";

	public Navi() throws InterruptedException
	{
		super(new BorderLayout());
		// Load options and data first before doing anything
		loadStorage();

		// Initialize browser
		JPanel webBrowserPanel = new JPanel(new BorderLayout());
		webBrowserPanel.setBorder(BorderFactory.createEmptyBorder());

		// Add args to enable using flash
		String[] args = new String[] {"--enable-system-flash"};

		// Initialize cefbrowser
		CefApp.addAppHandler(new CefAppHandlerAdapter(args) {
			@Override
			public void stateHasChanged(org.cef.CefApp.CefAppState state) {
				// Shutdown the app if the native CEF part is terminated
				if (state == CefAppState.TERMINATED)
					System.exit(0);
			}
		});

		// Setup settings
		_settings = new CefSettings();

		// Enable cache
		_settings.cache_path = "cache";

		_cefApp = CefApp.getInstance(args, _settings);
		_client = _cefApp.createClient();
		
		String loadUrl = TheOptions.URL_HOMEPAGE;
		
		if (TheOptions.wantLastVisited)
		{
			loadUrl = TheOptions.URL_LASTVISITED;
		}
		
		_browser = _client.createBrowser(loadUrl, false, true);

		webBrowserPanel.add(_browser.getUIComponent(), BorderLayout.CENTER);
		add(webBrowserPanel, BorderLayout.CENTER);

		TheClassLoader = this.getClass().getClassLoader();

		// Browser frame initializations in separate functions to reduce clutter
		sumDimensions();
		createMenu();
		styleFrame();
		createPopup();
		bindFrameFocus();
		bindFrameClose();
		enableGPS(TheOptions.wantGPS);
	}
	

	/**
	 * Reads the options and other text files and initializes them.
	 * Must be in this order.
	 */
	private void loadStorage()
	{
		try
		{
			TheOptions = new Option(new Ini(new File(FILENAME_OPTIONS)));
		}
		catch (IOException ex)
		{
			JOptionPane.showMessageDialog(TheFrame,
				FILENAME_OPTIONS + TEXT_FILELOADWARNING,
{'fraction_non_alphanumeric': 0.09287833827893176, 'fraction_numerical': 0.006824925816023739, 'mean_word_length': 4.491242362525458, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5381951', 'n_tokens_mistral': 4513, 'n_tokens_neox': 4145, 'n_words': 1254}