How can I tell which project is active?

Hi all -

I have a need to find out something about the active project, which I define as either:

- the project I'm currently creating, if I'm in the "New Project" dialog

- the project that I edit if I press the "Project Settings" button on the top ribbon, if I'm anywhere else

How do I find this out reliably in the API? My first idea was to retrieve the ProjectsController as follows:

ProjectsController c = SdlTradosStudio.Application.GetController<ProjectsController>();

and then grab the CurrentProject; but this is not necessarily the project that I'm lookin at in the project settings window. My next idea was to grab the first element from the SelectedProjects list; but this may be empty if no project is selected in the projects list. And if it IS selected, it's not the project in the New Project dialog, for obvious reasons.

If it's not possible to find the active project as I define it above, is it possible to figure out what view or dialog is active? That is, can I tell whether I'm in the New Project dialog, or in the Projects or Files views?

Thanks in advance -

Sam Bayer

Parents
  • Hi there,

    I just did a quick check by simply adding a little code to one of my plugins like this:

    var controller = SdlTradosStudio.Application.GetController<ProjectsController>();
    var cur = controller.CurrentProject;
    var sel = controller.SelectedProjects.FirstOrDefault();
    var curname = cur != null ? cur.GetProjectInfo().Name : "NULL";
    var selname = sel != null ? sel.GetProjectInfo().Name : "NULL";

    MessageBox.Show($"Current project: {curname}\r\nSelected: {selname}");

     

    Then I double clicked one project in the projects list to make it bold and current. I double clicked to open files view, then opened that file in editor.

    Then I triggered my code from projects view, from files view, and from editor. In all cases it always showed me the one bold formatted project, whose file is displayed in the files view as well as in the editor as "Current" and any project that I had selected by single clicking it in the projects view as the "Selected". When I had not selected any (click in an empty area in projectlist to clear selection) it displayed my current project as usual and NULL for selected project.

    So, yes, there is a clear distinction, and yes, you can rely on it pretty well.

     

    Hope this helps.
    Andreas

  • I've been playing with these various ways of accessing the dialog, and I think you may have missed a crucial case.

    If you double-click in the projects view, you'll make a project active. But that's not the only action you can take in the projects view; you can also single click on a project to make it selected. In that case, the selected project and the active/current project are different. Now, if you press "Project Settings" in the projects view, you edit the selected (not active/current) project, and if you press "Project Settings" while you're in the file view, you edit the active/current (not selected) project. But when you're in the Browse() method, there seems to be no way of programmatically distinguishing between the two cases.
  • Former Member
    0 Former Member in reply to Sam Bayer
    Let me try with my own fingers.
    Can you post your 'Browse()' part ?
  • I appreciate the offer. Let's see if I can snip the relevant portion of the code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Sdl.LanguagePlatform.Core;
    using Sdl.LanguagePlatform.TranslationMemory;
    using Sdl.LanguagePlatform.TranslationMemoryApi;
    using Sdl.ProjectAutomation.Core;
    using Sdl.ProjectAutomation.FileBased;
    using Sdl.TranslationStudioAutomation.IntegrationApi;

    namespace MySDLPlugin
    {
        [TranslationProviderWinFormsUi(Id = "Translation_Provider_Plug_inWinFormsUI",
                                       Name = "Translation_Provider_Plug_inWinFormsUI",
                                       Description = "Translation_Provider_Plug_inWinFormsUI")]
        class MyTranslationProviderWinFormsUI : ITranslationProviderWinFormsUI
        {

            public ITranslationProvider[] Browse(IWin32Window owner, LanguagePair[] languagePairs, ITranslationProviderCredentialStore credentialStore)
            {
                        ProjectsController c = SdlTradosStudio.Application.GetController<ProjectsController>();

                // Method A: use reflection to coerce the "owner" to yield the
                // title of the dialog, and use it to match the name of the project.


                dynamic tlc = owner.GetType().GetProperty("TopLevelControl").GetValue(owner);
                String tlTitle = (String) tlc.GetType().GetProperty("Title").GetValue(tlc);

                FileBasedProject selectedProject = null;
                TranslationProviderConfiguration tpc = null;

                foreach (FileBasedProject pr in c.GetAllProjects())
                {
                    string pName = pr.GetProjectInfo().Name;
                    if ("Project Settings - " + pName == tlTitle)
                    {
                        selectedProject = pr;
                        tpc = selectedProject.GetTranslationProviderConfiguration();
                        break;
                    }
                }
                // Method B: use the current or selected projects.
                tpc = c.SelectedProjects.First().GetTranslationProviderConfiguration(); // or
                tpc = c.CurrentProject.GetTranslationProviderConfiguration();
                ...
            }
        }
    }

    Method A works, but it's horrid and undocumented and I can't believe it's the only way to do it. Method B will reliably get the configuration of the selected project, if there is one (and there isn't always one), or the configuration of the current project, if there is one (and there isn't always one). And if there are both a CurrentProject and a SelectedProjects list of length 1, there's no way to know which one the Browse() method was invoked for, because, as I noted, in the situation where there is both a current/active project and a single selected project in the Projects view that isn't the active one, both CurrentProject and SelectedProjects will be populated and the Project Settings button in the ribbon will edit the CurrentProject if you're in the Files view and the single element in SelectedProjects if you're in the Projects view.

    Thanks for your attention.

  • Former Member
    0 Former Member in reply to Sam Bayer

    I've tried but failed.
    I do not know how to use Browse instance ('owner' part).

    Feels like a waste of time.
    Nothings unclear here. But, it is sucks clearly.
    Project Settings Configuration Icon do sucks.

    If you want to use that pop up, you must select a single "Selected project" strictly at Projects View.
    If you have "2 or more Selected  projects" or "0 Selected project" it goes to InActive at Projects View.

    You have to know different rule for Files View and Editor View.
    In these views, selection does not matter at all.
    It show you "Current/Active project" all the time.

Reply
  • Former Member
    0 Former Member in reply to Sam Bayer

    I've tried but failed.
    I do not know how to use Browse instance ('owner' part).

    Feels like a waste of time.
    Nothings unclear here. But, it is sucks clearly.
    Project Settings Configuration Icon do sucks.

    If you want to use that pop up, you must select a single "Selected project" strictly at Projects View.
    If you have "2 or more Selected  projects" or "0 Selected project" it goes to InActive at Projects View.

    You have to know different rule for Files View and Editor View.
    In these views, selection does not matter at all.
    It show you "Current/Active project" all the time.

Children
No Data