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

📄 selectprojectdeviceswizardpage.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/**
 * Copyright (c) 2003-2006 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.migration;

import java.util.ArrayList;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.model.device.IDevice;
import eclipseme.core.nature.J2MENature;
import eclipseme.ui.internal.EclipseMEUIPlugin;
import eclipseme.ui.viewers.TableColumnInfo;
import eclipseme.ui.viewers.TableViewerConfiguration;

/**
 * Final page in the migration wizard.  This page allows
 * the user to select a device for each open J2ME project
 * in the workspace.
 * <p />
 * Copyright (c) 2003-2006 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.2 $
 * <br>
 * $Date: 2006/05/15 21:31:40 $
 * <br>
 * @author Craig Setera
 */
public class SelectProjectDevicesWizardPage extends WizardPage {
	// Implementation of the table's content provider.
	private class TableContentProvider implements IStructuredContentProvider
	{
		/**
		 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
		 */
		public Object[] getElements(Object inputElement) {
			return getDeviceProjectMappings();
		}

		public void dispose() {}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
	}
	
	// Label provider for the mappings
	private class ProjectDeviceLabelProvider extends LabelProvider implements ITableLabelProvider {
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}

		public String getColumnText(Object element, int columnIndex) {
			String text = "";
			ProjectDeviceMapping mapping = (ProjectDeviceMapping) element;
			
			switch (columnIndex) {
				case 0:
					text = mapping.project.getName();
					break;
					
				case 1:
					text = mapping.device.toString();
					break;
			}
			
			return text;
		}
	}

	// A cell modifier implementation for the device libraries editor
	private class CellModifier implements ICellModifier {
		public boolean canModify(Object element, String property) {
			return PROP_DEVICE.equals(property);
		}

		public Object getValue(Object element, String property) {
			Object value = null;
			ProjectDeviceMapping mapping = (ProjectDeviceMapping) element;
			
			switch (getColumnIndex(property)) {
				case 1:
					{
						IDevice device = mapping.device;
						for (int i = 0; i < devices.length; i++) {
							if (devices[i] == device) {
								value = new Integer(i);
								break;
							}
						}
					}
					break;
			}
			
			return value;
		}

		public void modify(Object element, String property, Object value) {
			TableItem item = (TableItem) element;
			ProjectDeviceMapping mapping = (ProjectDeviceMapping) item.getData();
			
			switch (getColumnIndex(property)) {
				case 1:
					Integer index = (Integer) value;
					mapping.device = devices[index.intValue()];
					break;
			}
			
			viewer.refresh(mapping, true);
		}

		/**
		 * Return the column index for the property.
		 * 
		 * @param property
		 * @return
		 */
		private int getColumnIndex(String property) {
			int index = -1;
			
			for (int i = 0; i < PROPERTIES.length; i++) {
				if (PROPERTIES[i].equals(property)) {
					index = i;
					break;
				}
			}
			
			return index;
		}
	}
	
	// Column property names
	private static final String PROP_PROJECT = "project";
	private static final String PROP_DEVICE = "device";
	
	// All of the properties in order
	private static final String[] PROPERTIES = new String[] { PROP_PROJECT, PROP_DEVICE };

	// Column information structure
	private static final int DEFAULT_TABLE_WIDTH = 450;
	private static final TableColumnInfo[] COLUMN_INFO = new TableColumnInfo[] {
		new TableColumnInfo("Project", 50f, null),
		new TableColumnInfo("Device", 50f, null),
	};
	
	private TableViewer viewer;
	private ComboBoxCellEditor devicesEditor;
	private IDevice[] devices;
	private ProjectDeviceMapping[] deviceProjectMappings;
	
	/**
	 * Construct a new wizard page.
	 */
	public SelectProjectDevicesWizardPage() {
		super(SelectProjectDevicesWizardPage.class.getName());
		setTitle("Select Project Devices");
		setDescription("Select the device for each EclipseME project.");
		setPageComplete(false);
	}

	/**
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {
		viewer = createTableViewer(parent);
		setControl(viewer.getTable());
	}
	
	/**
	 * Return the project to device mappings.
	 * 
	 * @return
	 */
	public ProjectDeviceMapping[] getProjectDeviceMappings() {
		return deviceProjectMappings;
	}
	
	/**
	 * Set the devices selected during migration.
	 * 
	 * @param device
	 */
	public void setSelectedDevices(IDevice[] devices) {
		this.devices = devices;
		if ((devices != null) && (devices.length > 1)) {
			deviceProjectMappings = null;
			
			if (viewer != null) {
				devicesEditor.setItems(getDeviceNames());
				viewer.setInput(new Object());
				viewer.refresh();
				setPageComplete(true);
			}
		}
	}
	
	/**
	 * Create the table viewer.
	 * 
	 * @param parent
	 */
	private TableViewer createTableViewer(Composite composite) {
		// Create the table
		int styles = 
			SWT.SINGLE | SWT.V_SCROLL |  
			SWT.BORDER | SWT.FULL_SELECTION;
		Table table = new Table(composite, styles);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setLayoutData(new GridData(GridData.FILL_BOTH));

		// Wire up the viewer
		TableViewer viewer = new TableViewer(table);
		viewer.setContentProvider(new TableContentProvider());
		viewer.setLabelProvider(new ProjectDeviceLabelProvider());
		
		IDialogSettings viewerSettings = 
			EclipseMEUIPlugin.getDialogSettings(getDialogSettings(), "selectRootsPageViewerSettings");
		TableViewerConfiguration viewerConfiguration =
			new TableViewerConfiguration(viewerSettings, DEFAULT_TABLE_WIDTH, COLUMN_INFO, 1);
		viewerConfiguration.configure(viewer);
		
		// Wire up the cell modification handling
		devicesEditor = new ComboBoxCellEditor(table, new String[0]);
		viewer.setCellModifier(new CellModifier());
		viewer.setColumnProperties(PROPERTIES);
		viewer.setCellEditors(new CellEditor[] {
			null,
			devicesEditor
		});

		return viewer;
	}

	/**
	 * Return the device name array.
	 * 
	 * @return
	 */
	private String[] getDeviceNames() {
		String[] names = null;
		
		if (devices != null) {
			names = new String[devices.length];
			for (int i = 0; i < devices.length; i++) {
				names[i] = devices[i].toString();
			}
		} else {
			names = new String[0];
		}
		
		return names;
	}

	/**
	 * Return the current device project mappings.
	 * 
	 * @return
	 */
	private ProjectDeviceMapping[] getDeviceProjectMappings() {
		if (deviceProjectMappings == null) {
			try {
				IProject[] projects = getJ2MEProjects();
				deviceProjectMappings = new ProjectDeviceMapping[projects.length];
				
				for (int i = 0; i < projects.length; i++) {
					deviceProjectMappings[i] = new ProjectDeviceMapping();
					deviceProjectMappings[i].project = projects[i];
					deviceProjectMappings[i].device = devices[0];
				}
			} catch (CoreException e) {
				deviceProjectMappings = new ProjectDeviceMapping[0];
				EclipseMECorePlugin.log(IStatus.ERROR, "Error retrieving J2ME projects", e);
			}
		}
		
		return deviceProjectMappings;
	}
	
	/**
	 * Return the open J2ME projects in the workspace.
	 * 
	 * @return
	 * @throws CoreException 
	 */
	private IProject[] getJ2MEProjects() 
		throws CoreException 
	{	
		ArrayList projects = new ArrayList();
		
		IWorkspace workspace = EclipseMECorePlugin.getWorkspace();
		IWorkspaceRoot root = workspace.getRoot();
		IProject[] allProjects = root.getProjects();
		for (int i = 0; i < allProjects.length; i++) {
			IProject project = allProjects[i];
			if ((project.isOpen()) && J2MENature.hasJ2MENature(project)) {
				projects.add(project);
			}
		}
		
		return (IProject[]) projects.toArray(new IProject[projects.size()]);
	}
}

⌨️ 快捷键说明

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