1 package net.mograsim.plugin.launch;
3 import java.util.Arrays;
4 import java.util.HashSet;
7 import org.eclipse.core.resources.IProject;
8 import org.eclipse.core.resources.IResource;
9 import org.eclipse.core.resources.IWorkspace;
10 import org.eclipse.core.resources.ResourcesPlugin;
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.core.runtime.IStatus;
13 import org.eclipse.debug.core.ILaunchConfiguration;
14 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
15 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.layout.PixelConverter;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.window.Window;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.layout.FillLayout;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Group;
30 import org.eclipse.swt.widgets.Text;
31 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
32 import org.eclipse.ui.model.WorkbenchLabelProvider;
34 import net.mograsim.plugin.nature.MograsimNature;
35 import net.mograsim.plugin.util.ImageDescriptorWithMargins;
37 //a big part of this class is stolen from org.eclipse.jdt.debug.ui
38 public class MainMachineLaunchConfigTab extends AbstractLaunchConfigurationTab
40 private Text projSelText;
43 public void createControl(Composite parent)
45 parent.setLayout(new FillLayout());
46 Composite innerParent = new Composite(parent, SWT.NONE);
47 setControl(innerParent);
49 innerParent.setLayout(new GridLayout());
51 Group projSelGroup = new Group(innerParent, SWT.NONE);
52 projSelGroup.setLayout(new GridLayout(2, false));
53 projSelGroup.setText("&Project:");
54 projSelGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
55 projSelText = new Text(projSelGroup, SWT.BORDER);
56 projSelText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
57 projSelText.addModifyListener(e -> updateLaunchConfigurationDialog());
58 Button projSelButton = new Button(projSelGroup, SWT.PUSH);
59 GridData projSelButtonData = new GridData();
60 projSelButtonData.widthHint = calculateWidthHint(projSelButton);
61 projSelButtonData.horizontalAlignment = SWT.FILL;
62 projSelButton.setLayoutData(projSelButtonData);
63 projSelButton.setText("&Browse...");
64 projSelButton.addListener(SWT.Selection, e ->
66 IProject choosedProject = chooseMograsimProject();
67 if (choosedProject != null)
68 projSelText.setText(choosedProject.getName());
71 // TODO MPM / RAM selectors
74 private static int calculateWidthHint(Control c)
76 int wHint = new PixelConverter(c).convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
77 return Math.max(wHint, c.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
80 private IProject chooseMograsimProject()
82 WorkbenchLabelProvider renderer = new WorkbenchLabelProvider()
85 protected ImageDescriptor decorateImage(ImageDescriptor input, Object element)
87 return new ImageDescriptorWithMargins(input, new Point(22, 16));
90 ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), renderer);
91 dialog.setTitle("title");
92 dialog.setMessage("message");
93 dialog.setElements(filterOpenMograsimProjects(ResourcesPlugin.getWorkspace().getRoot().getProjects()));
94 if (dialog.open() == Window.OK)
95 return (IProject) dialog.getFirstResult();
99 private static IProject[] filterOpenMograsimProjects(IProject[] projects)
101 return Arrays.stream(projects).filter(p ->
105 return p.isAccessible() && p.hasNature(MograsimNature.NATURE_ID);
107 catch (CoreException e)
109 throw new RuntimeException(e);
111 }).toArray(IProject[]::new);
115 public void setDefaults(ILaunchConfigurationWorkingCopy configuration)
117 // TODO don't let the user have to specify everything
121 public void initializeFrom(ILaunchConfiguration configuration)
123 String projName = "";
126 projName = configuration.getAttribute(MachineLaunchConfigType.PROJECT_ATTR, "");
128 catch (CoreException e)
130 setErrorMessage(e.getStatus().getMessage());
132 projSelText.setText(projName);
136 public void performApply(ILaunchConfigurationWorkingCopy configuration)
138 Set<IResource> associatedResources = new HashSet<>();
139 String projName = projSelText.getText().trim();
140 if (projName.length() != 0)
142 IWorkspace workspace = ResourcesPlugin.getWorkspace();
143 IProject project = workspace.getRoot().getProject(projName);
146 if (project != null && project.isAccessible() && project.hasNature(MograsimNature.NATURE_ID))
147 associatedResources.add(project);
149 catch (CoreException e)
151 setErrorMessage(e.getStatus().getMessage());
154 configuration.setMappedResources(associatedResources.toArray(IResource[]::new));
155 configuration.setAttribute(MachineLaunchConfigType.PROJECT_ATTR, projName);
159 public boolean isValid(ILaunchConfiguration launchConfig)
161 setErrorMessage(null);
163 String projName = projSelText.getText().trim();
164 if (projName.length() == 0)
166 setErrorMessage("No project specified");
169 IWorkspace workspace = ResourcesPlugin.getWorkspace();
170 IStatus status = workspace.validateName(projName, IResource.PROJECT);
173 setErrorMessage(NLS.bind("Illegal project name: {0}", new String[] { status.getMessage() }));
176 IProject project = workspace.getRoot().getProject(projName);
177 if (!project.exists())
179 setErrorMessage(NLS.bind("Project {0} does not exist", new String[] { projName }));
182 if (!project.isOpen())
184 setErrorMessage(NLS.bind("Project {0} is closed", new String[] { projName }));
189 if (!project.hasNature(MograsimNature.NATURE_ID))
191 setErrorMessage(NLS.bind("Project {0} is not a Mograsim project", new String[] { projName }));
195 catch (CoreException e)
197 setErrorMessage(e.getStatus().getMessage());
204 public String getName()
206 return "testlaunchconfigtabname";