View Javadoc

1   package org.eparapher.rcp.properties;
2   
3   import java.io.File;
4   import java.util.Date;
5   
6   import org.apache.log4j.Logger;
7   import org.eclipse.jface.preference.PreferencePage;
8   import org.eclipse.swt.SWT;
9   import org.eclipse.swt.layout.GridData;
10  import org.eclipse.swt.layout.GridLayout;
11  import org.eclipse.swt.widgets.Button;
12  import org.eclipse.swt.widgets.Composite;
13  import org.eclipse.swt.widgets.Control;
14  import org.eclipse.swt.widgets.Label;
15  import org.eclipse.ui.dialogs.PropertyPage;
16  import org.eparapher.rcp.views.documents.SecuredDocumentsView;
17  
18  public class FilePropertyPage extends PropertyPage {
19  
20  	private static final String ID = "org.eparapher.rcp.properties.filePropertyPage";
21  	
22  	private static Logger log = Logger.getLogger(FilePropertyPage.class);
23  	
24  	private static final String PATH_TITLE    = "Path : ";
25  	private static final String SIZE_TITLE    = "Size : ";
26  	private static final String LASTMOD_TITLE = "Last modified : ";
27  	
28  	private static final String READ_TITLE    = "Readable : ";
29  	private static final String WRITE_TITLE   = "Writable : ";
30  	private static final String EXEC_TITLE    = "Executable : ";
31  	
32  	private static final int TEXT_FIELD_WIDTH = 150;
33  	
34  	private File file = null;
35  
36  	private Button readButton = null;
37  	private Button writeButton = null;
38  	private Button executeButton = null;
39  	/**
40  	 * Constructor for SamplePropertyPage.
41  	 */
42  	public FilePropertyPage() {
43  		super();
44  	}
45  
46  	private void setSelectedFile() {
47  		String filepath = ((SecuredDocumentsView.TreeObject) getElement()).getFilePath();
48  		file = new File(filepath);
49  	}
50  	
51  	private void addFirstSection(Composite parent) {
52  		Composite composite = createDefaultComposite(parent);
53  
54  		//Path
55  		Label pathLabel = new Label(composite, SWT.NONE);
56  		pathLabel.setText(PATH_TITLE);
57  		Label pathValue = new Label(composite, SWT.NONE);
58  		pathValue.setText(file.getAbsolutePath());
59  		//pathValue.setText(((SecuredDocumentsView.TreeObject) getElement()).getFilePath());
60  		//Text pathValueText = new Text(composite, SWT.WRAP | SWT.READ_ONLY);
61  		//pathValueText.setText(((IResource) getElement()).getFullPath().toString());
62  		
63  		//Size
64  		Label sizeLabel = new Label(composite, SWT.NONE);
65  		sizeLabel.setText(SIZE_TITLE);
66  		Label sizeValue = new Label(composite, SWT.NONE);
67  		sizeValue.setText( file.length() + " Bytes");
68  	}
69  
70  	private void addSeparator(Composite parent) {
71  		Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
72  		GridData gridData = new GridData();
73  		gridData.horizontalAlignment = GridData.FILL;
74  		gridData.grabExcessHorizontalSpace = true;
75  		separator.setLayoutData(gridData);
76  	}
77  
78  	private void addSecondSection(Composite parent) {
79  		Composite composite = createDefaultComposite(parent);
80  
81  		// Last Modified
82  		Label lastmodLabel = new Label(composite, SWT.NONE);
83  		lastmodLabel.setText(LASTMOD_TITLE);
84  		Label lastmodValue = new Label(composite, SWT.NONE);
85  		lastmodValue.setText(new Date(file.lastModified()).toString());
86  		
87  		//Read 
88  		Label readLabel = new Label(composite, SWT.NONE);
89  		readLabel.setText(READ_TITLE);
90  		readButton = new Button(composite, SWT.CHECK);
91  		readButton.setSelection(file.canRead());
92  		readButton.setEnabled(false);
93  
94  		//Write
95  		Label writeLabel = new Label(composite, SWT.NONE);
96  		writeLabel.setText(WRITE_TITLE);
97  		writeButton = new Button(composite, SWT.CHECK);
98  		writeButton.setSelection(file.canRead());
99  		writeButton.setEnabled(false);
100 
101 		//Execute
102 		Label execLabel = new Label(composite, SWT.NONE);
103 		execLabel.setText(EXEC_TITLE);
104 		executeButton = new Button(composite, SWT.CHECK);
105 		executeButton.setSelection(file.canRead());
106 		executeButton.setEnabled(false);
107 	}
108 	
109 	/**
110 	 * @see PreferencePage#createContents(Composite)
111 	 */
112 	protected Control createContents(Composite parent) {
113 		Composite composite = new Composite(parent, SWT.NONE);
114 		GridLayout layout = new GridLayout();
115 		composite.setLayout(layout);
116 		GridData data = new GridData(GridData.FILL);
117 		data.grabExcessHorizontalSpace = true;
118 		composite.setLayoutData(data);
119 
120 		setSelectedFile();
121 		addFirstSection(composite);
122 		addSeparator(composite);
123 		addSecondSection(composite);
124 		
125 		return composite;
126 	}
127 
128 	private Composite createDefaultComposite(Composite parent) {
129 		Composite composite = new Composite(parent, SWT.NULL);
130 		GridLayout layout = new GridLayout();
131 		layout.numColumns = 2;
132 		composite.setLayout(layout);
133 
134 		GridData data = new GridData();
135 		data.verticalAlignment = GridData.FILL;
136 		data.horizontalAlignment = GridData.FILL;
137 		composite.setLayoutData(data);
138 
139 		return composite;
140 	}
141 
142 	protected void performDefaults() {
143 		// Populate the owner text field with the default value
144 		//ownerText.setText(DEFAULT_OWNER);
145 	}
146 	
147 	public boolean performOk() {
148 		// store the value in the owner text field
149 		try {
150 			file.setReadable(readButton.getSelection());
151 		} catch (SecurityException e) {
152 			log.error("Cannot set "+file.getAbsolutePath()+" as read only");
153 			return false;
154 		}
155 		return true;
156 	}
157 
158 }