Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0f8f6fee40c2fe963a9d52b81c8dd838c2b3a5bb (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
/*****************************************************************************
 * Copyright (c) 2010 Atos Origin.
 *
 *
 * 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
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.ui.helper;

import java.util.Collection;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.PopupDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;

/**
 * The Class CustomPopupDialog.
 */
public abstract class CustomPopupDialog extends PopupDialog {

	/** The anchor. */
	private Point fAnchor;

	/** The composite. */
	protected Composite composite;

	/** The toolkit. */
	protected FormToolkit toolkit;

	/** The title. */
	private String title;

	/** The form head. */
	protected ScrolledForm formHead;

	/**
	 * The Class CloseAction.
	 */
	private class CloseAction extends Action {

		/**
		 * {@inheritDoc}
		 */
		@Override
		public ImageDescriptor getImageDescriptor() {
			return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_DELETE);
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public void run() {
			close();
		}
	}

	/**
	 * Instantiates a new custom popup dialog.
	 *
	 * @param parent
	 *            the parent
	 * @param point
	 *            the point
	 * @param title
	 *            the title
	 */
	public CustomPopupDialog(Shell parent, Point point, String title) {
		super(parent, SWT.NONE, true, true, false, false, false, null, null);
		this.fAnchor = point;
		this.toolkit = new FormToolkit(Display.getDefault());
		this.title = title;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Control createContents(Composite parent) {
		getShell().setBackground(getShell().getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY));
		initializeBounds();
		return createDialogArea(parent);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		this.composite = (Composite) super.createDialogArea(parent);
		composite.setLayout(new FillLayout());
		formHead = toolkit.createScrolledForm(composite);
		toolkit.decorateFormHeading(formHead.getForm());

		// set title and image
		formHead.setText(title);

		// add a Close button to the toolbar

		Collection<Action> actions = getActions();
		for (Action action : actions) {
			formHead.getToolBarManager().add(action);
		}
		formHead.getToolBarManager().add(new CloseAction());
		formHead.getToolBarManager().update(true);

		FillLayout layout = new FillLayout(SWT.VERTICAL);
		layout.marginHeight = 5;
		layout.marginWidth = 5;
		formHead.getBody().setLayout(layout);
		createSubsection();

		parent.pack();
		return composite;
	}

	/**
	 * Gets the actions.
	 *
	 * @return the actions
	 */
	abstract protected Collection<Action> getActions();

	/**
	 * Creates the subsection.
	 */
	abstract protected void createSubsection();

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Point getInitialLocation(Point size) {
		if (fAnchor == null) {
			return super.getInitialLocation(size);
		}
		Point point = fAnchor;
		Rectangle monitor = getShell().getMonitor().getClientArea();
		if (monitor.width < point.x + size.x) {
			point.x = Math.max(0, point.x - size.x);
		}
		if (monitor.height < point.y + size.y) {
			point.y = Math.max(0, point.y - size.y);
		}
		return point;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean close() {
		if (toolkit != null) {
			toolkit.dispose();
		}
		toolkit = null;
		return super.close();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Control getFocusControl() {
		return this.composite;
	}

}

Back to the top