Search in sources :

Example 1 with IScriptComponent

use of com.twinsoft.convertigo.beans.ngx.components.IScriptComponent in project convertigo by convertigo.

the class NgxUIComponentTreeObject method editFunction.

private void editFunction(final UIComponent uic, final String functionMarker, final String propertyName) {
    try {
        IScriptComponent main = uic.getMainScriptComponent();
        if (main == null) {
            return;
        }
        // Refresh project resources for editor
        String projectName = uic.getProject().getName();
        IProject project = ConvertigoPlugin.getDefault().getProjectPluginResource(projectName);
        project.refreshLocal(IResource.DEPTH_INFINITE, null);
        // Close editor and Reopen it after file has been rewritten
        String relativePath = uic.getProject().getMobileBuilder().getFunctionTempTsRelativePath(uic);
        IFile file = project.getFile(relativePath);
        if (!(uic instanceof UICustomAction)) {
            closeComponentFileEditor(file);
        }
        if (main instanceof ApplicationComponent) {
            if (uic.compareToTplVersion("7.5.2.0") < 0) {
                ConvertigoPlugin.logError("The ability to use forms or actions inside a menu is avalaible since 7.5.2 version." + "\nPlease change your Template project for the 'mobilebuilder_tpl_7_5_2' template.", true);
                return;
            }
        }
        uic.getProject().getMobileBuilder().writeFunctionTempTsFile(uic, functionMarker);
        file.refreshLocal(IResource.DEPTH_ZERO, null);
        // Open file in editor
        if (file.exists()) {
            IEditorInput input = new ComponentFileEditorInput(file, uic);
            if (input != null) {
                IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(file.getName());
                IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                String editorId = desc.getId();
                IEditorPart editorPart = activePage.openEditor(input, editorId);
                addMarkers(file, editorPart);
                editorPart.addPropertyListener(new IPropertyListener() {

                    boolean isFirstChange = false;

                    @Override
                    public void propertyChanged(Object source, int propId) {
                        if (source instanceof ITextEditor) {
                            if (propId == IEditorPart.PROP_DIRTY) {
                                if (!isFirstChange) {
                                    isFirstChange = true;
                                    return;
                                }
                                isFirstChange = false;
                                ITextEditor editor = (ITextEditor) source;
                                IDocumentProvider dp = editor.getDocumentProvider();
                                IDocument doc = dp.getDocument(editor.getEditorInput());
                                String marker = MobileBuilder.getMarker(doc.get(), functionMarker);
                                String content = MobileBuilder.getFormatedContent(marker, functionMarker);
                                FormatedContent formated = new FormatedContent(content);
                                NgxUIComponentTreeObject.this.setPropertyValue(propertyName, formated);
                            }
                        }
                    }
                });
            }
        }
    } catch (Exception e) {
        ConvertigoPlugin.logException(e, "Unable to edit function for '" + uic.getName() + "' component!");
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IScriptComponent(com.twinsoft.convertigo.beans.ngx.components.IScriptComponent) ApplicationComponent(com.twinsoft.convertigo.beans.ngx.components.ApplicationComponent) ComponentFileEditorInput(com.twinsoft.convertigo.eclipse.editors.ngx.ComponentFileEditorInput) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) IEditorPart(org.eclipse.ui.IEditorPart) IProject(org.eclipse.core.resources.IProject) IPropertyListener(org.eclipse.ui.IPropertyListener) CoreException(org.eclipse.core.runtime.CoreException) EngineException(com.twinsoft.convertigo.engine.EngineException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) UICustomAction(com.twinsoft.convertigo.beans.ngx.components.UICustomAction) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) FormatedContent(com.twinsoft.convertigo.beans.common.FormatedContent) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with IScriptComponent

use of com.twinsoft.convertigo.beans.ngx.components.IScriptComponent in project convertigo by convertigo.

the class NgxComponentImportVariablesAction method run.

public void run() {
    Display display = Display.getDefault();
    Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
    Shell shell = getParentShell();
    shell.setCursor(waitCursor);
    try {
        ProjectExplorerView explorerView = getProjectExplorerView();
        if (explorerView != null) {
            TreeObject treeObject = explorerView.getFirstSelectedTreeObject();
            Object databaseObject = treeObject.getObject();
            if (databaseObject != null) {
                if (databaseObject instanceof UIDynamicAction) {
                    UIDynamicAction dynAction = (UIDynamicAction) databaseObject;
                    IonBean ionBean = ((UIDynamicAction) dynAction).getIonBean();
                    if (ionBean != null) {
                        // Case of CallSequenceAction
                        if (ionBean.getName().equals("CallSequenceAction")) {
                            Object value = ionBean.getProperty("requestable").getValue();
                            if (!value.equals(false)) {
                                String target = value.toString();
                                if (!target.isEmpty()) {
                                    try {
                                        String projectName = target.substring(0, target.indexOf('.'));
                                        String sequenceName = target.substring(target.indexOf('.') + 1);
                                        Project p = Engine.theApp.databaseObjectsManager.getProjectByName(projectName);
                                        Sequence sequence = p.getSequenceByName(sequenceName);
                                        int size = sequence.numberOfVariables();
                                        for (int i = 0; i < size; i++) {
                                            RequestableVariable variable = (RequestableVariable) sequence.getVariable(i);
                                            if (variable != null) {
                                                String variableName = variable.getName();
                                                if (dynAction.getVariable(variableName) == null) {
                                                    if (!StringUtils.isNormalized(variableName))
                                                        throw new EngineException("Variable name is not normalized : \"" + variableName + "\".");
                                                    UIControlVariable uiVariable = new UIControlVariable();
                                                    uiVariable.setName(variableName);
                                                    uiVariable.setComment(variable.getDescription());
                                                    uiVariable.setVarSmartType(new MobileSmartSourceType(variable.getDefaultValue().toString()));
                                                    dynAction.addUIComponent(uiVariable);
                                                    uiVariable.bNew = true;
                                                    uiVariable.hasChanged = true;
                                                    dynAction.hasChanged = true;
                                                }
                                            }
                                        }
                                    } catch (Exception e) {
                                    }
                                }
                            }
                        } else // Case of InvokeAction
                        if (ionBean.getName().equals("InvokeAction")) {
                            UIDynamicInvoke dynInvoke = (UIDynamicInvoke) databaseObject;
                            UIActionStack stack = dynInvoke.getTargetSharedAction();
                            if (stack != null) {
                                for (UIStackVariable variable : stack.getVariables()) {
                                    String variableName = variable.getName();
                                    if (dynAction.getVariable(variableName) == null) {
                                        if (!StringUtils.isNormalized(variableName))
                                            throw new EngineException("Variable name is not normalized : \"" + variableName + "\".");
                                        UIControlVariable uiVariable = new UIControlVariable();
                                        uiVariable.setName(variableName);
                                        uiVariable.setComment(variable.getComment());
                                        MobileSmartSourceType msst = new MobileSmartSourceType();
                                        msst.setMode(MobileSmartSourceType.Mode.SCRIPT);
                                        msst.setSmartValue(variable.getVariableValue());
                                        uiVariable.setVarSmartType(msst);
                                        dynAction.addUIComponent(uiVariable);
                                        uiVariable.bNew = true;
                                        uiVariable.hasChanged = true;
                                        dynAction.hasChanged = true;
                                    }
                                }
                            }
                        }
                        if (dynAction.hasChanged) {
                            IScriptComponent main = dynAction.getMainScriptComponent();
                            if (main != null) {
                                if (main instanceof ApplicationComponent) {
                                    ((ApplicationComponent) main).markApplicationAsDirty();
                                }
                                if (main instanceof PageComponent) {
                                    ((PageComponent) main).markPageAsDirty();
                                }
                            }
                            explorerView.reloadTreeObject(treeObject);
                            StructuredSelection structuredSelection = new StructuredSelection(treeObject);
                            ConvertigoPlugin.getDefault().getPropertiesView().selectionChanged((IWorkbenchPart) explorerView, structuredSelection);
                        }
                    }
                } else if (databaseObject instanceof UIUseShared) {
                    UIUseShared useShared = (UIUseShared) databaseObject;
                    UISharedComponent sharedComp = useShared.getTargetSharedComponent();
                    if (sharedComp != null) {
                        for (UICompVariable variable : sharedComp.getVariables()) {
                            String variableName = variable.getName();
                            if (useShared.getVariable(variableName) == null) {
                                if (!StringUtils.isNormalized(variableName))
                                    throw new EngineException("Variable name is not normalized : \"" + variableName + "\".");
                                UIControlVariable uiVariable = new UIControlVariable();
                                uiVariable.setName(variableName);
                                uiVariable.setComment(variable.getComment());
                                MobileSmartSourceType msst = new MobileSmartSourceType();
                                msst.setMode(MobileSmartSourceType.Mode.SCRIPT);
                                msst.setSmartValue(variable.getVariableValue());
                                uiVariable.setVarSmartType(msst);
                                useShared.addUIComponent(uiVariable);
                                uiVariable.bNew = true;
                                uiVariable.hasChanged = true;
                                useShared.hasChanged = true;
                            }
                        }
                        if (useShared.hasChanged) {
                            IScriptComponent main = useShared.getMainScriptComponent();
                            if (main != null) {
                                if (main instanceof ApplicationComponent) {
                                    ((ApplicationComponent) main).markApplicationAsDirty();
                                }
                                if (main instanceof PageComponent) {
                                    ((PageComponent) main).markPageAsDirty();
                                }
                            }
                            explorerView.reloadTreeObject(treeObject);
                            StructuredSelection structuredSelection = new StructuredSelection(treeObject);
                            ConvertigoPlugin.getDefault().getPropertiesView().selectionChanged((IWorkbenchPart) explorerView, structuredSelection);
                        }
                    }
                }
            }
        }
    } catch (Throwable e) {
        ConvertigoPlugin.logException(e, "Unable to add variables to action !");
    } finally {
        shell.setCursor(null);
        waitCursor.dispose();
    }
}
Also used : IonBean(com.twinsoft.convertigo.beans.ngx.components.dynamic.IonBean) MobileSmartSourceType(com.twinsoft.convertigo.beans.ngx.components.MobileSmartSourceType) UIUseShared(com.twinsoft.convertigo.beans.ngx.components.UIUseShared) EngineException(com.twinsoft.convertigo.engine.EngineException) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) UIControlVariable(com.twinsoft.convertigo.beans.ngx.components.UIControlVariable) Cursor(org.eclipse.swt.graphics.Cursor) UISharedComponent(com.twinsoft.convertigo.beans.ngx.components.UISharedComponent) PageComponent(com.twinsoft.convertigo.beans.ngx.components.PageComponent) Shell(org.eclipse.swt.widgets.Shell) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) UICompVariable(com.twinsoft.convertigo.beans.ngx.components.UICompVariable) ProjectExplorerView(com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView) IScriptComponent(com.twinsoft.convertigo.beans.ngx.components.IScriptComponent) ApplicationComponent(com.twinsoft.convertigo.beans.ngx.components.ApplicationComponent) RequestableVariable(com.twinsoft.convertigo.beans.variables.RequestableVariable) UIStackVariable(com.twinsoft.convertigo.beans.ngx.components.UIStackVariable) Sequence(com.twinsoft.convertigo.beans.core.Sequence) EngineException(com.twinsoft.convertigo.engine.EngineException) Project(com.twinsoft.convertigo.beans.core.Project) UIActionStack(com.twinsoft.convertigo.beans.ngx.components.UIActionStack) UIDynamicAction(com.twinsoft.convertigo.beans.ngx.components.UIDynamicAction) UIDynamicInvoke(com.twinsoft.convertigo.beans.ngx.components.UIDynamicInvoke) TreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject) DatabaseObjectTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) TreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject) DatabaseObjectTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject) Display(org.eclipse.swt.widgets.Display)

Example 3 with IScriptComponent

use of com.twinsoft.convertigo.beans.ngx.components.IScriptComponent in project convertigo by convertigo.

the class NgxBuilder method getFunctionTempTsRelativePath.

@Override
public String getFunctionTempTsRelativePath(final IUIComponent uiComponent) throws EngineException {
    UIComponent uic = (UIComponent) uiComponent;
    IScriptComponent main = uic.getMainScriptComponent();
    if (main != null) {
        File tempTsDir = null;
        String tempTsFileName = null;
        if (main instanceof ApplicationComponent) {
            UIActionStack stack = uic.getSharedAction();
            tempTsDir = stack == null ? appDir : servicesDir;
            tempTsFileName = stack == null ? "app.component.function.temp.ts" : "actionbeans.service.function.temp.ts";
        }
        if (main instanceof PageComponent) {
            PageComponent page = (PageComponent) main;
            String pageName = page.getName();
            tempTsDir = pageDir(page);
            tempTsFileName = pageName.toLowerCase() + ".function.temp.ts";
        }
        if (tempTsDir != null && tempTsFileName != null) {
            if (uiComponent instanceof UICustomAction) {
                tempTsFileName = "CTS" + ((UICustomAction) uiComponent).priority + ".temp.ts";
            }
            File tempTsFile = new File(tempTsDir, tempTsFileName);
            return tempTsFile.getPath().replace(projectDir.getPath(), File.separator);
        }
    }
    return null;
}
Also used : UIActionStack(com.twinsoft.convertigo.beans.ngx.components.UIActionStack) IScriptComponent(com.twinsoft.convertigo.beans.ngx.components.IScriptComponent) ApplicationComponent(com.twinsoft.convertigo.beans.ngx.components.ApplicationComponent) IApplicationComponent(com.twinsoft.convertigo.beans.core.IApplicationComponent) IUIComponent(com.twinsoft.convertigo.beans.core.IUIComponent) UIComponent(com.twinsoft.convertigo.beans.ngx.components.UIComponent) UICustomAction(com.twinsoft.convertigo.beans.ngx.components.UICustomAction) File(java.io.File) IPageComponent(com.twinsoft.convertigo.beans.core.IPageComponent) PageComponent(com.twinsoft.convertigo.beans.ngx.components.PageComponent)

Example 4 with IScriptComponent

use of com.twinsoft.convertigo.beans.ngx.components.IScriptComponent in project convertigo by convertigo.

the class NgxBuilder method writeFunctionTempTsFile.

@Override
public void writeFunctionTempTsFile(final IUIComponent uiComponent, String functionMarker) throws EngineException {
    UIComponent uic = (UIComponent) uiComponent;
    try {
        IScriptComponent main = uic.getMainScriptComponent();
        if (main != null) {
            String tempTsFileName = null, tsContent = null;
            File tempTsDir = null;
            UIActionStack sharedAction = null;
            if (main instanceof ApplicationComponent) {
                sharedAction = uic.getSharedAction();
                tempTsDir = sharedAction == null ? appDir : servicesDir;
                tempTsFileName = sharedAction == null ? "app.component.function.temp.ts" : "actionbeans.service.function.temp.ts";
                File appTsFile = sharedAction == null ? new File(appDir, "app.component.ts") : new File(servicesDir, "actionbeans.service.ts");
                synchronized (writtenFiles) {
                    if (writtenFiles.contains(appTsFile)) {
                        File appTsFileTmp = toTmpFile(appTsFile);
                        if (appTsFileTmp.exists()) {
                            appTsFile = appTsFileTmp;
                        }
                    }
                }
                tsContent = FileUtils.readFileToString(appTsFile, "UTF-8");
            }
            if (main instanceof PageComponent) {
                PageComponent page = (PageComponent) main;
                String pageName = page.getName();
                tempTsDir = pageDir(page);
                tempTsFileName = pageName.toLowerCase() + ".function.temp.ts";
                if (page.isEnabled()) {
                    File pageTsFile = new File(tempTsDir, pageName.toLowerCase() + ".ts");
                    synchronized (writtenFiles) {
                        if (writtenFiles.contains(pageTsFile)) {
                            File pageTsFileTmp = toTmpFile(pageTsFile);
                            if (pageTsFileTmp.exists()) {
                                pageTsFile = pageTsFileTmp;
                            }
                        }
                    }
                    tsContent = FileUtils.readFileToString(pageTsFile, "UTF-8");
                } else {
                    tsContent = getPageTsContent(page);
                }
            }
            // Replace all Begin_c8o_XXX, End_c8o_XXX except for functionMarker
            Pattern pattern = Pattern.compile("/\\*Begin_c8o_(.+)\\*/");
            Matcher matcher = pattern.matcher(tsContent);
            while (matcher.find()) {
                String markerId = matcher.group(1);
                if (!markerId.equals(functionMarker)) {
                    String beginMarker = "/*Begin_c8o_" + markerId + "*/";
                    String endMarker = "/*End_c8o_" + markerId + "*/";
                    tsContent = tsContent.replace(beginMarker, "//---" + markerId + "---");
                    tsContent = tsContent.replace(endMarker, "//---" + markerId + "---");
                }
            }
            // CustomAction : reduce code lines (action's function only)
            if (tempTsDir != null && tempTsFileName != null) {
                if (uiComponent instanceof UICustomAction) {
                    UICustomAction uica = (UICustomAction) uic;
                    tempTsFileName = "CTS" + uica.priority + ".temp.ts";
                    int index = tsContent.indexOf("export class ");
                    if (index != -1) {
                        int i = tsContent.indexOf("{", index);
                        tsContent = tsContent.substring(0, i + 1) + System.lineSeparator() + uica.getActionCode() + System.lineSeparator() + "}" + System.lineSeparator();
                    }
                }
            }
            // Write file (do not need delay)
            tsContent = LsPattern.matcher(tsContent).replaceAll(System.lineSeparator());
            File tempTsFile = new File(tempTsDir, tempTsFileName);
            FileUtils.write(tempTsFile, tsContent, "UTF-8");
        }
    } catch (Exception e) {
        throw new EngineException("Unable to write function temp ts file", e);
    }
}
Also used : Pattern(java.util.regex.Pattern) IScriptComponent(com.twinsoft.convertigo.beans.ngx.components.IScriptComponent) Matcher(java.util.regex.Matcher) ApplicationComponent(com.twinsoft.convertigo.beans.ngx.components.ApplicationComponent) IApplicationComponent(com.twinsoft.convertigo.beans.core.IApplicationComponent) IUIComponent(com.twinsoft.convertigo.beans.core.IUIComponent) UIComponent(com.twinsoft.convertigo.beans.ngx.components.UIComponent) EngineException(com.twinsoft.convertigo.engine.EngineException) IPageComponent(com.twinsoft.convertigo.beans.core.IPageComponent) PageComponent(com.twinsoft.convertigo.beans.ngx.components.PageComponent) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException) UIActionStack(com.twinsoft.convertigo.beans.ngx.components.UIActionStack) UICustomAction(com.twinsoft.convertigo.beans.ngx.components.UICustomAction) File(java.io.File)

Aggregations

ApplicationComponent (com.twinsoft.convertigo.beans.ngx.components.ApplicationComponent)4 IScriptComponent (com.twinsoft.convertigo.beans.ngx.components.IScriptComponent)4 PageComponent (com.twinsoft.convertigo.beans.ngx.components.PageComponent)3 UIActionStack (com.twinsoft.convertigo.beans.ngx.components.UIActionStack)3 UICustomAction (com.twinsoft.convertigo.beans.ngx.components.UICustomAction)3 EngineException (com.twinsoft.convertigo.engine.EngineException)3 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)2 IApplicationComponent (com.twinsoft.convertigo.beans.core.IApplicationComponent)2 IPageComponent (com.twinsoft.convertigo.beans.core.IPageComponent)2 IUIComponent (com.twinsoft.convertigo.beans.core.IUIComponent)2 UIComponent (com.twinsoft.convertigo.beans.ngx.components.UIComponent)2 File (java.io.File)2 FormatedContent (com.twinsoft.convertigo.beans.common.FormatedContent)1 Project (com.twinsoft.convertigo.beans.core.Project)1 Sequence (com.twinsoft.convertigo.beans.core.Sequence)1 MobileSmartSourceType (com.twinsoft.convertigo.beans.ngx.components.MobileSmartSourceType)1 UICompVariable (com.twinsoft.convertigo.beans.ngx.components.UICompVariable)1 UIControlVariable (com.twinsoft.convertigo.beans.ngx.components.UIControlVariable)1 UIDynamicAction (com.twinsoft.convertigo.beans.ngx.components.UIDynamicAction)1 UIDynamicInvoke (com.twinsoft.convertigo.beans.ngx.components.UIDynamicInvoke)1