Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9a70c32c7243211e1293d2f0170bac6a932ffc49 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*******************************************************************************
 * Copyright (c) 2010 SAP AG.
 * 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:
 *    Mathias Kinzler (SAP AG) - initial implementation
 *******************************************************************************/
package org.eclipse.egit.ui.internal.repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.egit.ui.UIText;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.BaseLabelProvider;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

/**
 * Configures the Git configuration keys to display in the Properties view
 */
class ConfigureKeysDialog extends Dialog {

	private static final class ContentProvider implements
			IStructuredContentProvider {

		public Object[] getElements(Object inputElement) {
			return ((List) inputElement).toArray();
		}

		public void dispose() {
			// nothing
		}

		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			// nothing
		}

	}

	private static final class LabelProvider extends BaseLabelProvider
			implements ITableLabelProvider {

		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}

		public String getColumnText(Object element, int columnIndex) {
			return (String) element;
		}

	}

	/**
	 * The standard keys
	 */
	public static final List<String> standardKeys = new ArrayList<String>();

	static {
		standardKeys.add("core.logallrefupdates"); //$NON-NLS-1$
		standardKeys.add("core.compression"); //$NON-NLS-1$

		standardKeys.add("remote.?.url"); //$NON-NLS-1$
		standardKeys.add("remote.?.fetch"); //$NON-NLS-1$
		standardKeys.add("remote.?.push"); //$NON-NLS-1$

		standardKeys.add("user.name"); //$NON-NLS-1$
		standardKeys.add("user.email"); //$NON-NLS-1$

		Collections.sort(standardKeys);
	}

	private final List<String> activeKeys = new ArrayList<String>();

	/**
	 * @param parentShell
	 * @param activeKeys
	 */
	ConfigureKeysDialog(Shell parentShell, List<String> activeKeys) {
		super(parentShell);
		this.activeKeys.addAll(activeKeys);
		setShellStyle(getShellStyle() | SWT.SHELL_TRIM);
	}

	/**
	 * @return the strings
	 */
	public List<String> getActiveKeys() {
		return activeKeys;
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(UIText.ConfigureKeysDialog_DialogTitle);
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		Composite main = new Composite(parent, SWT.NONE);
		main.setLayout(new GridLayout(1, false));
		GridDataFactory.fillDefaults().grab(true, true).applyTo(main);

		final CheckboxTableViewer tv = CheckboxTableViewer.newCheckList(main,
				SWT.NONE);

		GridDataFactory.fillDefaults().grab(true, true).applyTo(tv.getTable());

		ToolBar tb = new ToolBar(main, SWT.HORIZONTAL);
		final ToolItem del = new ToolItem(tb, SWT.PUSH);
		del.setEnabled(false);
		del.setText(UIText.ConfigureKeysDialog_DeleteButton);
		del.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				for (Object ob : tv.getCheckedElements()) {
					activeKeys.remove(ob);
					tv.setInput(activeKeys);
				}
			}

		});

		final ToolItem addStandard = new ToolItem(tb, SWT.PUSH);
		addStandard.setText(UIText.ConfigureKeysDialog_AddStandardButton);
		addStandard.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				for (String key : standardKeys) {
					if (!activeKeys.contains(key)) {
						activeKeys.add(key);
					}
					tv.setInput(activeKeys);
				}
				Collections.sort(activeKeys);
			}

		});

		ToolItem add = new ToolItem(tb, SWT.PUSH);
		add.setText(UIText.ConfigureKeysDialog_NewButton);
		add.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				IInputValidator validator = new IInputValidator() {

					public String isValid(String newText) {
						if (activeKeys.contains(newText))
							return NLS
									.bind(
											UIText.ConfigureKeysDialog_AlreadyThere_Message,
											newText);
						return null;
					}
				};
				InputDialog id = new InputDialog(getShell(),
						UIText.ConfigureKeysDialog_NewKeyLabel,
						UIText.ConfigureKeysDialog_NewKeyLabel, null, validator);
				if (id.open() == Window.OK) {
					activeKeys.add(id.getValue());
					Collections.sort(activeKeys);
					tv.setInput(activeKeys);
				}
			}

		});

		tv.addCheckStateListener(new ICheckStateListener() {

			public void checkStateChanged(CheckStateChangedEvent event) {
				boolean anyChecked = tv.getCheckedElements().length > 0;
				del.setEnabled(anyChecked);

			}
		});

		tv.setLabelProvider(new LabelProvider());
		tv.setContentProvider(new ContentProvider());
		tv.setInput(this.activeKeys);

		return main;
	}
}

Back to the top