public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if(c instanceof JLabel) {
Object value = getValueAt(row, column);
if(value instanceof FileTypeLauncher) {
FileTypeLauncher fileTypeLauncher = (FileTypeLauncher)value;
JLabel label = (JLabel)c;
label.setIcon(fileTypeLauncher.getIcon());
label.setText(fileTypeLauncher.getName());
} else {
((JLabel)c).setIcon(null);
}
}
return c;
}
};
table.setRowHeight(FileTypeLauncher.getIconSize().height + 2);
ListSelectionModel selectionModel = table.getSelectionModel();
selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Object[][] data = new Object[fileTypeLaunchers.length][];
for(int i=0; i<fileTypeLaunchers.length; i++) {
FileTypeLauncher fileTypeLauncher = fileTypeLaunchers[i];
String[] registeredExtensions = fileTypeLauncher.getRegisteredExtensions();
StringBuilder sb = new StringBuilder();
for(int j=0; j<registeredExtensions.length; j++) {
if(j > 0) {
sb.append(", ");
}
sb.append(registeredExtensions[j]);
}
data[i] = new Object[] {fileTypeLauncher, sb.toString()};
}
DefaultTableModel tableModel = new DefaultTableModel(data, new Object[] {"File Type Launcher", "Registered Extensions"}) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
table.setModel(new TableSorter(tableModel, table.getTableHeader()) {
@Override
protected Comparator getComparator(int column) {
if(column == 0) {
return new Comparator<FileTypeLauncher>() {
public int compare(FileTypeLauncher o1, FileTypeLauncher o2) {
return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase());
}
};
}
return super.getComparator(column);
}
});
mainPane.add(new JScrollPane(table), BorderLayout.CENTER);
JPanel fileLaunchPanel = new JPanel(new BorderLayout());
fileLaunchPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 0, 4));
fileLaunchPanel.add(new JLabel("File: "), BorderLayout.WEST);
final JTextField fileLaunchTextField = new JTextField();
fileLaunchPanel.add(fileLaunchTextField, BorderLayout.CENTER);
JButton browseButton = new JButton("...");
browseButton.addActionListener(new ActionListener() {
JFileChooser fileChooser;
public void actionPerformed(ActionEvent e) {
if(fileChooser == null) {
fileChooser = new JFileChooser();
}
if(fileChooser.showOpenDialog(contentPane) == JFileChooser.APPROVE_OPTION) {
fileLaunchTextField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
}
});
fileLaunchPanel.add(browseButton, BorderLayout.EAST);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));
final JButton launchButton = new JButton("Launch");
launchButton.setEnabled(false);
launchButton.setIcon(EMPTY_ICON);
launchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FileTypeLauncher launcher = (FileTypeLauncher)table.getValueAt(table.getSelectedRow(), table.convertColumnIndexToView(0));
launcher.launch(fileLaunchTextField.getText());
}
});
buttonPanel.add(launchButton);
selectionModel.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
launchButton.setEnabled(table.getSelectedRow() != -1);
}
});
final JButton launchAssociatedButton = new JButton("Launch Associated Handler");
launchAssociatedButton.setEnabled(false);
launchAssociatedButton.setIcon(EMPTY_ICON);
launchAssociatedButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fileName = fileLaunchTextField.getText();
FileTypeLauncher.getLauncher(fileName).launch(fileName);
}
});
fileLaunchTextField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
adjustState();
}
public void insertUpdate(DocumentEvent e) {
adjustState();
}
public void removeUpdate(DocumentEvent e) {
adjustState();
}
protected void adjustState() {
FileTypeLauncher launcher = FileTypeLauncher.getLauncher(fileLaunchTextField.getText());
launchAssociatedButton.setEnabled(launcher != null);
launchAssociatedButton.setText(launcher == null? "Launch Associated Handler": "Launch " + launcher.getName());
launchAssociatedButton.setIcon(launcher == null? EMPTY_ICON: launcher.getIcon());
}
});
buttonPanel.add(launchAssociatedButton);
fileLaunchPanel.add(buttonPanel, BorderLayout.SOUTH);
mainPane.add(fileLaunchPanel, BorderLayout.SOUTH);