001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.ByteArrayInputStream;
013: import java.io.InputStream;
014:
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.jface.viewers.IStructuredSelection;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.layout.GridData;
019: import org.eclipse.swt.layout.GridLayout;
020: import org.eclipse.swt.widgets.Button;
021: import org.eclipse.swt.widgets.Composite;
022: import org.eclipse.swt.widgets.Event;
023: import org.eclipse.swt.widgets.Group;
024: import org.eclipse.swt.widgets.Widget;
025: import org.eclipse.ui.IWorkbench;
026: import org.eclipse.ui.IWorkbenchPage;
027: import org.eclipse.ui.IWorkbenchWindow;
028: import org.eclipse.ui.PartInitException;
029: import org.eclipse.ui.PlatformUI;
030: import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
031: import org.eclipse.ui.ide.IDE;
032:
033: /**
034: * This class is the only page of the Readme file resource creation wizard.
035: * It subclasses the standard file resource creation page class,
036: * and consequently inherits the file resource creation functionality.
037: *
038: * This page provides users with the choice of creating sample headings for
039: * sections and subsections. Additionally, the option is presented to open
040: * the file immediately for editing after creation.
041: */
042: public class ReadmeCreationPage extends WizardNewFileCreationPage {
043: private IWorkbench workbench;
044:
045: // widgets
046: private Button sectionCheckbox;
047:
048: private Button subsectionCheckbox;
049:
050: private Button openFileCheckbox;
051:
052: // constants
053: private static int nameCounter = 1;
054:
055: /**
056: * Creates the page for the readme creation wizard.
057: *
058: * @param workbench the workbench on which the page should be created
059: * @param selection the current selection
060: */
061: public ReadmeCreationPage(IWorkbench workbench,
062: IStructuredSelection selection) {
063: super ("sampleCreateReadmePage1", selection); //$NON-NLS-1$
064: this .setTitle(MessageUtil.getString("Create_Readme_File")); //$NON-NLS-1$
065: this .setDescription(MessageUtil
066: .getString("Create_a_new_Readme_file_resource")); //$NON-NLS-1$
067: this .workbench = workbench;
068: }
069:
070: /** (non-Javadoc)
071: * Method declared on IDialogPage.
072: */
073: public void createControl(Composite parent) {
074: // inherit default container and name specification widgets
075: super .createControl(parent);
076: Composite composite = (Composite) getControl();
077:
078: PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
079: IReadmeConstants.CREATION_WIZARD_PAGE_CONTEXT);
080:
081: this .setFileName("sample" + nameCounter + ".readme"); //$NON-NLS-1$ //$NON-NLS-2$
082:
083: // sample section generation group
084: Group group = new Group(composite, SWT.NONE);
085: group.setLayout(new GridLayout());
086: group.setText(MessageUtil
087: .getString("Automatic_sample_section_generation")); //$NON-NLS-1$
088: group.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
089: | GridData.HORIZONTAL_ALIGN_FILL));
090:
091: // sample section generation checkboxes
092: sectionCheckbox = new Button(group, SWT.CHECK);
093: sectionCheckbox.setText(MessageUtil
094: .getString("Generate_sample_section_titles")); //$NON-NLS-1$
095: sectionCheckbox.setSelection(true);
096: sectionCheckbox.addListener(SWT.Selection, this );
097:
098: subsectionCheckbox = new Button(group, SWT.CHECK);
099: subsectionCheckbox.setText(MessageUtil
100: .getString("Generate_sample_subsection_titles")); //$NON-NLS-1$
101: subsectionCheckbox.setSelection(true);
102: subsectionCheckbox.addListener(SWT.Selection, this );
103:
104: // open file for editing checkbox
105: openFileCheckbox = new Button(composite, SWT.CHECK);
106: openFileCheckbox.setText(MessageUtil
107: .getString("Open_file_for_editing_when_done")); //$NON-NLS-1$
108: openFileCheckbox.setSelection(true);
109:
110: setPageComplete(validatePage());
111:
112: }
113:
114: /**
115: * Creates a new file resource as requested by the user. If everything
116: * is OK then answer true. If not, false will cause the dialog
117: * to stay open.
118: *
119: * @return whether creation was successful
120: * @see ReadmeCreationWizard#performFinish()
121: */
122: public boolean finish() {
123: // create the new file resource
124: IFile newFile = createNewFile();
125: if (newFile == null)
126: return false; // ie.- creation was unsuccessful
127:
128: // Since the file resource was created fine, open it for editing
129: // if requested by the user
130: try {
131: if (openFileCheckbox.getSelection()) {
132: IWorkbenchWindow dwindow = workbench
133: .getActiveWorkbenchWindow();
134: IWorkbenchPage page = dwindow.getActivePage();
135: if (page != null) {
136: IDE.openEditor(page, newFile, true);
137: }
138: }
139: } catch (PartInitException e) {
140: e.printStackTrace();
141: return false;
142: }
143: nameCounter++;
144: return true;
145: }
146:
147: /**
148: * The <code>ReadmeCreationPage</code> implementation of this
149: * <code>WizardNewFileCreationPage</code> method
150: * generates sample headings for sections and subsections in the
151: * newly-created Readme file according to the selections of self's
152: * checkbox widgets
153: */
154: protected InputStream getInitialContents() {
155: if (!sectionCheckbox.getSelection())
156: return null;
157:
158: StringBuffer sb = new StringBuffer();
159: sb.append(MessageUtil.getString("SAMPLE_README_FILE")); //$NON-NLS-1$
160: sb.append(MessageUtil.getString("SECTION_1")); //$NON-NLS-1$
161: sb.append(MessageUtil.getString("SECTION_1_BODY_1")); //$NON-NLS-1$
162:
163: if (subsectionCheckbox.getSelection()) {
164: sb.append(MessageUtil.getString("Subsection_1_1")); //$NON-NLS-1$
165: sb.append(MessageUtil.getString("Subsection_1_1_Body_1")); //$NON-NLS-1$
166: }
167:
168: sb.append(MessageUtil.getString("SECTION_2")); //$NON-NLS-1$
169: sb.append(MessageUtil.getString("SECTION_2_BODY_1")); //$NON-NLS-1$
170: sb.append(MessageUtil.getString("SECTION_2_BODY_2")); //$NON-NLS-1$
171:
172: if (subsectionCheckbox.getSelection()) {
173: sb.append(MessageUtil.getString("Subsection_2_1")); //$NON-NLS-1$
174: sb.append(MessageUtil.getString("Subsection_2_1_BODY_1")); //$NON-NLS-1$
175: sb.append(MessageUtil.getString("Subsection_2_2")); //$NON-NLS-1$
176: sb.append(MessageUtil.getString("Subsection_2_2_BODY_1")); //$NON-NLS-1$
177: }
178:
179: return new ByteArrayInputStream(sb.toString().getBytes());
180: }
181:
182: /** (non-Javadoc)
183: * Method declared on WizardNewFileCreationPage.
184: */
185: protected String getNewFileLabel() {
186: return MessageUtil.getString("Readme_file_name"); //$NON-NLS-1$
187: }
188:
189: /** (non-Javadoc)
190: * Method declared on WizardNewFileCreationPage.
191: */
192: public void handleEvent(Event e) {
193: Widget source = e.widget;
194:
195: if (source == sectionCheckbox) {
196: if (!sectionCheckbox.getSelection())
197: subsectionCheckbox.setSelection(false);
198: subsectionCheckbox.setEnabled(sectionCheckbox
199: .getSelection());
200: }
201:
202: super.handleEvent(e);
203: }
204: }
|