Search in sources :

Example 1 with ExporterFactory

use of org.hibernate.eclipse.console.model.impl.ExporterFactory in project jbosstools-hibernate by jbosstools.

the class ExporterAttributes method oldSaveExporterFactories.

public static void oldSaveExporterFactories(ILaunchConfigurationWorkingCopy configuration, List<ExporterFactory> exporterFactories, List<ExporterFactory> enabledExporters) {
    for (ExporterFactory ef : exporterFactories) {
        boolean enabled = enabledExporters.contains(ef);
        // $NON-NLS-1$
        String propertiesId = ef.getId() + ".properties";
        ef.setEnabled(configuration, enabled, true);
        HashMap<String, String> map = new HashMap<String, String>(ef.getProperties());
        if (map.isEmpty()) {
            configuration.setAttribute(propertiesId, (Map<String, String>) null);
        } else {
            configuration.setAttribute(propertiesId, map);
        }
    }
}
Also used : ExporterFactory(org.hibernate.eclipse.console.model.impl.ExporterFactory) HashMap(java.util.HashMap)

Example 2 with ExporterFactory

use of org.hibernate.eclipse.console.model.impl.ExporterFactory in project jbosstools-hibernate by jbosstools.

the class ExporterAttributesTest method checkCorrectLaunchConfigurationFile.

private void checkCorrectLaunchConfigurationFile(final String fileName) {
    // IWorkspace ws = ResourcesPlugin.getWorkspace();
    String str1, str2;
    ILaunchConfiguration launchConfig = loadLaunchConfigFromFile(fileName);
    Map<String, Object> attrMap = null;
    try {
        attrMap = launchConfig.getAttributes();
    } catch (CoreException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(attrMap);
    ExporterAttributes exporterAttributes = null;
    try {
        exporterAttributes = new ExporterAttributes(launchConfig);
    } catch (CoreException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(exporterAttributes);
    // check is configuration correct
    Assert.assertNull(exporterAttributes.checkExporterAttributes());
    List<ExporterFactory> exporterFactories = exporterAttributes.getExporterFactories();
    Set<ExporterFactory> selectedExporters = new HashSet<ExporterFactory>();
    selectedExporters.addAll(exporterFactories);
    Set<String> deletedExporterIds = new HashSet<String>();
    ILaunchConfigurationWorkingCopy launchConfigWC = null;
    try {
        launchConfigWC = launchConfig.getWorkingCopy();
    } catch (CoreException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(launchConfigWC);
    // 
    str1 = project.getSample(fileName);
    str1 = ResourceReadUtils.adjustXmlText(str1);
    // 
    Assert.assertNotNull(project);
    Assert.assertNotNull(project.getIProject());
    Assert.assertNotNull(project.getIProject().getFile(fileName));
    // 
    InputStream is = null;
    try {
        is = project.getIProject().getFile(fileName).getContents();
    } catch (CoreException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(is);
    str2 = ResourceReadUtils.readStream(is);
    str2 = ResourceReadUtils.adjustXmlText(str2);
    Assert.assertEquals(str1, str2);
    // update and save lc - so fileName from the project updated
    try {
        ExporterAttributes.saveExporterFactories(launchConfigWC, exporterFactories, selectedExporters, deletedExporterIds);
        launchConfigWC.doSave();
    } catch (CoreException e) {
        Assert.fail(e.getMessage());
    }
    // 
    is = null;
    try {
        is = project.getIProject().getFile(fileName).getContents();
    } catch (CoreException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(is);
    str2 = ResourceReadUtils.readStream(is);
    str2 = ResourceReadUtils.adjustXmlText(str2);
    Assert.assertEquals(str1, str2);
    // 
    IArtifactCollector artifactCollector = service.newArtifactCollector();
    ExporterAttributes expAttr = exporterAttributes;
    // Global properties
    Properties props = new Properties();
    // $NON-NLS-1$
    props.put(CodeGenerationStrings.EJB3, "" + expAttr.isEJB3Enabled());
    // $NON-NLS-1$
    props.put(CodeGenerationStrings.JDK5, "" + expAttr.isJDK5Enabled());
    consoleCfg.build();
    IConfiguration cfg = consoleCfg.getConfiguration();
    Assert.assertNotNull(cfg);
    Set<String> outputDirectories = new HashSet<String>();
    for (int i = 0; i < exporterFactories.size(); i++) {
        Properties globalProperties = new Properties();
        globalProperties.putAll(props);
        ExporterFactory ef = exporterFactories.get(i);
        // 
        Properties propsForTesting = new Properties();
        propsForTesting.putAll(globalProperties);
        propsForTesting.putAll(ef.getProperties());
        // 
        IExporter exporter = null;
        outputDirectories.clear();
        try {
            exporter = ef.createConfiguredExporter(cfg, expAttr.getOutputPath(), expAttr.getTemplatePath(), globalProperties, outputDirectories, artifactCollector, service);
        } catch (CoreException e) {
            e.printStackTrace();
        }
        Assert.assertNotNull(exporter);
        Assert.assertTrue(outputDirectories.size() > 0);
        Properties propsFromExporter = exporter.getProperties();
        String exporterDefinitionId = ef.getExporterDefinitionId();
        // test special handling for GenericExporter
        if (exporterDefinitionId.equals("org.hibernate.tools.hbmtemplate")) {
            // $NON-NLS-1$
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.FILE_PATTERN));
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.TEMPLATE_NAME));
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.FOR_EACH));
            IGenericExporter ge = exporter.getGenericExporter();
            Assert.assertNotNull(ge);
            Assert.assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.FILE_PATTERN), ge.getFilePattern());
            Assert.assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.TEMPLATE_NAME), ge.getTemplateName());
        // to test GenericExporter should provide public getter but it doesn't
        // assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.FOR_EACH), ge.getForEach());
        }
        // test special handling for Hbm2DDLExporter
        if (exporterDefinitionId.equals("org.hibernate.tools.hbm2ddl")) {
            // $NON-NLS-1$
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.EXPORTTODATABASE));
            IHbm2DDLExporter ddlExporter = exporter.getHbm2DDLExporter();
            Assert.assertNotNull(ddlExporter);
        // to test Hbm2DDLExporter should provide public getter but it doesn't
        // assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.EXPORTTODATABASE), ddlExporter.getExport());
        }
        // test special handling for QueryExporter
        if (exporterDefinitionId.equals("org.hibernate.tools.query")) {
            // $NON-NLS-1$
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.QUERY_STRING));
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.OUTPUTFILENAME));
        // IQueryExporter queryExporter = exporter.getQueryExporter();
        // to test QueryExporter should provide public getter but it doesn't
        // List<String> queryStrings = queryExporter.getQueries();
        // assertEquals(1, queryStrings.size());
        // assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.QUERY_STRING), queryStrings.get(0));
        // assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.OUTPUTFILENAME), queryExporter.getFileName());
        }
    // try {
    // exporter.start();
    // } catch (HibernateException he) {
    // he.printStackTrace();
    // }
    }
    Assert.assertTrue(artifactCollector.getFileTypes().size() == 0);
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) InputStream(java.io.InputStream) ILaunchConfigurationWorkingCopy(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) Properties(java.util.Properties) ExporterAttributes(org.hibernate.eclipse.launch.ExporterAttributes) CoreException(org.eclipse.core.runtime.CoreException) IArtifactCollector(org.jboss.tools.hibernate.runtime.spi.IArtifactCollector) IHbm2DDLExporter(org.jboss.tools.hibernate.runtime.spi.IHbm2DDLExporter) ExporterFactory(org.hibernate.eclipse.console.model.impl.ExporterFactory) IExporter(org.jboss.tools.hibernate.runtime.spi.IExporter) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) IGenericExporter(org.jboss.tools.hibernate.runtime.spi.IGenericExporter) HashSet(java.util.HashSet)

Example 3 with ExporterFactory

use of org.hibernate.eclipse.console.model.impl.ExporterFactory in project jbosstools-hibernate by jbosstools.

the class ExporterTest method setUp.

@Before
public void setUp() throws Exception {
    map = new HashMap<String, ExporterProperty>();
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    map.put("ejb3", new ExporterProperty("ejb3", "Use ejb3 syntax", "true", true));
    definition = new // $NON-NLS-1$
    ExporterDefinition(// $NON-NLS-1$
    "exporterClass", // $NON-NLS-1$
    "exporterDescription", // $NON-NLS-1$
    "exporterId", map, null);
    factory = new ExporterFactory(definition, definition.getId());
}
Also used : ExporterFactory(org.hibernate.eclipse.console.model.impl.ExporterFactory) ExporterProperty(org.hibernate.eclipse.console.model.impl.ExporterProperty) Before(org.junit.Before)

Example 4 with ExporterFactory

use of org.hibernate.eclipse.console.model.impl.ExporterFactory in project jbosstools-hibernate by jbosstools.

the class AddPropertyDialog method initDefaultNames.

private void initDefaultNames(ExporterFactory ef2, ComboViewer viewer) {
    viewer.setContentProvider(new IStructuredContentProvider() {

        ExporterFactory localEf;

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            localEf = (ExporterFactory) newInput;
        }

        public void dispose() {
            localEf = null;
        }

        public Object[] getElements(Object inputElement) {
            Iterator<Map.Entry<String, ExporterProperty>> set = localEf.getDefaultExporterProperties().entrySet().iterator();
            List<ExporterProperty> values = new ArrayList<ExporterProperty>(4);
            while (set.hasNext()) {
                Map.Entry<String, ExporterProperty> element = set.next();
                // if(!localEf.hasLocalValueFor((String) element.getKey())) {
                ExporterProperty exporterProperty = localEf.getExporterProperty(element.getKey());
                if (exporterProperty != null) {
                    values.add(exporterProperty);
                }
            // }
            }
            return values.toArray(new ExporterProperty[values.size()]);
        }
    });
    viewer.setLabelProvider(new ILabelProvider() {

        public void removeListener(ILabelProviderListener listener) {
        }

        public boolean isLabelProperty(Object element, String property) {
            return false;
        }

        public void dispose() {
        }

        public void addListener(ILabelProviderListener listener) {
        }

        public String getText(Object element) {
            ExporterProperty exporterProperty = ((ExporterProperty) element);
            return exporterProperty.getDescriptionForLabel();
        }

        public Image getImage(Object element) {
            return null;
        }
    });
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        private SelectionListener getSelectionListener(ExporterProperty prop) {
            // $NON-NLS-1$//$NON-NLS-2$
            if (!("path".equals(prop.getType()) || "directory".equals(prop.getType())))
                return null;
            // $NON-NLS-1$
            final boolean isPath = "path".equals(prop.getType());
            return new SelectionListener() {

                public void widgetDefaultSelected(SelectionEvent e) {
                    widgetSelected(e);
                }

                public void widgetSelected(SelectionEvent e) {
                    String title = isPath ? HibernateConsoleMessages.ExporterSettingsTab_select_path : HibernateConsoleMessages.ExporterSettingsTab_select_dir;
                    String description = isPath ? HibernateConsoleMessages.ExporterSettingsTab_select_path2 : HibernateConsoleMessages.ExporterSettingsTab_select_dir2;
                    MessageDialog dialog = new MessageDialog(getShell(), title, null, description, MessageDialog.QUESTION, new String[] { HibernateConsoleMessages.CodeGenerationSettingsTab_filesystem, HibernateConsoleMessages.CodeGenerationSettingsTab_workspace, IDialogConstants.CANCEL_LABEL }, 1);
                    int answer = dialog.open();
                    String strPath = null;
                    if (answer == 0) {
                        // filesystem
                        DirectoryDialog dialog2 = new DirectoryDialog(getShell());
                        dialog2.setText(title);
                        dialog2.setMessage(description);
                        String dir = dialog2.open();
                        if (dir != null) {
                            strPath = dir;
                        }
                    } else if (answer == 1) {
                        // workspace
                        IPath[] paths = DialogSelectionHelper.chooseFileEntries(getShell(), (IPath) null, new Path[0], title, description, new String[0], isPath, true, false);
                        if (paths != null && paths.length > 0) {
                            strPath = paths[0].toOSString();
                            if (isPath) {
                                for (int i = 1; i < paths.length; i++) {
                                    strPath += ';' + paths[i].toOSString();
                                }
                            }
                        }
                    } else
                        return;
                    String oldPath = ((Text) value).getText();
                    if (isPath && oldPath.trim().length() > 0 && strPath != null)
                        ((Text) value).setText(oldPath + ';' + strPath);
                    else {
                        if (strPath != null)
                            ((Text) value).setText(strPath);
                    }
                }
            };
        }

        public void selectionChanged(SelectionChangedEvent event) {
            if (value == null)
                return;
            IStructuredSelection iss = (IStructuredSelection) event.getSelection();
            if (!iss.isEmpty()) {
                ExporterProperty prop = (ExporterProperty) iss.getFirstElement();
                if ("boolean".equalsIgnoreCase(prop.getType())) {
                    // $NON-NLS-1$
                    disposeBrowseButton();
                    createComboValueComposite(new String[] { String.valueOf(true), String.valueOf(false) });
                    ((Combo) value).select(Boolean.valueOf(ef.getPropertyValue(prop.getName())).booleanValue() ? 0 : 1);
                } else if (// $NON-NLS-1$
                "directory".equalsIgnoreCase(prop.getType()) || "path".equalsIgnoreCase(prop.getType())) {
                    // $NON-NLS-1$
                    disposeBrowseButton();
                    createTextValueComposite(1);
                    ((Text) value).setText(ef.getPropertyValue(prop.getName()));
                    createBrowseButton(getSelectionListener(prop), prop);
                } else {
                    disposeBrowseButton();
                    createTextValueComposite(2);
                    ((Text) value).setText(ef.getPropertyValue(prop.getName()));
                }
            } else {
                createTextValueComposite(2);
            }
        }
    });
    viewer.setInput(ef);
    if (viewer.getCombo().getItemCount() > 0) {
        Object selected = null;
        if (selectedPropertyId != null) {
            selected = ef.getExporterProperty(selectedPropertyId);
        } else {
            selected = viewer.getElementAt(0);
        }
        viewer.setSelection(new StructuredSelection(selected));
        viewer.getCombo().select(viewer.getCombo().getSelectionIndex());
    }
}
Also used : StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ComboViewer(org.eclipse.jface.viewers.ComboViewer) Viewer(org.eclipse.jface.viewers.Viewer) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Image(org.eclipse.swt.graphics.Image) Iterator(java.util.Iterator) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ArrayList(java.util.ArrayList) List(java.util.List) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) DirectoryDialog(org.eclipse.swt.widgets.DirectoryDialog) IPath(org.eclipse.core.runtime.IPath) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) Text(org.eclipse.swt.widgets.Text) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider) ExporterProperty(org.hibernate.eclipse.console.model.impl.ExporterProperty) ExporterFactory(org.hibernate.eclipse.console.model.impl.ExporterFactory) ILabelProviderListener(org.eclipse.jface.viewers.ILabelProviderListener) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) Map(java.util.Map) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 5 with ExporterFactory

use of org.hibernate.eclipse.console.model.impl.ExporterFactory in project jbosstools-hibernate by jbosstools.

the class ExporterSettingsTab method createExporterProperties.

private void createExporterProperties(Composite parent) {
    Composite exportersComposite = createComposite(parent, HibernateConsoleMessages.ExporterSettingsTab_properties);
    exportersComposite.setLayout(new GridLayout(2, false));
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.minimumHeight = 100;
    exportersComposite.setLayoutData(gd);
    Group gr = new Group(exportersComposite, SWT.NONE);
    GridLayout gridLayout = new GridLayout();
    gridLayout.marginHeight = 0;
    gridLayout.marginWidth = 0;
    gr.setLayout(gridLayout);
    gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.verticalSpan = 3;
    gr.setLayoutData(gd);
    Control sheet = createPropertySheet(gr);
    gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    sheet.setLayoutData(gd);
    add = new Button(exportersComposite, SWT.PUSH);
    add.setEnabled(false);
    add.setText(HibernateConsoleMessages.ExporterSettingsTab_add);
    gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
    gd.horizontalIndent = 5;
    add.setLayoutData(gd);
    add.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection ss = (IStructuredSelection) getExporterTable().getSelection();
            ExporterFactory ef = (ExporterFactory) ss.getFirstElement();
            if (ef != null) {
                AddPropertyDialog dialog = new AddPropertyDialog(getShell(), ef, null, false);
                if (dialog.open() == Dialog.OK) {
                    ef.setProperty(dialog.getPropertyName(), dialog.getPropertyValue());
                    dialogChanged();
                    refreshPropertySheet();
                }
            }
        }
    });
    remove = new Button(exportersComposite, SWT.PUSH);
    remove.setText(HibernateConsoleMessages.ExporterSettingsTab_remove);
    remove.setEnabled(false);
    remove.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent e) {
            if (currentDescriptor != null) {
                IStructuredSelection ss = (IStructuredSelection) getExporterTable().getSelection();
                ExporterFactory ef = (ExporterFactory) ss.getFirstElement();
                ef.removeProperty((String) currentDescriptor.getId());
                dialogChanged();
                refreshPropertySheet();
            }
        }
    });
    gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
    gd.horizontalIndent = 5;
    remove.setLayoutData(gd);
    edit = new Button(exportersComposite, SWT.PUSH);
    edit.setEnabled(false);
    edit.setText(HibernateConsoleMessages.ExporterSettingsTab_edit);
    gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
    gd.horizontalIndent = 5;
    edit.setLayoutData(gd);
    edit.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection ss = (IStructuredSelection) getExporterTable().getSelection();
            ExporterFactory ef = (ExporterFactory) ss.getFirstElement();
            if (ef != null) {
                String selectedPropertyId = null;
                if (currentDescriptor != null) {
                    selectedPropertyId = (String) currentDescriptor.getId();
                }
                AddPropertyDialog dialog = new AddPropertyDialog(getShell(), ef, selectedPropertyId, true);
                if (dialog.open() == Dialog.OK) {
                    ef.setProperty(dialog.getPropertyName(), dialog.getPropertyValue());
                    dialogChanged();
                    refreshPropertySheet();
                }
            }
        }
    });
}
Also used : Group(org.eclipse.swt.widgets.Group) GridLayout(org.eclipse.swt.layout.GridLayout) Control(org.eclipse.swt.widgets.Control) Composite(org.eclipse.swt.widgets.Composite) UpDownListComposite(org.hibernate.eclipse.console.wizards.UpDownListComposite) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) Button(org.eclipse.swt.widgets.Button) ExporterFactory(org.hibernate.eclipse.console.model.impl.ExporterFactory) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Aggregations

ExporterFactory (org.hibernate.eclipse.console.model.impl.ExporterFactory)16 ArrayList (java.util.ArrayList)4 CoreException (org.eclipse.core.runtime.CoreException)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 SelectionEvent (org.eclipse.swt.events.SelectionEvent)4 GridData (org.eclipse.swt.layout.GridData)4 UpDownListComposite (org.hibernate.eclipse.console.wizards.UpDownListComposite)4 HashMap (java.util.HashMap)3 ScrolledComposite (org.eclipse.swt.custom.ScrolledComposite)3 GridLayout (org.eclipse.swt.layout.GridLayout)3 Composite (org.eclipse.swt.widgets.Composite)3 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Properties (java.util.Properties)2 IPath (org.eclipse.core.runtime.IPath)2 CheckStateChangedEvent (org.eclipse.jface.viewers.CheckStateChangedEvent)2 ICheckStateListener (org.eclipse.jface.viewers.ICheckStateListener)2 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)2 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)2