FileDocCategorySizeDatePackage
ConfigSectionPlugins.javaAPI DocAzureus 3.0.3.420819Sun Aug 12 22:11:44 BST 2007org.gudy.azureus2.ui.swt.views.configsections

ConfigSectionPlugins

public class ConfigSectionPlugins extends Object implements org.gudy.azureus2.ui.swt.plugins.UISWTConfigSection
Configuration Section that lists all the plugins and sets up subsections for plugins that used the PluginConfigModel object. Moved from ConfigView
author
TuxPaper

Fields Summary
private static final String
HEADER_PREFIX
private static final String[]
COLUMN_HEADERS
private static final int[]
COLUMN_SIZES
private static final int[]
COLUMN_ALIGNS
private org.gudy.azureus2.ui.swt.views.ConfigView
configView
private com.aelitis.azureus.core.AzureusCore
azureusCore
FilterComparator
comparator
List
pluginIFs
Constructors Summary
public ConfigSectionPlugins(org.gudy.azureus2.ui.swt.views.ConfigView _configView, com.aelitis.azureus.core.AzureusCore _azureusCore)
Initialize

param
_configView

		configView = _configView;
		azureusCore = _azureusCore;
		comparator = new FilterComparator();
	
Methods Summary
public CompositeconfigSectionCreate(Composite parent)

		GridLayout layout;
		GridData gridData;

		Label label;

		Composite infoGroup = new Composite(parent, SWT.NULL);
		gridData = new GridData(GridData.VERTICAL_ALIGN_FILL
				| GridData.HORIZONTAL_ALIGN_FILL);
		infoGroup.setLayoutData(gridData);
		layout = new GridLayout();
		layout.numColumns = 1;
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		infoGroup.setLayout(layout);

		infoGroup.setLayout(new GridLayout());
		if (SWT.getVersion() < 3105) { // screws up scrolling on 3.2M2
			infoGroup.addControlListener(new Utils.LabelWrapControlListener());
		}

		String sep = System.getProperty("file.separator");

		File fUserPluginDir = FileUtil.getUserFile("plugins");
		
		String sUserPluginDir;
		
		try{
			sUserPluginDir = fUserPluginDir.getCanonicalPath();
		}catch( Throwable e ){
			sUserPluginDir = fUserPluginDir.toString();
		}
		
		if (!sUserPluginDir.endsWith(sep)) {
			sUserPluginDir += sep;
		}

		File fAppPluginDir = FileUtil.getApplicationFile("plugins");
		
		String sAppPluginDir;
		
		try{
			sAppPluginDir = fAppPluginDir.getCanonicalPath();
		}catch( Throwable e ){
			sAppPluginDir = fAppPluginDir.toString();
		}

		if (!sAppPluginDir.endsWith(sep)) {
			sAppPluginDir += sep;
		}

		label = new Label(infoGroup, SWT.WRAP);
		label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		Messages.setLanguageText(label, "ConfigView.pluginlist.whereToPut");

		label = new Label(infoGroup, SWT.WRAP);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalIndent = 10;
		label.setLayoutData(gridData);
		label.setText(sUserPluginDir.replaceAll("&", "&&"));
		label.setForeground(Colors.blue);
		label.setCursor(Cursors.handCursor);

		final String _sUserPluginDir = sUserPluginDir;

		//TODO : Fix it for windows
		label.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent arg0) {
				if (_sUserPluginDir.endsWith("/plugins/")
						|| _sUserPluginDir.endsWith("\\plugins\\")) {
					File f = new File(_sUserPluginDir);
					if (f.exists() && f.isDirectory()) {
						Utils.launch(_sUserPluginDir);
					} else {
						String azureusDir = _sUserPluginDir.substring(0, _sUserPluginDir
								.length() - 9);
						System.out.println(azureusDir);
						Utils.launch(azureusDir);
					}
				}
			}
		});

		label = new Label(infoGroup, SWT.WRAP);
		label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		Messages.setLanguageText(label, "ConfigView.pluginlist.whereToPutOr");

		label = new Label(infoGroup, SWT.WRAP);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalIndent = 10;
		label.setLayoutData(gridData);
		label.setText(sAppPluginDir.replaceAll("&", "&&"));
		label.setForeground(Colors.blue);
		label.setCursor(Cursors.handCursor);

		final String _sAppPluginDir = sAppPluginDir;

		//TODO : Fix it for windows
		label.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent arg0) {
				if (_sAppPluginDir.endsWith("/plugins/")
						|| _sAppPluginDir.endsWith("\\plugins\\")) {
					File f = new File(_sAppPluginDir);
					if (f.exists() && f.isDirectory()) {
						Utils.launch(_sAppPluginDir);
					} else {
						String azureusDir = _sAppPluginDir.substring(0, _sAppPluginDir
								.length() - 9);
						System.out.println(azureusDir);
						Utils.launch(azureusDir);
					}
				}
			}
		});

		pluginIFs = Arrays.asList(azureusCore.getPluginManager().getPlugins());

		Collections.sort(pluginIFs, new Comparator() {
			public int compare(Object o1, Object o2) {
				return (((PluginInterface) o1).getPluginName()
						.compareToIgnoreCase(((PluginInterface) o2).getPluginName()));
			}
		});

		Label labelInfo = new Label(infoGroup, SWT.WRAP);
		labelInfo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		Messages.setLanguageText(labelInfo, "ConfigView.pluginlist.info");

		final Table table = new Table(infoGroup, SWT.BORDER | SWT.SINGLE
				| SWT.CHECK | SWT.VIRTUAL | SWT.FULL_SELECTION);
		gridData = new GridData(GridData.FILL_BOTH);
		gridData.heightHint = 200;
		gridData.widthHint = 200;
		table.setLayoutData(gridData);
		for (int i = 0; i < COLUMN_HEADERS.length; i++) {
			final TableColumn tc = new TableColumn(table, COLUMN_ALIGNS[i]);
			tc.setWidth(COLUMN_SIZES[i]);
			tc.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					boolean ascending = comparator.setField(table.indexOf(tc));
					try {
						table.setSortColumn(tc);
						table.setSortDirection(ascending ? SWT.UP : SWT.DOWN);
					} catch (NoSuchMethodError ignore) {
						// Ignore Pre 3.0
					}
					Collections.sort(pluginIFs, comparator);
					table.clearAll();
				}
			});
			Messages.setLanguageText(tc, HEADER_PREFIX + COLUMN_HEADERS[i]);
		}
		table.setHeaderVisible(true);
		
		Composite cButtons = new Composite(infoGroup, SWT.NONE);
		layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		layout.numColumns = 3;
		cButtons.setLayout(layout);
		cButtons.setLayoutData(new GridData());
		
		final Button btnUnload = new Button(cButtons, SWT.PUSH);
		btnUnload.setLayoutData(new GridData());
		Messages.setLanguageText(btnUnload, "ConfigView.pluginlist.unloadSelected");
		btnUnload.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int[] items = table.getSelectionIndices();
				for (int i = 0; i < items.length; i++) {
					int index = items[i];
					if (index >= 0 && index < pluginIFs.size()) {
						PluginInterface pluginIF = (PluginInterface) pluginIFs.get(index);
						if (pluginIF.isOperational()) {
							if (pluginIF.isUnloadable()) {
								try {
									pluginIF.unload();
								} catch (PluginException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
							}
						}
						pluginIFs = Arrays.asList(azureusCore.getPluginManager().getPlugins());
						table.setItemCount(pluginIFs.size());
						Collections.sort(pluginIFs, comparator);
						table.clearAll();
					}
				}
			}
		});
		btnUnload.setEnabled( false );
		
		final Button btnLoad = new Button(cButtons, SWT.PUSH);
		btnUnload.setLayoutData(new GridData());
		Messages.setLanguageText(btnLoad, "ConfigView.pluginlist.loadSelected");
		btnLoad.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int[] items = table.getSelectionIndices();
				for (int i = 0; i < items.length; i++) {
					int index = items[i];
					if (index >= 0 && index < pluginIFs.size()) {
						PluginInterface pluginIF = (PluginInterface) pluginIFs.get(index);
						if (pluginIF.isDisabled()) {
							try {
								COConfigurationManager.setParameter("PluginInfo."
										+ pluginIF.getPluginID() + ".enabled", true );
								pluginIF.setDisabled( false );
								pluginIF.reload();
							} catch (PluginException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
						}
						pluginIFs = Arrays.asList(azureusCore.getPluginManager().getPlugins());
						table.setItemCount(pluginIFs.size());
						Collections.sort(pluginIFs, comparator);
						table.clearAll();
					}
				}
			}
		});
		btnLoad.setEnabled( false );
		
		
		final Button btnScan = new Button(cButtons, SWT.PUSH);
		btnScan.setLayoutData(new GridData());
		Messages.setLanguageText(btnScan, "ConfigView.pluginlist.scan");
		btnScan.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				azureusCore.getPluginManager().refreshPluginList();
				pluginIFs = Arrays.asList(azureusCore.getPluginManager().getPlugins());
				table.setItemCount(pluginIFs.size());
				Collections.sort(pluginIFs, comparator);
				table.clearAll();
			}
		});


		table.addListener(SWT.SetData, new Listener() {
			public void handleEvent(Event event) {
				TableItem item = (TableItem) event.item;
				int index = table.indexOf(item);
				PluginInterface pluginIF = (PluginInterface) pluginIFs.get(index);

				for (int i = 0; i < COLUMN_HEADERS.length; i++) {
					if (i == FilterComparator.FIELD_NAME)
						item.setImage(i, ImageRepository.getImage(pluginIF.isOperational()
								? "greenled" : "redled")); 
					
					String sText = comparator.getFieldValue(i, pluginIF);
					if (sText == null)
						sText = "";
					item.setText(i, sText);
				}

				item.setGrayed(pluginIF.isMandatory());
				boolean bEnabled = COConfigurationManager.getBooleanParameter("PluginInfo."
						+ pluginIF.getPluginID() + ".enabled", true);
		    Utils.setCheckedInSetData(item, bEnabled);
				item.setData("PluginID", pluginIF.getPluginID());
				Utils.alternateRowBackground(item);
			}
		});

		table.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (e.detail == SWT.CHECK){
						
					TableItem item = (TableItem) e.item;
					if (item.getGrayed()) {
						if (!item.getChecked())
							item.setChecked(true);
						return;
					}
					COConfigurationManager.setParameter("PluginInfo."
							+ item.getData("PluginID") + ".enabled", item.getChecked());
				}
				
				TableItem item = (TableItem) e.item;
				int index = table.indexOf(item);
				PluginInterface pluginIF = (PluginInterface) pluginIFs.get(index);
				btnUnload.setEnabled( pluginIF.isUnloadable());
				boolean bEnabled = COConfigurationManager.getBooleanParameter("PluginInfo."
						+ pluginIF.getPluginID() + ".enabled", true);
				btnLoad.setEnabled(pluginIF.isDisabled() && !bEnabled );
			}
		});

		table.setItemCount(pluginIFs.size());
		

		return infoGroup;
	
public voidconfigSectionDelete()

	
public java.lang.StringconfigSectionGetName()

		return ConfigSection.SECTION_PLUGINS;
	
public java.lang.StringconfigSectionGetParentSection()

		return ConfigSection.SECTION_ROOT;
	
public voidconfigSectionSave()

	
public voidinitPluginSubSections()

		// Create subsections for plugins that used the PluginConfigModel object
		// =====================================================================

		TreeItem treePlugins = configView
				.findTreeItem(ConfigSection.SECTION_PLUGINS);
		ParameterRepository repository = ParameterRepository.getInstance();

		String[] names = repository.getNames();

		Arrays.sort(names);

		for (int i = 0; i < names.length; i++) {
			String pluginName = names[i];
			Parameter[] parameters = repository.getParameterBlock(pluginName);

			// Note: 2070's plugin documentation for PluginInterface.addConfigUIParameters
			//       said to pass <"ConfigView.plugins." + displayName>.  This was
			//       never implemented in 2070.  2070 read the key <displayName> without
			//       the prefix.
			//
			//       2071+ uses <sSectionPrefix ("ConfigView.section.plugins.") + pluginName>
			//       and falls back to <displayName>.  Since 
			//       <"ConfigView.plugins." + displayName> was never implemented in the
			//       first place, a check for it has not been created
			boolean bUsePrefix = MessageText.keyExists(ConfigView.sSectionPrefix
					+ "plugins." + pluginName);
			Composite pluginGroup = configView.createConfigSection(treePlugins,
					pluginName, -2, bUsePrefix);
			GridLayout pluginLayout = new GridLayout();
			pluginLayout.numColumns = 3;
			pluginGroup.setLayout(pluginLayout);

			Map parameterToPluginParameter = new HashMap();
			//Add all parameters
			for (int j = 0; j < parameters.length; j++) {
				Parameter parameter = parameters[j];
				parameterToPluginParameter.put(parameter, new PluginParameter(
						pluginGroup, parameter));
			}
			//Check for dependencies
			for (int j = 0; j < parameters.length; j++) {
				Parameter parameter = parameters[j];
				if (parameter instanceof BooleanParameterImpl) {
					List parametersToEnable = ((BooleanParameterImpl) parameter)
							.getEnabledOnSelectionParameters();
					List controlsToEnable = new ArrayList();
					Iterator iter = parametersToEnable.iterator();
					while (iter.hasNext()) {
						Parameter parameterToEnable = (Parameter) iter.next();
						PluginParameter pp = (PluginParameter) parameterToPluginParameter
								.get(parameterToEnable);
						Control[] controls = pp.getControls();
						for (int k = 0; k < controls.length; k++) {
							controlsToEnable.add(controls[k]);
						}
					}

					List parametersToDisable = ((BooleanParameterImpl) parameter)
							.getDisabledOnSelectionParameters();
					List controlsToDisable = new ArrayList();
					iter = parametersToDisable.iterator();
					while (iter.hasNext()) {
						Parameter parameterToDisable = (Parameter) iter.next();
						PluginParameter pp = (PluginParameter) parameterToPluginParameter
								.get(parameterToDisable);
						Control[] controls = pp.getControls();
						for (int k = 0; k < controls.length; k++) {
							controlsToDisable.add(controls[k]);
						}
					}

					Control[] ce = new Control[controlsToEnable.size()];
					Control[] cd = new Control[controlsToDisable.size()];

					if (ce.length + cd.length > 0) {
						IAdditionalActionPerformer ap = new DualChangeSelectionActionPerformer(
								(Control[]) controlsToEnable.toArray(ce),
								(Control[]) controlsToDisable.toArray(cd));
						PluginParameter pp = (PluginParameter) parameterToPluginParameter
								.get(parameter);
						pp.setAdditionalActionPerfomer(ap);
					}

				}
			}
		}