]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/dnd/MarkupDialog.java
Ask to link library to model when dropping symbol from unlinked library
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / dnd / MarkupDialog.java
1 /*******************************************************************************
2  * Copyright (c) 2018 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor.dnd;
13
14 import org.eclipse.jface.dialogs.Dialog;
15 import org.eclipse.jface.dialogs.IDialogConstants;
16 import org.eclipse.jface.dialogs.IDialogSettings;
17 import org.eclipse.jface.layout.GridDataFactory;
18 import org.eclipse.jface.text.Document;
19 import org.eclipse.mylyn.wikitext.markdown.MarkdownLanguage;
20 import org.eclipse.mylyn.wikitext.ui.viewer.MarkupViewer;
21 import org.eclipse.mylyn.wikitext.ui.viewer.MarkupViewerConfiguration;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.swt.widgets.Text;
27 import org.simantics.modeling.ui.Activator;
28
29 /**
30  * @author Tuukka Lehtonen
31  * @since 1.37.0
32  */
33 class MarkupDialog extends Dialog {
34
35     private String title;
36     private String topText;
37     private String message;
38     private int dialogIconType;
39     private int viewerStyle;
40     private MarkupViewer viewer;
41
42     private IDialogSettings dialogBoundsSettings;
43
44     protected MarkupDialog(Shell parentShell, String dialogId, String title, String topText, String message, int dialogIconType, int viewerStyle) {
45         super(parentShell);
46         this.title = title;
47         this.topText = topText;
48         this.message = message;
49         this.dialogIconType = dialogIconType;
50         this.viewerStyle = viewerStyle;
51         setShellStyle(getShellStyle() | SWT.RESIZE);
52
53         IDialogSettings settings = Activator.getDefault().getDialogSettings();
54         dialogBoundsSettings = settings.getSection(dialogId);
55         if (dialogBoundsSettings == null)
56             dialogBoundsSettings = settings.addNewSection(dialogId);
57     }
58
59     @Override
60     protected IDialogSettings getDialogBoundsSettings() {
61         return dialogBoundsSettings;
62     }
63
64     @Override
65     protected void configureShell(Shell newShell) {
66         super.configureShell(newShell);
67         newShell.setText(title);
68         newShell.setImage(newShell.getDisplay().getSystemImage(dialogIconType));
69     }
70
71     @Override
72     protected Control createDialogArea(Composite parent) {
73         Composite composite = (Composite) super.createDialogArea(parent);
74
75         Text text = new Text(composite, SWT.READ_ONLY | SWT.MULTI | SWT.WRAP);
76         text.setEditable(false);
77         text.setText(topText);
78         GridDataFactory.fillDefaults()
79         .grab(true, false)
80         .hint(convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH), SWT.DEFAULT)
81         .applyTo(text);
82
83         viewer = new MarkupViewer(composite, null, viewerStyle);
84         viewer.setMarkupLanguage(new MarkdownLanguage());
85         viewer.setEditable(false);
86         MarkupViewerConfiguration configuration = new MarkupViewerConfiguration(viewer, Activator.getDefault().getPreferenceStore());
87         viewer.configure(configuration);
88         viewer.setDocument(new Document(message));
89         GridDataFactory.fillDefaults()
90         .grab(true, true)
91         .hint(convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH), SWT.DEFAULT)
92         .applyTo(viewer.getControl());
93
94         return composite;
95     }
96
97     protected void createButtonsForButtonBar(Composite parent) {
98         createButton(parent, IDialogConstants.PROCEED_ID, IDialogConstants.PROCEED_LABEL, true);
99         createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
100     }
101
102     @Override
103     protected void buttonPressed(int buttonId) {
104         if (IDialogConstants.PROCEED_ID == buttonId) {
105             okPressed();
106         } else if (IDialogConstants.CANCEL_ID == buttonId) {
107             cancelPressed();
108         }
109     }
110
111     public static int open(Shell parent, String dialogId, String title, String topText, String message, int dialogIconType, int dialogStyle, int sourceViewerStyle) {
112         MarkupDialog dialog = new MarkupDialog(parent, dialogId, title, topText, message, dialogIconType, sourceViewerStyle);
113         dialogStyle &= SWT.SHEET;
114         dialog.setShellStyle(dialog.getShellStyle() | dialogStyle);
115         return dialog.open();
116     }
117
118 }