| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| |
| package org.apache.oodt.filemgringest; |
| |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.custom.CCombo; |
| import org.eclipse.swt.events.*; |
| import org.eclipse.swt.layout.FormAttachment; |
| import org.eclipse.swt.layout.FormData; |
| import org.eclipse.swt.layout.FormLayout; |
| import org.eclipse.swt.widgets.*; |
| import org.pentaho.di.core.Const; |
| import org.pentaho.di.core.exception.KettleStepException; |
| import org.pentaho.di.core.row.RowMetaInterface; |
| import org.pentaho.di.core.row.ValueMetaInterface; |
| import org.pentaho.di.i18n.BaseMessages; |
| import org.pentaho.di.trans.TransMeta; |
| import org.pentaho.di.trans.step.BaseStepMeta; |
| import org.pentaho.di.trans.step.StepDialogInterface; |
| import org.pentaho.di.trans.step.StepMeta; |
| import org.pentaho.di.ui.trans.step.BaseStepDialog; |
| |
| /** |
| * This class is part of the demo step plug-in implementation. |
| * It demonstrates the basics of developing a plug-in step for PDI. |
| * |
| * The demo step adds a new string field to the row stream and sets its |
| * value to "Hello World!". The user may select the name of the new field. |
| * |
| * This class is the implementation of StepDialogInterface. |
| * Classes implementing this interface need to: |
| * |
| * - build and open a SWT dialog displaying the step's settings (stored in the step's meta object) |
| * - write back any changes the user makes to the step's meta object |
| * - report whether the user changed any settings when confirming the dialog |
| * |
| */ |
| public class FilemgrIngestStepDialog extends BaseStepDialog implements StepDialogInterface { |
| |
| /** |
| * The PKG member is used when looking up internationalized strings. |
| * The properties file with localized keys is expected to reside in |
| * {the package of the class specified}/messages/messages_{locale}.properties |
| */ |
| private static Class<?> PKG = FilemgrIngestStepMeta.class; // for i18n purposes |
| |
| // this is the object the stores the step's settings |
| // the dialog reads the settings from it when opening |
| // the dialog writes the settings to it when confirmed |
| private FilemgrIngestStepMeta meta; |
| |
| // text field holding the name of the field to add to the row stream |
| //private Text wHelloFieldName; |
| |
| // text field holding the name of the field to check the filename against |
| private CCombo wFilenameField; |
| private Text wServerURLField; |
| private Text wResultField; |
| private CCombo wMetadataField; |
| private Button m_getFieldsBut; |
| |
| /** |
| * The constructor should simply invoke super() and save the incoming meta |
| * object to a local variable, so it can conveniently read and write settings |
| * from/to it. |
| * |
| * @param parent the SWT shell to open the dialog in |
| * @param in the meta object holding the step's settings |
| * @param transMeta transformation description |
| * @param sname the step name |
| */ |
| public FilemgrIngestStepDialog(Shell parent, Object in, TransMeta transMeta, String sname) { |
| super(parent, (BaseStepMeta) in, transMeta, sname); |
| meta = (FilemgrIngestStepMeta) in; |
| } |
| |
| /** |
| * This method is called by Spoon when the user opens the settings dialog of the step. |
| * It should open the dialog and return only once the dialog has been closed by the user. |
| * |
| * If the user confirms the dialog, the meta object (passed in the constructor) must |
| * be updated to reflect the new step settings. The changed flag of the meta object must |
| * reflect whether the step configuration was changed by the dialog. |
| * |
| * If the user cancels the dialog, the meta object must not be updated, and its changed flag |
| * must remain unaltered. |
| * |
| * The open() method must return the name of the step after the user has confirmed the dialog, |
| * or null if the user cancelled the dialog. |
| */ |
| public String open() { |
| |
| // store some convenient SWT variables |
| Shell parent = getParent(); |
| Display display = parent.getDisplay(); |
| |
| // SWT code for preparing the dialog |
| shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MIN | SWT.MAX); |
| props.setLook(shell); |
| setShellImage(shell, meta); |
| |
| // Save the value of the changed flag on the meta object. If the user cancels |
| // the dialog, it will be restored to this saved value. |
| // The "changed" variable is inherited from BaseStepDialog |
| changed = meta.hasChanged(); |
| |
| // The ModifyListener used on all controls. It will update the meta object to |
| // indicate that changes are being made. |
| ModifyListener lsMod = new ModifyListener() { |
| public void modifyText(ModifyEvent e) { |
| meta.setChanged(); |
| } |
| }; |
| |
| // ------------------------------------------------------- // |
| // SWT code for building the actual settings dialog // |
| // ------------------------------------------------------- // |
| FormLayout formLayout = new FormLayout(); |
| formLayout.marginWidth = Const.FORM_MARGIN; |
| formLayout.marginHeight = Const.FORM_MARGIN; |
| |
| shell.setLayout(formLayout); |
| shell.setText(BaseMessages.getString(PKG, "FilemgrIngestStep.Name")); |
| |
| int middle = props.getMiddlePct(); |
| int margin = Const.MARGIN; |
| |
| // Stepname line |
| wlStepname = new Label(shell, SWT.RIGHT); |
| wlStepname.setText(BaseMessages.getString(PKG, "System.Label.StepName")); |
| props.setLook(wlStepname); |
| fdlStepname = new FormData(); |
| fdlStepname.left = new FormAttachment(0, 0); |
| fdlStepname.right = new FormAttachment(middle, -margin); |
| fdlStepname.top = new FormAttachment(0, margin); |
| wlStepname.setLayoutData(fdlStepname); |
| |
| wStepname = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER); |
| wStepname.setText(stepname); |
| props.setLook(wStepname); |
| wStepname.addModifyListener(lsMod); |
| fdStepname = new FormData(); |
| fdStepname.left = new FormAttachment(middle, 0); |
| fdStepname.top = new FormAttachment(0, margin); |
| fdStepname.right = new FormAttachment(100, 0); |
| wStepname.setLayoutData(fdStepname); |
| |
| // output field value |
| Label wlValName = new Label(shell, SWT.RIGHT); |
| wlValName.setText(BaseMessages.getString(PKG, "FilemgrIngest.FieldName.Label")); |
| props.setLook(wlValName); |
| FormData fdlValName = new FormData(); |
| fdlValName.left = new FormAttachment(0, 0); |
| fdlValName.right = new FormAttachment(middle, -margin); |
| fdlValName.top = new FormAttachment(wStepname, margin); |
| wlValName.setLayoutData(fdlValName); |
| |
| |
| wFilenameField = new CCombo(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER); |
| props.setLook(wFilenameField); |
| wFilenameField.addModifyListener(lsMod); |
| FormData fdValName = new FormData(); |
| fdValName.left = new FormAttachment(middle, 0); |
| ///fdValName.right = new FormAttachment(m_getFieldsBut, -margin); |
| fdValName.right = new FormAttachment(100,0); |
| fdValName.top = new FormAttachment(wStepname, margin); |
| wFilenameField.setLayoutData(fdValName); |
| |
| |
| /* |
| m_getFieldsBut = new Button(shell, SWT.PUSH | SWT.CENTER); |
| props.setLook(m_getFieldsBut); |
| m_getFieldsBut.setText(BaseMessages.getString(PKG, |
| "SSTableOutputDialog.GetFields.Button")); |
| FormData fd = new FormData(); |
| fd.right = new FormAttachment(100, 0); |
| fdValName.top = new FormAttachment(wStepname, margin); |
| m_getFieldsBut.setLayoutData(fd); |
| m_getFieldsBut.addSelectionListener(new SelectionAdapter() { |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| setupFieldsCombo(); |
| } |
| });*/ |
| // output field value |
| Label wlMetadataName = new Label(shell, SWT.RIGHT); |
| wlMetadataName.setText(BaseMessages.getString(PKG, "FilemgrIngest.MetadataFieldName.Label")); |
| props.setLook(wlMetadataName); |
| FormData fdlMetadataName = new FormData(); |
| fdlMetadataName.left = new FormAttachment(0, 0); |
| fdlMetadataName.right = new FormAttachment(middle, -margin); |
| fdlMetadataName.top = new FormAttachment(wFilenameField, margin); |
| wlMetadataName.setLayoutData(fdlMetadataName); |
| |
| wMetadataField = new CCombo(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER); |
| props.setLook(wMetadataField); |
| wMetadataField.addModifyListener(lsMod); |
| FormData fdMetadataName = new FormData(); |
| fdMetadataName.left = new FormAttachment(middle, 0); |
| fdMetadataName.right = new FormAttachment(100, 0); |
| fdMetadataName.top = new FormAttachment(wFilenameField, margin); |
| wMetadataField.setLayoutData(fdMetadataName); |
| |
| |
| // servername field value |
| Label wlServerName = new Label(shell, SWT.RIGHT); |
| wlServerName.setText(BaseMessages.getString(PKG, "FilemgrIngest.ServerURL.Label")); |
| props.setLook(wlServerName); |
| FormData fdlServerName = new FormData(); |
| fdlServerName.left = new FormAttachment(0, 0); |
| fdlServerName.right = new FormAttachment(middle, -margin); |
| fdlServerName.top = new FormAttachment(wMetadataField, margin); |
| wlServerName.setLayoutData(fdlServerName); |
| |
| wServerURLField = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER); |
| props.setLook(wServerURLField); |
| wServerURLField.addModifyListener(lsMod); |
| FormData fdServerName = new FormData(); |
| fdServerName.left = new FormAttachment(middle, 0); |
| fdServerName.right = new FormAttachment(100, 0); |
| fdServerName.top = new FormAttachment(wMetadataField, margin); |
| wServerURLField.setLayoutData(fdServerName); |
| |
| // servername field value |
| Label wlResultName = new Label(shell, SWT.RIGHT); |
| wlResultName.setText(BaseMessages.getString(PKG, "FilemgrIngest.Result.Label")); |
| props.setLook(wlResultName); |
| FormData fdlResultName = new FormData(); |
| fdlResultName.left = new FormAttachment(0, 0); |
| fdlResultName.right = new FormAttachment(middle, -margin); |
| fdlResultName.top = new FormAttachment(wServerURLField, margin); |
| wlResultName.setLayoutData(fdlResultName); |
| |
| wResultField = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER); |
| props.setLook(wResultField); |
| wResultField.addModifyListener(lsMod); |
| FormData fdResultName = new FormData(); |
| fdResultName.left = new FormAttachment(middle, 0); |
| fdResultName.right = new FormAttachment(100, 0); |
| fdResultName.top = new FormAttachment(wServerURLField, margin); |
| wResultField.setLayoutData(fdResultName); |
| |
| |
| // OK and cancel buttons |
| wOK = new Button(shell, SWT.PUSH); |
| wOK.setText(BaseMessages.getString(PKG, "System.Button.OK")); |
| wCancel = new Button(shell, SWT.PUSH); |
| wCancel.setText(BaseMessages.getString(PKG, "System.Button.Cancel")); |
| |
| BaseStepDialog.positionBottomButtons(shell, new Button[] { wOK, wCancel }, margin, wResultField); |
| |
| // Add listeners for cancel and OK |
| lsCancel = new Listener() { |
| public void handleEvent(Event e) {cancel();} |
| }; |
| lsOK = new Listener() { |
| public void handleEvent(Event e) {ok();} |
| }; |
| |
| wCancel.addListener(SWT.Selection, lsCancel); |
| wOK.addListener(SWT.Selection, lsOK); |
| |
| // default listener (for hitting "enter") |
| lsDef = new SelectionAdapter() { |
| public void widgetDefaultSelected(SelectionEvent e) {ok();} |
| }; |
| wStepname.addSelectionListener(lsDef); |
| wFilenameField.addSelectionListener(lsDef); |
| wServerURLField.addSelectionListener(lsDef); |
| wResultField.addSelectionListener(lsDef); |
| // Detect X or ALT-F4 or something that kills this window and cancel the dialog properly |
| shell.addShellListener(new ShellAdapter() { |
| public void shellClosed(ShellEvent e) {cancel();} |
| }); |
| |
| // Set/Restore the dialog size based on last position on screen |
| // The setSize() method is inherited from BaseStepDialog |
| setSize(); |
| |
| // populate the dialog with the values from the meta object |
| populateDialog(); |
| |
| // restore the changed flag to original value, as the modify listeners fire during dialog population |
| meta.setChanged(changed); |
| |
| // open dialog and enter event loop |
| shell.open(); |
| while (!shell.isDisposed()) { |
| if (!display.readAndDispatch()) |
| display.sleep(); |
| } |
| |
| // at this point the dialog has closed, so either ok() or cancel() have been executed |
| // The "stepname" variable is inherited from BaseStepDialog |
| return stepname; |
| } |
| |
| /** |
| * This helper method puts the step configuration stored in the meta object |
| * and puts it into the dialog controls. |
| */ |
| private void populateDialog() { |
| setupFieldsCombo(); |
| wStepname.selectAll(); |
| wFilenameField.setText(meta.getFilenameField()); |
| wServerURLField.setText(meta.getServerURLField()); |
| wResultField.setText(meta.getResultField()); |
| wMetadataField.setText(meta.getMetadataField()); |
| |
| } |
| |
| /** |
| * Called when the user cancels the dialog. |
| */ |
| private void cancel() { |
| // The "stepname" variable will be the return value for the open() method. |
| // Setting to null to indicate that dialog was cancelled. |
| stepname = null; |
| // Restoring original "changed" flag on the met aobject |
| meta.setChanged(changed); |
| // close the SWT dialog window |
| dispose(); |
| } |
| |
| /** |
| * Called when the user confirms the dialog |
| */ |
| private void ok() { |
| // The "stepname" variable will be the return value for the open() method. |
| // Setting to step name from the dialog control |
| stepname = wStepname.getText(); |
| // Setting the settings to the meta object |
| //meta.setOutputField(wFilenameField.getText()); |
| meta.setFilenameField(wFilenameField.getText()); |
| meta.setServerURLField(wServerURLField.getText()); |
| meta.setResultField(wResultField.getText()); |
| meta.setMetadataField(wMetadataField.getText()); |
| // close the SWT dialog window |
| dispose(); |
| } |
| |
| protected void setupFieldsCombo() { |
| |
| StepMeta stepMeta = transMeta.findStep(stepname); |
| if (stepMeta != null) { |
| try { |
| RowMetaInterface row = transMeta.getPrevStepFields(stepMeta); |
| |
| if (row.size() == 0) { |
| /*MessageDialog.openError(shell, BaseMessages.getString(PKG, |
| "SSTableOutputData.Message.NoIncomingFields.Title"), BaseMessages |
| .getString(PKG, "SSTableOutputData.Message.NoIncomingFields"));*/ |
| return; |
| } |
| wFilenameField.removeAll(); |
| for (int i = 0; i < row.size(); i++) { |
| ValueMetaInterface vm = row.getValueMeta(i); |
| wFilenameField.add(vm.getName()); |
| } |
| |
| wMetadataField.removeAll(); |
| for (int i = 0; i < row.size(); i++) { |
| ValueMetaInterface vm = row.getValueMeta(i); |
| wMetadataField.add(vm.getName()); |
| } |
| |
| } catch (KettleStepException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| } |