Search in sources :

Example 1 with FileSaveDialog

use of com.centurylink.mdw.plugin.designer.dialogs.FileSaveDialog in project mdw-designer by CenturyLinkCloud.

the class ConvertApplicationProperties method run.

public void run(IAction action) {
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        if (structuredSelection.getFirstElement() instanceof IFile) {
            final IFile inputFile = (IFile) structuredSelection.getFirstElement();
            final WorkflowProject project = WorkflowProjectManager.getInstance().getWorkflowProject(inputFile.getProject().getName());
            String ext = project == null || project.isOsgi() ? "cfg" : "properties";
            FileSaveDialog saveDialog = new FileSaveDialog(MdwPlugin.getActiveWorkbenchWindow().getShell());
            saveDialog.setFilterPath(inputFile.getParent().getRawLocation().makeAbsolute().toFile().getAbsolutePath());
            saveDialog.setFilterExtensions(new String[] { "*" + ext });
            final String filePath = saveDialog.open();
            if (filePath != null) {
                BusyIndicator.showWhile(MdwPlugin.getActiveWorkbenchWindow().getShell().getDisplay(), new Runnable() {

                    public void run() {
                        try {
                            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                            Document doc = dBuilder.parse(inputFile.getContents());
                            doc.getDocumentElement().normalize();
                            StringBuffer sb = new StringBuffer();
                            NodeList nList = doc.getElementsByTagName("PropertyGroup");
                            // for every property group
                            for (int temp = 0; temp < nList.getLength(); temp++) {
                                Node nNode = nList.item(temp);
                                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                                    Element eElement = (Element) nNode;
                                    String propertyGroupName = eElement.getAttribute("Name");
                                    // create PropertyGroup comment
                                    sb.append("\n");
                                    sb.append("#");
                                    sb.append(propertyGroupName);
                                    sb.append("\n");
                                    NodeList propertyList = eElement.getElementsByTagName("Property");
                                    // group
                                    for (int temp2 = 0; temp2 < propertyList.getLength(); temp2++) {
                                        Node nNode2 = propertyList.item(temp2);
                                        if (nNode2.getNodeType() == Node.ELEMENT_NODE) {
                                            Element eElement2 = (Element) nNode2;
                                            // format to:
                                            // propertyGroup-propertyName=elementValue
                                            sb.append(propertyGroupName);
                                            if (project == null || project.isOsgi())
                                                sb.append("-");
                                            else
                                                sb.append("/");
                                            sb.append(eElement2.getAttribute("Name"));
                                            sb.append("=");
                                            sb.append(nNode2.getTextContent());
                                            sb.append("\n");
                                        }
                                    }
                                }
                            }
                            PluginUtil.writeFile(new File(filePath), sb.toString().getBytes());
                            inputFile.getParent().refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor());
                        } catch (Exception ex) {
                            PluginMessages.uiError(shell, ex, "Convert Application Properties", project);
                        }
                    }
                });
            }
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) WorkflowProject(com.centurylink.mdw.plugin.project.model.WorkflowProject) FileSaveDialog(com.centurylink.mdw.plugin.designer.dialogs.FileSaveDialog) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Document(org.w3c.dom.Document) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File) IFile(org.eclipse.core.resources.IFile)

Example 2 with FileSaveDialog

use of com.centurylink.mdw.plugin.designer.dialogs.FileSaveDialog in project mdw-designer by CenturyLinkCloud.

the class ImportExportPage method createFileControls.

protected void createFileControls(Composite parent, int ncol) {
    new Label(parent, SWT.NONE).setText(isExport ? "Export File:" : "Import File:");
    filePathText = new Text(parent, SWT.SINGLE | SWT.BORDER);
    GridData gd = new GridData(GridData.BEGINNING);
    gd.widthHint = 350;
    gd.horizontalSpan = ncol - 2;
    filePathText.setLayoutData(gd);
    filePathText.addModifyListener(new ModifyListener() {

        public void modifyText(ModifyEvent e) {
            filePath = filePathText.getText().trim();
            handleFieldChanged();
        }
    });
    Button browseFileButton = new Button(parent, SWT.PUSH);
    browseFileButton.setText("Browse...");
    browseFileButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (isExport) {
                FileSaveDialog dlg = new FileSaveDialog(getShell());
                dlg.setFilterExtensions(new String[] { "*" + getFileExtension() });
                dlg.setFileName(getDefaultFileName());
                String path = dlg.open();
                if (path != null)
                    filePathText.setText(path);
            } else {
                FileDialog dlg = new FileDialog(getShell());
                dlg.setFilterExtensions(new String[] { "*" + getFileExtension() });
                String path = dlg.open();
                if (path != null)
                    filePathText.setText(path);
            }
        }
    });
}
Also used : ModifyEvent(org.eclipse.swt.events.ModifyEvent) ModifyListener(org.eclipse.swt.events.ModifyListener) Button(org.eclipse.swt.widgets.Button) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Label(org.eclipse.swt.widgets.Label) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Text(org.eclipse.swt.widgets.Text) FileSaveDialog(com.centurylink.mdw.plugin.designer.dialogs.FileSaveDialog) FileDialog(org.eclipse.swt.widgets.FileDialog)

Aggregations

FileSaveDialog (com.centurylink.mdw.plugin.designer.dialogs.FileSaveDialog)2 WorkflowProject (com.centurylink.mdw.plugin.project.model.WorkflowProject)1 File (java.io.File)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 IFile (org.eclipse.core.resources.IFile)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 ModifyEvent (org.eclipse.swt.events.ModifyEvent)1 ModifyListener (org.eclipse.swt.events.ModifyListener)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 GridData (org.eclipse.swt.layout.GridData)1 Button (org.eclipse.swt.widgets.Button)1 FileDialog (org.eclipse.swt.widgets.FileDialog)1 Label (org.eclipse.swt.widgets.Label)1 Text (org.eclipse.swt.widgets.Text)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1