<commit_before>/*******************************************************************************
 * Copyright (c) 2011 Bolton University, UK.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the License
 * which accompanies this distribution in the file LICENSE.txt
 *******************************************************************************/
package uk.ac.bolton.archimate.editor.ui.components;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.LineStyleEvent;
import org.eclipse.swt.custom.LineStyleListener;
import org.eclipse.swt.custom.ST;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;

import uk.ac.bolton.archimate.editor.utils.HTMLUtils;
import uk.ac.bolton.archimate.editor.utils.PlatformUtils;


/**
 * Wraps a StyledText Control to listen for hyperlinks
 * A lot of this code is adapted from org.eclipse.ui.internal.about.AboutTextManager
 * 
 * @author <NAME>
 */
public class StyledTextControl implements Listener, LineStyleListener {
    
    private StyledText fStyledText;
    
    private Cursor fHandCursor, fBusyCursor;
    private Cursor fCurrentCursor;
    
    private List<int[]> fLinkRanges;
    private List<String> fLinks;
        
    private IAction fActionSelectAll = new Action(Messages.StyledTextControl_0) {
        @Override
        public void run() {
            fStyledText.selectAll();
        }
    };
    
    private IAction fActionCut = new Action(Messages.StyledTextControl_1) {
        @Override
        public void run() {
            fStyledText.cut();
        }
    };
    
    private IAction fActionCopy = new Action(Messages.StyledTextControl_2) {
        @Override
        public void run() {
            fStyledText.copy();
        }
    };
    
    private IAction fActionPaste = new Action(Messages.StyledTextControl_3) {
        @Override
        public void run() {
            fStyledText.paste();
        }
    };
    
    private IAction fActionDelete = new Action(Messages.StyledTextControl_4) {
        @Override
        public void run() {
            fStyledText.invokeAction(SWT.DEL);
        }
    };

    public StyledTextControl(Composite parent, int style) {
        this(new StyledText(parent, style));
    }
    
    public StyledTextControl(StyledText styledText) {
        fStyledText = styledText;
        fStyledText.setLeftMargin(PlatformUtils.isWindows() ? 4 : 2);
        fStyledText.setKeyBinding(ST.SELECT_ALL, ST.SELECT_ALL);
        
        fHandCursor = new Cursor(styledText.getDisplay(), SWT.CURSOR_HAND);
        fBusyCursor = new Cursor(styledText.getDisplay(), SWT.CURSOR_WAIT);
        
        fStyledText.addDisposeListener(new DisposeListener() {
            @Override
            public void widgetDisposed(DisposeEvent e) {
                fHandCursor.dispose();
                fBusyCursor.dispose();
                
                fStyledText.removeListener(SWT.MouseDown, StyledTextControl.this);
                fStyledText.removeListener(SWT.MouseUp, StyledTextControl.this);
                fStyledText.removeListener(SWT.MouseMove, StyledTextControl.this);
                fStyledText.getDisplay().removeFilter(SWT.KeyDown, StyledTextControl.this);
                fStyledText.getDisplay().removeFilter(SWT.KeyUp, StyledTextControl.this);
                
                fStyledText.removeLineStyleListener(StyledTextControl.this);
                
                fHandCursor = null;
                fBusyCursor = null;
                fCurrentCursor = null;
                fLinks = null;
            }
        });
        
        fStyledText.addListener(SWT.MouseDown, this);
        fStyledText.addListener(SWT.MouseUp, this);
        fStyledText.addListener(SWT.MouseMove, this);
        fStyledText.getDisplay().addFilter(SWT.KeyDown, this);
        fStyledText.getDisplay().addFilter(SWT.KeyUp, this);
        
        fStyledText.addLineStyleListener(this);
        
        hookContextMenu();
    }
    
    @Override
    public void lineGetStyle(LineStyleEvent event) {
        // Do this on any text change because it will be needed for mouse over
        scanLinks(fStyledText.getText());
        
        int lineLength = event.lineText.length();<commit_msg>StyledTextControl: remove mouse down listener<commit_after>/*******************************************************************************
 * Copyright (c) 2011 Bolton University, UK.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the License
 * which accompanies this distribution in the file LICENSE.txt
 *******************************************************************************/
package uk.ac.bolton.archimate.editor.ui.components;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.LineStyleEvent;
import org.eclipse.swt.custom.LineStyleListener;
import org.eclipse.swt.custom.ST;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;

import uk.ac.bolton.archimate.editor.utils.HTMLUtils;
import uk.ac.bolton.archimate.editor.utils.PlatformUtils;


/**
 * Wraps a StyledText Control to listen for hyperlinks
 * A lot of this code is adapted from org.eclipse.ui.internal.about.AboutTextManager
 * 
 * @author <NAME>
 */
public class StyledTextControl implements Listener, LineStyleListener {
    
    private StyledText fStyledText;
    
    private Cursor fHandCursor, fBusyCursor;
    private Cursor fCurrentCursor;
    
    private List<int[]> fLinkRanges;
    private List<String> fLinks;
        
    private IAction fActionSelectAll = new Action(Messages.StyledTextControl_0) {
        @Override
        public void run() {
            fStyledText.selectAll();
        }
    };
    
    private IAction fActionCut = new Action(Messages.StyledTextControl_1) {
        @Override
        public void run() {
            fStyledText.cut();
        }
    };
    
    private IAction fActionCopy = new Action(Messages.StyledTextControl_2) {
        @Override
        public void run() {
            fStyledText.copy();
        }
    };
    
    private IAction fActionPaste = new Action(Messages.StyledTextControl_3) {
        @Override
        public void run() {
            fStyledText.paste();
        }
    };
    
    private IAction fActionDelete = new Action(Messages.StyledTextControl_4) {
        @Override
        public void run() {
            fStyledText.invokeAction(SWT.DEL);
        }
    };

    public StyledTextControl(Composite parent, int style) {
        this(new StyledText(parent, style));
    }
    
    public StyledTextControl(StyledText styledText) {
        fStyledText = styledText;
        fStyledText.setLeftMargin(PlatformUtils.isWindows() ? 4 : 2);
        fStyledText.setKeyBinding(ST.SELECT_ALL, ST.SELECT_ALL);
        
        fHandCursor = new Cursor(styledText.getDisplay(), SWT.CURSOR_HAND);
        fBusyCursor = new Cursor(styledText.getDisplay(), SWT.CURSOR_WAIT);
        
        fStyledText.addDisposeListener(new DisposeListener() {
            @Override
            public void widgetDisposed(DisposeEvent e) {
                fHandCursor.dispose();
                fBusyCursor.dispose();
                
                fStyledText.removeListener(SWT.MouseUp, StyledTextControl.this);
                fStyledText.removeListener(SWT.MouseMove, StyledTextControl.this);
                fStyledText.getDisplay().removeFilter(SWT.KeyDown, StyledTextControl.this);
                fStyledText.getDisplay().removeFilter(SWT.KeyUp, StyledTextControl.this);
                
                fStyledText.removeLineStyleListener(StyledTextControl.this);
                
                fHandCursor = null;
                fBusyCursor = null;
                fCurrentCursor = null;
                fLinks = null;
            }
        });
        
        fStyledText.addListener(SWT.MouseUp, this);
        fStyledText.addListener(SWT.MouseMove, this);
        fStyledText.getDisplay().addFilter(SWT.KeyDown, this);
        fStyledText.getDisplay().addFilter(SWT.KeyUp, this);
        
        fStyledText.addLineStyleListener(this);
        
        hookContextMenu();
    }
    
    @Override
    public void lineGetStyle(LineStyleEvent event) {
        // Do this on any text change because it will be needed for mouse over
        scanLinks(fStyledText.getText());
        
        int lineLength = event.lineText.length();
{'fraction_non_alphanumeric': 0.12108641975308641, 'fraction_numerical': 0.0023703703703703703, 'mean_word_length': 2.8472644376899696, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3115596', 'n_tokens_mistral': 3031, 'n_tokens_neox': 2881, 'n_words': 545}