Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1e4c9157e57b99e895c5e82be289cac1df033c12 (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
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.ui.controls.file;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;
import org.eclipse.tcf.te.ui.controls.BaseDialogSelectionControl;
import org.eclipse.tcf.te.ui.controls.nls.Messages;
import org.eclipse.tcf.te.ui.controls.validator.DirectoryNameValidator;
import org.eclipse.tcf.te.ui.controls.validator.Validator;
import org.eclipse.tcf.te.ui.interfaces.data.IDataExchangeNode;
import org.osgi.framework.Bundle;


/**
 * Base implementation of a simple directory selection control.
 * <p>
 * The control supports direct editing by the user or browsing for the directory. By
 * default, the control has a history of recently selected directories.
 */
public class DirectorySelectionControl extends BaseDialogSelectionControl implements IDataExchangeNode {
	private String dialogMessage = ""; //$NON-NLS-1$

	/**
	 * Constructor.
	 *
	 * @param parentPage The parent dialog page this control is embedded in.
	 *                   Might be <code>null</code> if the control is not associated with a page.
	 */
	public DirectorySelectionControl(IDialogPage parentPage) {
		super(parentPage);
		setDialogTitle(Messages.DirectorySelectionControl_title);
		setGroupLabel(Messages.DirectorySelectionControl_group_label);
		setEditFieldLabel(Messages.DirectorySelectionControl_editfield_label);
	}

	/**
	 * Sets the dialogs description message. If the given message is <code>null</code>, the
	 * dialogs description message is set to an empty string.
	 *
	 * @param message The dialogs description message or <code>null</code>.
	 */
	public void setDialogMessage(String message) {
		if (message == null) {
			this.dialogMessage = ""; //$NON-NLS-1$
		} else {
			this.dialogMessage = message;
		}
	}

	/**
	 * Returns the dialogs description message.
	 *
	 * @return The dialogs description message or an empty string.
	 */
	public String getDialogMessage() {
		return dialogMessage;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseDialogSelectionControl#doCreateDialogControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Dialog doCreateDialogControl(Composite parent) {
		Assert.isNotNull(parent);

		Dialog dialog = new DirectoryDialog(parent.getShell());
		return dialog;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseDialogSelectionControl#configureDialogControl(org.eclipse.swt.widgets.Dialog)
	 */
	@Override
	protected void configureDialogControl(Dialog dialog) {
		super.configureDialogControl(dialog);

		// We do expect a directory dialog here.
		if (dialog instanceof DirectoryDialog) {
			DirectoryDialog directoryDialog = (DirectoryDialog)dialog;
			// the dialog should open within the directory of the currently selected
			// directory. If no directory has been currently selected, it should open
			// within the last browsed directory.
			String selectedDirectory = doGetSelectedDirectory();
			if (selectedDirectory != null && selectedDirectory.trim().length() > 0) {
				directoryDialog.setFilterPath(selectedDirectory);
			} else if (Platform.getBundle("org.eclipse.core.resources") != null //$NON-NLS-1$
						&& Platform.getBundle("org.eclipse.core.resources").getState() == Bundle.ACTIVE) { //$NON-NLS-1$
				directoryDialog.setFilterPath(org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString());
			}
			// set the dialogs description message
			directoryDialog.setMessage(getDialogMessage());
		}
	}

	/**
	 * Returns the directory to set as initial directory. This method
	 * is called from {@link #configureDialogControl(Dialog)} in case the dialog
	 * is a {@link DirectoryDialog}.
	 *
	 * @return The initial directory to set to the directory dialog or <code>null</code> if none.
	 */
	protected String doGetSelectedDirectory() {
		return getEditFieldControlText();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl#doCreateEditFieldValidator()
	 */
	@Override
	protected Validator doCreateEditFieldValidator() {
		return new DirectoryNameValidator(
			Validator.ATTR_MANDATORY |
			DirectoryNameValidator.ATTR_MUST_EXIST |
			DirectoryNameValidator.ATTR_CAN_READ |
			DirectoryNameValidator.ATTR_CAN_WRITE);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl#setEditFieldValidator(org.eclipse.tcf.te.ui.controls.validator.Validator)
	 */
	@Override
	public void setEditFieldValidator(Validator editFieldValidator) {
		Assert.isTrue(editFieldValidator instanceof DirectoryNameValidator);

		if (editFieldValidator instanceof DirectoryNameValidator) {
			super.setEditFieldValidator(editFieldValidator);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseDialogSelectionControl#doOpenDialogControl(org.eclipse.swt.widgets.Dialog)
	 */
	@Override
	protected String doOpenDialogControl(Dialog dialog) {
		Assert.isNotNull(dialog);

		// We do expect a directory dialog here.
		if (dialog instanceof DirectoryDialog) {
			DirectoryDialog directoryDialog = (DirectoryDialog)dialog;
			return directoryDialog.open();
		}

		return null;
	}

	protected String getPropertiesKey() {
		return "Directory"; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.data.IDataExchangeNode#setupData(org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer)
	 */
	@Override
	public void setupData(IPropertiesContainer data) {
		String dir = data.getStringProperty(getPropertiesKey());
		IPath path = dir != null ? new Path(dir) : null;
		setEditFieldControlText(path != null ? path.toOSString() : ""); //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.data.IDataExchangeNode#extractData(org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer)
	 */
	@Override
	public void extractData(IPropertiesContainer data) {
		String dir = doGetSelectedDirectory();
		IPath path = dir.trim().length() > 0 ? new Path(dir) : null;
		data.setProperty(getPropertiesKey(), path != null ? path.toPortableString() : null);
	}

	public boolean checkDataChanged(IPropertiesContainer data) {
		IPropertiesContainer newData = new PropertiesContainer();
		extractData(newData);
		String newValue = newData.getStringProperty(getPropertiesKey());
		String oldValue = data.getStringProperty(getPropertiesKey());
		if (oldValue == null) {
			return newValue != null;
		}
		return newValue == null || !oldValue.equals(newValue);
	}
}

Back to the top