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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package org.herac.tuxguitar.gui.system.plugins;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.herac.tuxguitar.gui.util.MessageDialog;
import org.herac.tuxguitar.util.TGClassLoader;
import org.herac.tuxguitar.util.TGServiceReader;

public class TGPluginManager {
	
	private List plugins;
	
	public TGPluginManager(){
		this.plugins = new ArrayList();
		this.initPLugins();
	}
	
	public List getPlugins(){
		return this.plugins;
	}
	
	public void initPLugins(){
		try{
			//Search available providers
			Iterator it = TGServiceReader.lookupProviders(TGPlugin.class,TGClassLoader.instance().getClassLoader());
			while(it.hasNext()){
				try{
					TGPlugin plugin = (TGPlugin)it.next();
					plugin.init();
					this.plugins.add(plugin);
				}catch(TGPluginException exception){
					MessageDialog.errorMessage(exception);
				}catch(Throwable throwable){
					MessageDialog.errorMessage(new TGPluginException("An error ocurred when trying to init plugin",throwable));
				}
			}
		}catch(Throwable throwable){
			MessageDialog.errorMessage(new TGPluginException("An error ocurred when trying to init plugin",throwable));
		}
	}
	
	public void closePLugins(){
		Iterator it = this.plugins.iterator();
		while(it.hasNext()){
			try{
				((TGPlugin)it.next()).close();
			}catch(TGPluginException exception){
				MessageDialog.errorMessage(exception);
			}catch(Throwable throwable){
				MessageDialog.errorMessage(new TGPluginException("An error ocurred when trying to close plugin",throwable));
			}
		}
	}
	
	public void openPlugins(){
		Iterator it = this.plugins.iterator();
		while(it.hasNext()){
			try{
				TGPlugin plugin = (TGPlugin)it.next();
				plugin.setEnabled(isEnabled(plugin));
			}catch(TGPluginException exception){
				MessageDialog.errorMessage(exception);
			}catch(Throwable throwable){
				MessageDialog.errorMessage(new TGPluginException("An error ocurred when trying to set plugin status",throwable));
			}
		}
	}
	
	public void setEnabled(TGPlugin plugin,boolean enabled){
		try{
			TGPluginProperties.instance().setProperty(getEnabledProperty(plugin),enabled);
			TGPluginProperties.instance().save();
			plugin.setEnabled(enabled);
		}catch(TGPluginException exception){
			MessageDialog.errorMessage(exception);
		}catch(Throwable throwable){
			MessageDialog.errorMessage(new TGPluginException("An error ocurred when trying to set plugin status",throwable));
		}
	}
	
	public boolean isEnabled(TGPlugin plugin){
		try{
			return TGPluginProperties.instance().getBooleanConfigValue(getEnabledProperty(plugin),true);
		}catch(Throwable throwable){
			MessageDialog.errorMessage(new TGPluginException("An error ocurred when trying to get plugin status",throwable));
		}
		return false;
	}
	
	public String getEnabledProperty(TGPlugin plugin){
		return (plugin.getClass().getName() + ".enabled");
	}
	
}