Skip to main content
summaryrefslogtreecommitdiffstats
blob: d0260e57cea561dd36f48f1071fa0021a4bc21c0 (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
/*******************************************************************************
 * Copyright (c) 2015 QNX Software Systems and others.
 * 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
 *
 * Contributors:
 * QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.qt.ui.preferences;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.internal.qt.ui.Activator;
import org.eclipse.cdt.internal.qt.ui.Messages;
import org.eclipse.cdt.qt.core.IQtInstall;
import org.eclipse.cdt.qt.core.IQtInstallManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
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.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

public class QtPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {

	private IQtInstallManager manager;
	private Table installTable;
	private Button removeButton;

	private Map<Path, IQtInstall> installsToAdd = new HashMap<>();
	private Map<Path, IQtInstall> installsToRemove = new HashMap<>();

	@Override
	public void init(IWorkbench workbench) {
		manager = Activator.getService(IQtInstallManager.class);
	}

	@Override
	protected Control createContents(Composite parent) {
		Composite control = new Composite(parent, SWT.NONE);
		control.setLayout(new GridLayout());

		Group installsGroup = new Group(control, SWT.NONE);
		installsGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		installsGroup.setText(Messages.QtPreferencePage_0);
		installsGroup.setLayout(new GridLayout(2, false));

		Composite installTableComp = new Composite(installsGroup, SWT.NONE);
		installTableComp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		installTable = new Table(installTableComp, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL | SWT.FULL_SELECTION);
		installTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		installTable.setHeaderVisible(true);
		installTable.setLinesVisible(true);
		installTable.addListener(SWT.Selection, e -> {
			TableItem[] items = installTable.getSelection();
			removeButton.setEnabled(items.length > 0);
		});

		TableColumn nameColumn = new TableColumn(installTable, SWT.NONE);
		nameColumn.setText(Messages.QtPreferencePage_1);

		TableColumn locationColumn = new TableColumn(installTable, SWT.NONE);
		locationColumn.setText(Messages.QtPreferencePage_2);

		TableColumnLayout tableLayout = new TableColumnLayout();
		tableLayout.setColumnData(nameColumn, new ColumnWeightData(25, 50, true));
		tableLayout.setColumnData(locationColumn, new ColumnWeightData(75, 150, true));
		installTableComp.setLayout(tableLayout);

		Composite buttonsComp = new Composite(installsGroup, SWT.NONE);
		buttonsComp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
		buttonsComp.setLayout(new GridLayout());

		Button addButton = new Button(buttonsComp, SWT.PUSH);
		addButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
		addButton.setText(Messages.QtPreferencePage_3);
		addButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				NewQtInstallWizard wizard = new NewQtInstallWizard(getInstalls());
				WizardDialog dialog = new WizardDialog(getShell(), wizard);
				if (dialog.open() == Window.OK) {
					IQtInstall install = wizard.getInstall();
					installsToAdd.put(install.getQmakePath(), install);
					updateTable();
				}
			}
		});

		removeButton = new Button(buttonsComp, SWT.PUSH);
		removeButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
		removeButton.setText(Messages.QtPreferencePage_4);
		removeButton.setEnabled(false);
		removeButton.addListener(SWT.Selection, e -> {
			if (MessageDialog.openConfirm(getShell(), Messages.QtPreferencePage_5, Messages.QtPreferencePage_6)) {
				for (TableItem item : installTable.getSelection()) {
					IQtInstall install = (IQtInstall) item.getData();
					installsToRemove.put(install.getQmakePath(), install);
					updateTable();
				}
			}
		});

		updateTable();

		return control;
	}

	private Map<Path, IQtInstall> getInstalls() {
		Map<Path, IQtInstall> installs = new HashMap<>();
		for (IQtInstall install : manager.getInstalls()) {
			installs.put(install.getQmakePath(), install);
		}

		for (IQtInstall install : installsToAdd.values()) {
			installs.put(install.getQmakePath(), install);
		}

		for (IQtInstall install : installsToRemove.values()) {
			installs.remove(install.getQmakePath());
		}

		return installs;
	}

	private void updateTable() {
		List<IQtInstall> sorted = new ArrayList<>(getInstalls().values());
		Collections.sort(sorted, (o1, o2) -> o1.getQmakePath().toString().compareToIgnoreCase(o2.getQmakePath().toString()));

		installTable.removeAll();
		for (IQtInstall install : sorted) {
			TableItem item = new TableItem(installTable, SWT.NONE);
			item.setText(0, install.getQmakePath().toString());
			item.setText(1, install.getSpec());
			item.setData(install);
		}
	}

	@Override
	public boolean performOk() {
		for (IQtInstall install : installsToAdd.values()) {
			manager.addInstall(install);
		}

		for (IQtInstall install : installsToRemove.values()) {
			manager.removeInstall(install);
		}

		return true;
	}

}

Back to the top