View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.component;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Component;
24  import java.awt.event.ActionEvent;
25  import java.io.File;
26  import java.util.LinkedList;
27  
28  import javax.swing.AbstractAction;
29  import javax.swing.Action;
30  import javax.swing.JScrollPane;
31  import javax.swing.JTree;
32  import javax.swing.event.TreeSelectionEvent;
33  import javax.swing.event.TreeSelectionListener;
34  import javax.swing.tree.TreePath;
35  
36  import org.xnap.XNap;
37  import org.xnap.gui.tree.FileCellRenderer;
38  import org.xnap.gui.tree.FileNode;
39  import org.xnap.gui.tree.FileTreeModel;
40  import org.xnap.gui.util.IconHelper;
41  
42  /***
43   * Provides a dialog with a {@link JTree} that contains the local
44   * directory structure for directory selection.  
45   */
46  public class DirectoryChooser extends DefaultDialog
47  	implements TreeSelectionListener
48  {
49  
50      //--- Constant(s) ---
51  
52      public static final int APPROVE_OPTION = 1;
53      public static final int CANCEL_OPTION = 2;
54  
55      //--- Data field(s) ---
56  
57      private JTree jtDirectories;
58      private FileTreeModel dtmDirectories;
59      private File selectedDirectory = null;
60  
61      //--- Constructor(s) ---
62  
63      public DirectoryChooser()
64      {
65      	super(BUTTON_OKAY | BUTTON_CANCEL);
66      	
67  		// center
68  		dtmDirectories = new FileTreeModel(XNap.tr("Directories"),
69  										   File.listRoots());
70  		jtDirectories = new JTree(dtmDirectories);
71  		jtDirectories.setRootVisible(false);
72  		jtDirectories.setCellRenderer(new FileCellRenderer());
73  		jtDirectories.putClientProperty("JTree.lineStyle", "Angled");
74  		jtDirectories.addTreeSelectionListener(this);
75  
76          JScrollPane jspDirectories = new JScrollPane(jtDirectories);
77  
78  		// bottom
79  		XNapButton xbHome = new XNapButton(new HomeAction());
80  		getButtonPanel().add(xbHome, 0);
81  
82  		// set me up
83  		getMainPanel().setLayout(new BorderLayout());
84  		getMainPanel().add(jspDirectories, BorderLayout.CENTER);
85  
86  		pack();
87      }
88  
89      //--- Method(s) ---
90  
91  	public boolean apply()
92  	{
93  		Object f = jtDirectories.getLastSelectedPathComponent();
94  		
95  		if (f instanceof File) {
96  			selectedDirectory = (File)f;
97  		}	
98  		return true;
99  	}
100 
101 	public void cancelled()
102 	{	
103 		selectedDirectory = null;
104 	}
105 	
106     public File getSelectedDirectory()
107     {
108 		return selectedDirectory;
109     }
110 
111     public void setSelectedDirectory(File dir)
112     {
113 		LinkedList files = new LinkedList();
114 		File parent = dir;
115 		while (parent != null) {
116 			files.addFirst(new FileNode(parent));
117 			parent = parent.getParentFile();
118 		}
119 		Object[] path = new Object[files.size() + 1];
120 		path[0] = dtmDirectories.getRoot();
121 		System.arraycopy(files.toArray(), 0, path, 1, path.length - 1);
122 		TreePath tp = new TreePath(path);
123 		jtDirectories.setSelectionPath(tp);
124 		jtDirectories.scrollPathToVisible(tp);
125     }
126 
127     public int showChooseDialog(Component c)
128     {
129 		setTitle(XNap.tr("Choose a directory"));
130 		setModal(true);
131 		pack();
132 		show(c);
133 
134 		return (selectedDirectory == null ? CANCEL_OPTION : APPROVE_OPTION);
135     }
136 
137     public void valueChanged(TreeSelectionEvent e)
138     {
139 		getOkayAction().setEnabled(jtDirectories.getLastSelectedPathComponent()
140 								  instanceof File);
141     }
142 
143     public class HomeAction extends AbstractAction {
144 	
145 		public HomeAction()
146 		{
147 			putValue(Action.NAME, XNap.tr("Home"));
148             putValue(Action.SHORT_DESCRIPTION,
149 					 XNap.tr("Jump to home directory"));
150 			putValue(IconHelper.XNAP_ICON, "gohome.png");					 
151         }
152 
153         public void actionPerformed(ActionEvent event) 
154 		{
155 			setSelectedDirectory(new File(System.getProperty("user.home")));
156         }
157 
158     }
159 
160 }