Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: de48963259f3733991d82dd842432c72ff19192a (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*******************************************************************************
 * Copyright (c) 2010 Google, 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:
 * 	  Sergey Prigogin (Google) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.ui.sourcelookup;

import java.io.File;

import org.eclipse.cdt.debug.internal.ui.CDebugImages;
import org.eclipse.cdt.debug.internal.ui.ICDebugHelpContextIds;
import org.eclipse.cdt.debug.internal.ui.IInternalCDebugUIConstants;
import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
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.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

/**
 * The dialog for selecting the external folder for which a compilation directory source container
 * will be created.
 */
public class CompilationDirectorySourceContainerDialog extends TitleAreaDialog {
	private static final String LAST_PATH_SETTING = "EXT_FOLDER_LAST_PATH_SETTING"; //$NON-NLS-1$
	private static final String LAST_SUBDIR_SETTING = "EXT_FOLDER_LAST_SUBDIR_SETTING"; //$NON-NLS-1$

	private String fDirectory;
	private boolean fCompilationSubfolders;
	
	private Text fDirText;
	private Button fSubfoldersButton;
	private boolean fNewContainer;

	/**
	 * Creates a dialog to select a new file system folder.
	 * 
	 * @param shell shell
	 */
	public CompilationDirectorySourceContainerDialog(Shell shell) {
		this(shell, "", //$NON-NLS-1$
				CDebugUIPlugin.getDefault().getDialogSettings().getBoolean(LAST_SUBDIR_SETTING));
		fNewContainer = true;
	}

	/**
	 * Creates a dialog to edit file system folder.
	 *  
	 * @param shell shell
	 * @param directory directory to edit or empty string
	 * @param compilationSubfolders whether the 'Subdirectories are also used for compilation'
	 * 		checkbox should be checked
	 * @param newContainer 
	 */
	public CompilationDirectorySourceContainerDialog(Shell shell, String directory, boolean compilationSubfolders) {
		super(shell);
		setShellStyle(getShellStyle() | SWT.RESIZE);
		fDirectory = directory;
		fCompilationSubfolders = compilationSubfolders;
		fNewContainer = false;
	}
	
	/**
	 * Returns the result of the dialog.open() operation
	 * @return the dialog.open() result
	 */
	public String getDirectory() {
		return fDirectory;
	}

	/**
	 * Returns whether the 'Subdirectories are also used for compilation' option is selected.
	 * 
	 * @return whether the 'Subdirectories are also used for compilation' option is selected
	 */
	public boolean isCompilationSubfolders() {
		return fCompilationSubfolders;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Image image = fNewContainer ?
				CDebugImages.get(IInternalCDebugUIConstants.IMG_ADD_COMP_DIR_WIZ) : 
				CDebugImages.get(IInternalCDebugUIConstants.IMG_EDIT_COMP_DIR_WIZ);
		setTitle(SourceLookupUIMessages.CompilationDirectorySourceContainerDialog_2);
		setMessage(SourceLookupUIMessages.CompilationDirectorySourceContainerDialog_3);
		setTitleImage(image);
		Composite parentComposite = (Composite) super.createDialogArea(parent);
		Font font = parentComposite.getFont();
		Composite composite = new Composite(parentComposite, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
		layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
		layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
		layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
		composite.setLayout(layout);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		composite.setFont(font);

        Composite dirComposite = new Composite(composite, SWT.NONE);
        layout = new GridLayout(2, false);
		dirComposite.setLayout(layout);
		dirComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		dirComposite.setFont(font);

        Label label = new Label(dirComposite, SWT.NONE);
        label.setText(SourceLookupUIMessages.CompilationDirectorySourceContainerDialog_4);
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalSpan = 2;
        label.setLayoutData(data);
        label.setFont(font);
        
        fDirText = new Text(dirComposite, SWT.BORDER);
        data = new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalSpan = 1;
        fDirText.setLayoutData(data);
        fDirText.setFont(font);
        fDirText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				validate();
			}        	
        });

        Button button = new Button(dirComposite, SWT.PUSH);
        button.setText(SourceLookupUIMessages.CompilationDirectorySourceContainerDialog_5);
        data = new GridData();
        int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
        Point minSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
        data.widthHint = Math.max(widthHint, minSize.x);
        button.setLayoutData(data);
        button.setFont(JFaceResources.getDialogFont());
        button.addSelectionListener(new SelectionAdapter() {
            @Override
			public void widgetSelected(SelectionEvent event) {
            	browse();
            }
        });

        fSubfoldersButton = new Button(dirComposite, SWT.CHECK);
        fSubfoldersButton.setText(SourceLookupUIMessages.CompilationDirectorySourceContainerDialog_6);
        data = new GridData(GridData.FILL_HORIZONTAL);
        data.verticalIndent = layout.verticalSpacing;  // Extra vertical spacing
        fSubfoldersButton.setLayoutData(data);
        fSubfoldersButton.setFont(font);

        return parentComposite;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
	 */
	@Override
	protected void configureShell(Shell newShell) {
		String title = null;
		if (fNewContainer) {
			title = SourceLookupUIMessages.CompilationDirectorySourceContainerDialog_7;
		} else {
			title = SourceLookupUIMessages.CompilationDirectorySourceContainerDialog_8;
		}
		newShell.setText(title);
		super.configureShell(newShell);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.TitleAreaDialog#createContents(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createContents(Composite parent) {
		Control c = super.createContents(parent);
		fDirText.setText(fDirectory);
		fSubfoldersButton.setSelection(fCompilationSubfolders);
		validate();
		PlatformUI.getWorkbench().getHelpSystem().setHelp(c, ICDebugHelpContextIds.COMPILATION_DIRECTORY_SOURCE_CONTAINER_DIALOG);
		return c;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
	 */
	@Override
	protected void okPressed() {
		fDirectory = fDirText.getText().trim();
		fCompilationSubfolders = fSubfoldersButton.getSelection();
		CDebugUIPlugin.getDefault().getDialogSettings().put(LAST_PATH_SETTING, fDirectory);
		CDebugUIPlugin.getDefault().getDialogSettings().put(LAST_SUBDIR_SETTING, fCompilationSubfolders);	
		super.okPressed();
	}

	private void browse() {
		String last = fDirText.getText().trim();
		if (last.length() == 0) {
			last = CDebugUIPlugin.getDefault().getDialogSettings().get(LAST_PATH_SETTING);
		}
		if (last == null) {
			last = "";  //$NON-NLS-1$
		}
		DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.SINGLE);
		dialog.setText(SourceLookupUIMessages.CompilationDirectorySourceContainerDialog_0); 
		dialog.setMessage(SourceLookupUIMessages.CompilationDirectorySourceContainerDialog_1); 
		dialog.setFilterPath(last);
		String result = dialog.open();
		if (result == null) {
			return;
		}
		fDirText.setText(result);
	}

	private void validate() {
		File file = new File(fDirText.getText().trim());
		getButton(IDialogConstants.OK_ID).setEnabled(file.isDirectory() && file.exists());
	}
}

Back to the top