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.prefs;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Dimension;
24  import java.awt.GridBagLayout;
25  import java.awt.Insets;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  import java.awt.event.KeyEvent;
29  
30  import javax.swing.AbstractAction;
31  import javax.swing.Action;
32  import javax.swing.Box;
33  import javax.swing.BoxLayout;
34  import javax.swing.JButton;
35  import javax.swing.JComboBox;
36  import javax.swing.JPanel;
37  import javax.swing.JScrollPane;
38  import javax.swing.JTable;
39  import javax.swing.KeyStroke;
40  import javax.swing.ListSelectionModel;
41  import javax.swing.event.ListSelectionEvent;
42  import javax.swing.event.ListSelectionListener;
43  
44  import org.xnap.XNap;
45  import org.xnap.gui.component.DirectoryPanel;
46  import org.xnap.gui.component.XNapButton;
47  import org.xnap.gui.table.AbstractColumnTableModel;
48  import org.xnap.gui.table.Column;
49  import org.xnap.gui.util.GridBagHelper;
50  import org.xnap.gui.util.IconHelper;
51  import org.xnap.search.MediaType;
52  import org.xnap.search.SearchManager;
53  import org.xnap.util.Preferences;
54  
55  /***
56   *
57   */
58  public class MediaDownloadDirPanel extends JPanel 
59  {
60  
61      //--- Data field(s) ---
62  
63  	private static Preferences prefs = Preferences.getInstance();
64  
65  	private MyDirectoryPanel dtfDir;
66  	private JComboBox jcbTypes;
67      private MediaTableModel mtm;
68      private JTable jta;
69      private MediaType[] types =
70  		new MediaType[SearchManager.DEFAULT_MEDIA_TYPES.length - 1];
71  	
72      //--- Constructor(s) ---
73  
74      public MediaDownloadDirPanel()
75      {
76  		setLayout(new GridBagLayout());
77  
78  		System.arraycopy(SearchManager.DEFAULT_MEDIA_TYPES, 1, types, 0,
79  						 types.length);
80  
81  		// directory input field
82  		Box bxTop = new Box(BoxLayout.X_AXIS);
83  		jcbTypes = new JComboBox(types);
84  		jcbTypes.addActionListener(new ComboListener());
85  		bxTop.add(jcbTypes);
86  		dtfDir = new MyDirectoryPanel("", 20); 
87  		bxTop.add(dtfDir);
88  		JButton jbReset = new XNapButton(new ResetAction());
89  		jbReset.setMargin(new Insets(1, 1, 1, 1));
90  		bxTop.add(jbReset, BorderLayout.EAST);
91  
92  		// directory table
93  		mtm = new MediaTableModel();
94  		jta = mtm.createTable(prefs, "mediaDownloadDirs");
95  		ListSelectionModel model = jta.getSelectionModel();
96  		model.addListSelectionListener(new TableListener());
97  		model.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
98  		// set the height right
99  		jta.setPreferredScrollableViewportSize
100 			(new Dimension(0, (types.length - 1) * jta.getRowHeight()));
101 
102 		// panel
103 		GridBagHelper.add(this, bxTop);
104 		GridBagHelper.addPanel(this, new JScrollPane(jta));
105     }
106 
107     public void adjustWidth()
108     {
109 		mtm.setColumnWidth(MediaTableModel.MEDIA_TYPE, jcbTypes.getWidth());
110 		mtm.setColumnWidth(MediaTableModel.DOWNLOAD_DIR,
111 						   jta.getWidth() - jcbTypes.getWidth());
112 		jta.doLayout();
113     }
114 
115     public void apply() 
116     {
117 		for (int i = 0; i < types.length; i++) {
118 			prefs.setMediaTypeDownloadDir
119 				(types[i].getRealm(),
120 				 (String)mtm.get(i, MediaTableModel.DOWNLOAD_DIR));
121 		}
122     }
123 
124     private class MediaTableModel extends AbstractColumnTableModel
125     {
126 		public static final int MEDIA_TYPE = 0;
127 		public static final int DOWNLOAD_DIR = 1;
128 
129 		//--- Data field(s) ---
130 
131 		private Column columns[] = new Column[] {
132 			new Column("mediatype", XNap.tr("Mediatype"), String.class),
133 			new Column("downloaddir", XNap.tr("Download Directory"),
134 					   String.class),
135 		};
136 
137 		private String[] directories = new String[types.length];
138 
139 		//--- Constructor(s) ---
140 
141 		public MediaTableModel() 
142 		{
143 			addColumns(columns);
144 	    
145 			for (int i = 0; i < types.length; i++) {
146 				directories[i] =
147 					prefs.getMediaTypeDownloadDir(types[i].getRealm());
148 			}
149 			fireTableDataChanged();
150 		}
151 
152 		//--- Method(s) ---
153 
154 		public Object get(int row, int col) 
155 		{
156 			switch (col) {
157 			case MEDIA_TYPE:
158 				return types[row].getName();
159 			case DOWNLOAD_DIR:
160 				return directories[row];
161 			}
162 			return null;
163 		}
164 
165 		public void setColumnWidth(int i, int width)
166 		{
167 			getColumnAt(i).setPreferredWidth(width);
168 			getColumnAt(i).setWidth(width);
169 		}
170 
171 		public int mapFromIndex(int index)
172 		{
173 			return revIndexes[index];
174 		}
175 
176 		public int getRowCount() 
177 		{
178 			return types.length;
179 		}
180 
181 		/***
182 		 * Set new directories.
183 		 */
184 		public void set(String dir, int row, int col)
185 		{
186 			if (col == DOWNLOAD_DIR) {
187 				directories[row] = dir;
188 				fireTableRowsUpdated(row, row);
189 			}
190 		}
191     }
192 
193     private class ComboListener implements ActionListener
194     {
195 		public void actionPerformed(ActionEvent e) 
196 		{
197 			int index = mtm.mapFromIndex(jcbTypes.getSelectedIndex());
198 			jta.setRowSelectionInterval(index, index);
199 		}
200     }
201 
202     private class TableListener implements ListSelectionListener
203     {
204 		public void valueChanged(ListSelectionEvent e)
205 		{
206 			int index = jta.getSelectedRow();
207 			if (index != -1) {
208 				jcbTypes.setSelectedIndex(mtm.mapToIndex(index));
209 				String dir = (String)mtm.get(index,
210 											 MediaTableModel.DOWNLOAD_DIR);
211 				dtfDir.setDirectory(dir);
212 			}
213 		}
214        
215     }
216 
217     private class MyDirectoryPanel extends DirectoryPanel
218     {
219 		public MyDirectoryPanel(String text, int size) 
220 		{
221 			super(text, size);
222 			EnterAction ea = new EnterAction();
223 			KeyStroke k = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
224 			getTextField().getInputMap().put(k, ea);
225 			getTextField().getActionMap().put(ea, ea);
226 		}
227 	
228 		public void directorySelected(String dir)
229 		{
230 			int i = jcbTypes.getSelectedIndex();
231 			mtm.set(dir, i, MediaTableModel.DOWNLOAD_DIR);
232 		}
233 
234 		private class EnterAction extends AbstractAction
235 		{
236 			public void actionPerformed(ActionEvent event)
237 			{
238 				directorySelected(getDirectory());
239 			}
240 		}
241     }
242 
243     private class ResetAction extends AbstractAction
244     {
245 		public ResetAction()
246 		{
247 			putValue(Action.SHORT_DESCRIPTION, 
248 					 XNap.tr("Reset media directories"));
249 			putValue(IconHelper.XNAP_ICON, "undo.png");
250 		}
251 		
252 		public void actionPerformed(ActionEvent event)
253 		{
254 			for (int i = 0; i < types.length; i++) {
255 				mtm.set("", i, MediaTableModel.DOWNLOAD_DIR);
256 			}
257 		}
258     }
259 }