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

  • 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.

  • What I have tried now:

    I have moved my code to a timer_tick event on a non-modal form to give me time to enter various dialogs and browse dialogs.

    • Open Project
    • Project settings
    • Browse for TM / TB in Project Settings
    • New Project=>Browse save folder...

    The behaviour was consistent throughout: Upon start, the current project was also the selected project.

    When selecting a different project (single click) and entering Project settings, although I was in the Settings of said project, the "Current project" was still the bold marked one, the one I was in the settings of was the "Selected" project.

     

    So, if I understand you correctly, you wish to know the origin of the "Browse" dialog? From within Project Settings, this is always the Selected project, no matter whether or not it is also the "Current" one. From some "New Project" dialog, I fail to see the need for this. It might be more fruitful if you described what you actually want to achieve. Here, you may want to try to achieve something you don't really need.

  • Thanks to both of you for checking on this.

    Andreas: sadly, your generalization isn't true. If you open the Project Settings dialog from the button in the top ribbon, if you're in any view other than the Projects view (e.g., the File view), the project settings you're editing are the ones for the current project, not necessarily the selected one. This is the problem I have.

    You are correct that the "New Project" dialog issue is a bit of a red herring, but not completely; I need to know that I'm in a Browse() method that is called from that dialog so I don't bother checking either the selected or current project, since it's not yet a project.

    The problem I'm trying to solve is definitely an unusual one. For various reasons, the folks I'm working for have set up an automatic translation broker, behind which they've installed several MT tools. The translation provider plugin I'm writing is for this broker. Because of how Trados works, it's simplest to treat each of the MT tools behind the broker as a separate translation provider. So I'm trying to set up the Browse() method so that the dialog it presents allows the user to select which MT tools she wants, and it would be ideal at that point to know which ones have already been chosen. So I want to inspect the provider list for the project that's being edited.
  • Of course. I overlooked that.
    I just tried to find some way by hooking into events; e.g. when selection changes automatically make that the current project - alas, no setter.
    Short of creating your own Project Settings dialog that is to be used exclusively, I fear you may be stuck with using hwnd / your "horrible and undocumented" workaround. :-(
  • Thanks to both of you for your time and attention. I don't have a lot of experience with the Trados API, and it's good to get some independent confirmation.