001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.designstudio.editors.reportwizard.wizards;
014:
015: import org.eclipse.core.resources.IContainer;
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.resources.ResourcesPlugin;
018: import org.eclipse.core.runtime.Path;
019: import org.eclipse.jface.dialogs.IDialogPage;
020: import org.eclipse.jface.viewers.ISelection;
021: import org.eclipse.jface.viewers.IStructuredSelection;
022: import org.eclipse.jface.wizard.WizardPage;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.events.ModifyEvent;
025: import org.eclipse.swt.events.ModifyListener;
026: import org.eclipse.swt.events.SelectionAdapter;
027: import org.eclipse.swt.events.SelectionEvent;
028: import org.eclipse.swt.layout.GridData;
029: import org.eclipse.swt.layout.GridLayout;
030: import org.eclipse.swt.widgets.Button;
031: import org.eclipse.swt.widgets.Composite;
032: import org.eclipse.swt.widgets.Label;
033: import org.eclipse.swt.widgets.Text;
034: import org.eclipse.ui.dialogs.ContainerSelectionDialog;
035: import org.pentaho.designstudio.editors.reportwizard.messages.Messages;
036:
037: /**
038: * The "New" wizard page allows setting the container for the new file as well
039: * as the file name. The page will only accept file name without the extension
040: * OR with the extension that matches the expected one (xaction).
041: */
042:
043: public class ReportSpecNewWizardPage extends WizardPage {
044: public static final String BASE_FILENAME = Messages
045: .getString("ReportSpecNewWizardPage.UI_UNTITLED_REPORT_SPEC_NAME"); //$NON-NLS-1$
046: public static final String JFREE_REPORT_WIZ_FILE_EXTENSION = "xreportspec"; //$NON-NLS-1$
047:
048: private Text containerText;
049: private Text fileText;
050: private ISelection selection;
051:
052: /**
053: * Constructor for SampleNewWizardPage.
054: *
055: * @param pageName
056: */
057: public ReportSpecNewWizardPage(ISelection selection) {
058: super ("wizardPage"); //$NON-NLS-1$
059: setTitle(Messages
060: .getString("ReportSpecNewWizardPage.UI_WIZ_TITLE")); //$NON-NLS-1$
061: setDescription(Messages
062: .getString("ReportSpecNewWizardPage.UI_WIZ_DESCRIPTION")); //$NON-NLS-1$
063: this .selection = selection;
064: }
065:
066: /**
067: * @see IDialogPage#createControl(Composite)
068: */
069: public void createControl(Composite parent) {
070: Composite container = new Composite(parent, SWT.NULL);
071: GridLayout layout = new GridLayout();
072: container.setLayout(layout);
073: layout.numColumns = 3;
074: layout.verticalSpacing = 9;
075: Label label = new Label(container, SWT.NULL);
076: label
077: .setText(Messages
078: .getString("ReportSpecNewWizardPage.UI_WIZ_CONTAINER_LABEL")); //$NON-NLS-1$
079:
080: containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
081: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
082: containerText.setLayoutData(gd);
083: containerText.addModifyListener(new ModifyListener() {
084: public void modifyText(ModifyEvent e) {
085: dialogChanged();
086: }
087: });
088:
089: Button button = new Button(container, SWT.PUSH);
090: button
091: .setText(Messages
092: .getString("ReportSpecNewWizardPage.UI_WIZ_BROWSE_LABEL")); //$NON-NLS-1$
093: button.addSelectionListener(new SelectionAdapter() {
094: public void widgetSelected(SelectionEvent e) {
095: handleBrowse();
096: }
097: });
098: label = new Label(container, SWT.NULL);
099: label
100: .setText(Messages
101: .getString("ReportSpecNewWizardPage.UI_WIZ_FILE_NAME_LABEL")); //$NON-NLS-1$
102:
103: fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
104: gd = new GridData(GridData.FILL_HORIZONTAL);
105: gd.horizontalSpan = 2;
106: fileText.setLayoutData(gd);
107: fileText.addModifyListener(new ModifyListener() {
108: public void modifyText(ModifyEvent e) {
109: dialogChanged();
110: }
111: });
112:
113: initialize();
114: dialogChanged();
115: setControl(container);
116: }
117:
118: /**
119: * Tests if the current workbench selection is a suitable container to use.
120: */
121:
122: private void initialize() {
123: if (selection != null && !selection.isEmpty()
124: && selection instanceof IStructuredSelection) {
125: IStructuredSelection ssel = (IStructuredSelection) selection;
126:
127: Object obj = ssel.getFirstElement();
128: if (obj instanceof IResource) {
129: IContainer container;
130: if (obj instanceof IContainer)
131: container = (IContainer) obj;
132: else
133: container = ((IResource) obj).getParent();
134: containerText.setText(container.getFullPath()
135: .toString());
136:
137: String modelFilename = BASE_FILENAME
138: + "." + JFREE_REPORT_WIZ_FILE_EXTENSION; //$NON-NLS-1$
139: for (int i = 1; container.findMember(modelFilename) != null; ++i) {
140: modelFilename = BASE_FILENAME + i
141: + "." + JFREE_REPORT_WIZ_FILE_EXTENSION; //$NON-NLS-1$
142: }
143: fileText.setText(modelFilename);
144: }
145: }
146: }
147:
148: /**
149: * Uses the standard container selection dialog to choose the new value for
150: * the container field.
151: */
152:
153: private void handleBrowse() {
154: ContainerSelectionDialog dialog = new ContainerSelectionDialog(
155: getShell(),
156: ResourcesPlugin.getWorkspace().getRoot(),
157: false,
158: Messages
159: .getString("ActionSequenceNewWizardPage.UI_WIZ_NEW_CONTAINER_DLG_MSG")); //$NON-NLS-1$
160: if (dialog.open() == ContainerSelectionDialog.OK) {
161: Object[] result = dialog.getResult();
162: if (result.length == 1) {
163: containerText.setText(((Path) result[0]).toString());
164: }
165: }
166: }
167:
168: /**
169: * Ensures that both text fields are set.
170: */
171:
172: private void dialogChanged() {
173: IResource container = ResourcesPlugin.getWorkspace().getRoot()
174: .findMember(new Path(getContainerName()));
175: String fileName = getFileName();
176:
177: if (getContainerName().length() == 0) {
178: updateStatus(Messages
179: .getString("ActionSequenceNewWizardPage.UI_WIZ_NO_CONTAINER_ERROR_MSG")); //$NON-NLS-1$
180: return;
181: }
182: if (container == null
183: || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
184: updateStatus(Messages
185: .getString("ActionSequenceNewWizardPage.UI_WIZ_CONTAINER_NOT_EXIST_MSG")); //$NON-NLS-1$
186: return;
187: }
188: if (!container.isAccessible()) {
189: updateStatus(Messages
190: .getString("ActionSequenceNewWizardPage.UI_WIZ_PROJ_NOT_WRITABLE_MSG")); //$NON-NLS-1$
191: return;
192: }
193: if (fileName.length() == 0) {
194: updateStatus(Messages
195: .getString("ActionSequenceNewWizardPage.UI_WIZ_NO_FILENAME_ERROR_MSG")); //$NON-NLS-1$
196: return;
197: }
198: if (fileName.replace('\\', '/').indexOf('/', 1) > 0) {
199: updateStatus(Messages
200: .getString("ActionSequenceNewWizardPage.UI_WIZ_INVALID_FILENAME_MSG")); //$NON-NLS-1$
201: return;
202: }
203: int dotLoc = fileName.lastIndexOf('.');
204: if (dotLoc != -1) {
205: String ext = fileName.substring(dotLoc + 1);
206: if (ext.equalsIgnoreCase(JFREE_REPORT_WIZ_FILE_EXTENSION) == false) {
207: updateStatus(Messages
208: .getString("ReportSpecNewWizardPage.UI_WIZ_INVALID_FILE_EXT_MSG")); //$NON-NLS-1$
209: return;
210: }
211: }
212: updateStatus(null);
213: }
214:
215: private void updateStatus(String message) {
216: setErrorMessage(message);
217: setPageComplete(message == null);
218: }
219:
220: public String getContainerName() {
221: return containerText.getText();
222: }
223:
224: public String getFileName() {
225: return fileText.getText();
226: }
227: }
|