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 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
public class Explorateur extends JFrame implements TreeSelectionListener{
//ATTRIBUTS_________________________________________________________________
private File root,currentFile;private TreeModel model;private JTree tree;
private JTextPane textPane;private JButton jbDelete;
private JLabel statut,labelImage;private JPanel panelAffichage;
private JScrollPane jspTree,jspAffichage;
//CONSTRUCTEURS_____________________________________________________________
public Explorateur(String nomRepertoire){
super("Explorateur");
initRoot(nomRepertoire);initTree();
initAutresComposants();initFenetre();
}
//METHODES__________________________________________________________________
//fonctions-----------------------------------------------------------------
public boolean acceptFile(File file){
if(file==null||file.isDirectory()){return false;}
String nom = file.getName();
if(nom.endsWith(".rtf")||nom.endsWith(".htm")||nom.endsWith(".html")
||nom.endsWith(".gif")||nom.endsWith(".jpg")){return true;}
return false;
}
public boolean acceptFile(){return acceptFile(currentFile); }
public boolean isImageFile(){
if(currentFile==null){return false;}
String nom = currentFile.getName();
if(nom.endsWith(".gif")||nom.endsWith(".jpg")){return true;}
return false;
}
public boolean isTextFile(){
if(currentFile==null){return false;}
String nom = currentFile.getName();
if(nom.endsWith(".rtf")||nom.endsWith(".htm")||nom.endsWith(".html")){return true;}
return false;
}
public JWindow getFenetreChargement(){
JWindow w = new JWindow();
JLabel label = new JLabel("Chargement en cours ...");label.setOpaque(true);
label.setForeground(Color.BLUE);label.setBorder(BorderFactory.createLineBorder(Color.BLUE));
w.getContentPane().add(label);w.pack();w.setLocationRelativeTo(null);
return w;
}
//procedures----------------------------------------------------------------
public void delete(){
if(currentFile==null||currentFile.isDirectory()){return;}
int commande = JOptionPane.showConfirmDialog(this
,"Voulez vous vraiment supprimmer "+currentFile.getPath()
,"Confirmez",JOptionPane.YES_NO_OPTION);
switch(commande){
case JOptionPane.YES_OPTION :
currentFile.delete();currentFile = null;
jspAffichage.setViewportView(null);tree.updateUI();
break;
default : return;
}
}
public void chargerTexte(){
try{
if(!acceptFile())return;
if(currentFile.getName().indexOf(".htm")!=-1){textPane.setContentType("text/html");}
else if(currentFile.getName().endsWith(".rtf")){textPane.setContentType("text/rtf");}
textPane.setPage(currentFile.toURL());textPane.setPreferredSize(panelAffichage.getSize());
jspAffichage.setViewportView(textPane);jspAffichage.getViewport().setViewSize(textPane.getPreferredSize());
}
catch(Exception exc){exc.printStackTrace();}
}
public void chargerImage(){
try{
ImageIcon imageIcon = new ImageIcon(currentFile.getPath());
labelImage.setIcon(imageIcon);jspAffichage.setViewportView(labelImage);
jspAffichage.getViewport().setViewSize(labelImage.getPreferredSize());
}catch(Exception exc){exc.printStackTrace();}
}
public void afficherFichierTexte(){
Thread thread = new Thread(){
public void run(){
JWindow w = getFenetreChargement();w.show();
chargerTexte();w.dispose();
}
};
thread.start();
}
public void afficherFichierImage(){
Thread thread = new Thread(){
public void run(){
JWindow w = getFenetreChargement();w.show();
chargerImage();w.dispose();
}
};
thread.start();
}
//procedures TreeSelectionListener------------------------------------------
public void valueChanged(TreeSelectionEvent e){
TreePath path = e.getPath();jspAffichage.setViewportView(null);
if(e==null||path==null){
currentFile=null;
statut.setText("Current file = null");statut.setForeground(Color.RED);
jbDelete.setEnabled(false);return;
}
currentFile = (File)path.getLastPathComponent();
statut.setText("Current file = "+currentFile.getPath());
if(acceptFile()){
jbDelete.setEnabled(true); statut.setForeground(Color.BLUE);
if(isImageFile()){afficherFichierImage();}else if(isTextFile()){afficherFichierTexte();}
}else{
jbDelete.setEnabled(false); statut.setForeground(Color.RED);
}
}
//procedures d'initialisation-----------------------------------------------
public void initRoot(String nomRepertoire){
System.out.println("INIT ROOT");
if(nomRepertoire==null){System.out.println("Erreur : nom repertoire null");System.exit(0); }
root = new File(nomRepertoire);
if(!root.exists()){System.out.println("Erreur : "+nomRepertoire+" n'existe pas");System.exit(0); }
if(!root.isDirectory()){System.out.println("Erreur : "+nomRepertoire+" n'est pas un dossier");System.exit(0); }
currentFile = null;
}
public void initTree(){
System.out.println("INIT TREE");
System.out.println("->init modele");
model = new TreeModel(){
//methodes sur mesure
public java.util.List getFichiers(Object parent){
File fileParent = (File)parent;
File[] fichiers = fileParent.listFiles(new FileFilter(){
public boolean accept(File file){return acceptFile(file)||file.isDirectory();}
});
Arrays.sort(fichiers,new Comparator(){
public int compare(Object o1,Object o2){
File f1 = (File)o1;File f2 = (File)o2;
boolean dirf1 = f1.isDirectory();boolean dirf2 = f2.isDirectory();
if(dirf1&&!dirf2){return -1;}if(!dirf1&&dirf2){return 1;}
return f1.getPath().compareTo(f2.getPath());
}
});
return Arrays.asList(fichiers);
}
//surcharge de certaines methodes
public Object getRoot(){return root;}
public Object getChild(Object parent, int index){return getFichiers(parent).get(index);}
public int getChildCount(Object parent){return getFichiers(parent).size();}
public int getIndexOfChild(Object parent, Object child){return getFichiers(parent).indexOf(child);}
public boolean isLeaf(Object node){
File f = (File)node;if(!f.isDirectory()){return true;}
return false;
}
public void valueForPathChanged(TreePath path, Object newValue){}
public void addTreeModelListener(TreeModelListener l){}
public void removeTreeModelListener(TreeModelListener l){}
};
System.out.println("->init tree");
tree = new JTree(model);tree.setEditable(false);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addTreeSelectionListener(this);tree.putClientProperty("JTree.lineStyle","Angled");
System.out.println("->init renderer");
tree.setCellRenderer(new DefaultTreeCellRenderer(){
public Component getTreeCellRendererComponent(JTree tree,Object value
,boolean selected,boolean expanded
,boolean leaf,int row,boolean hasFocus){
JLabel label = (JLabel)super.getTreeCellRendererComponent(tree,value,selected,expanded,leaf,row,hasFocus);
File fichier = (File)value;label.setText(fichier.getName());
if(fichier.isDirectory()){label.setForeground(Color.BLACK);}
else if(acceptFile(fichier)){label.setForeground(Color.BLUE);}
else{label.setForeground(Color.RED);}
return label;
}
});
tree.addTreeExpansionListener(new TreeExpansionListener(){
public void treeCollapsed(TreeExpansionEvent event){jspTree.getViewport().setViewSize(tree.getPreferredScrollableViewportSize());}
public void treeExpanded(TreeExpansionEvent event){jspTree.getViewport().setViewSize(tree.getPreferredScrollableViewportSize());}
});
}
public void initAutresComposants(){
System.out.println("INIT AUTRES COMPOSANTS GRAPHIQUES");
System.out.println("->init pane");
textPane = new JTextPane();textPane.setEditable(false);
System.out.println("->init label image");labelImage = new JLabel();
System.out.println("->init bouton");
jbDelete = new JButton("Supprimmer");jbDelete.setEnabled(false);
jbDelete.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ae){delete();} });
System.out.println("->init barre de statut");
statut = new JLabel("Current file = null");statut.setOpaque(true);
statut.setForeground(Color.RED);statut.setBorder(BorderFactory.createLineBorder(Color.BLUE));
}
public void initFenetre(){
System.out.println("INIT FENETRE");
System.out.println("->init panneaux intermediaires");
panelAffichage = new JPanel();
panelAffichage.setPreferredSize(new Dimension(500,500));
JPanel panelOuest = new JPanel(new BorderLayout());
panelOuest.setPreferredSize(new Dimension(300,500));
System.out.println("->init jsp");
jspTree = new JScrollPane(tree);jspAffichage = new JScrollPane();
jspAffichage.setPreferredSize(panelAffichage.getPreferredSize());
jspAffichage.setBorder(BorderFactory.createTitledBorder("Zone d'affichage"));
System.out.println("->placement des composants");
panelOuest.add(jspTree,BorderLayout.CENTER);panelOuest.add(jbDelete,BorderLayout.SOUTH);
panelAffichage.add(jspAffichage);
getContentPane().add(panelAffichage,BorderLayout.CENTER);
getContentPane().add(panelOuest,BorderLayout.WEST);
getContentPane().add(statut,BorderLayout.SOUTH);
System.out.println("->finitions");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);pack();setLocationRelativeTo(null);setVisible(true);
}
//MAIN______________________________________________________________________
public static void main(String[] args){new Explorateur("c:\\ioan");}
} |
Partager