Skip to main content
summaryrefslogtreecommitdiffstats
blob: 260150093f9fd7e36832d0d16c2ac347b66fef21 (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
/*******************************************************************************
 * Copyright (c) 2006, 2008 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.actions.link;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.eclipse.gef.EditPart;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.viewer.DesignRange;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;

/**
 * @author mengbo
 * @version 1.5
 */
/*package*/ class LinkWizardPage extends WizardPage {
	private static final String GROUP_TITLE = PDPlugin
			.getResourceString("LinkWizardPage.GroupTitle"); //$NON-NLS-1$

	private static final String PREVIEW_TAG_LABEL = PDPlugin
			.getResourceString("LinkWizardPage.PreviewLabel"); //$NON-NLS-1$

	private StyledText _text = null;

	private final Map<String, ILinkCreator> _linkMap;

	private String _linkType = null;

	private final EditPart _part;

	private final DesignRange _range;

	/**
	 * @param pageName
	 * @param title
	 * @param editPart
	 * @param range
	 * @param linkMap
	 */
	public LinkWizardPage(String pageName, String title, EditPart editPart,
			DesignRange range, Map<String, ILinkCreator> linkMap) {
		super(pageName, title, null);
		this._part = editPart;
		this._range = range;
		this._linkMap = linkMap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {
		GridLayout layout;
		GridData data;

		layout = new GridLayout();
		layout.marginWidth = 20;
		parent.setLayout(layout);
		data = new GridData(GridData.FILL_BOTH | GridData.CENTER);
		parent.setLayoutData(data);

		Group group = new Group(parent, SWT.NONE);
		group.setText(GROUP_TITLE);
		layout = new GridLayout();
		group.setLayout(layout);
		data = new GridData(GridData.FILL_HORIZONTAL);
		group.setLayoutData(data);

		String defaultLink = ""; //$NON-NLS-1$
		Set<String> set = this._linkMap.keySet();
		int size = set.size();
		String[] keys = new String[size];
		Iterator<String> itr = set.iterator();
		int i = 0;
		while (itr.hasNext()) {
			String key = itr.next();
			keys[i++] = key;
		}
		Arrays.sort(keys);
		for (int j = 0; j < size; j++) {
			Button bt = new Button(group, SWT.RADIO);
			data = new GridData(GridData.FILL_HORIZONTAL);
			bt.setLayoutData(data);
			bt.setText(keys[j]);
			if (j == 0) {
				bt.setSelection(true);
				defaultLink = keys[j];
			}
			bt.addSelectionListener(new SelectLinkListener(keys[j]));
		}

		Label label = new Label(parent, SWT.NONE);
		label.setText(PREVIEW_TAG_LABEL);

		_text = new StyledText(parent, SWT.WRAP | SWT.V_SCROLL | SWT.BORDER);
		data = new GridData(GridData.FILL_BOTH);
		data.heightHint = 50;
		_text.setLayoutData(data);

		ILinkCreator creator = _linkMap.get(defaultLink);
		_linkType = creator.getLinkIdentifier();
		String previewText = creator.getSourcePreview(_part, _range);
		previewText = previewText == null ? "" : previewText; //$NON-NLS-1$
		_text.setText(previewText);
		_text.setEditable(false);

		super.setControl(group);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.wizard.IWizardPage#isPageComplete()
	 */
	public boolean isPageComplete() {
		return true;
	}

	/**
	 * @return the link type
	 */
	public String getChosenLinkType() {
		return this._linkType;
	}

    class SelectLinkListener extends SelectionAdapter {
		private String _key;

		/**
		 * @param key
		 */
		public SelectLinkListener(String key) {
			this._key = key;
		}

		public void widgetSelected(SelectionEvent e) {
			ILinkCreator creator = _linkMap.get(this._key);
			_linkType = creator.getLinkIdentifier();
			String previewText = creator.getSourcePreview(_part, _range);
			previewText = previewText == null ? "" : previewText; //$NON-NLS-1$
			_text.setText(previewText);
			super.widgetSelected(e);
		}
	}
}

Back to the top