Search in sources :

Example 1 with HQLEditor

use of org.hibernate.eclipse.hqleditor.HQLEditor in project jbosstools-hibernate by jbosstools.

the class HQLEditorTest method testHQLEditorOpen.

@Test
public void testHQLEditorOpen() {
    IEditorPart editorPart = HibernateConsolePlugin.getDefault().openScratchHQLEditor(consoleConfiguration.getName(), // $NON-NLS-1$
    "");
    // $NON-NLS-1$
    Assert.assertNotNull("Editor was not opened", editorPart);
    // $NON-NLS-1$
    Assert.assertTrue("Opened editor is not HQLEditor", editorPart instanceof HQLEditor);
    HQLEditor editor = (HQLEditor) editorPart;
    QueryInputModel model = editor.getQueryInputModel();
    // $NON-NLS-1$
    Assert.assertNotNull("Model is NULL", model);
}
Also used : HQLEditor(org.hibernate.eclipse.hqleditor.HQLEditor) IEditorPart(org.eclipse.ui.IEditorPart) QueryInputModel(org.hibernate.console.QueryInputModel) Test(org.junit.Test)

Example 2 with HQLEditor

use of org.hibernate.eclipse.hqleditor.HQLEditor in project jbosstools-hibernate by jbosstools.

the class HQLEditorTest method testHQLEditorCodeCompletionWithTabs.

@Test
public void testHQLEditorCodeCompletionWithTabs() throws CoreException, NoSuchFieldException, IllegalAccessException {
    cleanUpProject();
    project = new SimpleTestProjectWithMapping(PROJ_NAME);
    IPackageFragmentRoot sourceFolder = project.createSourceFolder();
    IPackageFragment pf = sourceFolder.createPackageFragment(SimpleTestProject.PACKAGE_NAME, false, null);
    ConsoleConfigUtils.customizeCfgXmlForPack(pf);
    List<IPath> libs = new ArrayList<IPath>();
    project.generateClassPath(libs, sourceFolder);
    project.fullBuild();
    // setup console configuration
    IPath cfgFilePath = new Path(project.getIProject().getName() + File.separator + TestProject.SRC_FOLDER + File.separator + ConsoleConfigUtils.CFG_FILE_NAME);
    ConsoleConfigUtils.createConsoleConfig(PROJ_NAME, cfgFilePath, CONSOLE_NAME);
    ConsoleConfiguration cc = KnownConfigurations.getInstance().find(CONSOLE_NAME);
    // $NON-NLS-1$
    Assert.assertNotNull("Console Configuration not found", cc);
    cc.build();
    // $NON-NLS-1$
    final String codeCompletionPlaceMarker = " from ";
    final String query = // $NON-NLS-1$
    "select\t \tt1." + codeCompletionPlaceMarker + project.getFullyQualifiedTestClassName() + // $NON-NLS-1$
    " t1";
    IEditorPart editorPart = HibernateConsolePlugin.getDefault().openScratchHQLEditor(CONSOLE_NAME, query);
    // $NON-NLS-1$
    Assert.assertTrue("Opened editor is not HQLEditor", editorPart instanceof HQLEditor);
    HQLEditor editor = (HQLEditor) editorPart;
    Assert.assertEquals(editor.getEditorText(), query);
    QueryInputModel model = editor.getQueryInputModel();
    Assert.assertTrue(model.getParameterCount() == 0);
    editor.setConsoleConfigurationName(CONSOLE_NAME);
    IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
    HQLCompletionProcessor processor = new HQLCompletionProcessor(editor);
    int position = query.indexOf(codeCompletionPlaceMarker);
    ICompletionProposal[] proposals = processor.computeCompletionProposals(doc, position);
    Assert.assertTrue(proposals.length > 0);
    cc.reset();
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ConsoleConfiguration(org.hibernate.console.ConsoleConfiguration) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) IEditorPart(org.eclipse.ui.IEditorPart) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) QueryInputModel(org.hibernate.console.QueryInputModel) SimpleTestProjectWithMapping(org.jboss.tools.hibernate.orm.test.utils.project.SimpleTestProjectWithMapping) HQLCompletionProcessor(org.hibernate.eclipse.hqleditor.HQLCompletionProcessor) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) HQLEditor(org.hibernate.eclipse.hqleditor.HQLEditor) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

Example 3 with HQLEditor

use of org.hibernate.eclipse.hqleditor.HQLEditor in project jbosstools-hibernate by jbosstools.

the class HQLEditorTest method testSingleLineCommentsCutOff.

@Test
public void testSingleLineCommentsCutOff() throws PartInitException {
    String query = // $NON-NLS-1$
    "from pack.Article a\n" + // $NON-NLS-1$
    "where a.articleid in (:a, :b) --or a.articleid = :c";
    IEditorPart editorPart = HibernateConsolePlugin.getDefault().openScratchHQLEditor(consoleConfiguration.getName(), query);
    // $NON-NLS-1$
    Assert.assertTrue("Opened editor is not HQLEditor", editorPart instanceof HQLEditor);
    HQLEditor editor = (HQLEditor) editorPart;
    Assert.assertEquals(editor.getEditorText(), query);
    // $NON-NLS-1$ //$NON-NLS-2$
    Assert.assertFalse("Comments were not cut off", editor.getQueryString().contains("--"));
    QueryInputModel model = editor.getQueryInputModel();
    Assert.assertTrue(model.getParameterCount() == 0);
    IViewPart view = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(// $NON-NLS-1$
    "org.hibernate.eclipse.console.views.QueryParametersView");
    // $NON-NLS-1$
    Assert.assertNotNull("View was not opened", view);
    // $NON-NLS-1$
    Assert.assertTrue("Opened view is not QueryParametersView", view instanceof QueryParametersView);
    QueryParametersView paramView = (QueryParametersView) view;
    IPage ipage = paramView.getCurrentPage();
    // $NON-NLS-1$
    Assert.assertNotNull("Current Page is NULL", ipage);
    // $NON-NLS-1$
    Assert.assertTrue("Page is not Query Parameters Page", ipage instanceof QueryParametersPage);
    QueryParametersPage page = (QueryParametersPage) ipage;
    IToolBarManager manager = page.getSite().getActionBars().getToolBarManager();
    IContributionItem[] items = manager.getItems();
    ActionContributionItem addParamItem = null;
    for (int i = 0; i < items.length; i++) {
        ActionContributionItem item = (ActionContributionItem) items[i];
        if (item.getAction().getClass().getName().endsWith("NewRowAction")) {
            // $NON-NLS-1$
            addParamItem = item;
            break;
        }
    }
    Assert.assertNotNull(HibernateConsoleMessages.QueryParametersPage_add_query_parameter_tooltip + " item not found", // $NON-NLS-1$
    addParamItem);
    // add query parameters automatically
    addParamItem.getAction().run();
    // a and b
    Assert.assertTrue(model.getParameterCount() == 2);
}
Also used : QueryParametersPage(org.hibernate.eclipse.console.views.QueryParametersPage) IViewPart(org.eclipse.ui.IViewPart) IContributionItem(org.eclipse.jface.action.IContributionItem) IEditorPart(org.eclipse.ui.IEditorPart) QueryInputModel(org.hibernate.console.QueryInputModel) IPage(org.eclipse.ui.part.IPage) ActionContributionItem(org.eclipse.jface.action.ActionContributionItem) QueryParametersView(org.hibernate.eclipse.console.views.QueryParametersView) IToolBarManager(org.eclipse.jface.action.IToolBarManager) HQLEditor(org.hibernate.eclipse.hqleditor.HQLEditor) Test(org.junit.Test)

Example 4 with HQLEditor

use of org.hibernate.eclipse.hqleditor.HQLEditor in project jbosstools-hibernate by jbosstools.

the class SaveQueryEditorListener method propertyChanged.

/* (non-Javadoc)
	 * @see org.eclipse.ui.IPropertyListener#propertyChanged(java.lang.Object, int)
	 */
public void propertyChanged(Object source, int propId) {
    if (IEditorPart.PROP_DIRTY == propId && !editor.isDirty()) {
        IDocumentProvider docProvider = fromEditorPart.getDocumentProvider();
        final IFile file = ((IFileEditorInput) fromEditorPart.getEditorInput()).getFile();
        final IDocument doc = docProvider.getDocument(fromEditorPart.getEditorInput());
        boolean isDocChanged = true;
        try {
            if (query.equals(doc.get(position.x, position.y))) {
                isDocChanged = false;
            }
        } catch (BadLocationException e1) {
        // document changed and we can get the exception
        }
        final String editorTitle = fromEditorPart.getTitle();
        final String editor_name = editor instanceof HQLEditor ? JdtUiMessages.SaveQueryEditorListener_hql_editor : JdtUiMessages.SaveQueryEditorListener_cri_editor;
        if (isDocChanged) {
            String information_message = NLS.bind(JdtUiMessages.SaveQueryEditorListener_replacequestion_confirm, editorTitle);
            MessageDialog.openInformation(null, JdtUiMessages.SaveQueryEditorListener_replacetitle_info, information_message);
            return;
        }
        final String newQuery = editor.getEditorText();
        final String wizard_title = NLS.bind(JdtUiMessages.SaveQueryEditorListener_refactoringtitle, editor_name);
        Refactoring ref = new Refactoring() {

            @Override
            public RefactoringStatus checkFinalConditions(IProgressMonitor pm) {
                return RefactoringStatus.create(Status.OK_STATUS);
            }

            @Override
            public RefactoringStatus checkInitialConditions(IProgressMonitor pm) {
                return RefactoringStatus.create(Status.OK_STATUS);
            }

            @Override
            public Change createChange(IProgressMonitor pm) {
                String change_name = NLS.bind(JdtUiMessages.SaveQueryEditorListener_change_name, editor_name, editorTitle);
                DocumentChange change = new DocumentChange(change_name, doc);
                TextEdit replaceEdit = new ReplaceEdit(position.x, position.y, newQuery);
                change.setEdit(replaceEdit);
                String cc_name = NLS.bind(JdtUiMessages.SaveQueryEditorListener_composite_change_name, editor_name);
                MultiStateTextFileChange mChange = new MultiStateTextFileChange(cc_name, file);
                mChange.addChange(change);
                return mChange;
            }

            @Override
            public String getName() {
                return JdtUiMessages.SaveQueryEditorListener_composite_change_name;
            }
        };
        RefactoringWizard wizard = new RefactoringWizard(ref, RefactoringWizard.DIALOG_BASED_USER_INTERFACE) {

            @Override
            protected void addUserInputPages() {
                UserInputWizardPage page = new UserInputWizardPage(wizard_title) {

                    public void createControl(Composite parent) {
                        Composite container = new Composite(parent, SWT.NULL);
                        GridLayout layout = new GridLayout();
                        container.setLayout(layout);
                        layout.numColumns = 1;
                        layout.verticalSpacing = 9;
                        Label label = new Label(container, SWT.NULL);
                        label.setText(NLS.bind(JdtUiMessages.SaveQueryEditorListener_replacequestion, editor_name, editorTitle));
                        setControl(container);
                    }
                };
                addPage(page);
            }
        };
        wizard.setWindowTitle(wizard_title);
        wizard.setDefaultPageTitle(wizard_title);
        IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (new RefactoringStarter().activate(wizard, win.getShell(), wizard_title, RefactoringSaveHelper.SAVE_ALL)) {
            query = newQuery;
            position.y = query.length();
            fromEditorPart.doSave(null);
        } else {
            if (editor.getDocumentProvider() instanceof TextFileDocumentProvider) {
                ((TextFileDocumentProvider) editor.getDocumentProvider()).setCanSaveDocument(editor.getEditorInput());
            }
        }
    }
}
Also used : MultiStateTextFileChange(org.eclipse.ltk.core.refactoring.MultiStateTextFileChange) UserInputWizardPage(org.eclipse.ltk.ui.refactoring.UserInputWizardPage) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IFile(org.eclipse.core.resources.IFile) Composite(org.eclipse.swt.widgets.Composite) Label(org.eclipse.swt.widgets.Label) RefactoringStarter(org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter) DocumentChange(org.eclipse.ltk.core.refactoring.DocumentChange) TextFileDocumentProvider(org.eclipse.ui.editors.text.TextFileDocumentProvider) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) GridLayout(org.eclipse.swt.layout.GridLayout) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) RefactoringWizard(org.eclipse.ltk.ui.refactoring.RefactoringWizard) IFileEditorInput(org.eclipse.ui.IFileEditorInput) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) HQLEditor(org.hibernate.eclipse.hqleditor.HQLEditor) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

HQLEditor (org.hibernate.eclipse.hqleditor.HQLEditor)4 IEditorPart (org.eclipse.ui.IEditorPart)3 QueryInputModel (org.hibernate.console.QueryInputModel)3 Test (org.junit.Test)3 IDocument (org.eclipse.jface.text.IDocument)2 ArrayList (java.util.ArrayList)1 IFile (org.eclipse.core.resources.IFile)1 IPath (org.eclipse.core.runtime.IPath)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Path (org.eclipse.core.runtime.Path)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)1 RefactoringStarter (org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter)1 ActionContributionItem (org.eclipse.jface.action.ActionContributionItem)1 IContributionItem (org.eclipse.jface.action.IContributionItem)1 IToolBarManager (org.eclipse.jface.action.IToolBarManager)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 DocumentChange (org.eclipse.ltk.core.refactoring.DocumentChange)1 MultiStateTextFileChange (org.eclipse.ltk.core.refactoring.MultiStateTextFileChange)1