package bridgeconstructor;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import expertsystem.Rule;
import expertsystem.RulesBase;
import java.awt.Desktop;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Florian
 */
public class RulesBaseInterface extends JFrame implements MouseListener {

	private static final long serialVersionUID = 1L;

	private static final String title = "Base de Règles";
	private static final String file_path = "./ressources/bridge_rules.xml";
	
	
	private final RulesBase RB;
	
	private JPanel main_panel;
		private JScrollPane scroll_pane;
		private JLabel rule;
	
    /**
     *
     */
    public RulesBaseInterface() {
		super(title);
		
		RB = BridgeRules.initRulesBase(file_path);
		
		buildComposants();
		buildInterface();
		buildEvents();
		
		this.setLocationRelativeTo(null);
		this.pack();
		this.setVisible(true);
	}
    
	private void buildComposants() {
		main_panel = new JPanel();
			main_panel.setLayout(new BoxLayout(main_panel, BoxLayout.Y_AXIS));
			scroll_pane = new JScrollPane(main_panel);
		
		rule = new JLabel("<html><u>bridge_rules.xml\n</u><html>");
		rule.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			main_panel.add(rule);
			rule.addMouseListener(this);
			rule.setForeground(Color.BLUE);
        for (Rule R : RB) {
            System.out.println(R);
            String s = R.toString();
            rule = new JLabel(s);
            main_panel.add(rule);
        }
	}
	
	private void buildInterface() {
		setContentPane(scroll_pane);
	}
	
	private void buildEvents() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

    /**
     *
     * @param e
     */
    @Override
	public void mouseClicked(MouseEvent e) {
    	String OS = System.getProperty("os.name");
		try {
			if(OS.equals("Linux")) Runtime.getRuntime().exec("gedit " + file_path);
			else if(OS.startsWith("Windows")){
				Desktop desktop;
                
                try {
                    if (Desktop.isDesktopSupported()) {
                        desktop = Desktop.getDesktop();

                        File file= new File(file_path);
                        desktop.open(file);
                    }else Runtime.getRuntime().exec("notepad " + file_path);
                } catch (IOException ex) {
                    Runtime.getRuntime().exec("notepad " + file_path);
                }
            }
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

    /**
     *
     * @param e
     */
    @Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

    /**
     *
     * @param e
     */
    @Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

    /**
     *
     * @param e
     */
    @Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

    /**
     *
     * @param e
     */
    @Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}

@@ -1989,102 +1989,56 @@
     
-/**%0D%0A     *%0D%0A     * @param e%0D%0A     */%0D%0A    @Override%0D%0A%09public void mouseClicked(MouseEvent e) 
+private void openFileInNotepad(String file_path)
 %7B%0D%0A 
@@ -2089,20 +2089,35 @@
 );%0D%0A
-%09%09try %7B%0D%0A%09%09%09
+        try %7B%0D%0A            
 if(O
@@ -2189,66 +2189,199 @@
 );%0D%0A
-%09%09%09else if(OS.startsWith(%22Windows%22))%7B%0D%0A%09%09%09%09Desktop desktop
+            else%0D%0A                if(OS.startsWith(%22Windows%22)) Runtime.getRuntime().exec(%22notepad %22 + file_path);%0D%0A      %0D%0A        %7D catch (IOException e1) %7B%0D%0A            e1.printStackTrace()
 ;%0D%0A 
@@ -2383,28 +2383,34 @@
 );%0D%0A        
+%7D%0D%0A
     
+%7D%0D%0A
     %0D%0A      
@@ -2407,26 +2407,147 @@
   %0D%0A    
+/**%0D%0A
      
+*%0D%0A
      
+* @param e%0D%0A     */%0D%0A    @Override%0D%0A%09public void mouseClicked(MouseEvent e) %7B%0D%0A        Desktop desktop;%0D%0A%0D%0A      
   try %7B%0D
@@ -2543,32 +2543,24 @@
      try %7B%0D%0A
-        
             
@@ -2608,32 +2608,24 @@
             
-  
-      
 desktop = De
@@ -2663,24 +2663,16 @@
         
-     
-   
 File fil
@@ -2692,32 +2692,24 @@
 ile_path);%0D%0A
-        
             
@@ -2749,61 +2749,51 @@
     
-        %7Delse Runtime.getRuntime().exec(%22n
+%7Delse %7B%0D%0A                openFileInN
 otepad
- %22 + 
+(
 file
@@ -2801,32 +2801,39 @@
 path);%0D%0A        
+    %7D%0D%0A
         %7D catch 
@@ -2868,55 +2868,135 @@
     
-        Runtime.getRuntime().exec(%22n
+System.err.println(%22Erreur lors ouverture du fichier par d%C3%A9faut, essai avec %C3%A9diteur de texte%22);%0D%0A            openFileInN
 otepad
- %22 + 
+(
 file
@@ -3000,32 +3000,24 @@
 ile_path);%0D%0A
-        
         %7D%0D%0A 
@@ -3027,74 +3027,8 @@
     
- 
-   %7D%0D%0A%09%09%7D catch (IOException e1) %7B%0D%0A%09%09%09e1.printStackTrace();%0D%0A%09%09%7D
 %0D%0A%09%7D

version plus propre éditeur