1 2 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
|
public class MyRP extends JFrame {
private static final long serialVersionUID = 8649207663033200521L;
GridBagConstraints c = new GridBagConstraints();
JEditorPane editorPane = new JEditorPane();
public MyRP() {
setTitle("My RP");
// Modifier la taille
setSize(1500, 800);
// Taille non modifiable par l'utilisateur
setResizable(true);
// Un clic sur croix ferme la fenetre
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Centrer la fenetre par rapport à l'écran
setLocationRelativeTo(null);
getContentPane().setLayout(new GridBagLayout());
c.weightx = 1.0;
c.gridx = 1;
c.gridy = 0;
c.ipady = 1500;
c.ipadx = 1200;
c.fill = GridBagConstraints.VERTICAL;
c.insets = new Insets(2, 0, 2, 2);
getContentPane().add(new JScrollPane(editorPane), c);
// Build the panel of controls
JPanel upperPanel = new JPanel();
upperPanel.setLayout(new BorderLayout());
// Ajout de l'arbre hierarchique
JTree tree = new JTree(addNodes(null, new File(".")));
JScrollPane scrollPane = new JScrollPane(tree);
upperPanel.add(scrollPane, "Center"); // Add the tree
Box controlPanel = new Box(BoxLayout.Y_AXIS);
// Add a button to install the text
JButton bouton = new JButton("Ouvrir un fichier");
controlPanel.add(bouton);
upperPanel.add(controlPanel, "South");
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
c.ipadx = 50;
c.weighty = 1;
c.insets = new Insets(2, 10, 2, 2);
getContentPane().add(upperPanel, c);
// Add a listener to the button
bouton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser dialogue = new JFileChooser(new File("."));
File fichier = new File("");
if (dialogue.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
fichier = dialogue.getSelectedFile();
}
if (fichier.toString().contains("MyRP")) {
readSelectedFile(fichier);
} else if(StringUtils.isNotBlank(fichier.toString())){
JOptionPane.showMessageDialog(new JFrame(), "Le fichier n'est pas au bon format : MyRP - Mois - Année.html");
}
}
});
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
String chemin = "";
DefaultMutableTreeNode node2 = node;
while (node2.getParent() != null) {
if (node2.getParent() != null) {
chemin = node2.getUserObject() + "\\" + chemin;
}
node2 = (DefaultMutableTreeNode) node2.getParent();
}
chemin = chemin.substring(0, chemin.length() - 1);
if (chemin.toString().contains("MyRP")) {
readSelectedFile(new File(System.getProperty("user.dir") + "\\" + chemin));
} else {
JOptionPane.showMessageDialog(new JFrame(), "Le fichier n'est pas au bon format : MyRP - Mois - Année.html");
}
}
});
}
private void readSelectedFile(File fichier) {
try {
File file = new File(fichier.toString());
editorPane.setPage(file.toURI().toURL());
editorPane.setContentType("text/html;charset=UTF-8");
editorPane.setEditable(false);
} catch (Exception exception) {
exception.printStackTrace();
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
String curPath = dir.getPath();
DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
if (curTop != null) {
curTop.add(curDir);
}
Vector ol = new Vector();
String[] tmp = dir.list();
for (int i = 0; i < tmp.length; i++)
ol.addElement(tmp[i]);
Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
File f;
Vector files = new Vector();
for (int i = 0; i < ol.size(); i++) {
String thisObject = (String) ol.elementAt(i);
String newPath;
if (curPath.equals("."))
newPath = thisObject;
else
newPath = curPath + File.separator + thisObject;
if ((f = new File(newPath)).isDirectory())
addNodes(curDir, f);
else
files.addElement(thisObject);
}
for (int fnum = 0; fnum < files.size(); fnum++)
curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
return curDir;
}
} |
Partager