List of usage examples for weka.core.xml XStream isPresent
public static boolean isPresent()
From source file:org.pentaho.di.scoring.WekaScoringData.java
License:Open Source License
/** * Loads a serialized model. Models can either be binary serialized Java * objects, objects deep-serialized to xml, or PMML. * * @param modelFile a <code>File</code> value * @return the model/* w w w . j a v a 2 s . com*/ * @throws Exception if there is a problem laoding the model. */ public static WekaScoringModel loadSerializedModel(String modelFile, LogChannelInterface log, VariableSpace space) throws Exception { Object model = null; Instances header = null; int[] ignoredAttsForClustering = null; modelFile = space.environmentSubstitute(modelFile); FileObject modelF = KettleVFS.getFileObject(modelFile); if (!modelF.exists()) { throw new Exception(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoring.Error.NonExistentModelFile", space.environmentSubstitute(modelFile))); //$NON-NLS-1$ } InputStream is = KettleVFS.getInputStream(modelF); BufferedInputStream buff = new BufferedInputStream(is); if (modelFile.toLowerCase().endsWith(".xml")) { //$NON-NLS-1$ // assume it is PMML model = PMMLFactory.getPMMLModel(buff, null); // we will use the mining schema as the instance structure header = ((PMMLModel) model).getMiningSchema().getMiningSchemaAsInstances(); buff.close(); } else if (modelFile.toLowerCase().endsWith(".xstreammodel")) { //$NON-NLS-1$ log.logBasic(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringData.Log.LoadXMLModel")); //$NON-NLS-1$ if (XStream.isPresent()) { Vector v = (Vector) XStream.read(buff); model = v.elementAt(0); if (v.size() == 2) { // try and grab the header header = (Instances) v.elementAt(1); } buff.close(); } else { buff.close(); throw new Exception( BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringData.Error.CantLoadXMLModel")); //$NON-NLS-1$ } } else { InputStream stream = buff; if (modelFile.toLowerCase().endsWith(".gz")) { //$NON-NLS-1$ stream = new GZIPInputStream(buff); } ObjectInputStream oi = new ObjectInputStream(stream); model = oi.readObject(); // try and grab the header header = (Instances) oi.readObject(); if (model instanceof weka.clusterers.Clusterer) { // try and grab any attributes to be ignored during clustering try { ignoredAttsForClustering = (int[]) oi.readObject(); } catch (Exception ex) { // Don't moan if there aren't any :-) } } oi.close(); } WekaScoringModel wsm = WekaScoringModel.createScorer(model); wsm.setHeader(header); if (wsm instanceof WekaScoringClusterer && ignoredAttsForClustering != null) { ((WekaScoringClusterer) wsm).setAttributesToIgnore(ignoredAttsForClustering); } wsm.setLog(log); return wsm; }
From source file:org.pentaho.di.scoring.WekaScoringDialog.java
License:Open Source License
/** * Open the dialog/*from ww w . j ava 2 s.co m*/ * * @return the step name */ public String open() { Shell parent = getParent(); Display display = parent.getDisplay(); shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MIN | SWT.MAX); props.setLook(shell); setShellImage(shell, m_currentMeta); // used to listen to a text field (m_wStepname) ModifyListener lsMod = new ModifyListener() { public void modifyText(ModifyEvent e) { m_currentMeta.setChanged(); } }; changed = m_currentMeta.hasChanged(); FormLayout formLayout = new FormLayout(); formLayout.marginWidth = Const.FORM_MARGIN; formLayout.marginHeight = Const.FORM_MARGIN; shell.setLayout(formLayout); shell.setText(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.Shell.Title")); //$NON-NLS-1$ int middle = props.getMiddlePct(); int margin = Const.MARGIN; // Stepname line m_wlStepname = new Label(shell, SWT.RIGHT); m_wlStepname.setText(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.StepName.Label")); //$NON-NLS-1$ props.setLook(m_wlStepname); m_fdlStepname = new FormData(); m_fdlStepname.left = new FormAttachment(0, 0); m_fdlStepname.right = new FormAttachment(middle, -margin); m_fdlStepname.top = new FormAttachment(0, margin); m_wlStepname.setLayoutData(m_fdlStepname); m_wStepname = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER); m_wStepname.setText(stepname); props.setLook(m_wStepname); m_wStepname.addModifyListener(lsMod); // format the text field m_fdStepname = new FormData(); m_fdStepname.left = new FormAttachment(middle, 0); m_fdStepname.top = new FormAttachment(0, margin); m_fdStepname.right = new FormAttachment(100, 0); m_wStepname.setLayoutData(m_fdStepname); m_wTabFolder = new CTabFolder(shell, SWT.BORDER); props.setLook(m_wTabFolder, Props.WIDGET_STYLE_TAB); m_wTabFolder.setSimple(false); // Start of the file tab m_wFileTab = new CTabItem(m_wTabFolder, SWT.NONE); m_wFileTab.setText(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.FileTab.TabTitle")); //$NON-NLS-1$ Composite wFileComp = new Composite(m_wTabFolder, SWT.NONE); props.setLook(wFileComp); FormLayout fileLayout = new FormLayout(); fileLayout.marginWidth = 3; fileLayout.marginHeight = 3; wFileComp.setLayout(fileLayout); // Filename line m_wlFilename = new Label(wFileComp, SWT.RIGHT); m_wlFilename.setText(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.Filename.Label")); //$NON-NLS-1$ props.setLook(m_wlFilename); m_fdlFilename = new FormData(); m_fdlFilename.left = new FormAttachment(0, 0); m_fdlFilename.top = new FormAttachment(0, margin); m_fdlFilename.right = new FormAttachment(middle, -margin); m_wlFilename.setLayoutData(m_fdlFilename); // file browse button m_wbFilename = new Button(wFileComp, SWT.PUSH | SWT.CENTER); props.setLook(m_wbFilename); m_wbFilename.setText(BaseMessages.getString(WekaScoringMeta.PKG, "System.Button.Browse")); //$NON-NLS-1$ m_fdbFilename = new FormData(); m_fdbFilename.right = new FormAttachment(100, 0); m_fdbFilename.top = new FormAttachment(0, 0); m_wbFilename.setLayoutData(m_fdbFilename); // combined text field and env variable widget m_wFilename = new TextVar(transMeta, wFileComp, SWT.SINGLE | SWT.LEFT | SWT.BORDER); props.setLook(m_wFilename); m_wFilename.addModifyListener(lsMod); m_fdFilename = new FormData(); m_fdFilename.left = new FormAttachment(middle, 0); m_fdFilename.top = new FormAttachment(0, margin); m_fdFilename.right = new FormAttachment(m_wbFilename, -margin); m_wFilename.setLayoutData(m_fdFilename); // store model in meta data Label saveModelMetaLab = new Label(wFileComp, SWT.RIGHT); props.setLook(saveModelMetaLab); saveModelMetaLab .setText(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.SaveModelToMeta.Label")); //$NON-NLS-1$ FormData fd = new FormData(); fd.left = new FormAttachment(0, 0); fd.top = new FormAttachment(m_wFilename, margin); fd.right = new FormAttachment(middle, -margin); saveModelMetaLab.setLayoutData(fd); m_storeModelInStepMetaData = new Button(wFileComp, SWT.CHECK); props.setLook(m_storeModelInStepMetaData); fd = new FormData(); fd.left = new FormAttachment(middle, 0); fd.top = new FormAttachment(m_wFilename, margin); fd.right = new FormAttachment(100, 0); m_storeModelInStepMetaData.setLayoutData(fd); m_wUpdateModelLab = new Label(wFileComp, SWT.RIGHT); m_wUpdateModelLab .setText(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.UpdateModel.Label")); //$NON-NLS-1$ props.setLook(m_wUpdateModelLab); m_fdlUpdateModel = new FormData(); m_fdlUpdateModel.left = new FormAttachment(0, 0); m_fdlUpdateModel.top = new FormAttachment(m_storeModelInStepMetaData, margin); m_fdlUpdateModel.right = new FormAttachment(middle, -margin); m_wUpdateModelLab.setLayoutData(m_fdlUpdateModel); m_wUpdateModel = new Button(wFileComp, SWT.CHECK); props.setLook(m_wUpdateModel); m_fdUpdateModel = new FormData(); m_fdUpdateModel.left = new FormAttachment(middle, 0); m_fdUpdateModel.top = new FormAttachment(m_storeModelInStepMetaData, margin); m_fdUpdateModel.right = new FormAttachment(100, 0); m_wUpdateModel.setLayoutData(m_fdUpdateModel); m_wUpdateModel.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { m_currentMeta.setChanged(); m_wbSaveFilename.setEnabled(m_wUpdateModel.getSelection()); m_wSaveFilename.setEnabled(m_wUpdateModel.getSelection()); } }); // ---------------------------- // Save filename line m_wlSaveFilename = new Label(wFileComp, SWT.RIGHT); m_wlSaveFilename .setText(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.SaveFilename.Label")); //$NON-NLS-1$ props.setLook(m_wlSaveFilename); m_fdlSaveFilename = new FormData(); m_fdlSaveFilename.left = new FormAttachment(0, 0); m_fdlSaveFilename.top = new FormAttachment(m_wUpdateModel, margin); m_fdlSaveFilename.right = new FormAttachment(middle, -margin); m_wlSaveFilename.setLayoutData(m_fdlSaveFilename); // Save file browse button m_wbSaveFilename = new Button(wFileComp, SWT.PUSH | SWT.CENTER); props.setLook(m_wbSaveFilename); m_wbSaveFilename.setText(BaseMessages.getString(WekaScoringMeta.PKG, "System.Button.Browse")); //$NON-NLS-1$ m_fdbSaveFilename = new FormData(); m_fdbSaveFilename.right = new FormAttachment(100, 0); m_fdbSaveFilename.top = new FormAttachment(m_wUpdateModel, 0); m_wbSaveFilename.setLayoutData(m_fdbSaveFilename); m_wbSaveFilename.setEnabled(false); // combined text field and env variable widget m_wSaveFilename = new TextVar(transMeta, wFileComp, SWT.SINGLE | SWT.LEFT | SWT.BORDER); props.setLook(m_wSaveFilename); m_wSaveFilename.addModifyListener(lsMod); m_fdSaveFilename = new FormData(); m_fdSaveFilename.left = new FormAttachment(middle, 0); m_fdSaveFilename.top = new FormAttachment(m_wUpdateModel, margin); m_fdSaveFilename.right = new FormAttachment(m_wbSaveFilename, -margin); m_wSaveFilename.setLayoutData(m_fdSaveFilename); m_wSaveFilename.setEnabled(false); m_fdFileComp = new FormData(); m_fdFileComp.left = new FormAttachment(0, 0); m_fdFileComp.top = new FormAttachment(0, 0); m_fdFileComp.right = new FormAttachment(100, 0); m_fdFileComp.bottom = new FormAttachment(100, 0); wFileComp.setLayoutData(m_fdFileComp); wFileComp.layout(); m_wFileTab.setControl(wFileComp); m_wAcceptFileNameFromFieldCheckLab = new Label(wFileComp, SWT.RIGHT); m_wAcceptFileNameFromFieldCheckLab.setText(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.AcceptFileNamesFromFieldCheck.Label")); //$NON-NLS-1$ props.setLook(m_wAcceptFileNameFromFieldCheckLab); FormData fdAcceptCheckLab = new FormData(); fdAcceptCheckLab.left = new FormAttachment(0, 0); fdAcceptCheckLab.top = new FormAttachment(m_wSaveFilename, margin); fdAcceptCheckLab.right = new FormAttachment(middle, -margin); m_wAcceptFileNameFromFieldCheckLab.setLayoutData(fdAcceptCheckLab); m_wAcceptFileNameFromFieldCheckBox = new Button(wFileComp, SWT.CHECK); props.setLook(m_wAcceptFileNameFromFieldCheckBox); FormData fdAcceptCheckBox = new FormData(); fdAcceptCheckBox.left = new FormAttachment(middle, 0); fdAcceptCheckBox.top = new FormAttachment(m_wSaveFilename, margin); fdAcceptCheckBox.right = new FormAttachment(100, 0); m_wAcceptFileNameFromFieldCheckBox.setLayoutData(fdAcceptCheckBox); m_wAcceptFileNameFromFieldCheckBox.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { m_currentMeta.setChanged(); if (m_wAcceptFileNameFromFieldCheckBox.getSelection()) { m_wUpdateModel.setSelection(false); m_wUpdateModel.setEnabled(false); m_wSaveFilename.setText(""); //$NON-NLS-1$ m_wlFilename.setText( BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.Default.Label")); //$NON-NLS-1$ if (!Const.isEmpty(m_wFilename.getText())) { // load - loadModel() will take care of storing it in // either the main or default model in the current meta loadModel(); } else { // try and shift the main model (if non-null) over into the // default model in current meta m_currentMeta.setDefaultModel(m_currentMeta.getModel()); m_currentMeta.setModel(null); } } else { if (!Const.isEmpty(m_wFilename.getText())) { // load - loadModel() will take care of storing it in // either the main or default model in the current meta loadModel(); } else { // try and shift the default model (if non-null) over into the // main model in current meta m_currentMeta.setModel(m_currentMeta.getDefaultModel()); m_currentMeta.setDefaultModel(null); } m_wCacheModelsCheckBox.setSelection(false); m_wlFilename.setText( BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.Filename.Label")); //$NON-NLS-1$ } m_wCacheModelsCheckBox.setEnabled(m_wAcceptFileNameFromFieldCheckBox.getSelection()); m_wAcceptFileNameFromFieldText.setEnabled(m_wAcceptFileNameFromFieldCheckBox.getSelection()); m_wbSaveFilename.setEnabled( !m_wAcceptFileNameFromFieldCheckBox.getSelection() && m_wUpdateModel.getSelection()); m_wSaveFilename.setEnabled( !m_wAcceptFileNameFromFieldCheckBox.getSelection() && m_wUpdateModel.getSelection()); } }); // m_wAcceptFileNameFromFieldTextLab = new Label(wFileComp, SWT.RIGHT); m_wAcceptFileNameFromFieldTextLab.setText( BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.AcceptFileNamesFromField.Label")); //$NON-NLS-1$ props.setLook(m_wAcceptFileNameFromFieldTextLab); FormData fdAcceptLab = new FormData(); fdAcceptLab.left = new FormAttachment(0, 0); fdAcceptLab.top = new FormAttachment(m_wAcceptFileNameFromFieldCheckBox, margin); fdAcceptLab.right = new FormAttachment(middle, -margin); m_wAcceptFileNameFromFieldTextLab.setLayoutData(fdAcceptLab); m_wAcceptFileNameFromFieldText = new TextVar(transMeta, wFileComp, SWT.SINGLE | SWT.LEFT | SWT.BORDER); props.setLook(m_wAcceptFileNameFromFieldText); m_wAcceptFileNameFromFieldText.addModifyListener(lsMod); FormData fdAcceptText = new FormData(); fdAcceptText.left = new FormAttachment(middle, 0); fdAcceptText.top = new FormAttachment(m_wAcceptFileNameFromFieldCheckBox, margin); fdAcceptText.right = new FormAttachment(100, 0); m_wAcceptFileNameFromFieldText.setLayoutData(fdAcceptText); m_wAcceptFileNameFromFieldText.setEnabled(false); m_wCacheModelsLab = new Label(wFileComp, SWT.RIGHT); m_wCacheModelsLab .setText(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.CacheModels.Label")); //$NON-NLS-1$ props.setLook(m_wCacheModelsLab); FormData fdCacheLab = new FormData(); fdCacheLab.left = new FormAttachment(0, 0); fdCacheLab.top = new FormAttachment(m_wAcceptFileNameFromFieldText, margin); fdCacheLab.right = new FormAttachment(middle, -margin); m_wCacheModelsLab.setLayoutData(fdCacheLab); // m_wCacheModelsCheckBox = new Button(wFileComp, SWT.CHECK); props.setLook(m_wCacheModelsCheckBox); FormData fdCacheCheckBox = new FormData(); fdCacheCheckBox.left = new FormAttachment(middle, 0); fdCacheCheckBox.top = new FormAttachment(m_wAcceptFileNameFromFieldText, margin); fdCacheCheckBox.right = new FormAttachment(100, 0); m_wCacheModelsCheckBox.setLayoutData(fdCacheCheckBox); m_wCacheModelsCheckBox.setEnabled(false); m_wOutputProbsLab = new Label(wFileComp, SWT.RIGHT); m_wOutputProbsLab .setText(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.OutputProbs.Label")); //$NON-NLS-1$ props.setLook(m_wOutputProbsLab); m_fdlOutputProbs = new FormData(); m_fdlOutputProbs.left = new FormAttachment(0, 0); m_fdlOutputProbs.top = new FormAttachment(m_wCacheModelsCheckBox, margin); m_fdlOutputProbs.right = new FormAttachment(middle, -margin); m_wOutputProbsLab.setLayoutData(m_fdlOutputProbs); m_wOutputProbs = new Button(wFileComp, SWT.CHECK); props.setLook(m_wOutputProbs); m_fdOutputProbs = new FormData(); m_fdOutputProbs.left = new FormAttachment(middle, 0); m_fdOutputProbs.top = new FormAttachment(m_wCacheModelsCheckBox, margin); m_fdOutputProbs.right = new FormAttachment(100, 0); m_wOutputProbs.setLayoutData(m_fdOutputProbs); // batch scoring size line Label batchLab = new Label(wFileComp, SWT.RIGHT); batchLab.setText("Batch scoring batch size"); //$NON-NLS-1$ props.setLook(batchLab); FormData fdd = new FormData(); fdd.left = new FormAttachment(0, 0); fdd.top = new FormAttachment(m_wOutputProbs, margin); fdd.right = new FormAttachment(middle, -margin); batchLab.setLayoutData(fdd); m_batchScoringBatchSizeText = new TextVar(transMeta, wFileComp, SWT.SINGLE | SWT.LEFT | SWT.BORDER); props.setLook(m_batchScoringBatchSizeText); m_batchScoringBatchSizeText.addModifyListener(lsMod); fdd = new FormData(); fdd.left = new FormAttachment(middle, 0); fdd.top = new FormAttachment(m_wOutputProbs, margin); fdd.right = new FormAttachment(100, 0); m_batchScoringBatchSizeText.setLayoutData(fdd); m_batchScoringBatchSizeText.setEnabled(false); // Fields mapping tab m_wFieldsTab = new CTabItem(m_wTabFolder, SWT.NONE); m_wFieldsTab.setText(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.FieldsTab.TabTitle")); //$NON-NLS-1$ FormLayout fieldsLayout = new FormLayout(); fieldsLayout.marginWidth = 3; fieldsLayout.marginHeight = 3; Composite wFieldsComp = new Composite(m_wTabFolder, SWT.NONE); props.setLook(wFieldsComp); wFieldsComp.setLayout(fieldsLayout); // body of tab to be a scrolling text area // to display the mapping m_wMappingText = new Text(wFieldsComp, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); m_wMappingText.setEditable(false); FontData fontd = new FontData("Courier New", 12, SWT.NORMAL); //$NON-NLS-1$ m_wMappingText.setFont(new Font(getParent().getDisplay(), fontd)); props.setLook(m_wMappingText); // format the fields mapping text area m_fdMappingText = new FormData(); m_fdMappingText.left = new FormAttachment(0, 0); m_fdMappingText.top = new FormAttachment(0, margin); m_fdMappingText.right = new FormAttachment(100, 0); m_fdMappingText.bottom = new FormAttachment(100, 0); m_wMappingText.setLayoutData(m_fdMappingText); m_fdFieldsComp = new FormData(); m_fdFieldsComp.left = new FormAttachment(0, 0); m_fdFieldsComp.top = new FormAttachment(0, 0); m_fdFieldsComp.right = new FormAttachment(100, 0); m_fdFieldsComp.bottom = new FormAttachment(100, 0); wFieldsComp.setLayoutData(m_fdFieldsComp); wFieldsComp.layout(); m_wFieldsTab.setControl(wFieldsComp); // Model display tab m_wModelTab = new CTabItem(m_wTabFolder, SWT.NONE); m_wModelTab.setText(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.ModelTab.TabTitle")); //$NON-NLS-1$ FormLayout modelLayout = new FormLayout(); modelLayout.marginWidth = 3; modelLayout.marginHeight = 3; Composite wModelComp = new Composite(m_wTabFolder, SWT.NONE); props.setLook(wModelComp); wModelComp.setLayout(modelLayout); // body of tab to be a scrolling text area // to display the pre-learned model m_wModelText = new Text(wModelComp, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); m_wModelText.setEditable(false); fontd = new FontData("Courier New", 12, SWT.NORMAL); //$NON-NLS-1$ m_wModelText.setFont(new Font(getParent().getDisplay(), fontd)); props.setLook(m_wModelText); // format the model text area m_fdModelText = new FormData(); m_fdModelText.left = new FormAttachment(0, 0); m_fdModelText.top = new FormAttachment(0, margin); m_fdModelText.right = new FormAttachment(100, 0); m_fdModelText.bottom = new FormAttachment(100, 0); m_wModelText.setLayoutData(m_fdModelText); m_fdModelComp = new FormData(); m_fdModelComp.left = new FormAttachment(0, 0); m_fdModelComp.top = new FormAttachment(0, 0); m_fdModelComp.right = new FormAttachment(100, 0); m_fdModelComp.bottom = new FormAttachment(100, 0); wModelComp.setLayoutData(m_fdModelComp); wModelComp.layout(); m_wModelTab.setControl(wModelComp); m_fdTabFolder = new FormData(); m_fdTabFolder.left = new FormAttachment(0, 0); m_fdTabFolder.top = new FormAttachment(m_wStepname, margin); m_fdTabFolder.right = new FormAttachment(100, 0); m_fdTabFolder.bottom = new FormAttachment(100, -50); m_wTabFolder.setLayoutData(m_fdTabFolder); // Buttons inherited from BaseStepDialog wOK = new Button(shell, SWT.PUSH); wOK.setText(BaseMessages.getString(WekaScoringMeta.PKG, "System.Button.OK")); //$NON-NLS-1$ wCancel = new Button(shell, SWT.PUSH); wCancel.setText(BaseMessages.getString(WekaScoringMeta.PKG, "System.Button.Cancel")); //$NON-NLS-1$ setButtonPositions(new Button[] { wOK, wCancel }, margin, m_wTabFolder); // Add listeners 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); lsDef = new SelectionAdapter() { @Override public void widgetDefaultSelected(SelectionEvent e) { ok(); } }; m_wStepname.addSelectionListener(lsDef); // Detect X or ALT-F4 or something that kills this window... shell.addShellListener(new ShellAdapter() { @Override public void shellClosed(ShellEvent e) { cancel(); } }); // Whenever something changes, set the tooltip to the expanded version: m_wFilename.addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent e) { m_wFilename.setToolTipText(transMeta.environmentSubstitute(m_wFilename.getText())); } }); m_wSaveFilename.addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent e) { m_wSaveFilename.setToolTipText(transMeta.environmentSubstitute(m_wSaveFilename.getText())); } }); // listen to the file name text box and try to load a model // if the user presses enter m_wFilename.addSelectionListener(new SelectionAdapter() { @Override public void widgetDefaultSelected(SelectionEvent e) { if (!loadModel()) { log.logError( BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.Log.FileLoadingError")); //$NON-NLS-1$ } } }); m_wbFilename.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { String[] extensions = null; String[] filterNames = null; if (XStream.isPresent()) { extensions = new String[4]; filterNames = new String[4]; extensions[0] = "*.model"; //$NON-NLS-1$ filterNames[0] = BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.FileType.ModelFileBinary"); //$NON-NLS-1$ extensions[1] = "*.xstreammodel"; //$NON-NLS-1$ filterNames[1] = BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.FileType.ModelFileXML"); //$NON-NLS-1$ extensions[2] = "*.xml"; //$NON-NLS-1$ filterNames[2] = BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.FileType.ModelFilePMML"); //$NON-NLS-1$ extensions[3] = "*"; //$NON-NLS-1$ filterNames[3] = BaseMessages.getString(WekaScoringMeta.PKG, "System.FileType.AllFiles"); //$NON-NLS-1$ } else { extensions = new String[3]; filterNames = new String[3]; extensions[0] = "*.model"; //$NON-NLS-1$ filterNames[0] = BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.FileType.ModelFileBinary"); //$NON-NLS-1$ extensions[1] = "*.xml"; //$NON-NLS-1$ filterNames[1] = BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.FileType.ModelFilePMML"); //$NON-NLS-1$ extensions[2] = "*"; //$NON-NLS-1$ filterNames[2] = BaseMessages.getString(WekaScoringMeta.PKG, "System.FileType.AllFiles"); //$NON-NLS-1$ } // get current file FileObject rootFile = null; FileObject initialFile = null; FileObject defaultInitialFile = null; try { if (m_wFilename.getText() != null) { String fname = transMeta.environmentSubstitute(m_wFilename.getText()); if (!Const.isEmpty(fname)) { initialFile = KettleVFS.getFileObject(fname); rootFile = initialFile.getFileSystem().getRoot(); } else { defaultInitialFile = KettleVFS.getFileObject(Spoon.getInstance().getLastFileOpened()); } } else { defaultInitialFile = KettleVFS.getFileObject("file:///c:/"); //$NON-NLS-1$ } if (rootFile == null) { rootFile = defaultInitialFile.getFileSystem().getRoot(); } VfsFileChooserDialog fileChooserDialog = Spoon.getInstance().getVfsFileChooserDialog(rootFile, initialFile); fileChooserDialog.setRootFile(rootFile); fileChooserDialog.setInitialFile(initialFile); fileChooserDialog.defaultInitialFile = rootFile; String in = (!Const.isEmpty(m_wFilename.getText())) ? initialFile.getName().getPath() : null; FileObject selectedFile = fileChooserDialog.open(shell, null, "file", //$NON-NLS-1$ true, in, extensions, filterNames, VfsFileChooserDialog.VFS_DIALOG_OPEN_FILE); if (selectedFile != null) { m_wFilename.setText(selectedFile.getURL().toString()); } // try to load model file and display model if (!loadModel()) { log.logError(BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.Log.FileLoadingError")); //$NON-NLS-1$ } } catch (Exception ex) { logError("A problem occurred", ex); //$NON-NLS-1$ } } }); m_wbSaveFilename.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { FileDialog dialog = new FileDialog(shell, SWT.SAVE); String[] extensions = null; String[] filterNames = null; if (XStream.isPresent()) { extensions = new String[3]; filterNames = new String[3]; extensions[0] = "*.model"; //$NON-NLS-1$ filterNames[0] = BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.FileType.ModelFileBinary"); //$NON-NLS-1$ extensions[1] = "*.xstreammodel"; //$NON-NLS-1$ filterNames[1] = BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.FileType.ModelFileXML"); //$NON-NLS-1$ extensions[2] = "*"; //$NON-NLS-1$ filterNames[2] = BaseMessages.getString(WekaScoringMeta.PKG, "System.FileType.AllFiles"); //$NON-NLS-1$ } else { extensions = new String[2]; filterNames = new String[2]; extensions[0] = "*.model"; //$NON-NLS-1$ filterNames[0] = BaseMessages.getString(WekaScoringMeta.PKG, "WekaScoringDialog.FileType.ModelFileBinary"); //$NON-NLS-1$ extensions[1] = "*"; //$NON-NLS-1$ filterNames[1] = BaseMessages.getString(WekaScoringMeta.PKG, "System.FileType.AllFiles"); //$NON-NLS-1$ } dialog.setFilterExtensions(extensions); if (m_wSaveFilename.getText() != null) { dialog.setFileName(transMeta.environmentSubstitute(m_wSaveFilename.getText())); } dialog.setFilterNames(filterNames); if (dialog.open() != null) { m_wSaveFilename.setText( dialog.getFilterPath() + System.getProperty("file.separator") + dialog.getFileName()); //$NON-NLS-1$ } } }); m_wTabFolder.setSelection(0); // Set the shell size, based upon previous time... setSize(); getData(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } return stepname; }