MediaDownloadDirPanel.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui.prefs |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.gui.prefs;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.xnap.XNap;
import org.xnap.gui.component.DirectoryPanel;
import org.xnap.gui.component.XNapButton;
import org.xnap.gui.table.AbstractColumnTableModel;
import org.xnap.gui.table.Column;
import org.xnap.gui.util.GridBagHelper;
import org.xnap.gui.util.IconHelper;
import org.xnap.search.MediaType;
import org.xnap.search.SearchManager;
import org.xnap.util.Preferences;
/**
*
*/
public class MediaDownloadDirPanel extends JPanel
{
//--- Data field(s) ---
private static Preferences prefs = Preferences.getInstance();
private MyDirectoryPanel dtfDir;
private JComboBox jcbTypes;
private MediaTableModel mtm;
private JTable jta;
private MediaType[] types =
new MediaType[SearchManager.DEFAULT_MEDIA_TYPES.length - 1];
//--- Constructor(s) ---
public MediaDownloadDirPanel()
{
setLayout(new GridBagLayout());
System.arraycopy(SearchManager.DEFAULT_MEDIA_TYPES, 1, types, 0,
types.length);
// directory input field
Box bxTop = new Box(BoxLayout.X_AXIS);
jcbTypes = new JComboBox(types);
jcbTypes.addActionListener(new ComboListener());
bxTop.add(jcbTypes);
dtfDir = new MyDirectoryPanel("", 20);
bxTop.add(dtfDir);
JButton jbReset = new XNapButton(new ResetAction());
jbReset.setMargin(new Insets(1, 1, 1, 1));
bxTop.add(jbReset, BorderLayout.EAST);
// directory table
mtm = new MediaTableModel();
jta = mtm.createTable(prefs, "mediaDownloadDirs");
ListSelectionModel model = jta.getSelectionModel();
model.addListSelectionListener(new TableListener());
model.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// set the height right
jta.setPreferredScrollableViewportSize
(new Dimension(0, (types.length - 1) * jta.getRowHeight()));
// panel
GridBagHelper.add(this, bxTop);
GridBagHelper.addPanel(this, new JScrollPane(jta));
}
public void adjustWidth()
{
mtm.setColumnWidth(MediaTableModel.MEDIA_TYPE, jcbTypes.getWidth());
mtm.setColumnWidth(MediaTableModel.DOWNLOAD_DIR,
jta.getWidth() - jcbTypes.getWidth());
jta.doLayout();
}
public void apply()
{
for (int i = 0; i < types.length; i++) {
prefs.setMediaTypeDownloadDir
(types[i].getRealm(),
(String)mtm.get(i, MediaTableModel.DOWNLOAD_DIR));
}
}
private class MediaTableModel extends AbstractColumnTableModel
{
public static final int MEDIA_TYPE = 0;
public static final int DOWNLOAD_DIR = 1;
//--- Data field(s) ---
private Column columns[] = new Column[] {
new Column("mediatype", XNap.tr("Mediatype"), String.class),
new Column("downloaddir", XNap.tr("Download Directory"),
String.class),
};
private String[] directories = new String[types.length];
//--- Constructor(s) ---
public MediaTableModel()
{
addColumns(columns);
for (int i = 0; i < types.length; i++) {
directories[i] =
prefs.getMediaTypeDownloadDir(types[i].getRealm());
}
fireTableDataChanged();
}
//--- Method(s) ---
public Object get(int row, int col)
{
switch (col) {
case MEDIA_TYPE:
return types[row].getName();
case DOWNLOAD_DIR:
return directories[row];
}
return null;
}
public void setColumnWidth(int i, int width)
{
getColumnAt(i).setPreferredWidth(width);
getColumnAt(i).setWidth(width);
}
public int mapFromIndex(int index)
{
return revIndexes[index];
}
public int getRowCount()
{
return types.length;
}
/**
* Set new directories.
*/
public void set(String dir, int row, int col)
{
if (col == DOWNLOAD_DIR) {
directories[row] = dir;
fireTableRowsUpdated(row, row);
}
}
}
private class ComboListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int index = mtm.mapFromIndex(jcbTypes.getSelectedIndex());
jta.setRowSelectionInterval(index, index);
}
}
private class TableListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
int index = jta.getSelectedRow();
if (index != -1) {
jcbTypes.setSelectedIndex(mtm.mapToIndex(index));
String dir = (String)mtm.get(index,
MediaTableModel.DOWNLOAD_DIR);
dtfDir.setDirectory(dir);
}
}
}
private class MyDirectoryPanel extends DirectoryPanel
{
public MyDirectoryPanel(String text, int size)
{
super(text, size);
EnterAction ea = new EnterAction();
KeyStroke k = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
getTextField().getInputMap().put(k, ea);
getTextField().getActionMap().put(ea, ea);
}
public void directorySelected(String dir)
{
int i = jcbTypes.getSelectedIndex();
mtm.set(dir, i, MediaTableModel.DOWNLOAD_DIR);
}
private class EnterAction extends AbstractAction
{
public void actionPerformed(ActionEvent event)
{
directorySelected(getDirectory());
}
}
}
private class ResetAction extends AbstractAction
{
public ResetAction()
{
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Reset media directories"));
putValue(IconHelper.XNAP_ICON, "undo.png");
}
public void actionPerformed(ActionEvent event)
{
for (int i = 0; i < types.length; i++) {
mtm.set("", i, MediaTableModel.DOWNLOAD_DIR);
}
}
}
}
The table below shows all metrics for MediaDownloadDirPanel.java.



