<commit_before>/*******************************************************************************
 * Copyright (C) 2007 The University of Manchester   
 * 
 *  Modifications to the initial code base are copyright of their
 *  respective authors, or their employers as appropriate.
 * 
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1 of
 *  the License, or (at your option) any later version.
 *    
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *    
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 ******************************************************************************/
package net.sf.taverna.t2.reference.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;

import net.sf.taverna.t2.lang.ui.DialogTextArea;
import net.sf.taverna.t2.lang.ui.ValidatingUserInputDialog;
import net.sf.taverna.t2.reference.ui.tree.PreRegistrationTree;
import net.sf.taverna.t2.reference.ui.tree.PreRegistrationTreeModel;

import org.apache.log4j.Logger;

/**
 * A JPanel containing a pre-registration tree along with a toolbar for adding
 * collections, strings, files and url's directly rather than through drag and
 * drop on the tree. Any runtime exceptions thrown within this method are
 * trapped and displayed as error messages in the status bar.
 * 
 * @author <NAME>
 * @author <NAME>
 */
@SuppressWarnings("serial")
public class RegistrationPanel extends JPanel {

	private static final String NO_EXAMPLE_VALUE = "No example value";

	private static final String NO_PORT_DESCRIPTION = "No port description";

	private static final String NEW_VALUE = "Some input data goes here";

	@SuppressWarnings("unused")
	private static Logger logger = Logger.getLogger(RegistrationPanel.class);

	
	private static final ImageIcon addFileIcon = new ImageIcon(
			RegistrationPanel.class.getResource("/icons/topic.gif"));
	private static final ImageIcon addListIcon = new ImageIcon(
			RegistrationPanel.class.getResource("/icons/newfolder_wiz.gif"));
	private static final ImageIcon addTextIcon = new ImageIcon(
			RegistrationPanel.class.getResource("/icons/addtext_co.gif"));
	private static final ImageIcon addUrlIcon = new ImageIcon(
			RegistrationPanel.class.getResource("/icons/web.gif"));
	private static final ImageIcon deleteNodeIcon = new ImageIcon(
			RegistrationPanel.class.getResource("/icons/delete_obj.gif"));
	public static final ImageIcon infoIcon = new ImageIcon(
			RegistrationPanel.class.getResource("/icons/information.gif"));

	// If the depth is greater than 1 we can add sub-folders to the collection
	// structure (it doesn't make sense to do this for single depth lists or
	// individual items). This list is initialized to contain actions to add new
	// folders at each valid collection level
	private final List<Action> addCollectionActions = new ArrayList<Action>();

	private AddFileAction addFileAction = new AddFileAction();
	private AddTextAction addTextAction = new AddTextAction();
	private AddURLAction addUrlAction = new AddURLAction();
	private DeleteNodeAction deleteNodeAction = new DeleteNodeAction();

	private int depth;
	private JSplitPane splitPaneVertical;
	private JSplitPane splitPaneHorizontal;
	private final JLabel status;
	private DialogTextArea descriptionArea;
	private DialogTextArea exampleArea;
	private DialogTextArea textArea;
	private final PreRegistrationTree tree;
	private final PreRegistrationTreeModel treeModel;
	private TextAreaDocumentListener textAreaDocumentListener;


	private String example;

	/**
	 * Construct a new registration panel for an input with the specified depth.
	 * 
	 * @param depth
	 *            Depth of the POJO to construct from this panel
	 * @param example 
	 * @param inputDescription
	 * @param inputName
	 */
	public RegistrationPanel(int depth, String name, String description, String example) {
		super(new BorderLayout());
		this.depth = depth;
		this.example = example;
		tree = new PreRegistrationTree(depth, name) {
			@Override
			public void setStatusMessage(String message, boolean isError) {
				if (isError) {
					setStatus(message, Color.red);
				} else {
					setStatus(message, Color.black);
				}
			}
		};
		treeModel = tree.getPreRegistrationTreeModel();

		final UpdateEditorPaneOnSelection treeSelectionListener = new UpdateEditorPaneOnSelection();
		tree.addTreeSelectionListener(treeSelectionListener);
		
		tree.setRootVisible(false);

		new JPanel(new BorderLayout());

		descriptionArea = new DialogTextArea(NO_PORT_DESCRIPTION, 5, 40);
		descriptionArea.setBorder(new TitledBorder("Port description"));
		descriptionArea.setEditable(false);
		descriptionArea.setLineWrap(true);
		descriptionArea.setWrapStyleWord(true);
		
		exampleArea = new DialogTextArea(NO_EXAMPLE_VALUE, 2, 40);
		exampleArea.setBorder(new TitledBorder("Example value"));
		exampleArea.setEditable(false);
		exampleArea.setLineWrap(true);
		exampleArea.setWrapStyleWord(true);
		
		setDescription(description);
		setExample(example);
		
		JPanel annotationsPanel = new JPanel();
		annotationsPanel.setLayout(new BoxLayout(annotationsPanel, BoxLayout.Y_AXIS));
		JScrollPane descriptionAreaScrollPane = new JScrollPane(descriptionArea);
		JScrollPane exampleAreaScrollPane = new JScrollPane(exampleArea);
		annotationsPanel.add(descriptionAreaScrollPane);
		annotationsPanel.add(Box.createRigidArea(new Dimension(0,5))); // add some empty space
		annotationsPanel.add(exampleAreaScrollPane);
		
		JToolBar toolbar = createToolBar();
		
		textArea = new DialogTextArea();
		textAreaDocumentListener = new TextAreaDocumentListener(textArea);
		textArea.setEditable(false);
		splitPaneHorizontal = new JSplitPane();
		splitPaneHorizontal.add(new JScrollPane(this.tree), JSplitPane.LEFT);
		splitPaneHorizontal.add(new JScrollPane(textArea), JSplitPane.RIGHT);
		splitPaneHorizontal.setDividerLocation(150);
		JPanel toolbarAndInputsPanel = new JPanel(new BorderLayout());
		toolbarAndInputsPanel.add(splitPaneHorizontal, BorderLayout.CENTER);
		toolbarAndInputsPanel.add(toolbar, BorderLayout.NORTH);
		
		splitPaneVertical = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		splitPaneVertical.add(annotationsPanel, JSplitPane.TOP);
		splitPaneVertical.add(toolbarAndInputsPanel, JSplitPane.BOTTOM);
		splitPaneVertical.setDividerLocation(175);
		
		add(splitPaneVertical, BorderLayout.CENTER);
		
		// Listen to selections on the tree to enable or disable actions<commit_msg>Changed the position of the divider.<commit_after>/*******************************************************************************
 * Copyright (C) 2007 The University of Manchester   
 * 
 *  Modifications to the initial code base are copyright of their
 *  respective authors, or their employers as appropriate.
 * 
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1 of
 *  the License, or (at your option) any later version.
 *    
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *    
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 ******************************************************************************/
package net.sf.taverna.t2.reference.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;

import net.sf.taverna.t2.lang.ui.DialogTextArea;
import net.sf.taverna.t2.lang.ui.ValidatingUserInputDialog;
import net.sf.taverna.t2.reference.ui.tree.PreRegistrationTree;
import net.sf.taverna.t2.reference.ui.tree.PreRegistrationTreeModel;

import org.apache.log4j.Logger;

/**
 * A JPanel containing a pre-registration tree along with a toolbar for adding
 * collections, strings, files and url's directly rather than through drag and
 * drop on the tree. Any runtime exceptions thrown within this method are
 * trapped and displayed as error messages in the status bar.
 * 
 * @author <NAME>
 * @author <NAME>
 */
@SuppressWarnings("serial")
public class RegistrationPanel extends JPanel {

	private static final String NO_EXAMPLE_VALUE = "No example value";

	private static final String NO_PORT_DESCRIPTION = "No port description";

	private static final String NEW_VALUE = "Some input data goes here";

	@SuppressWarnings("unused")
	private static Logger logger = Logger.getLogger(RegistrationPanel.class);

	
	private static final ImageIcon addFileIcon = new ImageIcon(
			RegistrationPanel.class.getResource("/icons/topic.gif"));
	private static final ImageIcon addListIcon = new ImageIcon(
			RegistrationPanel.class.getResource("/icons/newfolder_wiz.gif"));
	private static final ImageIcon addTextIcon = new ImageIcon(
			RegistrationPanel.class.getResource("/icons/addtext_co.gif"));
	private static final ImageIcon addUrlIcon = new ImageIcon(
			RegistrationPanel.class.getResource("/icons/web.gif"));
	private static final ImageIcon deleteNodeIcon = new ImageIcon(
			RegistrationPanel.class.getResource("/icons/delete_obj.gif"));
	public static final ImageIcon infoIcon = new ImageIcon(
			RegistrationPanel.class.getResource("/icons/information.gif"));

	// If the depth is greater than 1 we can add sub-folders to the collection
	// structure (it doesn't make sense to do this for single depth lists or
	// individual items). This list is initialized to contain actions to add new
	// folders at each valid collection level
	private final List<Action> addCollectionActions = new ArrayList<Action>();

	private AddFileAction addFileAction = new AddFileAction();
	private AddTextAction addTextAction = new AddTextAction();
	private AddURLAction addUrlAction = new AddURLAction();
	private DeleteNodeAction deleteNodeAction = new DeleteNodeAction();

	private int depth;
	private JSplitPane splitPaneVertical;
	private JSplitPane splitPaneHorizontal;
	private final JLabel status;
	private DialogTextArea descriptionArea;
	private DialogTextArea exampleArea;
	private DialogTextArea textArea;
	private final PreRegistrationTree tree;
	private final PreRegistrationTreeModel treeModel;
	private TextAreaDocumentListener textAreaDocumentListener;


	private String example;

	/**
	 * Construct a new registration panel for an input with the specified depth.
	 * 
	 * @param depth
	 *            Depth of the POJO to construct from this panel
	 * @param example 
	 * @param inputDescription
	 * @param inputName
	 */
	public RegistrationPanel(int depth, String name, String description, String example) {
		super(new BorderLayout());
		this.depth = depth;
		this.example = example;
		tree = new PreRegistrationTree(depth, name) {
			@Override
			public void setStatusMessage(String message, boolean isError) {
				if (isError) {
					setStatus(message, Color.red);
				} else {
					setStatus(message, Color.black);
				}
			}
		};
		treeModel = tree.getPreRegistrationTreeModel();

		final UpdateEditorPaneOnSelection treeSelectionListener = new UpdateEditorPaneOnSelection();
		tree.addTreeSelectionListener(treeSelectionListener);
		
		tree.setRootVisible(false);

		new JPanel(new BorderLayout());

		descriptionArea = new DialogTextArea(NO_PORT_DESCRIPTION, 4, 40);
		descriptionArea.setBorder(new TitledBorder("Port description"));
		descriptionArea.setEditable(false);
		descriptionArea.setLineWrap(true);
		descriptionArea.setWrapStyleWord(true);
		
		exampleArea = new DialogTextArea(NO_EXAMPLE_VALUE, 2, 40);
		exampleArea.setBorder(new TitledBorder("Example value"));
		exampleArea.setEditable(false);
		exampleArea.setLineWrap(true);
		exampleArea.setWrapStyleWord(true);
		
		setDescription(description);
		setExample(example);
		
		JPanel annotationsPanel = new JPanel();
		annotationsPanel.setLayout(new BoxLayout(annotationsPanel, BoxLayout.Y_AXIS));
		JScrollPane descriptionAreaScrollPane = new JScrollPane(descriptionArea);
		JScrollPane exampleAreaScrollPane = new JScrollPane(exampleArea);
		annotationsPanel.add(descriptionAreaScrollPane);
		annotationsPanel.add(Box.createRigidArea(new Dimension(0,5))); // add some empty space
		annotationsPanel.add(exampleAreaScrollPane);
		
		JToolBar toolbar = createToolBar();
		
		textArea = new DialogTextArea();
		textAreaDocumentListener = new TextAreaDocumentListener(textArea);
		textArea.setEditable(false);
		splitPaneHorizontal = new JSplitPane();
		splitPaneHorizontal.add(new JScrollPane(this.tree), JSplitPane.LEFT);
		splitPaneHorizontal.add(new JScrollPane(textArea), JSplitPane.RIGHT);
		splitPaneHorizontal.setDividerLocation(150);
		JPanel toolbarAndInputsPanel = new JPanel(new BorderLayout());
		toolbarAndInputsPanel.add(splitPaneHorizontal, BorderLayout.CENTER);
		toolbarAndInputsPanel.add(toolbar, BorderLayout.NORTH);
		
		splitPaneVertical = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		splitPaneVertical.add(annotationsPanel, JSplitPane.TOP);
		splitPaneVertical.add(toolbarAndInputsPanel, JSplitPane.BOTTOM);
		int dividerPosition = (int) Math.round(annotationsPanel.getPreferredSize().getHeight()) + 10;
		splitPaneVertical.setDividerLocation(dividerPosition);
		
		add(splitPaneVertical, BorderLayout.CENTER);
		
		// Listen to selections on the tree to enable or disable actions
{'fraction_non_alphanumeric': 0.1016959759684586, 'fraction_numerical': 0.0050691532636585514, 'mean_word_length': 5.420249096022499, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '262279', 'n_tokens_mistral': 4606, 'n_tokens_neox': 4252, 'n_words': 1271}