001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.readmetool;
011:
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.io.InputStreamReader;
015: import java.io.Reader;
016:
017: import org.eclipse.core.resources.IFile;
018: import org.eclipse.core.resources.IResource;
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.IAdaptable;
021: import org.eclipse.core.runtime.IStatus;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.layout.GridData;
024: import org.eclipse.swt.layout.GridLayout;
025: import org.eclipse.swt.widgets.Canvas;
026: import org.eclipse.swt.widgets.Composite;
027: import org.eclipse.swt.widgets.Control;
028: import org.eclipse.swt.widgets.Label;
029: import org.eclipse.ui.PlatformUI;
030: import org.eclipse.ui.dialogs.PropertyPage;
031:
032: /**
033: * This page will be added to the property page dialog
034: * when the "Properties..." popup menu item is selected
035: * for Readme files.
036: */
037: public class ReadmeFilePropertyPage extends PropertyPage {
038:
039: /**
040: * Utility method that creates a new composite and
041: * sets up its layout data.
042: *
043: * @param parent the parent of the composite
044: * @param numColumns the number of columns in the new composite
045: * @return the newly-created composite
046: */
047: protected Composite createComposite(Composite parent, int numColumns) {
048: Composite composite = new Composite(parent, SWT.NULL);
049: GridLayout layout = new GridLayout();
050: layout.numColumns = numColumns;
051: composite.setLayout(layout);
052: GridData data = new GridData();
053: data.verticalAlignment = GridData.FILL;
054: data.horizontalAlignment = GridData.FILL;
055: composite.setLayoutData(data);
056: return composite;
057: }
058:
059: /** (non-Javadoc)
060: * Method declared on PreferencePage
061: */
062: public Control createContents(Composite parent) {
063:
064: // ensure the page has no special buttons
065: noDefaultAndApplyButton();
066: Composite panel = createComposite(parent, 2);
067:
068: PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
069: IReadmeConstants.PROPERTY_PAGE_CONTEXT);
070:
071: // layout the page
072:
073: IResource resource = (IResource) getElement();
074: IStatus result = null;
075: if (resource.getType() == IResource.FILE) {
076: Label label = createLabel(panel, MessageUtil
077: .getString("File_name")); //$NON-NLS-1$
078: label = createLabel(panel, resource.getName());
079: grabExcessSpace(label);
080:
081: //
082: createLabel(panel, MessageUtil.getString("Path")); //$NON-NLS-1$
083: label = createLabel(panel, resource.getFullPath()
084: .setDevice(null).toString());
085: grabExcessSpace(label);
086:
087: //
088: createLabel(panel, MessageUtil.getString("Size")); //$NON-NLS-1$
089: InputStream contentStream = null;
090: try {
091: IFile file = (IFile) resource;
092: contentStream = file.getContents();
093: Reader in = new InputStreamReader(contentStream);
094: int chunkSize = contentStream.available();
095: StringBuffer buffer = new StringBuffer(chunkSize);
096: char[] readBuffer = new char[chunkSize];
097: int n = in.read(readBuffer);
098:
099: while (n > 0) {
100: buffer.append(readBuffer);
101: n = in.read(readBuffer);
102: }
103:
104: contentStream.close();
105: label = createLabel(panel, Integer.toString(buffer
106: .length()));
107: } catch (CoreException e) {
108: result = e.getStatus();
109: String message = result.getMessage();
110: if (message == null)
111: label = createLabel(panel, MessageUtil
112: .getString("<Unknown>")); //$NON-NLS-1$
113: else
114: label = createLabel(panel, message);
115: } catch (IOException e) {
116: label = createLabel(panel, MessageUtil
117: .getString("<Unknown>")); //$NON-NLS-1$
118: } finally {
119: if (contentStream != null) {
120: try {
121: contentStream.close();
122: } catch (IOException e) {
123: // do nothing
124: }
125: }
126: }
127: grabExcessSpace(label);
128: createLabel(panel, MessageUtil
129: .getString("Number_of_sections")); //$NON-NLS-1$
130: // We will get the sections property and simply
131: // report number of elements found.
132: IAdaptable sections = getSections(resource);
133: if (sections instanceof AdaptableList) {
134: AdaptableList list = (AdaptableList) sections;
135: label = createLabel(panel, String.valueOf(list.size()));
136: grabExcessSpace(label);
137: }
138: }
139:
140: //
141: Label label = createLabel(panel, MessageUtil
142: .getString("Additional_information")); //$NON-NLS-1$
143: grabExcessSpace(label);
144: GridData gd = (GridData) label.getLayoutData();
145: gd.horizontalSpan = 2;
146: return new Canvas(panel, 0);
147: }
148:
149: /**
150: * Utility method that creates a new label and sets up its layout data.
151: *
152: * @param parent the parent of the label
153: * @param text the text of the label
154: * @return the newly-created label
155: */
156: protected Label createLabel(Composite parent, String text) {
157: Label label = new Label(parent, SWT.LEFT);
158: label.setText(text);
159: GridData data = new GridData();
160: data.horizontalAlignment = GridData.FILL;
161: label.setLayoutData(data);
162: return label;
163: }
164:
165: /**
166: * Returns the readme sections for this resource, or null
167: * if not applicable (resource is not a readme file).
168: */
169: private AdaptableList getSections(IAdaptable adaptable) {
170: if (adaptable instanceof IFile)
171: return ReadmeModelFactory.getInstance().getSections(
172: (IFile) adaptable);
173: return null;
174: }
175:
176: /**
177: * Sets this control to grab any excess horizontal space
178: * left in the window.
179: *
180: * @param control the control for which to grab excess space
181: */
182: private void grabExcessSpace(Control control) {
183: GridData gd = (GridData) control.getLayoutData();
184: if (gd != null) {
185: gd.grabExcessHorizontalSpace = true;
186: }
187: }
188:
189: /** (non-Javadoc)
190: * Method declared on PreferencePage
191: */
192: public boolean performOk() {
193: // nothing to do - read-only page
194: return true;
195: }
196: }
|