


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public class jfileviewer02 extends JDialog {

	private final JPanel contentPanel = new JPanel();

	 private JTree tree;
    private JTextArea preview;
	
    public jfileviewer02(JFrame f, boolean modal) 
	{
	       super(f, modal);
	       
	       // fermer la fenetre
	       addWindowListener(new java.awt.event.WindowAdapter() 
	       {
	           public void windowClosing(java.awt.event.WindowEvent e) 
	           {
	        	   dispose();
	           }
	       });
	      
	     
	      
     
	
    
    
	
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		

		 tree = new JTree(new FileSelectorModel("c:\\"));
		 
	     preview = new JTextArea();
	     preview.setWrapStyleWord(true);
	     preview.setLineWrap(true);
	     preview.setEditable(false);
	     
	     
	     
	     tree.addTreeSelectionListener(new TreeSelectionListener() {

	            public void valueChanged(TreeSelectionEvent e) {
	                FileNode selectedNode = (FileNode) tree.getLastSelectedPathComponent();
	                
	                if (selectedNode.isFile()) {
	                    preview.setText(null);
	                    try {
	                        BufferedReader br = new BufferedReader(new FileReader(selectedNode.getAbsolutePath()));
	                        String line = "";
	                        while ((line = br.readLine()) != null) {
	                            preview.append(line);
	                            preview.append(System.getProperty("line.separator"));
	                        }
	                    } catch (Exception exc) {
	                        exc.printStackTrace();
	                    }
	                }
	            }
	        });
	        tree.setCellRenderer(new DefaultTreeCellRenderer() 
	        {

	            @Override
	            public Component getTreeCellRendererComponent( JTree tree, Object value, boolean sel,  boolean expanded, boolean leaf, int row, boolean hasFocus) 
	            {

	                // Call parent rendering to keep the default behaviour
	                super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

	                // And now specific stuff
	                File currentFile = (File) value;
	                // If the current node is a directory, and if it has no child,
	                // or if they are not accessible, change the icon.
	                if (currentFile.isDirectory() && (currentFile.list() == null || currentFile.list().length == 0)) 
	                {
	                    if (expanded) 
	                    {
	                        setIcon(openIcon);
	                    } 
	                    else 
	                    {
	                        setIcon(closedIcon);
	                    }
	                }

	                return this;
	            }
	        });
	     
	     
	        JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
	        								new JScrollPane(tree),
	        								new JScrollPane(preview));
	        //split.setContinuousLayout(true);
	        // barre centrale
	        split.setEnabled(false);

	        //  getContentPane().add(BorderLayout.SOUTH, status);
	        getContentPane().add(split);

	        setSize(800, 600);
	        //    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        setTitle("Quick'n'Dirty File Preview");
	        //     setVisible(true);
		
		
	}

}


// https://www.codejava.net/java-se/file-io/how-to-read-and-write-text-file-in-java

// https://www.algosome.com/articles/line-numbers-java-jtextarea-jtable.html   +++++

// https://tips4java.wordpress.com/2009/05/23/text-component-line-number/

// https://github.com/errordeveloper/mist/blob/master/tools/mspsim/se/sics/mspsim/extutil/highlight/LineNumberedBorder.java

// https://sourceforge.net/projects/violet/files/violetumleditor/

// https://sourceforge.net/projects/violet/files/violetumleditor/

//  cayhorstmann

// https://sourceforge.net/projects/umhdl/files/src/

// https://github.com/Crusader99/digital-logic-simulator
