| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 
 | import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
 
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.*;
import java.io.*;
 
import javax.swing.text.EditorKit;
 
 
public class DocumentViewer extends JFrame implements HyperlinkListener, ActionListener {
 
	JEditorPane viewer       = new JEditorPane ();
	JTextField  urlTextField = new JTextField ();
 
	private JMenuBar menuBar = new JMenuBar();
	private JMenu menu1 = new JMenu("Fichier");
	private JMenuItem item1 = new JMenuItem("Ouvrir");
	private JMenuItem item2 = new JMenuItem("Fermer");
 
	public DocumentViewer () {	
		// Construction de l'Interface Graphique
		// Panel en haut avec un label et le champ de saisie
		JPanel inputPanel = new JPanel (new BorderLayout ());
		JLabel label1 = new JLabel ("URL : ");    
		inputPanel.add (label1, BorderLayout.WEST);
		JButton LoadHash = new JButton ("      Load Hashtable      ");    
		inputPanel.add (LoadHash, BorderLayout.EAST);
		inputPanel.add (urlTextField, BorderLayout.CENTER);
		// Zone scrollée au centre avec le document    
		JScrollPane scrollPane = new JScrollPane (viewer);
		// Ajout des composants à la fenêtre
		getContentPane ().add (inputPanel, BorderLayout.NORTH);
		getContentPane ().add (scrollPane, BorderLayout.CENTER);
 
 
		//Bar de menu
 
		item1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				int returnVal = chooser.showOpenDialog(getParent());
				if(returnVal == JFileChooser.APPROVE_OPTION) {
					System.out.println("Fichier choisi : " +chooser.getSelectedFile().getName());
 
					try {
						viewer.read(new FileReader(new File(chooser.getSelectedFile(), getTitle())), null);
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
 
				}
 
			}
		});
 
 
		item2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);}
		});
 
		LoadHash.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				loadPage2(htData);
			}
		});
 
 
		this.menu1.add(item1);
		this.menu1.add(item2);
		this.menuBar.add(menu1);		
		this.setJMenuBar(menuBar);
		this.setVisible(true);
 
 
		EditorKit htmlKit = viewer.getEditorKitForContentType("text/html");
		viewer.setEditorKit(htmlKit);
 
		viewer.setEditable (true);
		// Ajout du listener de modification de la saisie
		urlTextField.addActionListener (this);
	}
 
 
	// Méthode appelée après un clic sur un lien hyper texte
	public void hyperlinkUpdate (HyperlinkEvent event) {
		if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
			urlTextField.setText (event.getURL ().toString ());
			if (event instanceof HTMLFrameHyperlinkEvent) {
				HTMLDocument doc = (HTMLDocument)viewer.getDocument ();
				doc.processHTMLFrameHyperlinkEvent ((HTMLFrameHyperlinkEvent)event);
			} else {
				loadPage (urlTextField.getText ());
			}
		}
	}
 
	// Méthode appelée après une modification de la saisie
	public void actionPerformed (ActionEvent event) {
		loadPage (urlTextField.getText ());
	}
 
 
	public void loadPage (String urlText) {
 
 
		//String recup = viewer.getText();
		//System.out.println(recup);
 
		viewer.setText("<html><head></head><body>Fichier non trouvé ! </body></html>");
 
		//recup = viewer.getText();
		//System.out.println(recup);
 
		try {
			viewer.read(new FileReader(new File(urlText)), null);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
 
	}
 
	Hashtable htData = new Hashtable();
 
 
	public void loadPage2(Hashtable htData){
		htData.put("id","52603");
		htData.put("nom","TEST NOM");
		htData.put("prenom","TEST PRENOM");
		htData.put("adresse","4 rue frédéric chopin");
		htData.put("comm","Ici on tape du texte!");
		htData.put("fin","fin du Hashtable");
 
		/*
		Enumeration e = htData.elements();
 
		while(e.hasMoreElements()){
			System.out.println(e.nextElement());
		}
		 */
 
		InputStream in = new ByteArrayInputStream(htData.toString().getBytes()); 
		try {
			viewer.read(in, null);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
 
	// Méthode main () d'exemple de mise en oeuvre.
	// Utilisation : java DocumentViewer
 
	public static void main (String [] args) {
		JFrame viewerFrame = new DocumentViewer ();
		viewerFrame.setSize (400, 300);
		viewerFrame.show ();
	}
} | 
Partager