Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c519861c770a735c8828227ebb24a4b07f5afee5 (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
/*******************************************************************************
 * Copyright (c) 2000, 2012 IBM Corporation and others.
 *
 * 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:
 *     IBM Corporation - initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.typehierarchy;

import java.util.Arrays;
import java.util.List;

import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.internal.ui.dialogs.StatusInfo;
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
import org.eclipse.cdt.internal.ui.viewsupport.CUILabelProvider;
import org.eclipse.cdt.internal.ui.wizards.dialogfields.DialogField;
import org.eclipse.cdt.internal.ui.wizards.dialogfields.IListAdapter;
import org.eclipse.cdt.internal.ui.wizards.dialogfields.LayoutUtil;
import org.eclipse.cdt.internal.ui.wizards.dialogfields.ListDialogField;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.StatusDialog;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

public class THHistoryListAction extends Action {

	private class HistoryListDialog extends StatusDialog {
		private ListDialogField<ICElement> fHistoryList;
		private IStatus fHistoryStatus;
		private ICElement fResult;

		private HistoryListDialog(Shell shell, ICElement[] historyEntries) {
			super(shell);
			setHelpAvailable(false);
			setTitle(Messages.THHistoryListAction_HistoryList_title);
			String[] buttonLabels = new String[] { Messages.THHistoryListAction_Remove, };

			IListAdapter<ICElement> adapter = new IListAdapter<>() {
				@Override
				public void customButtonPressed(ListDialogField<ICElement> field, int index) {
					doCustomButtonPressed();
				}

				@Override
				public void selectionChanged(ListDialogField<ICElement> field) {
					doSelectionChanged();
				}

				@Override
				public void doubleClicked(ListDialogField<ICElement> field) {
					doDoubleClicked();
				}
			};

			ILabelProvider labelProvider = new CUILabelProvider(THHistoryAction.LABEL_OPTIONS,
					CElementImageProvider.OVERLAY_ICONS);

			fHistoryList = new ListDialogField<>(adapter, buttonLabels, labelProvider);
			fHistoryList.setLabelText(Messages.THHistoryListAction_HistoryList_label);
			fHistoryList.setElements(Arrays.asList(historyEntries));

			ISelection sel;
			if (historyEntries.length > 0) {
				sel = new StructuredSelection(historyEntries[0]);
			} else {
				sel = new StructuredSelection();
			}

			fHistoryList.selectElements(sel);
		}

		/*
		 * @see Dialog#createDialogArea(Composite)
		 */
		@Override
		protected Control createDialogArea(Composite parent) {
			initializeDialogUnits(parent);

			Composite composite = (Composite) super.createDialogArea(parent);

			Composite inner = new Composite(composite, SWT.NONE);
			inner.setFont(parent.getFont());

			inner.setLayoutData(new GridData(GridData.FILL_BOTH));

			LayoutUtil.doDefaultLayout(inner, new DialogField[] { fHistoryList }, true, 0, 0);
			LayoutUtil.setHeightHint(fHistoryList.getListControl(null), convertHeightInCharsToPixels(12));
			LayoutUtil.setHorizontalGrabbing(fHistoryList.getListControl(null), true);

			applyDialogFont(composite);
			return composite;
		}

		/**
		 * Method doCustomButtonPressed.
		 */
		private void doCustomButtonPressed() {
			fHistoryList.removeElements(fHistoryList.getSelectedElements());
		}

		private void doDoubleClicked() {
			if (fHistoryStatus.isOK()) {
				okPressed();
			}
		}

		private void doSelectionChanged() {
			StatusInfo status = new StatusInfo();
			List<?> selected = fHistoryList.getSelectedElements();
			if (selected.size() != 1) {
				status.setError(""); //$NON-NLS-1$
				fResult = null;
			} else {
				fResult = (ICElement) selected.get(0);
			}
			fHistoryList.enableButton(0, fHistoryList.getSize() > selected.size() && selected.size() != 0);
			fHistoryStatus = status;
			updateStatus(status);
		}

		public ICElement getResult() {
			return fResult;
		}

		public ICElement[] getRemaining() {
			List<?> elems = fHistoryList.getElements();
			return elems.toArray(new ICElement[elems.size()]);
		}

		/*
		 * @see org.eclipse.jface.window.Window#configureShell(Shell)
		 */
		@Override
		protected void configureShell(Shell newShell) {
			super.configureShell(newShell);
			//			PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, ...);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.window.Window#create()
		 */
		@Override
		public void create() {
			setShellStyle(getShellStyle() | SWT.RESIZE);
			super.create();
		}
	}

	private THViewPart fView;

	public THHistoryListAction(THViewPart hierarchyView) {
		fView = hierarchyView;
		setText(Messages.THHistoryListAction_label);
	}

	/*
	 * @see IAction#run()
	 */
	@Override
	public void run() {
		ICElement[] historyEntries = fView.getHistoryEntries();
		HistoryListDialog dialog = new HistoryListDialog(fView.getSite().getShell(), historyEntries);
		if (dialog.open() == Window.OK) {
			fView.setHistoryEntries(dialog.getRemaining());
			fView.setInput(dialog.getResult(), null);
		}
	}
}

Back to the top