1 11 12 package org.eclipse.jdt.internal.debug.ui.launcher; 13 14 import org.eclipse.core.resources.IWorkspaceRoot; 15 import org.eclipse.core.resources.ResourcesPlugin; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.debug.core.ILaunchConfiguration; 18 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 19 import org.eclipse.jdt.core.IJavaModel; 20 import org.eclipse.jdt.core.IJavaProject; 21 import org.eclipse.jdt.core.JavaCore; 22 import org.eclipse.jdt.core.JavaModelException; 23 import org.eclipse.jdt.debug.ui.launchConfigurations.JavaLaunchTab; 24 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 25 import org.eclipse.jdt.internal.launching.JavaMigrationDelegate; 26 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 27 import org.eclipse.jdt.ui.JavaElementLabelProvider; 28 import org.eclipse.jface.viewers.ILabelProvider; 29 import org.eclipse.jface.window.Window; 30 import org.eclipse.swt.SWT; 31 import org.eclipse.swt.events.ModifyEvent; 32 import org.eclipse.swt.events.ModifyListener; 33 import org.eclipse.swt.events.SelectionEvent; 34 import org.eclipse.swt.events.SelectionListener; 35 import org.eclipse.swt.graphics.Font; 36 import org.eclipse.swt.layout.GridData; 37 import org.eclipse.swt.layout.GridLayout; 38 import org.eclipse.swt.widgets.Button; 39 import org.eclipse.swt.widgets.Composite; 40 import org.eclipse.swt.widgets.Group; 41 import org.eclipse.swt.widgets.Text; 42 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 43 44 52 public abstract class AbstractJavaMainTab extends JavaLaunchTab { 53 54 58 private class WidgetListener implements ModifyListener, SelectionListener { 59 60 public void modifyText(ModifyEvent e) { 61 updateLaunchConfigurationDialog(); 62 } 63 64 public void widgetDefaultSelected(SelectionEvent e) {} 65 66 public void widgetSelected(SelectionEvent e) { 67 Object source = e.getSource(); 68 if (source == fProjButton) { 69 handleProjectButtonSelected(); 70 } 71 else { 72 updateLaunchConfigurationDialog(); 73 } 74 } 75 } 76 77 protected static final String EMPTY_STRING = ""; protected Text fProjText; 80 81 private Button fProjButton; 82 83 private WidgetListener fListener = new WidgetListener(); 84 85 89 private IJavaProject chooseJavaProject() { 90 ILabelProvider labelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT); 91 ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), labelProvider); 92 dialog.setTitle(LauncherMessages.AbstractJavaMainTab_4); 93 dialog.setMessage(LauncherMessages.AbstractJavaMainTab_3); 94 try { 95 dialog.setElements(JavaCore.create(getWorkspaceRoot()).getJavaProjects()); 96 } 97 catch (JavaModelException jme) {JDIDebugUIPlugin.log(jme);} 98 IJavaProject javaProject= getJavaProject(); 99 if (javaProject != null) { 100 dialog.setInitialSelections(new Object [] { javaProject }); 101 } 102 if (dialog.open() == Window.OK) { 103 return (IJavaProject) dialog.getFirstResult(); 104 } 105 return null; 106 } 107 108 113 protected void createProjectEditor(Composite parent) { 114 Font font= parent.getFont(); 115 Group group= new Group(parent, SWT.NONE); 116 group.setText(LauncherMessages.AbstractJavaMainTab_0); 117 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 118 group.setLayoutData(gd); 119 GridLayout layout = new GridLayout(); 120 layout.numColumns = 2; 121 group.setLayout(layout); 122 group.setFont(font); 123 fProjText = new Text(group, SWT.SINGLE | SWT.BORDER); 124 gd = new GridData(GridData.FILL_HORIZONTAL); 125 fProjText.setLayoutData(gd); 126 fProjText.setFont(font); 127 fProjText.addModifyListener(fListener); 128 fProjButton = createPushButton(group, LauncherMessages.AbstractJavaMainTab_1, null); 129 fProjButton.addSelectionListener(fListener); 130 } 131 132 138 protected WidgetListener getDefaultListener() { 139 return fListener; 140 } 141 142 145 private IJavaModel getJavaModel() { 146 return JavaCore.create(getWorkspaceRoot()); 147 } 148 149 153 protected IJavaProject getJavaProject() { 154 String projectName = fProjText.getText().trim(); 155 if (projectName.length() < 1) { 156 return null; 157 } 158 return getJavaModel().getJavaProject(projectName); 159 } 160 161 164 protected IWorkspaceRoot getWorkspaceRoot() { 165 return ResourcesPlugin.getWorkspace().getRoot(); 166 } 167 168 173 protected void handleProjectButtonSelected() { 174 IJavaProject project = chooseJavaProject(); 175 if (project == null) { 176 return; 177 } 178 String projectName = project.getElementName(); 179 fProjText.setText(projectName); 180 } 181 182 185 public void initializeFrom(ILaunchConfiguration config) { 186 updateProjectFromConfig(config); 187 super.initializeFrom(config); 188 } 189 190 194 private void updateProjectFromConfig(ILaunchConfiguration config) { 195 String projectName = EMPTY_STRING; 196 try { 197 projectName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, EMPTY_STRING); 198 } 199 catch (CoreException ce) { 200 setErrorMessage(ce.getStatus().getMessage()); 201 } 202 fProjText.setText(projectName); 203 } 204 205 210 protected void mapResources(ILaunchConfigurationWorkingCopy config) { 211 try { 212 IJavaProject javaProject = getJavaProject(); 214 if (javaProject != null && javaProject.exists() && javaProject.isOpen()) { 215 JavaMigrationDelegate.updateResourceMapping(config); 216 } 217 } catch(CoreException ce) { 218 setErrorMessage(ce.getStatus().getMessage()); 219 } 220 } 221 222 } 223 | Popular Tags |