001/** 002 * 003 */ 004package icy.gui.dialog; 005 006import java.awt.BorderLayout; 007import java.awt.Dimension; 008import java.awt.Font; 009import java.awt.GridBagConstraints; 010import java.awt.GridBagLayout; 011import java.awt.Insets; 012import java.awt.event.MouseAdapter; 013import java.awt.event.MouseEvent; 014import java.util.ArrayList; 015import java.util.List; 016 017import javax.swing.JLabel; 018import javax.swing.JList; 019import javax.swing.JPanel; 020import javax.swing.JScrollPane; 021import javax.swing.ListSelectionModel; 022import javax.swing.ScrollPaneConstants; 023import javax.swing.border.EmptyBorder; 024 025import icy.file.FileUtil; 026import icy.main.Icy; 027import icy.plugin.PluginDescriptor; 028import icy.plugin.PluginLoader; 029 030/** 031 * Dialog to let the user select the appropriate importer to open a file when several importers are 032 * available. 033 * 034 * @author Stephane 035 */ 036public class ImporterSelectionDialog extends ActionDialog 037{ 038 /** 039 * 040 */ 041 private static final long serialVersionUID = -7233417975763216494L; 042 043 JList importerList; 044 JLabel pathLabel; 045 046 public ImporterSelectionDialog(List<? extends Object> importers, String path) 047 { 048 super("Select importer"); 049 050 initializeGui(); 051 052 pathLabel.setText(" " + FileUtil.getFileName(path)); 053 054 importerList.setListData(getItems(importers).toArray()); 055 if (importers.size() > 0) 056 importerList.setSelectedIndex(0); 057 058 importerList.addMouseListener(new MouseAdapter() 059 { 060 @Override 061 public void mouseClicked(MouseEvent e) 062 { 063 // double click ? 064 if ((e.getClickCount() == 2) && (e.getButton() == MouseEvent.BUTTON1)) 065 { 066 // have an item selected ? select it ! 067 if (importerList.getSelectedIndex() != -1) 068 getOkBtn().doClick(); 069 } 070 } 071 }); 072 073 setPreferredSize(new Dimension(360, 240)); 074 pack(); 075 setLocationRelativeTo(Icy.getMainInterface().getMainFrame()); 076 setVisible(true); 077 } 078 079 private void initializeGui() 080 { 081 setTitle("Importer selection"); 082 083 importerList = new JList(); 084 importerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 085 086 JScrollPane scrollPane = new JScrollPane(importerList); 087 scrollPane.setPreferredSize(new Dimension(320, 80)); 088 scrollPane.setMinimumSize(new Dimension(320, 80)); 089 scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 090 getMainPanel().add(scrollPane, BorderLayout.CENTER); 091 092 JPanel panel = new JPanel(); 093 panel.setBorder(new EmptyBorder(4, 0, 4, 0)); 094 getMainPanel().add(panel, BorderLayout.NORTH); 095 GridBagLayout gbl_panel = new GridBagLayout(); 096 gbl_panel.columnWidths = new int[] {0, 0}; 097 gbl_panel.rowHeights = new int[] {0, 0, 0}; 098 gbl_panel.columnWeights = new double[] {1.0, Double.MIN_VALUE}; 099 gbl_panel.rowWeights = new double[] {0.0, 0.0, Double.MIN_VALUE}; 100 panel.setLayout(gbl_panel); 101 102 JLabel newLabel = new JLabel(" Select the importer to open the following file:"); 103 newLabel.setFont(new Font("Tahoma", Font.BOLD, 11)); 104 GridBagConstraints gbc_newLabel = new GridBagConstraints(); 105 gbc_newLabel.anchor = GridBagConstraints.WEST; 106 gbc_newLabel.insets = new Insets(0, 0, 5, 0); 107 gbc_newLabel.gridx = 0; 108 gbc_newLabel.gridy = 0; 109 panel.add(newLabel, gbc_newLabel); 110 111 pathLabel = new JLabel(" "); 112 pathLabel.setFont(new Font("Tahoma", Font.PLAIN, 11)); 113 GridBagConstraints gbc_pathLabel = new GridBagConstraints(); 114 gbc_pathLabel.anchor = GridBagConstraints.WEST; 115 gbc_pathLabel.gridx = 0; 116 gbc_pathLabel.gridy = 1; 117 panel.add(pathLabel, gbc_pathLabel); 118 } 119 120 private List<ImporterPluginItem> getItems(List<? extends Object> importers) 121 { 122 final List<ImporterPluginItem> result = new ArrayList<ImporterPluginItem>(); 123 124 for (Object importer : importers) 125 { 126 final PluginDescriptor plugin = PluginLoader.getPlugin(importer.getClass().getName()); 127 if (plugin != null) 128 result.add(new ImporterPluginItem(plugin, importer)); 129 } 130 131 return result; 132 } 133 134 public Object getSelectedImporter() 135 { 136 return ((ImporterPluginItem) importerList.getSelectedValue()).getImporter(); 137 } 138 139 private class ImporterPluginItem 140 { 141 final PluginDescriptor plugin; 142 final Object importer; 143 144 ImporterPluginItem(PluginDescriptor plugin, Object importer) 145 { 146 super(); 147 148 this.plugin = plugin; 149 this.importer = importer; 150 } 151 152 public Object getImporter() 153 { 154 return importer; 155 } 156 157 @Override 158 public String toString() 159 { 160 return plugin.toString(); 161 } 162 } 163}