Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3bbf2f4c31ae63844f7dcb65a04b5dc122d86643 (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
/*******************************************************************************
 * Copyright (c) 2016-2017, Thales Global Services S.A.S.
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Abel Hegedus, Tamas Borbas, Balazs Grill, Peter Lunk, Daniel Segesdi (IncQuery Labs Ltd.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.diffmerge.patch.ui.utils

import org.eclipse.core.resources.IFile
import org.eclipse.core.resources.IResource
import org.eclipse.core.resources.ResourcesPlugin
import org.eclipse.core.runtime.CoreException
import org.eclipse.core.runtime.IStatus
import org.eclipse.core.runtime.MultiStatus
import org.eclipse.core.runtime.Status
import org.eclipse.jface.dialogs.ErrorDialog
import org.eclipse.jface.dialogs.MessageDialog
import org.eclipse.jface.viewers.IStructuredSelection
import org.eclipse.jface.window.Window
import org.eclipse.jface.wizard.WizardDialog
import org.eclipse.swt.SWT
import org.eclipse.swt.widgets.FileDialog
import org.eclipse.swt.widgets.Shell
import org.eclipse.ui.IWorkbench
import org.eclipse.ui.IWorkbenchWindow
import org.eclipse.ui.PartInitException
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog
import org.eclipse.ui.dialogs.WizardNewFileCreationPage
import org.eclipse.ui.ide.IDE
import org.eclipse.ui.model.WorkbenchContentProvider
import org.eclipse.ui.model.WorkbenchLabelProvider
import org.eclipse.ui.wizards.newresource.BasicNewFileResourceWizard

class DialogFactory {
  private val Shell shell

  new(Shell shell) {
    this.shell = shell
  }

  public def openInformationDialog(String title, String message) {
    MessageDialog.openInformation(shell, title, message);
  }

  public def openFileDialog(String[] supportedFileExtensions) {
    return openFileDialog("File Selection", supportedFileExtensions)
  }

  public def openFileDialog(String title, String[] supportedFileExtensions) {
    (new FileDialog(shell, SWT.OPEN) => [
      it.text = title
      it.filterExtensions = supportedFileExtensions
    ]).open
  }

  public def openSaveFileDialog(IWorkbench workbench, IStructuredSelection selection, String fextension) {
    val wizard = new BasicNewFileResourceWizard() {
      public String path = null
      public IFile file = null
      private WizardNewFileCreationPage mainPage

      override addPages() {
        super.addPages()
        mainPage = (pages.get(0) as WizardNewFileCreationPage)
        mainPage.fileExtension = fextension
      }

      override performFinish() {
        file = mainPage.createNewFile();
        if (file == null) {
          return false
        }

        selectAndReveal(file)
        if (file.location != null) {
          path = file.location.toString
        }

        return true;
      }

    } => [
      it.windowTitle = "Modelpatch file"
    ]
    wizard.init(workbench, selection)
    val dialog = new WizardDialog(shell, wizard) => [
      it.blockOnOpen = true
    ]
    dialog.open()
    return wizard.file
  }

  public def openModelPatchEditor(IWorkbenchWindow window, IFile file) {
    try {
      if (window != null) {
        val page = window.getActivePage()
        if (page != null) {
          IDE.openEditor(page, file, true)
        }
      }
    } catch (PartInitException e) {
      openError(window.getShell(), "Modelpatch editor cannot be opened", e.getMessage(), e)
    }
  }

  public def openWorkspaceFileDialog() {
    return openWorkspaceFileDialog("File Selection")
  }

  public def openWorkspaceFileDialog(String title) {
    val dialog = new ElementTreeSelectionDialog(shell, new WorkbenchLabelProvider(), new WorkbenchContentProvider())

    dialog.title = title
    dialog.setInput(ResourcesPlugin.getWorkspace().getRoot())
    dialog.setAllowMultiple(false)

    if (dialog.open() == Window.OK) {
      return (dialog.firstResult as IResource).location.toOSString
    }
    return null
  }

  public def openErrorDialog(String dialogTitle, String message, String errorMessage, Throwable ex, String ID) {
    ErrorDialog.openError(shell, dialogTitle, message, createMultiStatus(errorMessage, ex, ID))
  }

  public def openErrorDialog(String dialogTitle, String message, Throwable ex, String ID) {
    ErrorDialog.openError(shell, dialogTitle, message, createMultiStatus(ex.getMessage(), ex, ID))
  }

  public def openErrorDialog(String dialogTitle, String message) {
    MessageDialog.openError(shell, dialogTitle, message)
  }

  /**
   * Copied from {@link org.eclipse.ui.internal.ide.DialogUtil#openError(Shell, String, String, PartInitException)}
   */
  private def void openError(Shell parent, String title, String message, PartInitException exception) {
    // Check for a nested CoreException
    var CoreException nestedException = null
    var status = exception.getStatus()
    if (status !== null && status.getException() instanceof CoreException) {
      nestedException = status.getException() as CoreException
    }
    if (nestedException !== null) {
      // Open an error dialog and include the extra
      // status information from the nested CoreException
      ErrorDialog.openError(parent, title, message, nestedException.getStatus())
    } else {
      // Open a regular error dialog since there is no
      // extra information to display. Don't use SWT.SHEET because
      // we don't know if the title contains important information.
      MessageDialog.openError(parent, title, message)
    }
  }

  private def MultiStatus createMultiStatus(String msg, Throwable t, String ID) {
    val childStatuses = <Status>newArrayList
    val stackTraces = t.getStackTrace()

    for (StackTraceElement stackTrace : stackTraces) {
      val status = new Status(IStatus.ERROR, ID, stackTrace.toString)
      childStatuses.add(status)
    }

    return new MultiStatus(ID, IStatus.ERROR, childStatuses.toArray(#[]), t.toString(), t)
  }
}

Back to the top