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

📄 midlettab.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.launching;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.debug.ui.launchConfigurations.JavaMainTab;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.SelectionDialog;

import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.launching.ILaunchConstants;
import eclipseme.ui.EclipseMEUIStrings;
import eclipseme.ui.internal.utils.MidletSelectionDialogCreator;

/**
 * Specialization of the JavaMainTab for selecting Midlets
 * for launch.
 * <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.11 $
 * <br>
 * $Date: 2006/08/06 01:44:38 $
 * <br>
 * @author Craig Setera
 */
public class MidletTab extends JavaMainTab {
	// Dialog widgets
	private Text projectText;
	private Label projectLabel;
	private Text midletText;
	private Button midletRadio;
	private Button otaRadio;
	private Button searchButton;
	private Button projectButton;

	private Button jadRadio;
	private Text jadText;
	private Button jadBrowseButton;

	/**
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite comp = new Composite(parent, SWT.NONE);
		setControl(comp);
		
		// TODO WorkbenchHelp.setHelp(getControl(), IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_MAIN_TAB);
		comp.setLayout(new GridLayout());		
		
		createVerticalSpacer(comp, 3);
		createProjectComponents(comp);
		
		createVerticalSpacer(comp, 3);
		createExecutableComponents(comp);
		
		updateEnablement();
	}
		
	/**
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
	 */
	public String getName() {
		return EclipseMEUIStrings.getString("launchtab.midlet.title");
	}

	/**
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(ILaunchConfiguration)
	 */
	public void initializeFrom(ILaunchConfiguration config) {
		updateProjectFromConfig(config);
		
		String midletName = getStringOrDefault(
			config,
			ILaunchConstants.EMULATED_CLASS,
			"");
		midletText.setText(midletName);
		
		String jadUrl = getStringOrDefault(
			config, 
			ILaunchConstants.SPECIFIED_JAD_URL, 
			"");
		jadText.setText(jadUrl);
		
		boolean doOTA = true;
		boolean doJAD = false;
		try {
			doOTA = config.getAttribute(
				ILaunchConstants.DO_OTA,
				true);
			doJAD = config.getAttribute(
				ILaunchConstants.DO_JAD_LAUNCH,
				false);
		} catch (CoreException e) {
		}
		
		midletRadio.setSelection(!doOTA && !doJAD);
		jadRadio.setSelection(doJAD);
		otaRadio.setSelection(doOTA && !doJAD);
		
		updateEnablement();
	}
	
	/**
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(ILaunchConfiguration)
	 */
	public boolean isValid(ILaunchConfiguration config) {
		String errorMessage = null;
		
		String name = projectText.getText().trim();
		if (name.length() > 0) {
			if (!EclipseMECorePlugin.getWorkspace().getRoot().getProject(name).exists()) {
				errorMessage =
					EclipseMEUIStrings.getString("launchtab.midlet.error_project_does_not_exist");
			}
		}

		// TODO Check this is really a Midlet class
		if ((errorMessage == null) && midletRadio.getSelection()) {
			name = midletText.getText().trim();
			if (name.length() == 0) {
				errorMessage =
					EclipseMEUIStrings.getString("launchtab.midlet.error_Midlet_not_specified");
			}
		}
		
		if ((errorMessage == null) && (jadRadio.getSelection())) {
			// Validate the JAD URL that has been specified
			try {
				new URL(jadText.getText());
			} catch (MalformedURLException e) {
				errorMessage = "Invalid JAD URL specified";
			}
		}
		
		setErrorMessage(errorMessage);
				
		return (errorMessage == null);
	}

	/**
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(ILaunchConfigurationWorkingCopy)
	 */
	public void performApply(ILaunchConfigurationWorkingCopy config) {
		config.setAttribute(
			IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, 
			projectText.getText().trim());
		
		config.setAttribute(ILaunchConstants.DO_JAD_LAUNCH, jadRadio.getSelection());
		config.setAttribute(ILaunchConstants.SPECIFIED_JAD_URL, jadText.getText());
		
		config.setAttribute(ILaunchConstants.DO_OTA, otaRadio.getSelection());
		config.setAttribute(ILaunchConstants.EMULATED_CLASS, midletText.getText());
	}
			
	/**
	 * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(ILaunchConfigurationWorkingCopy)
	 */
	public void setDefaults(ILaunchConfigurationWorkingCopy config) {
		IJavaElement javaElement = getContext();
		if (javaElement != null) {
			initializeJavaProject(javaElement, config);
		} else {
			// We set empty attributes for project & main type so that when one config is
			// compared to another, the existence of empty attributes doesn't cause an
			// incorrect result (the performApply() method can result in empty values
			// for these attributes being set on a config if there is nothing in the
			// corresponding text boxes)
			config.setAttribute(
				IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, 
				"");
		}
		
		config.setAttribute(ILaunchConstants.EMULATED_CLASS, "");
	}
	
	/**
	 * Return the IJavaProject corresponding to the project name in the project name
	 * text field, or null if the text does not match a project name.
	 */
	protected IJavaProject getJavaProject() {
		IJavaProject javaProject = null;
		
		String projectName = projectText.getText().trim();
		if (projectName.length() > 0) {
			IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
			javaProject = JavaCore.create(project);
		}
		
		return javaProject;		
	}

	/**
	 * The JAD Browse button was selected.  Allow the user to select
	 * a JAD file and then enter in the URL of that JAD file into
	 * the text.
	 */
	protected void handleJadBrowseSelected() {
		FileDialog fileDialog = new FileDialog(getShell());
		fileDialog.setFilterExtensions(new String[] { "*.jad" });
		fileDialog.setFilterNames(new String[] { "JAD File" });
		
		String filename = fileDialog.open();
		if (filename != null) {
			File file = new File(filename);
			try {
				jadText.setText(file.toURL().toString());
			} catch (MalformedURLException e) {
				// This shouldn't happen...
				EclipseMECorePlugin.log(IStatus.WARNING, e);
			}
		}
	}
	
	/**
	 * Show a dialog that allows selection of Midlet
	 * subclasses.
	 */
	protected void handleSearchButtonSelected() {
		try {
			IJavaProject javaProject = getJavaProject();
			SelectionDialog dialog = 
				MidletSelectionDialogCreator.createMidletSelectionDialog(
					getShell(), 
					getLaunchConfigurationDialog(), 
					javaProject,
					false);

			if (dialog.open() == SelectionDialog.OK) {
				Object[] results = dialog.getResult();
				if ((results != null) && (results.length > 0)) {
					IType type = (IType) results[0];
					if (type != null) {
						midletText.setText(type.getFullyQualifiedName());
						javaProject = type.getJavaProject();
						projectText.setText(javaProject.getElementName());
					}
				}
			}
		} catch (JavaModelException e) {
			EclipseMECorePlugin.log(IStatus.ERROR, "Choose Midlet", e);
		}
	}

	/**
	 * Create the components that handle the executable information.
	 * 
	 * @param comp
	 */
	private void createExecutableComponents(Composite parent) {
		GridData gd;
		
		Font font = parent.getFont();
		Group group = new Group(parent, SWT.NONE);
		group.setText(" Executable ");
		
		// Set up the layout
		GridLayout mainLayout = new GridLayout(3, false);
		mainLayout.marginHeight = 0;
		mainLayout.marginWidth = 0;
		group.setLayout(mainLayout);
		
		gd = new GridData(GridData.FILL_HORIZONTAL);
		group.setLayoutData(gd);
		group.setFont(font);
		
		createMidletComponents(group);
		createJadComponents(group);
		
		otaRadio = new Button(group, SWT.RADIO);
		otaRadio.setText("Over the Air");
		otaRadio.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				updateEnablement();
				updateLaunchConfigurationDialog();
			}
		});
		
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		otaRadio.setLayoutData(gd);
	}

	/**
	 * Create the components that make up the JAD prompting.
	 * 
	 * @param parent
	 */
	private void createJadComponents(Group parent) {
		GridData gd;

		Font font = parent.getFont();		
		
		jadRadio = new Button(parent, SWT.RADIO);
		jadRadio.setText("JAD URL: ");
		jadRadio.setSelection(false);
		jadRadio.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				updateEnablement();
				updateLaunchConfigurationDialog();
			}
		});
		
		jadText = new Text(parent, SWT.SINGLE | SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		jadText.setLayoutData(gd);
		jadText.setFont(font);
		jadText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent evt) {
				updateLaunchConfigurationDialog();
			}
		});
		
		jadBrowseButton = createPushButton(parent, "Browse...", null);
		jadBrowseButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				handleJadBrowseSelected();
			}
		});
	}

	/**
	 * Create the components that make up the midlet prompting.
	 * 
	 * @param parent
	 */
	private void createMidletComponents(Group parent) {
		GridData gd;

		Font font = parent.getFont();
		
		midletRadio = new Button(parent, SWT.RADIO);
		midletRadio.setText(EclipseMEUIStrings.getString("launchtab.midlet.midlet"));
		midletRadio.setSelection(true);
		midletRadio.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				updateEnablement();
				updateLaunchConfigurationDialog();
			}
		});
		
		midletText = new Text(parent, SWT.SINGLE | SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		midletText.setLayoutData(gd);
		midletText.setFont(font);
		midletText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent evt) {
				updateLaunchConfigurationDialog();
			}
		});
		
		searchButton = createPushButton(
			parent, 
			EclipseMEUIStrings.getString("launchtab.midlet.search"), 
			null);
		searchButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				handleSearchButtonSelected();
			}
		});
	}

	/**
	 * Create the components that handle the project information.
	 * 
	 * @param parent
	 */
	private void createProjectComponents(Composite parent) {
		GridData gd;

		Font font = parent.getFont();
		Composite projComp = new Composite(parent, SWT.NONE);
		GridLayout projLayout = new GridLayout();
		projLayout.numColumns = 2;
		projLayout.marginHeight = 0;
		projLayout.marginWidth = 0;
		projComp.setLayout(projLayout);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		projComp.setLayoutData(gd);
		projComp.setFont(font);
		
		projectLabel = new Label(projComp, SWT.NONE);
		projectLabel.setText(EclipseMEUIStrings.getString("launchtab.midlet.project"));
		gd = new GridData();
		gd.horizontalSpan = 2;
		projectLabel.setLayoutData(gd);
		projectLabel.setFont(font);
		
		projectText = new Text(projComp, SWT.SINGLE | SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		projectText.setLayoutData(gd);
		projectText.setFont(font);
		projectText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent evt) {
				updateLaunchConfigurationDialog();
			}
		});
		
		projectButton = createPushButton(
			projComp, 
			EclipseMEUIStrings.getString("launchtab.midlet.browse"), 
			null);
		projectButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				handleProjectButtonSelected();
			}
		});
	}

	/**
	 * Get a string attribute from the launch configuration or
	 * the specified default value.
	 * 
	 * @param launchConfig
	 * @param attributeName
	 * @param defaultValue
	 * @return
	 */
	private String getStringOrDefault(
		ILaunchConfiguration launchConfig,
		String attributeName,
		String defaultValue)
	{
		String value = null;
		
		try {
			value = launchConfig.getAttribute(attributeName, defaultValue);
		} catch (CoreException e) {
			EclipseMECorePlugin.log(IStatus.WARNING, e);
			value = defaultValue;
		}
		
		return value;
	}
	
	/**
	 * Update the enablement of controls based on the status of the
	 * other controls.
	 */
	private void updateEnablement() {
		boolean doMidlet = midletRadio.getSelection();
		midletText.setEnabled(doMidlet);
		searchButton.setEnabled(doMidlet);
		
		boolean doJAD = jadRadio.getSelection();
		jadText.setEnabled(doJAD);
		jadBrowseButton.setEnabled(doJAD);
	}
	
	/**
	 * Update the project field from the launch configuration.
	 */
	protected void updateProjectFromConfig(ILaunchConfiguration config) {
		String projectName = ""; //$NON-NLS-1$
		try {
			projectName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, "");	
		} catch (CoreException ce) {
			EclipseMECorePlugin.log(IStatus.WARNING, "Error updating project field", ce);
		}
		
		projectText.setText(projectName);
	}
}

⌨️ 快捷键说明

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