package org.eparapher.rcp.properties; import java.io.File; import java.util.Date; import org.apache.log4j.Logger; import org.eclipse.jface.preference.PreferencePage; import org.eclipse.swt.SWT; 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.ui.dialogs.PropertyPage; import org.eparapher.rcp.views.documents.SecuredDocumentsView; public class FilePropertyPage extends PropertyPage { private static final String ID = "org.eparapher.rcp.properties.filePropertyPage"; private static Logger log = Logger.getLogger(FilePropertyPage.class); private static final String PATH_TITLE = "Path : "; private static final String SIZE_TITLE = "Size : "; private static final String LASTMOD_TITLE = "Last modified : "; private static final String READ_TITLE = "Readable : "; private static final String WRITE_TITLE = "Writable : "; private static final String EXEC_TITLE = "Executable : "; private static final int TEXT_FIELD_WIDTH = 150; private File file = null; private Button readButton = null; private Button writeButton = null; private Button executeButton = null; /** * Constructor for SamplePropertyPage. */ public FilePropertyPage() { super(); } private void setSelectedFile() { String filepath = ((SecuredDocumentsView.TreeObject) getElement()).getFilePath(); file = new File(filepath); } private void addFirstSection(Composite parent) { Composite composite = createDefaultComposite(parent); //Path Label pathLabel = new Label(composite, SWT.NONE); pathLabel.setText(PATH_TITLE); Label pathValue = new Label(composite, SWT.NONE); pathValue.setText(file.getAbsolutePath()); //pathValue.setText(((SecuredDocumentsView.TreeObject) getElement()).getFilePath()); //Text pathValueText = new Text(composite, SWT.WRAP | SWT.READ_ONLY); //pathValueText.setText(((IResource) getElement()).getFullPath().toString()); //Size Label sizeLabel = new Label(composite, SWT.NONE); sizeLabel.setText(SIZE_TITLE); Label sizeValue = new Label(composite, SWT.NONE); sizeValue.setText( file.length() + " Bytes"); } private void addSeparator(Composite parent) { Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL); GridData gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; separator.setLayoutData(gridData); } private void addSecondSection(Composite parent) { Composite composite = createDefaultComposite(parent); // Last Modified Label lastmodLabel = new Label(composite, SWT.NONE); lastmodLabel.setText(LASTMOD_TITLE); Label lastmodValue = new Label(composite, SWT.NONE); lastmodValue.setText(new Date(file.lastModified()).toString()); //Read Label readLabel = new Label(composite, SWT.NONE); readLabel.setText(READ_TITLE); readButton = new Button(composite, SWT.CHECK); readButton.setSelection(file.canRead()); readButton.setEnabled(false); //Write Label writeLabel = new Label(composite, SWT.NONE); writeLabel.setText(WRITE_TITLE); writeButton = new Button(composite, SWT.CHECK); writeButton.setSelection(file.canRead()); writeButton.setEnabled(false); //Execute Label execLabel = new Label(composite, SWT.NONE); execLabel.setText(EXEC_TITLE); executeButton = new Button(composite, SWT.CHECK); executeButton.setSelection(file.canRead()); executeButton.setEnabled(false); } /** * @see PreferencePage#createContents(Composite) */ protected Control createContents(Composite parent) { Composite composite = new Composite(parent, SWT.NONE); GridLayout layout = new GridLayout(); composite.setLayout(layout); GridData data = new GridData(GridData.FILL); data.grabExcessHorizontalSpace = true; composite.setLayoutData(data); setSelectedFile(); addFirstSection(composite); addSeparator(composite); addSecondSection(composite); return composite; } private Composite createDefaultComposite(Composite parent) { Composite composite = new Composite(parent, SWT.NULL); GridLayout layout = new GridLayout(); layout.numColumns = 2; composite.setLayout(layout); GridData data = new GridData(); data.verticalAlignment = GridData.FILL; data.horizontalAlignment = GridData.FILL; composite.setLayoutData(data); return composite; } protected void performDefaults() { // Populate the owner text field with the default value //ownerText.setText(DEFAULT_OWNER); } public boolean performOk() { // store the value in the owner text field try { file.setReadable(readButton.getSelection()); } catch (SecurityException e) { log.error("Cannot set "+file.getAbsolutePath()+" as read only"); return false; } return true; } }