Skip to main content
summaryrefslogtreecommitdiffstats
blob: 910361a518138c2d0decdd90b3e6263f97342aac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 * Copyright (c) 2014 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.releng.setup.presentation;

import org.eclipse.emf.cdo.releng.internal.setup.util.EMFUtil;
import org.eclipse.emf.cdo.releng.setup.ComponentDefinition;
import org.eclipse.emf.cdo.releng.setup.SetupFactory;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.equinox.p2.metadata.Version;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.IWizardContainer;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.part.ISetSelectionTarget;

import java.util.HashMap;
import java.util.Map;

/**
 * This is a simple wizard for creating a new model file.
 */
public abstract class ComponentModelWizard extends Wizard implements INewWizard
{
  /**
   * Remember the selection during initialization for populating the default container.
   */
  private IStructuredSelection selection;

  /**
   * Remember the workbench during initialization.
   */
  private IWorkbench workbench;

  private IFile file;

  /**
   * This just records the information.
   */
  public void init(IWorkbench workbench, IStructuredSelection selection)
  {
    this.workbench = workbench;
    this.selection = selection;
    setWindowTitle(getModelName());
    setDefaultPageImageDescriptor(ExtendedImageRegistry.INSTANCE.getImageDescriptor(getImage()));
  }

  /**
   * The framework calls this to create the contents of the wizard.
   */
  @Override
  public void addPages()
  {
    String modelName = getModelName().toLowerCase();
    String message = null;

    if (selection != null && !selection.isEmpty())
    {
      Object element = selection.getFirstElement();
      if (element instanceof IResource)
      {
        IProject project = ((IResource)element).getProject();
        if (!project.isAccessible())
        {
          message = "The selected project is not accessible.\nSelect an accessible project to create the new "
              + modelName + " in.";
        }
        else
        {
          file = project.getFile(getFileName());
          if (file.exists())
          {
            message = "A " + modelName
                + " file does already exist in the selected project.\nDelete it first if you want to create a new "
                + modelName + " in this project.";
          }
          else
          {
            message = getErrorMessage(file);
          }
        }
      }
    }

    if (file == null && message == null)
    {
      message = "No project is selected.\nSelect a project to create the new " + modelName + " in.";
    }

    if (message != null)
    {
      file = null;
      addPage(new ErrorPage(getModelName(), message));
    }
    else
    {
      IWizardPage page = createPage();
      page.setTitle(getModelName());
      page.setDescription("Create a new " + modelName + " file.");

      addPage(page);
    }
  }

  @Override
  public boolean canFinish()
  {
    return file != null && super.canFinish();
  }

  @Override
  public boolean performFinish()
  {
    try
    {
      WorkspaceModifyOperation operation = new WorkspaceModifyOperation()
      {
        @Override
        protected void execute(IProgressMonitor progressMonitor)
        {
          try
          {
            ResourceSet resourceSet = EMFUtil.createResourceSet();

            URI fileURI = URI.createPlatformResourceURI(file.getFullPath().toString(), true);

            Resource resource = resourceSet.createResource(fileURI);
            EObject rootObject = createModel();
            if (rootObject != null)
            {
              resource.getContents().add(rootObject);
            }

            Map<Object, Object> options = new HashMap<Object, Object>();
            options.put(XMLResource.OPTION_ENCODING, "UTF-8");
            resource.save(options);
          }
          catch (Exception exception)
          {
            SetupEditorPlugin.INSTANCE.log(exception);
          }
          finally
          {
            progressMonitor.done();
          }
        }
      };

      IWizardContainer container = getContainer();
      container.run(false, false, operation);

      IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
      IWorkbenchPage page = workbenchWindow.getActivePage();
      final IWorkbenchPart activePart = page.getActivePart();
      if (activePart instanceof ISetSelectionTarget)
      {
        final ISelection targetSelection = new StructuredSelection(file);
        getShell().getDisplay().asyncExec(new Runnable()
        {
          public void run()
          {
            ((ISetSelectionTarget)activePart).selectReveal(targetSelection);
          }
        });
      }

      try
      {
        page.openEditor(new FileEditorInput(file),
            workbench.getEditorRegistry().getDefaultEditor(file.getFullPath().toString()).getId());
      }
      catch (PartInitException exception)
      {
        MessageDialog.openError(workbenchWindow.getShell(),
            SetupEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage());
        return false;
      }

      return true;
    }
    catch (Exception exception)
    {
      SetupEditorPlugin.INSTANCE.log(exception);
      return false;
    }
  }

  protected abstract Object getImage();

  protected abstract String getModelName();

  protected abstract String getFileName();

  protected abstract EObject createModel();

  protected abstract IWizardPage createPage();

  protected abstract String getErrorMessage(IFile file);

  /**
   * This is the error page of the wizard.
   *
   * @author Eike Stepper
   */
  public static class ErrorPage extends WizardPage
  {
    private String message;

    public ErrorPage(String title, String message)
    {
      super("Error");
      this.message = message;
      setTitle(title);
      setErrorMessage(SetupEditorPlugin.INSTANCE.getString("_UI_ErrorPage_description"));
    }

    public void createControl(Composite parent)
    {
      Label label = new Label(parent, SWT.WRAP);
      label.setText(message);
      setControl(label);
    }
  }

  /**
   * @author Eike Stepper
   */
  public static final class CDef extends ComponentModelWizard
  {
    private ComponentDefinition model;

    @Override
    protected Object getImage()
    {
      return SetupEditorPlugin.INSTANCE.getImage("full/wizban/cdef_wiz");
    }

    @Override
    protected String getModelName()
    {
      return SetupEditorPlugin.INSTANCE.getString("_UI_CDef_ModelName");
    }

    @Override
    protected String getFileName()
    {
      return "component.def";
    }

    @Override
    protected EObject createModel()
    {
      return model;
    }

    @Override
    protected IWizardPage createPage()
    {
      model = SetupFactory.eINSTANCE.createComponentDefinition();

      return new WizardPage(getModelName())
      {
        public void createControl(Composite parent)
        {
          Composite composite = new Composite(parent, SWT.NONE);
          composite.setLayout(new GridLayout(2, false));

          Label idLabel = new Label(composite, SWT.NONE);
          idLabel.setText("Component ID:");
          idLabel.setLayoutData(new GridData());

          final Text idText = new Text(composite, SWT.BORDER);
          idText.setText(model.getID() == null ? "" : model.getID());
          idText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
          idText.addModifyListener(new ModifyListener()
          {
            public void modifyText(ModifyEvent e)
            {
              model.setID(idText.getText());
              validatePage();
            }
          });

          Label versionLabel = new Label(composite, SWT.NONE);
          versionLabel.setText("Component Version:");
          versionLabel.setLayoutData(new GridData());

          final Text versionText = new Text(composite, SWT.BORDER);
          versionText.setText(model.getVersion() == null ? "" : model.getVersion().toString());
          versionText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
          versionText.addModifyListener(new ModifyListener()
          {
            public void modifyText(ModifyEvent e)
            {
              try
              {
                model.setVersion(Version.create(versionText.getText()));
              }
              catch (Exception ex)
              {
                //$FALL-THROUGH$
              }

              validatePage();
            }
          });

          validatePage();
          setControl(composite);
        }

        private void validatePage()
        {
          setPageComplete(model.getID() != null && model.getID().length() != 0 && model.getVersion() != null);
        }
      };
    }

    @Override
    protected String getErrorMessage(IFile file)
    {
      IProject project = file.getProject();
      if (project.getFile("META-INF/MANIFEST.MF").exists())
      {
        return getErrorMessage(project, "The selected project appears to be a plugin component.");
      }

      if (project.getFile("feature.xml").exists())
      {
        return getErrorMessage(project, "The selected project appears to be a feature component.");
      }

      return null;
    }

    private String getErrorMessage(IProject project, String message)
    {
      if (project.getFile("component.ext").exists())
      {
        return message + "\nSelect a project that is not already a component.";
      }

      return message + "\nEither select a project that is not already a component or create a component extension.";
    }
  }

  /**
   * @author Eike Stepper
   */
  public static final class CExt extends ComponentModelWizard
  {
    @Override
    protected Object getImage()
    {
      return SetupEditorPlugin.INSTANCE.getImage("full/wizban/cext_wiz");
    }

    @Override
    protected String getModelName()
    {
      return SetupEditorPlugin.INSTANCE.getString("_UI_CExt_ModelName");
    }

    @Override
    protected String getFileName()
    {
      return "component.ext";
    }

    @Override
    protected EObject createModel()
    {
      return SetupFactory.eINSTANCE.createComponentExtension();
    }

    @Override
    protected IWizardPage createPage()
    {
      return new WizardPage(getModelName())
      {
        public void createControl(Composite parent)
        {
          Label label = new Label(parent, SWT.WRAP);
          label.setText("Press Finish to create the component extension file.");
          setControl(label);
        }
      };
    }

    @Override
    protected String getErrorMessage(IFile file)
    {
      IProject project = file.getProject();
      if (project.getFile("component.def").exists())
      {
        return "A component definition already exists in the selected project.\nEdit the 'component.def' file directly if you want to change the dependencies of this component.";
      }

      return null;
    }
  }
}

Back to the top