⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 preverificationpreferencepage.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/**
 * Copyright (c) 2003-2005 Craig Setera
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package eclipseme.ui.internal.preferences;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

import eclipseme.core.IEclipseMECoreConstants;
import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.model.LibrarySpecification;
import eclipseme.ui.internal.EclipseMEUIPlugin;
import eclipseme.ui.internal.IEmbeddableWorkbenchPreferencePage;

/**
 * Preference page implementation for setting preverification preferences.
 * <p />
 * Copyright (c) 2003-2005 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.5 $
 * <br>
 * $Date: 2006/03/06 00:55:27 $
 * <br>
 * @author Craig Setera
 */
public class PreverificationPreferencePage
	extends FieldEditorPreferencePage
	implements IEmbeddableWorkbenchPreferencePage
{
	private class VerifyingBooleanFieldEditor extends BooleanFieldEditor {
		private Button checkbox;
		
		/**
		 * @param name
		 * @param label
		 * @param parent
		 */
		public VerifyingBooleanFieldEditor(String name, String label, Composite parent) {
			super(name, label, parent);
		}

		/**
		 * @see org.eclipse.jface.preference.BooleanFieldEditor#getChangeControl(org.eclipse.swt.widgets.Composite)
		 */
		protected Button getChangeControl(Composite parent) {
			checkbox = super.getChangeControl(parent);
			
			return checkbox;
		}

		/**
		 * @see org.eclipse.jface.preference.BooleanFieldEditor#valueChanged(boolean, boolean)
		 */
		protected void valueChanged(boolean oldValue, boolean newValue) {
			if (newValue) {
				if (!newValue) checkbox.setSelection(newValue);
			}
			
			super.valueChanged(oldValue, newValue);
		}
	}
	
	private static final String[] BUTTON_TEXTS = new String[] {
		"Use JAD file setting",
		"Use project device configuration",
		"Use specific configuration",
	};
	
	private static final String[] CONFIG_FILE_LOCATIONS = new String[] {
		IEclipseMECoreConstants.PREF_PREVERIFY_CONFIG_LOCATION_JAD,
		IEclipseMECoreConstants.PREF_PREVERIFY_CONFIG_LOCATION_PLATFORM,
		IEclipseMECoreConstants.PREF_PREVERIFY_CONFIG_LOCATION_SPECIFIED,
	};
	
	private boolean embeddedInProperties;
	private Button[] preverificationRadios;
	private LibrarySpecification[] configSpecs;
	private Combo configCombo;
	
	/**
	 * Default constructor.
	 */
	public PreverificationPreferencePage() {
		this(false, EclipseMEUIPlugin.getDefault().getCorePreferenceStore());
	}

	/**
	 * Constructor for use when embedding the preference page within a properties
	 * page.
	 * 
	 * @param embeddedInProperties
	 * @param preferenceStore
	 */
	public PreverificationPreferencePage(boolean embeddedInProperties, IPreferenceStore preferenceStore) {
		super(GRID);
		
		this.embeddedInProperties = embeddedInProperties;
		setPreferenceStore(preferenceStore);
		
		try {
			configSpecs = EclipseMECorePlugin.getConfigurationSpecifications();
		} catch (CoreException e) {
			EclipseMECorePlugin.log(IStatus.WARNING, e.getMessage(), e);
			configSpecs = new LibrarySpecification[0];
		}
	}

	/**
	 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
	 */
	protected Control createContents(Composite parent)
	{
		if (embeddedInProperties) {
			noDefaultAndApplyButton();
		}
		
		// PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, "eclipseme.ui.help_PackagingPreferencePage");
		return super.createContents(parent);
	}

	/**
	 * Creates the field editors.
	 */
	public void createFieldEditors() {
		final Composite parent = getFieldEditorParent();
		
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		addConfigurationControls(composite);
		addWorkInProgressControls(composite);		

		setControlsFromPreferences();
	}

	/**
	 * Initialize the preference page as necessary
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	public void init(IWorkbench workbench) {
		setControlsFromPreferences();
	}

	/**
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#performDefaults()
	 */
	public void performDefaults() {
		setControlsFromPreferences();
		super.performDefaults();
	}

	/**
	 * @see org.eclipse.jface.preference.PreferencePage#performApply()
	 */
	public void performApply() {
		setPreferencesFromControls();
		super.performApply();
	}

	/**
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#performOk()
	 */
	public boolean performOk() {
		setPreferencesFromControls();
		if (!embeddedInProperties) {
			buildSuites();
		}
		
		return super.performOk();
	}

	/**
	 * Add the controls for choosing the preverification configuration.
	 * 
	 * @param composite
	 */
	private void addConfigurationControls(Composite composite) {
		Group preverifyConfigGroup = new Group(composite, SWT.NONE);
		preverifyConfigGroup.setText("J2ME Configuration for Preverification");
		preverifyConfigGroup.setLayout(new GridLayout(1, false));
		preverifyConfigGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		preverificationRadios = new Button[BUTTON_TEXTS.length];
		for (int i = 0; i < BUTTON_TEXTS.length; i++) {
			preverificationRadios[i] = new Button(preverifyConfigGroup, SWT.RADIO);
			preverificationRadios[i].setText(BUTTON_TEXTS[i]);
			
			if (CONFIG_FILE_LOCATIONS[i].equals(IEclipseMECoreConstants.PREF_PREVERIFY_CONFIG_LOCATION_SPECIFIED)) {
				addSpecificConfigurationControls(preverifyConfigGroup);
			}
		}
	}

	/**
	 * Add the controls that are for work-in-progress function.
	 * 
	 * @param composite
	 */
	private void addWorkInProgressControls(Composite composite) {
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.verticalAlignment = GridData.VERTICAL_ALIGN_END;

		Group workInProgressGroup = new Group(composite, SWT.NONE);
		workInProgressGroup.setText("Work In Progress");
		workInProgressGroup.setLayoutData(gd);
		workInProgressGroup.setLayout(new GridLayout(1, false));

		Composite innerComposite = new Composite(workInProgressGroup, SWT.NONE);
		innerComposite.setLayout(new GridLayout(1, false));
		innerComposite.setLayoutData(new GridData(GridData.FILL));
		
		addField(new VerifyingBooleanFieldEditor(
				IEclipseMECoreConstants.PREF_PREVERIFY_BUILT_IN,
				"Use Built-in Preverifier",
				innerComposite));
	}

	/**
	 * Add the configuration specific controls.
	 * 
	 * @param preverifyConfigGroup
	 * @throws CoreException 
	 */
	private void addSpecificConfigurationControls(Group preverifyConfigGroup) 
	{
		configCombo = new Combo(preverifyConfigGroup, SWT.READ_ONLY);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalIndent = 15;
		configCombo.setLayoutData(gridData);
		
		String[] displayValues = new String[configSpecs.length];
		for (int i = 0; i < displayValues.length; i++) {
			displayValues[i] = configSpecs[i].getName();
		}
		
		configCombo.setItems(displayValues);
	}

	/**
	 * Build the midlet suites in the workspace because
	 * preverification settings have changed.
	 */
	private void buildSuites() {
		// TODO Add some more smarts to what needs to get rebuilt.
		IWorkspaceRoot root = EclipseMECorePlugin.getWorkspace().getRoot();
		IProject[] projects = root.getProjects();
		
		ArrayList toBuild = new ArrayList();
		
		// Collect the projects to be built
		for (int i = 0; i < projects.length; i++) {
			IProject project = projects[i];
			
			try {
				if (
					project.isOpen() &&
					project.hasNature(IEclipseMECoreConstants.J2ME_NATURE_ID) && 
					!usesProjectSpecificPreverification(project))
				{
					toBuild.add(project);
				}
			} catch (CoreException e) {
				EclipseMECorePlugin.log(IStatus.ERROR, "Error building midlet suites", e);
			}
		}
		
		// Do the build of those projects
		IProject[] projectArray = (IProject[]) toBuild.toArray(new IProject[toBuild.size()]);
		if (projectArray.length > 0) {
			doBuild(projectArray);
		}
	}
	
	/**
	 * Launch the build of the midlet suites.
	 * 
	 * @param toBuild
	 */
	public void doBuild(final IProject[] toBuild) {
		// The work to be done.
		IRunnableWithProgress runnable = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) throws InvocationTargetException {
				try {
					for (int i = 0; i < toBuild.length; i++) {
						SubProgressMonitor subMonitor = 
							new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN);
						toBuild[i].build(IncrementalProjectBuilder.FULL_BUILD, subMonitor);
						subMonitor.done();
					}
				} catch (CoreException e) {
					throw new InvocationTargetException(e);
				}
			}
		};
		try {
			PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable);
		} catch (InterruptedException e) {
		} catch (InvocationTargetException e) {
			EclipseMECorePlugin.log(IStatus.WARNING, "Error building suites", e.getCause());
		}
	}	

	/**
	 * Set the state of the controls based on the preferences.
	 */
	private void setControlsFromPreferences() {
		if (preverificationRadios != null) {
			IPreferenceStore store = getPreferenceStore();
			String location = store.getString(IEclipseMECoreConstants.PREF_PREVERIFY_CONFIG_LOCATION);
			for (int i = 0; i < preverificationRadios.length; i++) {
				String fieldValue = CONFIG_FILE_LOCATIONS[i];
				preverificationRadios[i].setSelection(fieldValue.equals(location));
			}
			
			int specIndex = 0;
			String config = store.getString(IEclipseMECoreConstants.PREF_PREVERIFY_CONFIG_VALUE);
			for (int i = 0; i < configSpecs.length; i++) {
				LibrarySpecification spec = configSpecs[i];
				if (spec.getIdentifier().equals(config)) {
					specIndex = i;
					break;
				}
			}
			
			configCombo.select(specIndex);
		}
	}
	
	/**
	 * Set the state of the preferences based on the controls.
	 */
	private void setPreferencesFromControls() {
		IPreferenceStore store = getPreferenceStore();
		for (int i = 0; i < preverificationRadios.length; i++) {
			if (preverificationRadios[i].getSelection()) {
				store.setValue(
					IEclipseMECoreConstants.PREF_PREVERIFY_CONFIG_LOCATION, 
					CONFIG_FILE_LOCATIONS[i]);
				break;
			}
		}
		
		int index = configCombo.getSelectionIndex();
		store.setValue(
			IEclipseMECoreConstants.PREF_PREVERIFY_CONFIG_VALUE,
			configSpecs[index].getIdentifier());
	}

	/**
	 * Return a boolean indicating if the specified project is using
	 * project-specific preverification settings.
	 * 
	 * @param project
	 * @return
	 */
	private boolean usesProjectSpecificPreverification(IProject project) {
		ProjectScope projectScope = new ProjectScope(project);
		IEclipsePreferences prefNode = 
			projectScope.getNode(IEclipseMECoreConstants.PLUGIN_ID);
		
		return prefNode.getBoolean(IEclipseMECoreConstants.PREF_PREVERIFY_USE_PROJECT, false);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -