Coverage Report - org.homeunix.thecave.buddi.plugin.builtin.preference.PluginPreferences
 
Classes in this File Line Coverage Branch Coverage Complexity
PluginPreferences
64%
53/82
34%
11/32
3.636
PluginPreferences$1
25%
1/4
0%
0/4
3.636
PluginPreferences$2
12%
3/24
0%
0/14
3.636
PluginPreferences$3
100%
5/5
75%
3/4
3.636
 
 1  
 /*
 2  
  * Created on Aug 12, 2007 by wyatt
 3  
  */
 4  
 package org.homeunix.thecave.buddi.plugin.builtin.preference;
 5  
 
 6  
 import java.awt.BorderLayout;
 7  
 import java.awt.Component;
 8  
 import java.awt.Dimension;
 9  
 import java.awt.FlowLayout;
 10  
 import java.awt.event.ActionEvent;
 11  
 import java.awt.event.ActionListener;
 12  
 import java.io.File;
 13  
 import java.io.IOException;
 14  
 import java.io.InputStream;
 15  
 import java.util.HashMap;
 16  
 import java.util.LinkedList;
 17  
 import java.util.List;
 18  
 import java.util.Map;
 19  
 import java.util.Properties;
 20  
 
 21  
 import javax.swing.DefaultListCellRenderer;
 22  
 import javax.swing.JButton;
 23  
 import javax.swing.JFileChooser;
 24  
 import javax.swing.JList;
 25  
 import javax.swing.JOptionPane;
 26  
 import javax.swing.JPanel;
 27  
 import javax.swing.JScrollPane;
 28  
 import javax.swing.event.ListSelectionEvent;
 29  
 import javax.swing.event.ListSelectionListener;
 30  
 import javax.swing.filechooser.FileFilter;
 31  
 
 32  
 import org.homeunix.thecave.buddi.Buddi;
 33  
 import org.homeunix.thecave.buddi.Const;
 34  
 import org.homeunix.thecave.buddi.i18n.BuddiKeys;
 35  
 import org.homeunix.thecave.buddi.i18n.keys.ButtonKeys;
 36  
 import org.homeunix.thecave.buddi.model.prefs.PrefsModel;
 37  
 import org.homeunix.thecave.buddi.plugin.BuddiPluginFactory;
 38  
 import org.homeunix.thecave.buddi.plugin.api.BuddiPreferencePlugin;
 39  
 import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter;
 40  
 import org.homeunix.thecave.buddi.util.FileFunctions;
 41  
 
 42  
 import ca.digitalcave.moss.application.plugin.MossPlugin;
 43  
 import ca.digitalcave.moss.common.ClassLoaderFunctions;
 44  
 import ca.digitalcave.moss.swing.model.DefaultGenericListModel;
 45  
 
 46  0
 public class PluginPreferences extends BuddiPreferencePlugin implements ActionListener {
 47  
         public static final long serialVersionUID = 0;
 48  
 
 49  
         private final JButton addButton;
 50  
         private final JButton removeButton;
 51  
         private final JList pluginList;
 52  
         private final DefaultGenericListModel<File> pluginListModel;
 53  
 
 54  695
         private final List<File> filesToAdd = new LinkedList<File>();
 55  695
         private final List<File> filesToRemove = new LinkedList<File>();
 56  
 
 57  695
         public PluginPreferences() {
 58  695
                 addButton = new JButton(PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_ADD));
 59  695
                 removeButton = new JButton(PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_REMOVE));
 60  695
                 pluginListModel = new DefaultGenericListModel<File>();
 61  695
                 pluginList = new JList(pluginListModel);
 62  695
         }
 63  
 
 64  
         public JPanel getPreferencesPanel() {
 65  695
                 JPanel panel = new JPanel();
 66  695
                 panel.setLayout(new BorderLayout());
 67  
 
 68  695
                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
 69  
 
 70  695
                 Dimension buttonSize = new Dimension(Math.max(100, removeButton.getPreferredSize().width), removeButton.getPreferredSize().height);
 71  695
                 addButton.setPreferredSize(buttonSize);
 72  695
                 removeButton.setPreferredSize(buttonSize);
 73  695
                 removeButton.setEnabled(false);
 74  695
                 buttonPanel.add(addButton);
 75  695
                 buttonPanel.add(removeButton);
 76  
 
 77  695
                 addButton.addActionListener(this);
 78  695
                 removeButton.addActionListener(this);
 79  
 
 80  695
                 JScrollPane pluginListScroller = new JScrollPane(pluginList);
 81  
 
 82  695
                 pluginList.addListSelectionListener(new ListSelectionListener(){
 83  
                         public void valueChanged(ListSelectionEvent e) {
 84  0
                                 if (!e.getValueIsAdjusting()){
 85  0
                                         removeButton.setEnabled(pluginList.getSelectedIndex() != -1);
 86  
                                 }
 87  0
                         }
 88  
                 });
 89  
 
 90  695
                 pluginList.setCellRenderer(new DefaultListCellRenderer(){
 91  
                         public static final long serialVersionUID = 0;
 92  695
                         private final Map<File, String> fileToVersionMap = new HashMap<File, String>();
 93  695
                         private final Map<File, String> fileToNameMap = new HashMap<File, String>();
 94  
 
 95  
                         @Override
 96  
                         public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
 97  0
                                 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
 98  
 
 99  0
                                 if (value instanceof File && ((File) value).getName().endsWith(Const.PLUGIN_EXTENSION)){
 100  0
                                         File file = (File) value;
 101  0
                                         if (fileToVersionMap.get(file) == null){
 102  0
                                                 String version = ""; 
 103  0
                                                 Properties p = new Properties();
 104  0
                                                 InputStream is = ClassLoaderFunctions.getResourceAsStreamFromJar(file, Const.PLUGIN_PROPERTIES);
 105  0
                                                 if (is != null) {
 106  
                                                         try {
 107  0
                                                                 p.load(is);
 108  0
                                                                 if (p.getProperty(Const.PLUGIN_PROPERTIES_VERSION) != null)
 109  0
                                                                         version = "  (" + p.getProperty(Const.PLUGIN_PROPERTIES_VERSION) + ")";
 110  0
                                                                 fileToVersionMap.put(file, version);
 111  
                                                         }
 112  0
                                                         catch (IOException ioe){}
 113  
                                                 }
 114  
                                         }
 115  0
                                         if (fileToNameMap.get(file) == null){
 116  0
                                                 fileToNameMap.put(file, file.getName().replaceAll(Const.PLUGIN_EXTENSION, ""));
 117  
                                         }
 118  0
                                         if (fileToVersionMap.get(file) == null){
 119  0
                                                 fileToVersionMap.put(file, "");
 120  
                                         }
 121  
                                         
 122  0
                                         this.setText(fileToNameMap.get(file) + fileToVersionMap.get(file));
 123  0
                                 }
 124  
                                 else 
 125  0
                                         this.setText("Not of correct file type");
 126  
 
 127  0
                                 return this;
 128  
                         }
 129  
                 });
 130  
 
 131  695
                 panel.add(pluginListScroller, BorderLayout.CENTER);
 132  695
                 panel.add(buttonPanel, BorderLayout.SOUTH);
 133  
 
 134  695
                 return panel;
 135  
         }
 136  
 
 137  
         public void load() {
 138  695
                 pluginListModel.clear();
 139  695
                 for (File file : BuddiPluginFactory.getPluginFiles()) {
 140  0
                         pluginListModel.addElement(file);
 141  
                 }                
 142  695
                 pluginList.setSelectedIndices(new int[0]);
 143  695
         }
 144  
 
 145  
         public boolean save() {
 146  
                 //At save time, we actually copy the files over to the new location, and remove ones which
 147  
                 // are to be removed.                
 148  43
                 for (File f : filesToRemove) {
 149  0
                         if (!f.delete()){
 150  0
                                 f.deleteOnExit();
 151  0
                                 String[] options = new String[1];
 152  0
                                 options[0] = PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_OK);
 153  0
                                 JOptionPane.showOptionDialog(
 154  
                                                 null, 
 155  
                                                 TextFormatter.getTranslation(BuddiKeys.MESSAGE_ERROR_DELETING_PLUGIN_POSTPONED), 
 156  
                                                 TextFormatter.getTranslation(BuddiKeys.MESSAGE_ERROR_DELETING_PLUGIN_TITLE), 
 157  
                                                 JOptionPane.DEFAULT_OPTION,
 158  
                                                 JOptionPane.ERROR_MESSAGE,
 159  
                                                 null,
 160  
                                                 options,
 161  
                                                 options[0]
 162  
                                 );
 163  0
                         }
 164  
                 }
 165  
 
 166  43
                 if (!Buddi.getPluginsFolder().exists()){
 167  0
                         if (!Buddi.getPluginsFolder().mkdirs()){
 168  0
                                 String[] options = new String[1];
 169  0
                                 options[0] = PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_OK);
 170  
 
 171  0
                                 JOptionPane.showOptionDialog(
 172  
                                                 null, 
 173  
                                                 TextFormatter.getTranslation(BuddiKeys.MESSAGE_ERROR_DELETING_PLUGIN), 
 174  
                                                 TextFormatter.getTranslation(BuddiKeys.MESSAGE_ERROR_DELETING_PLUGIN_TITLE), 
 175  
                                                 JOptionPane.DEFAULT_OPTION,
 176  
                                                 JOptionPane.ERROR_MESSAGE,
 177  
                                                 null,
 178  
                                                 options,
 179  
                                                 options[0]
 180  
                                 );
 181  
                         }
 182  
                 }
 183  
 
 184  
 
 185  43
                 for (File f : filesToAdd) {
 186  
                         try {
 187  0
                                 FileFunctions.copyFile(f, new File(Buddi.getPluginsFolder().getAbsolutePath() + File.separator + f.getName()));
 188  
                         }
 189  0
                         catch (IOException ioe){
 190  0
                                 String[] options = new String[1];
 191  0
                                 options[0] = PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_OK);
 192  
 
 193  0
                                 JOptionPane.showOptionDialog(
 194  
                                                 null, 
 195  
                                                 TextFormatter.getTranslation(BuddiKeys.MESSAGE_ERROR_COPYING_PLUGIN), 
 196  
                                                 TextFormatter.getTranslation(BuddiKeys.MESSAGE_ERROR_COPYING_PLUGIN_TITLE), 
 197  
                                                 JOptionPane.DEFAULT_OPTION,
 198  
                                                 JOptionPane.ERROR_MESSAGE,
 199  
                                                 null,
 200  
                                                 options,
 201  
                                                 options[0]
 202  
                                 );
 203  0
                         }
 204  
                 }
 205  
 
 206  43
                 BuddiPluginFactory.forcePluginRefresh();
 207  
                 
 208  43
                 return filesToAdd.size() > 0 || filesToRemove.size() > 0;
 209  
         }
 210  
 
 211  
         public String getName() {
 212  695
                 return BuddiKeys.PLUGINS.toString();
 213  
         }
 214  
 
 215  
         public void actionPerformed(ActionEvent e) {
 216  119
                 if (e.getSource().equals(addButton)){
 217  119
                         final JFileChooser jfc = new JFileChooser();
 218  119
                         jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
 219  119
                         jfc.setFileFilter(new FileFilter(){
 220  
                                 public boolean accept(File arg0) {
 221  6565
                                         if (arg0.getAbsolutePath().endsWith(Const.PLUGIN_EXTENSION) ||
 222  
                                                         arg0.isDirectory())
 223  5875
                                                 return true;
 224  
                                         else
 225  690
                                                 return false;
 226  
                                 }
 227  
 
 228  
                                 @Override
 229  
                                 public String getDescription() {
 230  447
                                         return PrefsModel.getInstance().getTranslator().get(BuddiKeys.FILE_DESCRIPTION_BUDDI_PLUGINS);
 231  
                                 }
 232  
                         });
 233  119
                         jfc.setDialogTitle(PrefsModel.getInstance().getTranslator().get(BuddiKeys.CHOOSE_PLUGIN_JAR));
 234  119
                         if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
 235  1
                                 Properties props = new Properties();
 236  
                                 try {
 237  1
                                         InputStream is = ClassLoaderFunctions.getResourceAsStreamFromJar(jfc.getSelectedFile(), Const.PLUGIN_PROPERTIES);
 238  1
                                         if (is != null)
 239  0
                                                 props.load(is);
 240  
                                 }
 241  1
                                 catch (IOException ioe){}                                
 242  1
                                 List<MossPlugin> plugins = BuddiPluginFactory.getMossPluginsFromJar(jfc.getSelectedFile(), Buddi.getVersion(), props.getProperty(Const.PLUGIN_PROPERTIES_ROOT));
 243  1
                                 if (plugins.size() == 0){
 244  1
                                         String[] options = new String[1];
 245  1
                                         options[0] = PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_OK);
 246  
 
 247  1
                                         JOptionPane.showOptionDialog(
 248  
                                                         null, 
 249  
                                                         PrefsModel.getInstance().getTranslator().get(BuddiKeys.NO_PLUGINS_IN_JAR), 
 250  
                                                         PrefsModel.getInstance().getTranslator().get(BuddiKeys.NO_PLUGINS_IN_JAR_TITLE), 
 251  
                                                         JOptionPane.DEFAULT_OPTION,
 252  
                                                         JOptionPane.WARNING_MESSAGE,
 253  
                                                         null,
 254  
                                                         options,
 255  
                                                         options[0]
 256  
                                         );
 257  0
                                 }
 258  
                                 else {
 259  0
                                         filesToAdd.add(jfc.getSelectedFile());
 260  0
                                         pluginListModel.addElement(jfc.getSelectedFile());
 261  
                                 }
 262  
                         }
 263  19
                 }
 264  0
                 else if (e.getSource().equals(removeButton)){
 265  0
                         if (pluginList.getSelectedValues().length > 0) {
 266  0
                                 for (Object o : pluginList.getSelectedValues()) {
 267  0
                                         filesToAdd.remove(o);
 268  0
                                         filesToRemove.add((File) o);
 269  
                                 }
 270  
                         }
 271  
                         
 272  0
                         for (File f : filesToRemove) {
 273  0
                                 pluginListModel.removeElement(f);
 274  
                         }
 275  
                 }
 276  19
         }
 277  
 
 278  
         @Override
 279  
         public boolean isUseWrapper() {
 280  695
                 return false;
 281  
         }
 282  
 }