<commit_before>/*
 * AcceleratorDocument.java
 *
 * Created on May 20, 2003, 12:37 PM
 */

package xal.extension.smf.application;

import xal.smf.*;
import xal.extension.application.*;
import xal.smf.data.*;

import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;


/**
 * AcceleratorDocument is a subclass of XalDocument for accelerator based applications.
 * @author  tap
 */
abstract public class AcceleratorDocument extends XalDocument {
    protected Accelerator accelerator;
    protected AcceleratorSeq selectedSequence;
    protected String acceleratorFilePath;
    protected List<AcceleratorSeq> selectedSequenceList;
    
	
    /** Creates a new instance of AcceleratorDocument */
    public AcceleratorDocument() {
        super();
        accelerator = null;
		acceleratorFilePath = null;
		selectedSequenceList = new ArrayList<AcceleratorSeq>();
     }
	
	
	/**
	 * Get the accelerator window managed by this document
	 * @return this document's accelerator window
	 */
	public AcceleratorWindow getAcceleratorWindow() {
		return (AcceleratorWindow)getMainWindow();
	}
	
	
	/** Generate and set the title for this document. */
	protected void generateDocumentTitle() {
		final String filePath = getDisplayFilePath();
		final String title = selectedSequence != null ? " (" + selectedSequence.getId() + ") - " + filePath : filePath;
		setTitle( title );
	}
	
	
	/** 
	 * Get the prefix for a new file (precedes timestamp) defaulting to the selected sequence ID if any or the super class's default if no sequence is selected.
	 * @return prefix for a new file
	 */
	protected String getNewFileNamePrefix() {
		return selectedSequence != null ? selectedSequence.getId().replace( ":", "-" ) : super.getNewFileNamePrefix();
	}
	
	
	/**
	 * Attempt to load the accelerator with the specified path and if none exists, then request a substitute accelerator from the user.
	 * @param filePath file path to the accelerator for which to first attempt to load
	 */
	public Accelerator applySelectedAcceleratorWithDefaultPath( final String filePath ) {
		if ( filePath == null || filePath.length() == 0 ) {
			return requestAndSetAccelerator( "No accelerator file has been specified. \n Please select an accelerator file." );
		}
		else if ( new File( filePath ).exists() ) {
			setAcceleratorWithPath( filePath );
			return getAccelerator();
		}
		else {
			return requestAndSetAccelerator( "Cannot locate the accelerator file: \n" + filePath + " \nPlease select a substitute accelerator." );
		}
	}
	
	
	/** 
	 * Request and set an accelerator
	 * @param message the text to display in the dialog box
	 */
	private Accelerator requestAndSetAccelerator( final String message ) {
		final java.awt.Component targetWindow = getMainWindow();<commit_msg>Handle the optics version exception and offer the user the opportunity to substitute another accelerator file.<commit_after>/*
 * AcceleratorDocument.java
 *
 * Created on May 20, 2003, 12:37 PM
 */

package xal.extension.smf.application;

import xal.smf.*;
import xal.extension.application.*;
import xal.smf.data.*;

import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;


/**
 * AcceleratorDocument is a subclass of XalDocument for accelerator based applications.
 * @author  tap
 */
abstract public class AcceleratorDocument extends XalDocument {
    protected Accelerator accelerator;
    protected AcceleratorSeq selectedSequence;
    protected String acceleratorFilePath;
    protected List<AcceleratorSeq> selectedSequenceList;
    
	
    /** Creates a new instance of AcceleratorDocument */
    public AcceleratorDocument() {
        super();
        accelerator = null;
		acceleratorFilePath = null;
		selectedSequenceList = new ArrayList<AcceleratorSeq>();
     }
	
	
	/**
	 * Get the accelerator window managed by this document
	 * @return this document's accelerator window
	 */
	public AcceleratorWindow getAcceleratorWindow() {
		return (AcceleratorWindow)getMainWindow();
	}
	
	
	/** Generate and set the title for this document. */
	protected void generateDocumentTitle() {
		final String filePath = getDisplayFilePath();
		final String title = selectedSequence != null ? " (" + selectedSequence.getId() + ") - " + filePath : filePath;
		setTitle( title );
	}
	
	
	/** 
	 * Get the prefix for a new file (precedes timestamp) defaulting to the selected sequence ID if any or the super class's default if no sequence is selected.
	 * @return prefix for a new file
	 */
	protected String getNewFileNamePrefix() {
		return selectedSequence != null ? selectedSequence.getId().replace( ":", "-" ) : super.getNewFileNamePrefix();
	}
	
	
	/**
	 * Attempt to load the accelerator with the specified path and if none exists, then request a substitute accelerator from the user.
	 * @param filePath file path to the accelerator for which to first attempt to load
	 */
	public Accelerator applySelectedAcceleratorWithDefaultPath( final String filePath ) {
		if ( filePath == null || filePath.length() == 0 ) {
			return requestAndSetAccelerator( "No accelerator file has been specified. \n Please select an accelerator file." );
		}
		else if ( new File( filePath ).exists() ) {
			try {
				setAcceleratorWithPath( filePath );
				return getAccelerator();
			}
			catch( OpticsVersionException exception ) {
				return requestAndSetAccelerator( exception.getMessage() + " \nPlease select a substitute accelerator." );
			}
		}
		else {
			return requestAndSetAccelerator( "Cannot locate the accelerator file: \n" + filePath + " \nPlease select a substitute accelerator." );
		}
	}
	
	
	/** 
	 * Request and set an accelerator
	 * @param message the text to display in the dialog box
	 */
	private Accelerator requestAndSetAccelerator( final String message ) {
		final java.awt.Component targetWindow = getMainWindow();
{'fraction_non_alphanumeric': 0.09064576588856704, 'fraction_numerical': 0.0037485091156926223, 'mean_word_length': 3.8998330550918197, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, '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': '6003291', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1528, 'n_words': 558}