Added a test launch config
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / launch / MainMachineLaunchConfigTab.java
1 package net.mograsim.plugin.launch;
2
3 import java.util.Arrays;
4 import java.util.HashSet;
5 import java.util.Set;
6
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;
33
34 import net.mograsim.plugin.nature.MograsimNature;
35 import net.mograsim.plugin.util.ImageDescriptorWithMargins;
36
37 //a big part of this class is stolen from org.eclipse.jdt.debug.ui
38 public class MainMachineLaunchConfigTab extends AbstractLaunchConfigurationTab
39 {
40         private Text projSelText;
41
42         @Override
43         public void createControl(Composite parent)
44         {
45                 parent.setLayout(new FillLayout());
46                 Composite innerParent = new Composite(parent, SWT.NONE);
47                 setControl(innerParent);
48
49                 innerParent.setLayout(new GridLayout());
50
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 ->
65                 {
66                         IProject choosedProject = chooseMograsimProject();
67                         if (choosedProject != null)
68                                 projSelText.setText(choosedProject.getName());
69                 });
70
71                 // TODO MPM / RAM selectors
72         }
73
74         private static int calculateWidthHint(Control c)
75         {
76                 int wHint = new PixelConverter(c).convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
77                 return Math.max(wHint, c.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
78         }
79
80         private IProject chooseMograsimProject()
81         {
82                 WorkbenchLabelProvider renderer = new WorkbenchLabelProvider()
83                 {
84                         @Override
85                         protected ImageDescriptor decorateImage(ImageDescriptor input, Object element)
86                         {
87                                 return new ImageDescriptorWithMargins(input, new Point(22, 16));
88                         }
89                 };
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();
96                 return null;
97         }
98
99         private static IProject[] filterOpenMograsimProjects(IProject[] projects)
100         {
101                 return Arrays.stream(projects).filter(p ->
102                 {
103                         try
104                         {
105                                 return p.isAccessible() && p.hasNature(MograsimNature.NATURE_ID);
106                         }
107                         catch (CoreException e)
108                         {
109                                 throw new RuntimeException(e);
110                         }
111                 }).toArray(IProject[]::new);
112         }
113
114         @Override
115         public void setDefaults(ILaunchConfigurationWorkingCopy configuration)
116         {
117                 // TODO don't let the user have to specify everything
118         }
119
120         @Override
121         public void initializeFrom(ILaunchConfiguration configuration)
122         {
123                 String projName = "";
124                 try
125                 {
126                         projName = configuration.getAttribute(MachineLaunchConfigType.PROJECT_ATTR, "");
127                 }
128                 catch (CoreException e)
129                 {
130                         setErrorMessage(e.getStatus().getMessage());
131                 }
132                 projSelText.setText(projName);
133         }
134
135         @Override
136         public void performApply(ILaunchConfigurationWorkingCopy configuration)
137         {
138                 Set<IResource> associatedResources = new HashSet<>();
139                 String projName = projSelText.getText().trim();
140                 if (projName.length() != 0)
141                 {
142                         IWorkspace workspace = ResourcesPlugin.getWorkspace();
143                         IProject project = workspace.getRoot().getProject(projName);
144                         try
145                         {
146                                 if (project != null && project.isAccessible() && project.hasNature(MograsimNature.NATURE_ID))
147                                         associatedResources.add(project);
148                         }
149                         catch (CoreException e)
150                         {
151                                 setErrorMessage(e.getStatus().getMessage());
152                         }
153                 }
154                 configuration.setMappedResources(associatedResources.toArray(IResource[]::new));
155                 configuration.setAttribute(MachineLaunchConfigType.PROJECT_ATTR, projName);
156         }
157
158         @Override
159         public boolean isValid(ILaunchConfiguration launchConfig)
160         {
161                 setErrorMessage(null);
162                 setMessage(null);
163                 String projName = projSelText.getText().trim();
164                 if (projName.length() == 0)
165                 {
166                         setErrorMessage("No project specified");
167                         return false;
168                 }
169                 IWorkspace workspace = ResourcesPlugin.getWorkspace();
170                 IStatus status = workspace.validateName(projName, IResource.PROJECT);
171                 if (!status.isOK())
172                 {
173                         setErrorMessage(NLS.bind("Illegal project name: {0}", new String[] { status.getMessage() }));
174                         return false;
175                 }
176                 IProject project = workspace.getRoot().getProject(projName);
177                 if (!project.exists())
178                 {
179                         setErrorMessage(NLS.bind("Project {0} does not exist", new String[] { projName }));
180                         return false;
181                 }
182                 if (!project.isOpen())
183                 {
184                         setErrorMessage(NLS.bind("Project {0} is closed", new String[] { projName }));
185                         return false;
186                 }
187                 try
188                 {
189                         if (!project.hasNature(MograsimNature.NATURE_ID))
190                         {
191                                 setErrorMessage(NLS.bind("Project {0} is not a Mograsim project", new String[] { projName }));
192                                 return false;
193                         }
194                 }
195                 catch (CoreException e)
196                 {
197                         setErrorMessage(e.getStatus().getMessage());
198                         return false;
199                 }
200                 return true;
201         }
202
203         @Override
204         public String getName()
205         {
206                 return "testlaunchconfigtabname";
207         }
208 }