~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/system/plugins/TGPluginManager.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.gui.system.plugins;
 
2
 
 
3
import java.util.ArrayList;
 
4
import java.util.Iterator;
 
5
import java.util.List;
 
6
 
 
7
import org.herac.tuxguitar.gui.util.MessageDialog;
 
8
import org.herac.tuxguitar.util.TGClassLoader;
 
9
import org.herac.tuxguitar.util.TGServiceReader;
 
10
 
 
11
public class TGPluginManager {
 
12
        
 
13
        private List plugins;
 
14
        
 
15
        public TGPluginManager(){
 
16
                this.plugins = new ArrayList();
 
17
                this.initPLugins();
 
18
        }
 
19
        
 
20
        public List getPlugins(){
 
21
                return this.plugins;
 
22
        }
 
23
        
 
24
        public void initPLugins(){
 
25
                try{
 
26
                        //Search available providers
 
27
                        Iterator it = TGServiceReader.lookupProviders(TGPlugin.class,TGClassLoader.instance().getClassLoader());
 
28
                        while(it.hasNext()){
 
29
                                try{
 
30
                                        TGPlugin plugin = (TGPlugin)it.next();
 
31
                                        plugin.init();
 
32
                                        this.plugins.add(plugin);
 
33
                                }catch(TGPluginException exception){
 
34
                                        MessageDialog.errorMessage(exception);
 
35
                                }catch(Throwable throwable){
 
36
                                        MessageDialog.errorMessage(new TGPluginException("An error ocurred when trying to init plugin",throwable));
 
37
                                }
 
38
                        }
 
39
                }catch(Throwable throwable){
 
40
                        MessageDialog.errorMessage(new TGPluginException("An error ocurred when trying to init plugin",throwable));
 
41
                }
 
42
        }
 
43
        
 
44
        public void closePLugins(){
 
45
                Iterator it = this.plugins.iterator();
 
46
                while(it.hasNext()){
 
47
                        try{
 
48
                                ((TGPlugin)it.next()).close();
 
49
                        }catch(TGPluginException exception){
 
50
                                MessageDialog.errorMessage(exception);
 
51
                        }catch(Throwable throwable){
 
52
                                MessageDialog.errorMessage(new TGPluginException("An error ocurred when trying to close plugin",throwable));
 
53
                        }
 
54
                }
 
55
        }
 
56
        
 
57
        public void openPlugins(){
 
58
                Iterator it = this.plugins.iterator();
 
59
                while(it.hasNext()){
 
60
                        try{
 
61
                                TGPlugin plugin = (TGPlugin)it.next();
 
62
                                plugin.setEnabled(isEnabled(plugin));
 
63
                        }catch(TGPluginException exception){
 
64
                                MessageDialog.errorMessage(exception);
 
65
                        }catch(Throwable throwable){
 
66
                                MessageDialog.errorMessage(new TGPluginException("An error ocurred when trying to set plugin status",throwable));
 
67
                        }
 
68
                }
 
69
        }
 
70
        
 
71
        public void setEnabled(TGPlugin plugin,boolean enabled){
 
72
                try{
 
73
                        TGPluginProperties.instance().setProperty(getEnabledProperty(plugin),enabled);
 
74
                        TGPluginProperties.instance().save();
 
75
                        plugin.setEnabled(enabled);
 
76
                }catch(TGPluginException exception){
 
77
                        MessageDialog.errorMessage(exception);
 
78
                }catch(Throwable throwable){
 
79
                        MessageDialog.errorMessage(new TGPluginException("An error ocurred when trying to set plugin status",throwable));
 
80
                }
 
81
        }
 
82
        
 
83
        public boolean isEnabled(TGPlugin plugin){
 
84
                try{
 
85
                        return TGPluginProperties.instance().getBooleanConfigValue(getEnabledProperty(plugin),true);
 
86
                }catch(Throwable throwable){
 
87
                        MessageDialog.errorMessage(new TGPluginException("An error ocurred when trying to get plugin status",throwable));
 
88
                }
 
89
                return false;
 
90
        }
 
91
        
 
92
        public String getEnabledProperty(TGPlugin plugin){
 
93
                return (plugin.getClass().getName() + ".enabled");
 
94
        }
 
95
        
 
96
}