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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class Arb extends JPanel //extends JFrame
{
public static final ImageIcon ICON_COMPUTER =
new ImageIcon("pt2.JPG");
public static final ImageIcon ICON_DISK =
new ImageIcon("pt4.JPG");
public static final ImageIcon ICON_FOLDER =
new ImageIcon("bib.JPG");
public static final ImageIcon ICON_EXPANDEDFOLDER =
new ImageIcon("bibouv.JPG");
protected JTree m_tree;
protected DefaultTreeModel m_model;
protected JTextField m_display;
public Arb() {
// super("Arbre MICRO");
// setSize(400, 300);
DefaultMutableTreeNode top = new DefaultMutableTreeNode(
new IconData(ICON_COMPUTER, null, "MICRO"));
DefaultMutableTreeNode node;
File[] roots = File.listRoots();
for (int k=0; k<roots.length; k++) {
node = new DefaultMutableTreeNode(
new IconData(ICON_DISK,
null, new FileNode(roots[k])));
top.add(node);
node.add( new DefaultMutableTreeNode(
new Boolean(true)));
}
m_model = new DefaultTreeModel(top);
m_tree = new JTree(m_model);
m_tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
m_tree.putClientProperty("JTree.lineStyle", "Angled");
TreeCellRenderer renderer = new IconCellRenderer();
m_tree.setCellRenderer(renderer);
m_tree.addTreeExpansionListener(new DirExpansionListener());
m_tree.addTreeSelectionListener(new DirSelectionListener());
m_tree.setShowsRootHandles(true);
m_tree.setEditable(false);
JScrollPane pane3 = new JScrollPane(m_tree);
setLayout(new GridLayout(1,0));
pane3.setBorder(BorderFactory.createTitledBorder("MICRO"));
add(pane3);
// JScrollPane s = new JScrollPane();
// s.getViewport().add(m_tree);
// getContentPane().add(s, BorderLayout.CENTER);
//
// m_display = new JTextField();
// m_display.setEditable(false);
// getContentPane().add(m_display, BorderLayout.NORTH);
// WindowListener wndCloser = new WindowAdapter() {
// public void windowClosing(WindowEvent e) {
// System.exit(0);
// }
// };
// addWindowListener(wndCloser);
//
// setVisible(true);
}
DefaultMutableTreeNode getTreeNode(TreePath path) {
return (DefaultMutableTreeNode) (path.getLastPathComponent());
}
FileNode getFileNode(DefaultMutableTreeNode node) {
if (node == null)
return null;
Object obj = node.getUserObject();
if (obj instanceof IconData)
obj = ((IconData)obj).getObject();
if (obj instanceof FileNode)
return (FileNode)obj;
else
return null;
}
// Make sure expansion is threaded and updating the tree model
// only occurs within the event dispatching thread.
class DirExpansionListener implements TreeExpansionListener
{
public void treeExpanded(TreeExpansionEvent event) {
final DefaultMutableTreeNode node = getTreeNode(
event.getPath());
final FileNode fnode = getFileNode(node);
Thread runner = new Thread() {
public void run() {
if (fnode != null && fnode.expand(node)) {
Runnable runnable = new Runnable() {
public void run() {
m_model.reload(node);
}
};
SwingUtilities.invokeLater(runnable);
}
}
};
runner.start();
}
public void treeCollapsed(TreeExpansionEvent event) {}
}
class DirSelectionListener implements TreeSelectionListener
{
public void valueChanged(TreeSelectionEvent event) {
DefaultMutableTreeNode node = getTreeNode(event.getPath());
FileNode fnode = getFileNode(node);
if (fnode != null)
m_display.setText(fnode.getFile().getAbsolutePath());
else
m_display.setText("");
}
}
// public static void main(String argv[]) {
// try {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// } catch (Exception exc) {
// System.err.println("Error loading L&F: " + exc);
// }
// new Arb(); }
}
class IconCellRenderer extends JLabel implements TreeCellRenderer
{
protected Color m_textSelectionColor;
protected Color m_textNonSelectionColor;
protected Color m_bkSelectionColor;
protected Color m_bkNonSelectionColor;
protected Color m_borderSelectionColor;
protected boolean m_selected;
public IconCellRenderer() {
super();
m_textSelectionColor = UIManager.getColor(
"Tree.selectionForeground");
m_textNonSelectionColor = UIManager.getColor(
"Tree.textForeground");
m_bkSelectionColor = UIManager.getColor(
"Tree.selectionBackground");
m_bkNonSelectionColor = UIManager.getColor(
"Tree.textBackground");
m_borderSelectionColor = UIManager.getColor(
"Tree.selectionBorderColor");
setOpaque(false);
}
public Component getTreeCellRendererComponent(JTree tree,
Object value, boolean sel, boolean expanded, boolean leaf,
int row, boolean hasFocus)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
Object obj = node.getUserObject();
setText(obj.toString());
if (obj instanceof Boolean)
setText("Chargement.....");
if (obj instanceof IconData) {
IconData idata = (IconData)obj;
if (expanded)
setIcon(idata.getExpandedIcon());
else
setIcon(idata.getIcon());
}
else
setIcon(null);
setFont(tree.getFont());
setForeground(sel ? m_textSelectionColor :
m_textNonSelectionColor);
setBackground(sel ? m_bkSelectionColor :
m_bkNonSelectionColor);
m_selected = sel;
return this;
}
public void paintComponent(Graphics g) {
Color bColor = getBackground();
Icon icon = getIcon();
g.setColor(bColor);
int offset = 0;
if(icon != null && getText() != null)
offset = (icon.getIconWidth() + getIconTextGap());
g.fillRect(offset, 0, getWidth() - 1 - offset,
getHeight() - 1);
if (m_selected) {
g.setColor(m_borderSelectionColor);
g.drawRect(offset, 0, getWidth()-1-offset, getHeight()-1);
}
super.paintComponent(g);
}
}
class IconData
{
protected Icon m_icon;
protected Icon m_expandedIcon;
protected Object m_data;
public IconData(Icon icon, Object data) {
m_icon = icon;
m_expandedIcon = null;
m_data = data;
}
public IconData(Icon icon, Icon expandedIcon, Object data) {
m_icon = icon;
m_expandedIcon = expandedIcon;
m_data = data;
}
public Icon getIcon() { return m_icon; }
public Icon getExpandedIcon() {
return m_expandedIcon!=null ? m_expandedIcon : m_icon;
}
public Object getObject() { return m_data; }
public String toString() { return m_data.toString(); }
}
class FileNode
{
protected File m_file;
public FileNode(File file) { m_file = file; }
public File getFile() { return m_file; }
public String toString() {
return m_file.getName().length() > 0 ? m_file.getName() :
m_file.getPath();
}
public boolean expand(DefaultMutableTreeNode parent) {
DefaultMutableTreeNode flag =
(DefaultMutableTreeNode)parent.getFirstChild();
if (flag==null) // No flag
return false;
Object obj = flag.getUserObject();
if (!(obj instanceof Boolean))
return false; // Already expanded
parent.removeAllChildren(); // Remove Flag
File[] files = listFiles();
if (files == null)
return true;
Vector v = new Vector();
for (int k=0; k<files.length; k++) {
File f = files[k];
if (!(f.isDirectory()))
continue;
FileNode newNode = new FileNode(f);
boolean isAdded = false;
for (int i=0; i<v.size(); i++) {
FileNode nd = (FileNode)v.elementAt(i);
if (newNode.compareTo(nd) < 0) {
v.insertElementAt(newNode, i);
isAdded = true;
break;
}
}
if (!isAdded)
v.addElement(newNode);
}
for (int i=0; i<v.size(); i++) {
FileNode nd = (FileNode)v.elementAt(i);
IconData idata = new IconData(Arb.ICON_FOLDER,
Arb.ICON_EXPANDEDFOLDER, nd);
DefaultMutableTreeNode node =
new DefaultMutableTreeNode(idata);
parent.add(node);
if (nd.hasSubDirs())
node.add(new DefaultMutableTreeNode(
new Boolean(true) ));
}
return true;
}
public boolean hasSubDirs() {
File[] files = listFiles();
if (files == null)
return false;
for (int k=0; k<files.length; k++) {
if (files[k].isDirectory())
return true;
}
return false;
}
public int compareTo(FileNode toCompare) {
return m_file.getName().compareToIgnoreCase(
toCompare.m_file.getName() );
}
protected File[] listFiles() {
if (!m_file.isDirectory())
return null;
try {
return m_file.listFiles();
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null,
"Error reading directory "+m_file.getAbsolutePath(),
"Warning", JOptionPane.WARNING_MESSAGE);
return null;
}
}
} |
Partager