Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0829634819b5f09b7f46810617d9a17cca6b32d0 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*********************************************************************************************
 * Copyright (c) 2011, 2012 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:
 * William Chen (Wind River)	- [345384]Provide property pages for remote file system nodes
 *                                [361322]Minor improvements to the properties dialog of a file.
 *********************************************************************************************/
package org.eclipse.tcf.te.tcf.filesystem.ui.internal.properties;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.Date;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
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.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.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.operations.IOpExecutor;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.operations.JobExecutor;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.operations.NullOpExecutor;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.operations.OpCommitAttr;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.operations.OpRefresh;
import org.eclipse.tcf.te.tcf.filesystem.core.model.FSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.ui.nls.Messages;
import org.eclipse.ui.dialogs.PropertyPage;

/**
 * The general information page of a file's properties dialog.
 */
public class GeneralInformationPage extends PropertyPage {
	// The times of retrying before failure.
	private static final int RETRY_TIMES = 3;
	// The formatter for the size of a file.
	private static final DecimalFormat SIZE_FORMAT = new DecimalFormat();
	// The original node.
	FSTreeNode node;
	// Cloned node for modification.
	FSTreeNode clone;
	// The button of "Read-Only"
	Button btnReadOnly;
	// The button of "Hidden"
	Button btnHidden;
	// The button of "Permissions"
	Button[] btnPermissions;

	/**
	 * Create a horizontal separator between field sections.
	 *
	 * @param parent
	 *            The parent composite of the separator.
	 */
	protected void createSeparator(Composite parent) {
		Label label = new Label(parent, SWT.SEPARATOR | SWT.SHADOW_ETCHED_IN | SWT.HORIZONTAL);
		GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
		data.horizontalSpan = 2;
		label.setLayoutData(data);
	}

	/**
	 * Create a field displaying the a specific value with a specific label.
	 *
	 * @param text
	 *            The label text for the field.
	 * @param value
	 *            The value to be displayed.
	 * @param parent
	 *            The parent composite of the field.
	 */
	protected void createField(String text, String value, Composite parent) {
		Label label = new Label(parent, SWT.NONE);
		label.setText(text);
		GridData data = new GridData();
		data.horizontalAlignment = SWT.LEFT;
		data.verticalAlignment = SWT.TOP;
		label.setLayoutData(data);
		Text txt = new Text(parent, SWT.WRAP | SWT.READ_ONLY);
		data = new GridData();
		data.verticalAlignment = SWT.TOP;
		data.widthHint = 300;
		data.grabExcessHorizontalSpace = true;
		data.horizontalAlignment = GridData.FILL;
		txt.setLayoutData(data);
		txt.setBackground(txt.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		txt.setText(value);
	}

	/**
	 * Get the string of the file size using using the formatter, SIZE_FORMAT.
	 *
	 * @param size
	 *            The size of the file to be formatted.
	 * @return The string in the format of SIZE_FORMAT.
	 */
	protected String getSizeText(long size) {
		return NLS.bind(Messages.GeneralInformationPage_FileSizeInfo, SIZE_FORMAT.format(size / 1024), SIZE_FORMAT.format(size));
	}

	/**
	 * Get the string of the specific time using the formatter, DATE_FORMAT.
	 *
	 * @param time
	 *            The time to be formatted.
	 * @return The string in the format of DATE_FORMAT.
	 */
	protected String getDateText(long time) {
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
		return dateFormat.format(new Date(time));
	}

	/**
	 * Create the attributes section for a Windows file/folder.
	 *
	 * @param parent
	 *            The parent composite on which it is created.
	 */
	protected void createAttributesSection(Composite parent) {
		// Attributes
		Label label = new Label(parent, SWT.NONE);
		label.setText(Messages.GeneralInformationPage_Attributes);
		GridData data = new GridData();
		data.horizontalAlignment = SWT.LEFT;
		label.setLayoutData(data);

		Composite attr = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(3, true);
		layout.marginHeight = 0;
		attr.setLayout(layout);
		// Read-only
		btnReadOnly = new Button(attr, SWT.CHECK);
		btnReadOnly.setText(Messages.GeneralInformationPage_ReadOnly);
		// Only the owner can edit this property
		btnReadOnly.setEnabled(node.isAgentOwner());
		btnReadOnly.addSelectionListener(new SelectionAdapter(){
			@Override
            public void widgetSelected(SelectionEvent e) {
				if(btnReadOnly.getSelection()!=clone.isReadOnly()){
					clone.setReadOnly(btnReadOnly.getSelection());
				}
            }
		});
		// Hidden
		btnHidden = new Button(attr, SWT.CHECK);
		btnHidden.setText(Messages.GeneralInformationPage_Hidden);
		// Only the owner can edit this property
		btnHidden.setEnabled(node.isAgentOwner());
		btnHidden.addSelectionListener(new SelectionAdapter(){
			@Override
            public void widgetSelected(SelectionEvent e) {
				Button btnHidden = (Button) e.getSource();
				if(btnHidden.getSelection()!=clone.isHidden()){
					clone.setHidden(btnHidden.getSelection());
				}
            }
		});
		// Advanced Attributes
		Button btnAdvanced = new Button(attr, SWT.PUSH);
		btnAdvanced.setText(Messages.GeneralInformationPage_Advanced);
		btnAdvanced.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showAdvancedAttributes();
			}
		});
		// Update the attribute values.
		updateAttributes();
	}

	/**
	 * Update the value of attributes section.
	 */
	private void updateAttributes() {
		btnReadOnly.setSelection(clone.isReadOnly());
		btnHidden.setSelection(clone.isHidden());
	}

	/**
	 * Show the advanced attributes dialog for the specified file/folder.
	 */
	void showAdvancedAttributes() {
		AdvancedAttributesDialog dialog = new AdvancedAttributesDialog(this.getShell(), (FSTreeNode)(clone.clone()));
		if (dialog.open() == Window.OK) {
			FSTreeNode result = dialog.getResult();
			clone.attr = result.attr;
		}
	}

	/**
	 * Create the permissions section for a Unix/Linux file/folder.
	 *
	 * @param parent
	 *            The parent composite on which it is created.
	 */
	protected void createPermissionsSection(Composite parent) {
		GridLayout gridLayout;
		Label label = new Label(parent, SWT.NONE);
		label.setText(Messages.GeneralInformationPage_PermissionText);
		GridData data = new GridData();
		data.horizontalAlignment = SWT.LEFT;
		data.verticalAlignment = SWT.TOP;
		label.setLayoutData(data);
		Composite perms = new Composite(parent, SWT.NONE);
		gridLayout = new GridLayout(2, false);
		gridLayout.marginHeight = 0;
		perms.setLayout(gridLayout);
		btnPermissions = new Button[9];
		createPermissionGroup(perms, 0,
				Messages.PermissionsGroup_UserPermissions);
		createPermissionGroup(perms, 3,
				Messages.PermissionsGroup_GroupPermissions);
		createPermissionGroup(perms, 6,
				Messages.PermissionsGroup_OtherPermissions);
		// Update the permission values.
		updatePermissions();
	}

	/**
	 * Create a permission group for a role, such as a user, a group or others.
	 *
	 * @param parent
	 *            The parent composite.
	 * @param bit
	 *            The permission bit index.
	 * @param header
	 *            The group's header label.
	 */
	protected void createPermissionGroup(Composite parent, int bit, String header) {
		Label label = new Label(parent, SWT.NONE);
		label.setText(header);
		GridData data = new GridData();
		data.horizontalAlignment = SWT.LEFT;
		label.setLayoutData(data);
		Composite group = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(3, true);
		layout.marginHeight = 0;
		group.setLayout(layout);
		createPermissionButton(Messages.PermissionsGroup_Readable, bit, group);
		createPermissionButton(Messages.PermissionsGroup_Writable, bit + 1, group);
		createPermissionButton(Messages.PermissionsGroup_Executable, bit + 2, group);
	}

	/**
	 * Create a check-box field for a single permission item.
	 *
	 * @param label
	 *            The label of the permission.
	 * @param index
	 *            The index of current permission bit mask index.
	 * @param parent
	 *            The parent to hold the check-box field.
	 */
	private void createPermissionButton(String label, final int index, Composite parent) {
		btnPermissions[index] = new Button(parent, SWT.CHECK);
		btnPermissions[index].setText(label);
		// Only the owner can edit its permission.
		btnPermissions[index].setEnabled(node.isAgentOwner());
		btnPermissions[index].addSelectionListener(new SelectionAdapter(){
			@Override
            public void widgetSelected(SelectionEvent e) {
				int bit = 1 << (8 - index);
				boolean on = clone.attr != null && (clone.attr.permissions & bit) != 0;
				boolean newOn = btnPermissions[index].getSelection();
				if (newOn != on) {
					int permissions = clone.attr != null ? clone.attr.permissions : 0;
					permissions = newOn ? (permissions | bit) : (permissions & ~bit);
					clone.setPermissions(permissions);
				}
            }
		});
	}

	/**
	 * Update the value of permissions section.
	 */
	private void updatePermissions(){
		for (int i = 0; i < 9; i++) {
			final int bit = 1 << (8 - i);
			final boolean on = clone.attr != null && (clone.attr.permissions & bit) != 0;
			btnPermissions[i].setSelection(on);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
	 */
	@Override
    protected void performDefaults() {
		clone = (FSTreeNode) node.clone();
		if (node.isWindowsNode()) {
			updateAttributes();
		}
		else {
			updatePermissions();
		}
	    super.performDefaults();
    }

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#performOk()
	 */
	@Override
    public boolean performOk() {
		if (hasAttrsChanged()) {
			IStatus status = commitAttr();
			if(!status.isOK()) {
				setErrorMessage(status.getMessage());
				return false;
			}
		}
		return true;
    }

	/**
	 * Commit the new attributes of the file and
	 * return a status. This operation will try
	 * several times before reporting failure.
	 *
	 * @return The committing status.
	 */
	private IStatus commitAttr() {
		OpCommitAttr op = new OpCommitAttr(node, clone.attr);
		IOpExecutor executor = new NullOpExecutor();
		IStatus status = null;
		for (int i = 0; i < RETRY_TIMES; i++) {
			status = executor.execute(op);
			if (status.isOK()) {
				if (!node.isRoot()) {
					// Refresh the parent so that the filters work!
					executor = new JobExecutor();
					executor.execute(new OpRefresh(node.getParent()));
				}
				return status;
			}
		}
		return status;
	}

	/**
	 * If the attributes has been changed.
	 * @return If the attributes has been changed.
	 */
	private boolean hasAttrsChanged(){
		if(node.isWindowsNode()){
			// If it is a Windows file, only check its attributes.
			return node.getWin32Attrs() != clone.getWin32Attrs();
		}
		// If it is not a Windows file, only check its permissions.
		return node.attr != null && clone.attr != null && node.attr.permissions != clone.attr.permissions;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createContents(Composite parent) {
		IAdaptable element = getElement();
		Assert.isTrue(element instanceof FSTreeNode);

		node = (FSTreeNode) element;
		clone = (FSTreeNode) node.clone();
		Composite page = new Composite(parent, SWT.NONE);
		GridLayout gridLayout = new GridLayout(2, false);
		page.setLayout(gridLayout);
		// Field "Name"
		createField(Messages.GeneralInformationPage_Name, clone.name, page);
		// Field "Type"
		createField(Messages.GeneralInformationPage_Type, clone.getFileType(), page);
		// Field "Location"
		String location = clone.isSystemRoot() || clone.isRoot() ?
						Messages.GeneralInformationPage_Computer : clone.getLocation();
		createField(Messages.GeneralInformationPage_Location, location, page);
		// Field "Size"
		if (clone.isFile()) {
			createField(Messages.GeneralInformationPage_Size, clone.attr != null ? getSizeText(clone.attr.size) : "", page); //$NON-NLS-1$
		}
		// Field "Modified"
		createField(Messages.GeneralInformationPage_Modified, clone.attr != null ? getDateText(clone.attr.mtime) : "", page); //$NON-NLS-1$
		// Field "Accessed"
		if (clone.isFile()) {
			createField(Messages.GeneralInformationPage_Accessed, clone.attr != null ? getDateText(clone.attr.atime) : "", page); //$NON-NLS-1$
		}
		createSeparator(page);
		if (clone.isWindowsNode()) {
			createAttributesSection(page);
		} else {
			createPermissionsSection(page);
		}
		return page;
	}
}

Back to the top