KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > ResourceInfoPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.ide.dialogs;
12
13 import java.net.URI JavaDoc;
14
15 import org.eclipse.core.filesystem.EFS;
16 import org.eclipse.core.filesystem.IFileSystem;
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.resources.ResourceAttributes;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.content.IContentDescription;
25 import org.eclipse.jface.dialogs.ErrorDialog;
26 import org.eclipse.jface.preference.FieldEditor;
27 import org.eclipse.jface.util.IPropertyChangeListener;
28 import org.eclipse.jface.util.PropertyChangeEvent;
29 import org.eclipse.osgi.util.TextProcessor;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.graphics.Font;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Text;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.dialogs.PropertyPage;
41 import org.eclipse.ui.ide.dialogs.ResourceEncodingFieldEditor;
42 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
43 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
44 import org.eclipse.ui.internal.ide.LineDelimiterEditor;
45
46 /**
47  * The ResourceInfoPage is the page that shows the basic info about the
48  * resource.
49  */

50 public class ResourceInfoPage extends PropertyPage {
51
52     private Button editableBox;
53
54     private Button executableBox;
55
56     private Button archiveBox;
57
58     private Button derivedBox;
59
60     private boolean previousReadOnlyValue;
61
62     private boolean previousExecutableValue;
63
64     private boolean previousArchiveValue;
65
66     private boolean previousDerivedValue;
67
68     private IContentDescription cachedContentDescription;
69
70     private ResourceEncodingFieldEditor encodingEditor;
71
72     private LineDelimiterEditor lineDelimiterEditor;
73
74     private static String JavaDoc READ_ONLY = IDEWorkbenchMessages.ResourceInfo_readOnly;
75
76     private static String JavaDoc EXECUTABLE = IDEWorkbenchMessages.ResourceInfo_executable;
77
78     private static String JavaDoc ARCHIVE = IDEWorkbenchMessages.ResourceInfo_archive;
79
80     private static String JavaDoc DERIVED = IDEWorkbenchMessages.ResourceInfo_derived;
81
82     private static String JavaDoc TYPE_TITLE = IDEWorkbenchMessages.ResourceInfo_type;
83
84     private static String JavaDoc LOCATION_TITLE = IDEWorkbenchMessages.ResourceInfo_location;
85
86     private static String JavaDoc RESOLVED_LOCATION_TITLE = IDEWorkbenchMessages.ResourceInfo_resolvedLocation;
87
88     private static String JavaDoc SIZE_TITLE = IDEWorkbenchMessages.ResourceInfo_size;
89
90     private static String JavaDoc PATH_TITLE = IDEWorkbenchMessages.ResourceInfo_path;
91
92     private static String JavaDoc TIMESTAMP_TITLE = IDEWorkbenchMessages.ResourceInfo_lastModified;
93
94     private static String JavaDoc FILE_ENCODING_TITLE = IDEWorkbenchMessages.WorkbenchPreference_encoding;
95
96     private static String JavaDoc CONTAINER_ENCODING_TITLE = IDEWorkbenchMessages.ResourceInfo_fileEncodingTitle;
97
98     // Max value width in characters before wrapping
99
private static final int MAX_VALUE_WIDTH = 80;
100
101     /**
102      * Create the group that shows the name, location, size and type.
103      *
104      * @param parent
105      * the composite the group will be created in
106      * @param resource
107      * the resource the information is being taken from.
108      * @return the composite for the group
109      */

110     private Composite createBasicInfoGroup(Composite parent, IResource resource) {
111
112         Font font = parent.getFont();
113
114         Composite basicInfoComposite = new Composite(parent, SWT.NULL);
115         GridLayout layout = new GridLayout();
116         layout.numColumns = 2;
117         layout.marginWidth = 0;
118         layout.marginHeight = 0;
119         basicInfoComposite.setLayout(layout);
120         GridData data = new GridData();
121         data.verticalAlignment = GridData.FILL;
122         data.horizontalAlignment = GridData.FILL;
123         basicInfoComposite.setLayoutData(data);
124         basicInfoComposite.setFont(font);
125
126         // The group for path
127
Label pathLabel = new Label(basicInfoComposite, SWT.NONE);
128         pathLabel.setText(PATH_TITLE);
129         GridData gd = new GridData();
130         gd.verticalAlignment = SWT.TOP;
131         pathLabel.setLayoutData(gd);
132         pathLabel.setFont(font);
133
134         // path value label
135
Text pathValueText = new Text(basicInfoComposite, SWT.WRAP
136                 | SWT.READ_ONLY);
137         String JavaDoc pathString = TextProcessor.process(resource.getFullPath()
138                 .toString());
139         pathValueText.setText(pathString);
140         gd = new GridData();
141         gd.widthHint = convertWidthInCharsToPixels(MAX_VALUE_WIDTH);
142         gd.grabExcessHorizontalSpace = true;
143         gd.horizontalAlignment = GridData.FILL;
144         pathValueText.setLayoutData(gd);
145         pathValueText.setFont(font);
146         pathValueText.setBackground(pathValueText.getDisplay().getSystemColor(
147                 SWT.COLOR_WIDGET_BACKGROUND));
148
149         // The group for types
150
Label typeTitle = new Label(basicInfoComposite, SWT.LEFT);
151         typeTitle.setText(TYPE_TITLE);
152         typeTitle.setFont(font);
153
154         Text typeValue = new Text(basicInfoComposite, SWT.LEFT | SWT.READ_ONLY);
155         typeValue.setText(IDEResourceInfoUtils.getTypeString(resource,
156                 getContentDescription(resource)));
157         typeValue.setBackground(typeValue.getDisplay().getSystemColor(
158                 SWT.COLOR_WIDGET_BACKGROUND));
159         typeValue.setFont(font);
160
161         // The group for location
162
Label locationTitle = new Label(basicInfoComposite, SWT.LEFT);
163         locationTitle.setText(LOCATION_TITLE);
164         gd = new GridData();
165         gd.verticalAlignment = SWT.TOP;
166         locationTitle.setLayoutData(gd);
167         locationTitle.setFont(font);
168
169         Text locationValue = new Text(basicInfoComposite, SWT.WRAP
170                 | SWT.READ_ONLY);
171         String JavaDoc locationStr = TextProcessor.process(IDEResourceInfoUtils
172                 .getLocationText(resource));
173         locationValue.setText(locationStr);
174         gd = new GridData();
175         gd.widthHint = convertWidthInCharsToPixels(MAX_VALUE_WIDTH);
176         gd.grabExcessHorizontalSpace = true;
177         gd.horizontalAlignment = GridData.FILL;
178         locationValue.setLayoutData(gd);
179         locationValue.setFont(font);
180         locationValue.setBackground(locationValue.getDisplay().getSystemColor(
181                 SWT.COLOR_WIDGET_BACKGROUND));
182
183         if (isPathVariable(resource)) {
184             Label resolvedLocationTitle = new Label(basicInfoComposite,
185                     SWT.LEFT);
186             resolvedLocationTitle.setText(RESOLVED_LOCATION_TITLE);
187             gd = new GridData();
188             gd.verticalAlignment = SWT.TOP;
189             resolvedLocationTitle.setLayoutData(gd);
190             resolvedLocationTitle.setFont(font);
191
192             Text resolvedLocationValue = new Text(basicInfoComposite, SWT.WRAP
193                     | SWT.READ_ONLY);
194             resolvedLocationValue.setText(IDEResourceInfoUtils
195                     .getResolvedLocationText(resource));
196             gd = new GridData();
197             gd.widthHint = convertWidthInCharsToPixels(MAX_VALUE_WIDTH);
198             gd.grabExcessHorizontalSpace = true;
199             gd.horizontalAlignment = GridData.FILL;
200             resolvedLocationValue.setLayoutData(gd);
201             resolvedLocationValue.setFont(font);
202             resolvedLocationValue.setBackground(resolvedLocationValue
203                     .getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
204         }
205         if (resource.getType() == IResource.FILE) {
206             // The group for size
207
Label sizeTitle = new Label(basicInfoComposite, SWT.LEFT);
208             sizeTitle.setText(SIZE_TITLE);
209             sizeTitle.setFont(font);
210
211             Text sizeValue = new Text(basicInfoComposite, SWT.LEFT
212                     | SWT.READ_ONLY);
213             sizeValue.setText(IDEResourceInfoUtils.getSizeString(resource));
214             gd = new GridData();
215             gd.widthHint = convertWidthInCharsToPixels(MAX_VALUE_WIDTH);
216             gd.grabExcessHorizontalSpace = true;
217             gd.horizontalAlignment = GridData.FILL;
218             sizeValue.setLayoutData(gd);
219             sizeValue.setFont(font);
220             sizeValue.setBackground(sizeValue.getDisplay().getSystemColor(
221                     SWT.COLOR_WIDGET_BACKGROUND));
222         }
223
224         return basicInfoComposite;
225     }
226
227     protected Control createContents(Composite parent) {
228
229         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
230                 IIDEHelpContextIds.RESOURCE_INFO_PROPERTY_PAGE);
231
232         // layout the page
233
IResource resource = (IResource) getElement().getAdapter(
234                 IResource.class);
235         
236         if (resource == null) {
237             Label label = new Label(parent, SWT.NONE);
238             label.setText(IDEWorkbenchMessages.ResourceInfoPage_noResource);
239             return label;
240         }
241         
242         if (resource.getType() != IResource.PROJECT) {
243             ResourceAttributes attrs = resource.getResourceAttributes();
244             if (attrs != null) {
245                 previousReadOnlyValue = attrs.isReadOnly();
246                 previousExecutableValue = attrs.isExecutable();
247                 previousArchiveValue = attrs.isArchive();
248             } else {
249                 previousReadOnlyValue = previousExecutableValue = previousArchiveValue = false;
250             }
251             previousDerivedValue = resource.isDerived();
252         }
253
254         // top level group
255
Composite composite = new Composite(parent, SWT.NONE);
256         GridLayout layout = new GridLayout();
257         layout.marginWidth = 0;
258         layout.marginHeight = 0;
259         composite.setLayout(layout);
260         GridData data = new GridData(GridData.FILL);
261         data.grabExcessHorizontalSpace = true;
262         composite.setLayoutData(data);
263         composite.setFont(parent.getFont());
264
265         createBasicInfoGroup(composite, resource);
266         createSeparator(composite);
267         createStateGroup(composite, resource);
268         new Label(composite, SWT.NONE); // a vertical spacer
269
encodingEditor = new ResourceEncodingFieldEditor(
270                 getFieldEditorLabel(resource), composite, resource);
271         encodingEditor.setPage(this);
272         encodingEditor.load();
273
274         encodingEditor.setPropertyChangeListener(new IPropertyChangeListener() {
275             /*
276              * (non-Javadoc)
277              *
278              * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
279              */

280             public void propertyChange(PropertyChangeEvent event) {
281                 if (event.getProperty().equals(FieldEditor.IS_VALID)) {
282                     setValid(encodingEditor.isValid());
283                 }
284
285             }
286         });
287
288         
289         if (resource.getType() == IResource.PROJECT) {
290             lineDelimiterEditor = new LineDelimiterEditor(composite, resource
291                     .getProject());
292             lineDelimiterEditor.doLoad();
293         }
294         
295         //We can't save the preferences for close
296
if (resource.getType() == IResource.PROJECT && !((IProject)resource).isOpen()){
297             encodingEditor.setEnabled(false, composite);
298             lineDelimiterEditor.setEnabled(false);
299         }
300
301         return composite;
302     }
303
304     /**
305      * Return the label for the encoding field editor for the resource.
306      *
307      * @param resource -
308      * the resource to edit.
309      * @return String
310      */

311     private String JavaDoc getFieldEditorLabel(IResource resource) {
312         if (resource instanceof IContainer) {
313             return CONTAINER_ENCODING_TITLE;
314         }
315         return FILE_ENCODING_TITLE;
316     }
317
318     /**
319      * Create the isEditable button and it's associated label as a child of
320      * parent using the editableValue of the receiver. The Composite will be the
321      * parent of the button.
322      *
323      * @param composite
324      * the parent of the button
325      */

326     private void createEditableButton(Composite composite) {
327
328         this.editableBox = new Button(composite, SWT.CHECK | SWT.RIGHT);
329         this.editableBox.setAlignment(SWT.LEFT);
330         this.editableBox.setText(READ_ONLY);
331         this.editableBox.setSelection(this.previousReadOnlyValue);
332         this.editableBox.setFont(composite.getFont());
333         GridData data = new GridData();
334         data.horizontalSpan = 2;
335         this.editableBox.setLayoutData(data);
336     }
337
338     /**
339      * Create the isExecutable button and it's associated label as a child of
340      * parent using the editableValue of the receiver. The Composite will be the
341      * parent of the button.
342      *
343      * @param composite
344      * the parent of the button
345      */

346     private void createExecutableButton(Composite composite) {
347
348         this.executableBox = new Button(composite, SWT.CHECK | SWT.RIGHT);
349         this.executableBox.setAlignment(SWT.LEFT);
350         this.executableBox.setText(EXECUTABLE);
351         this.executableBox.setSelection(this.previousExecutableValue);
352         this.executableBox.setFont(composite.getFont());
353         GridData data = new GridData();
354         data.horizontalSpan = 2;
355         this.executableBox.setLayoutData(data);
356     }
357
358     /**
359      * Create the isArchive button and it's associated label as a child of
360      * parent using the editableValue of the receiver. The Composite will be the
361      * parent of the button.
362      *
363      * @param composite
364      * the parent of the button
365      */

366     private void createArchiveButton(Composite composite) {
367
368         this.archiveBox = new Button(composite, SWT.CHECK | SWT.RIGHT);
369         this.archiveBox.setAlignment(SWT.LEFT);
370         this.archiveBox.setText(ARCHIVE);
371         this.archiveBox.setSelection(this.previousArchiveValue);
372         this.archiveBox.setFont(composite.getFont());
373         GridData data = new GridData();
374         data.horizontalSpan = 2;
375         this.archiveBox.setLayoutData(data);
376     }
377
378     /**
379      * Create the derived button and it's associated label as a child of parent
380      * using the derived of the receiver. The Composite will be the parent of
381      * the button.
382      *
383      * @param composite
384      * the parent of the button
385      */

386     private void createDerivedButton(Composite composite) {
387
388         this.derivedBox = new Button(composite, SWT.CHECK | SWT.RIGHT);
389         this.derivedBox.setAlignment(SWT.LEFT);
390         this.derivedBox.setText(DERIVED);
391         this.derivedBox.setSelection(this.previousDerivedValue);
392         this.derivedBox.setFont(composite.getFont());
393         GridData data = new GridData();
394         data.horizontalSpan = 2;
395         this.derivedBox.setLayoutData(data);
396     }
397
398     /**
399      * Create a separator that goes across the entire page
400      *
401      * @param composite
402      * The parent of the seperator
403      */

404     private void createSeparator(Composite composite) {
405
406         Label separator = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
407         GridData gridData = new GridData();
408         gridData.horizontalAlignment = GridData.FILL;
409         gridData.grabExcessHorizontalSpace = true;
410         separator.setLayoutData(gridData);
411     }
412
413     /**
414      * Create the group that shows the read only state and the timestamp.
415      *
416      * @param parent
417      * the composite the group will be created in
418      * @param resource
419      * the resource the information is being taken from.
420      */

421     private void createStateGroup(Composite parent, IResource resource) {
422
423         Font font = parent.getFont();
424
425         Composite composite = new Composite(parent, SWT.NULL);
426         GridLayout layout = new GridLayout();
427         layout.numColumns = 2;
428         layout.marginWidth = 0;
429         layout.marginHeight = 0;
430         composite.setLayout(layout);
431         GridData data = new GridData();
432         data.horizontalAlignment = GridData.FILL;
433         composite.setLayoutData(data);
434         composite.setFont(font);
435
436         Label timeStampLabel = new Label(composite, SWT.NONE);
437         timeStampLabel.setText(TIMESTAMP_TITLE);
438         timeStampLabel.setFont(font);
439
440         // timeStamp value label
441
Text timeStampValue = new Text(composite, SWT.READ_ONLY);
442         timeStampValue.setText(IDEResourceInfoUtils
443                 .getDateStringValue(resource));
444         timeStampValue.setFont(font);
445         timeStampValue.setBackground(timeStampValue.getDisplay()
446                 .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
447         timeStampValue.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
448                 | GridData.GRAB_HORIZONTAL));
449
450         // Not relevant to projects
451
if (resource.getType() != IResource.PROJECT) {
452             URI JavaDoc location = resource.getLocationURI();
453             if (location != null && location.getScheme() != null) {
454                 try {
455                     IFileSystem fs = EFS.getFileSystem(location.getScheme());
456                     int attributes = fs.attributes();
457                     if ((attributes & EFS.ATTRIBUTE_READ_ONLY) != 0) {
458                         createEditableButton(composite);
459                     }
460                     if ((attributes & EFS.ATTRIBUTE_EXECUTABLE) != 0) {
461                         createExecutableButton(composite);
462                     }
463                     if ((attributes & EFS.ATTRIBUTE_ARCHIVE) != 0) {
464                         createArchiveButton(composite);
465                     }
466                 } catch (CoreException e) {
467                     // ignore if we can't access the file system for this
468
// resource
469
}
470             }
471             createDerivedButton(composite);
472             // create warning for executable flag
473
if (executableBox != null && resource.getType() == IResource.FOLDER) {
474                 Composite noteComposite = createNoteComposite(font, composite,
475                         IDEWorkbenchMessages.Preference_note,
476                         IDEWorkbenchMessages.ResourceInfo_exWarning);
477                 GridData noteData = new GridData();
478                 noteData.horizontalSpan = 2;
479                 noteComposite.setLayoutData(noteData);
480             }
481         }
482     }
483
484     private IContentDescription getContentDescription(IResource resource) {
485         if (resource.getType() != IResource.FILE) {
486             return null;
487         }
488
489         if (cachedContentDescription == null) {
490             try {
491                 cachedContentDescription = ((IFile) resource)
492                         .getContentDescription();
493             } catch (CoreException e) {
494                 // silently ignore
495
}
496         }
497         return cachedContentDescription;
498     }
499
500     /**
501      * Returns whether the given resource is a linked resource bound to a path
502      * variable.
503      *
504      * @param resource
505      * resource to test
506      * @return boolean <code>true</code> the given resource is a linked
507      * resource bound to a path variable. <code>false</code> the given
508      * resource is either not a linked resource or it is not using a
509      * path variable.
510      */

511     private boolean isPathVariable(IResource resource) {
512         if (!resource.isLinked()) {
513             return false;
514         }
515
516         IPath resolvedLocation = resource.getLocation();
517         if (resolvedLocation == null) {
518             // missing path variable
519
return true;
520         }
521         IPath rawLocation = resource.getRawLocation();
522         if (resolvedLocation.equals(rawLocation)) {
523             return false;
524         }
525
526         return true;
527     }
528
529     /**
530      * Reset the editableBox to the false.
531      */

532     protected void performDefaults() {
533
534         IResource resource = (IResource) getElement().getAdapter(
535                 IResource.class);
536         
537         if (resource == null)
538             return;
539
540         // Nothing to update if we never made the box
541
if (this.editableBox != null) {
542             this.editableBox.setSelection(false);
543         }
544
545         // Nothing to update if we never made the box
546
if (this.executableBox != null) {
547             this.executableBox.setSelection(false);
548         }
549
550         // Nothing to update if we never made the box
551
if (this.derivedBox != null) {
552             this.derivedBox.setSelection(false);
553         }
554
555         encodingEditor.loadDefault();
556
557         if (lineDelimiterEditor != null) {
558             lineDelimiterEditor.loadDefault();
559         }
560
561     }
562
563     /**
564      * Apply the read only state and the encoding to the resource.
565      */

566     public boolean performOk() {
567
568         IResource resource = (IResource) getElement().getAdapter(
569                 IResource.class);
570         
571         if (resource == null)
572             return true;
573
574         encodingEditor.store();
575
576         if (lineDelimiterEditor != null) {
577             lineDelimiterEditor.store();
578         }
579
580         try {
581             ResourceAttributes attrs = resource.getResourceAttributes();
582             if (attrs != null) {
583                 boolean hasChange = false;
584                 // Nothing to update if we never made the box
585
if (editableBox != null
586                         && editableBox.getSelection() != previousReadOnlyValue) {
587                     attrs.setReadOnly(editableBox.getSelection());
588                     hasChange = true;
589                 }
590                 if (executableBox != null
591                         && executableBox.getSelection() != previousExecutableValue) {
592                     attrs.setExecutable(executableBox.getSelection());
593                     hasChange = true;
594                 }
595                 if (archiveBox != null
596                         && archiveBox.getSelection() != previousArchiveValue) {
597                     attrs.setArchive(archiveBox.getSelection());
598                     hasChange = true;
599                 }
600                 if (hasChange) {
601                     resource.setResourceAttributes(attrs);
602                     attrs = resource.getResourceAttributes();
603                     if (attrs != null) {
604                         previousReadOnlyValue = attrs.isReadOnly();
605                         previousExecutableValue = attrs.isExecutable();
606                         previousArchiveValue = attrs.isArchive();
607                         if (editableBox != null) {
608                             editableBox.setSelection(attrs.isReadOnly());
609                         }
610                         if (executableBox != null) {
611                             executableBox.setSelection(attrs.isExecutable());
612                         }
613                         if (archiveBox != null) {
614                             archiveBox.setSelection(attrs.isArchive());
615                         }
616                     }
617                 }
618             }
619
620             // Nothing to update if we never made the box
621
if (this.derivedBox != null) {
622                 boolean localDerivedValue = derivedBox.getSelection();
623                 if (previousDerivedValue != localDerivedValue) {
624                     resource.setDerived(localDerivedValue);
625                     boolean isDerived = resource.isDerived();
626                     previousDerivedValue = isDerived;
627                     derivedBox.setSelection(isDerived);
628                 }
629             }
630         } catch (CoreException exception) {
631             ErrorDialog.openError(getShell(),
632                     IDEWorkbenchMessages.InternalError, exception
633                             .getLocalizedMessage(), exception.getStatus());
634             return false;
635         }
636         return true;
637     }
638 }
639
Popular Tags