001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.swt.SWT;
021: import org.eclipse.swt.layout.GridData;
022: import org.eclipse.swt.layout.GridLayout;
023: import org.eclipse.swt.widgets.Canvas;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Control;
026: import org.eclipse.swt.widgets.Label;
027: import org.eclipse.ui.PlatformUI;
028: import org.eclipse.ui.dialogs.PropertyPage;
029:
030: /**
031: * This page will be added to the property page dialog
032: * when "Properties..." popup menu item is selected
033: * for Readme files.
034: *
035: * This page demonstrates conditional property pages which look
036: * different depending on the state of the element. In this example,
037: * the arbitrary condition chosen is whether the Readme file is
038: * greater than 256 bytes in length. If it is smaller than 256 bytes
039: * in length, this will be a placeholder page containing
040: * a simple message. If it is 256 bytes or larger, additional
041: * information will be provided. This information is determined at
042: * runtime.
043: *
044: * This class may be reused to implement a conditional property page.
045: * The getPageIndex() method tests the condition and returns the
046: * index of the page to create. The createPage*() methods are called
047: * upon to create the actual pages.
048: */
049: public class ReadmeFilePropertyPage2 extends PropertyPage {
050:
051: /**
052: * Utility method that creates a new composite and
053: * sets up its layout data.
054: *
055: * @param parent the parent of the composite
056: * @param numColumns the number of columns in the new composite
057: * @return the newly-created composite
058: */
059: protected Composite createComposite(Composite parent, int numColumns) {
060: Composite composite = new Composite(parent, SWT.NULL);
061: GridLayout layout = new GridLayout();
062: layout.numColumns = numColumns;
063: composite.setLayout(layout);
064: GridData data = new GridData();
065: data.verticalAlignment = GridData.FILL;
066: data.horizontalAlignment = GridData.FILL;
067: composite.setLayoutData(data);
068: return composite;
069: }
070:
071: /** (non-Javadoc)
072: * Method declared on PreferencePage
073: */
074: public Control createContents(Composite parent) {
075: // ensure the page has no special buttons
076: noDefaultAndApplyButton();
077: Composite panel = createComposite(parent, 2);
078:
079: PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
080: IReadmeConstants.PROPERTY_PAGE2_CONTEXT);
081:
082: // layout the page
083: int page = getPageIndex();
084: switch (page) {
085: case 1:
086: createPageOne(panel);
087: break;
088: case 2:
089: createPageTwo(panel);
090: break;
091: default:
092: }
093: return new Canvas(panel, 0);
094: }
095:
096: /**
097: * Utility method that creates a new label and sets up
098: * its layout data.
099: *
100: * @param parent the parent of the label
101: * @param text the text of the label
102: * @return the newly-created label
103: */
104: protected Label createLabel(Composite parent, String text) {
105: Label label = new Label(parent, SWT.LEFT);
106: label.setText(text);
107: GridData data = new GridData();
108: data.horizontalAlignment = GridData.FILL;
109: label.setLayoutData(data);
110: return label;
111: }
112:
113: /**
114: * Creates the first version of the page. This is a placeholder page which
115: * notified the user that the page is not available.
116: *
117: * @param panel the panel in which to create the page
118: */
119: protected void createPageOne(Composite panel) {
120: Label l = createLabel(
121: panel,
122: MessageUtil
123: .getString("Additional_Readme_properties_not_available.")); //$NON-NLS-1$
124: GridData gd = (GridData) l.getLayoutData();
125: gd.horizontalSpan = 2;
126: gd.grabExcessHorizontalSpace = true;
127: l = createLabel(
128: panel,
129: MessageUtil
130: .getString("This_illustrates_a_property_page_that_is_dynamically_determined")); //$NON-NLS-1$
131: gd = (GridData) l.getLayoutData();
132: gd.horizontalSpan = 2;
133: gd.grabExcessHorizontalSpace = true;
134: l = createLabel(
135: panel,
136: MessageUtil
137: .getString("not_to_be_available_based_on_the_state_of_the_object.")); //$NON-NLS-1$
138: gd = (GridData) l.getLayoutData();
139: gd.horizontalSpan = 2;
140: gd.grabExcessHorizontalSpace = true;
141: }
142:
143: /**
144: * Creates the second version of the page. This page might contain more information
145: * about the file or other information.
146: *
147: * @param panel the panel in which to create the page
148: */
149: protected void createPageTwo(Composite panel) {
150: Label l = createLabel(
151: panel,
152: MessageUtil
153: .getString("The_size_of_the_Readme_file_is_at_least_256_bytes.")); //$NON-NLS-1$
154: GridData gd = (GridData) l.getLayoutData();
155: gd.horizontalSpan = 2;
156: gd.grabExcessHorizontalSpace = true;
157: l = createLabel(
158: panel,
159: MessageUtil
160: .getString("Had_it_been_less_than_256_bytes_this_page_would_be_a_placeholder_page.")); //$NON-NLS-1$
161: gd = (GridData) l.getLayoutData();
162: gd.horizontalSpan = 2;
163: gd.grabExcessHorizontalSpace = true;
164: l = createLabel(panel, MessageUtil
165: .getString("Additional_information")); //$NON-NLS-1$
166: gd = (GridData) l.getLayoutData();
167: gd.horizontalSpan = 2;
168: gd.grabExcessHorizontalSpace = true;
169: l = createLabel(
170: panel,
171: MessageUtil
172: .getString("This_illustrates_a_property_page_that_is_dynamically_determined")); //$NON-NLS-1$
173: gd = (GridData) l.getLayoutData();
174: gd.horizontalSpan = 2;
175: gd.grabExcessHorizontalSpace = true;
176: l = createLabel(
177: panel,
178: MessageUtil
179: .getString("to_be_available_based_on_the_state_of_the_object.")); //$NON-NLS-1$
180: gd = (GridData) l.getLayoutData();
181: gd.horizontalSpan = 2;
182: gd.grabExcessHorizontalSpace = true;
183: }
184:
185: /**
186: * Returns which page to display. This implementation
187: * answers 1 if the size of the Readme file is less than 256 bytes
188: * and 2 otherwise.
189: *
190: * @return the index of the page to display
191: */
192: protected int getPageIndex() {
193: IResource resource = (IResource) getElement();
194:
195: if (resource.getType() == IResource.FILE) {
196: InputStream contentStream = null;
197: int length = 0;
198: try {
199: IFile file = (IFile) resource;
200: contentStream = file.getContents();
201: Reader in = new InputStreamReader(contentStream);
202: int chunkSize = contentStream.available();
203: StringBuffer buffer = new StringBuffer(chunkSize);
204: char[] readBuffer = new char[chunkSize];
205: int n = in.read(readBuffer);
206:
207: while (n > 0) {
208: buffer.append(readBuffer);
209: n = in.read(readBuffer);
210: }
211:
212: contentStream.close();
213: length = buffer.length();
214: } catch (CoreException e) {
215: length = 0;
216: } catch (IOException e) {
217: // do nothing
218: } finally {
219: if (contentStream != null) {
220: try {
221: contentStream.close();
222: } catch (IOException e) {
223: // do nothing
224: }
225: }
226: }
227:
228: if (length < 256)
229: return 1;
230: return 2;
231: }
232:
233: return 0;
234: }
235:
236: /** (non-Javadoc)
237: * Method declared on PreferencePage
238: */
239: public boolean performOk() {
240: // nothing to do - read-only page
241: return true;
242: }
243: }
|