View Javadoc

1   /*
2    * JDiskCatalog
3    *
4    * Copyright 2007 Przemek Więch
5    *
6    * This file is part of JDiskCatalog.
7    *
8    * JDiskCatalog is free software; you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17  
18   * You should have received a copy of the GNU General Public License
19   * along with this program; if not, write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21   */
22  
23  package net.sf.jdiskcatalog.view;
24  
25  import java.awt.Dimension;
26  import java.awt.event.MouseAdapter;
27  import java.awt.event.MouseEvent;
28  
29  import javax.swing.JComponent;
30  import javax.swing.JList;
31  import javax.swing.JScrollPane;
32  import javax.swing.event.ListSelectionEvent;
33  import javax.swing.event.ListSelectionListener;
34  
35  import net.sf.jdiskcatalog.api.Document;
36  import net.sf.jdiskcatalog.api.Node;
37  import net.sf.jdiskcatalog.api.NodeSelectionListener;
38  import net.sf.jdiskcatalog.api.View;
39  
40  
41  public class FileListView implements View, ListSelectionListener
42  {
43  	private JScrollPane panel;
44  	private JList list;
45  
46  	private NodeSelectionListener nodeSelectionListener;
47  
48  	private boolean selecting = false;
49  
50  	private Node node;
51  
52  	public FileListView()
53  	{
54  		list = new JList();
55  		list.addListSelectionListener(this);
56  		list.addMouseListener(new MouseCallback());
57  		panel = new JScrollPane(list);
58  		panel.setPreferredSize(new Dimension(500, 300));
59  	}
60  
61  	public JComponent getComponent()
62  	{
63  		return panel;
64  	}
65  
66  	public void nodeSelected(Node node)
67  	{
68  		if (selecting)
69  			return;
70  		if (node != null)
71  			list.setListData(node.getChildren().toArray());
72  		else
73  			list.setListData(new Object[0]);
74  		this.node = node;
75  	}
76  
77  	public void setDocument(Document document)
78  	{
79  		nodeSelected(null);
80  	}
81  
82  	public void setNodeSelectionListener(NodeSelectionListener listener)
83  	{
84  		this.nodeSelectionListener = listener;
85  	}
86  
87  	public void valueChanged(ListSelectionEvent e)
88  	{
89  		selecting = true;
90  		Node node = (Node)list.getSelectedValue();
91  		nodeSelectionListener.nodeSelected(node);
92  		selecting = false;
93  	}
94  
95  	private class MouseCallback extends MouseAdapter
96  	{
97  	     @Override
98  		public void mouseClicked(MouseEvent e)
99  	     {
100 	         if (e.getClickCount() == 2) {
101 	             int index = list.locationToIndex(e.getPoint());
102 	             Node node = (Node)list.getModel().getElementAt(index);
103 	             selecting = !node.hasChildren();
104 	             nodeSelectionListener.nodeSelected(node);
105 	             selecting = false;
106 	          }
107 	     }
108 	 }
109 
110 	public void refresh()
111 	{
112 		nodeSelected(node);
113 	}
114 }