content: package org.netbeans.modules.python.statusbar;
  
  import java.awt.EventQueue;
  import java.io.IOException;
  import java.util.Collection;
  import javax.swing.JLabel;
  import org.netbeans.api.options.OptionsDisplayer;
  import org.netbeans.api.project.FileOwnerQuery;
  import org.netbeans.api.project.Project;
  import org.netbeans.modules.python.PythonUtility;
  import org.netbeans.modules.python.packagemanager.PythonPackagesModel;
  import org.netbeans.modules.python.project.PythonProject;
  import org.openide.filesystems.FileObject;
  import org.openide.loaders.DataObject;
  import org.openide.util.Exceptions;
  import org.openide.util.Lookup;
  import org.openide.util.LookupEvent;
  import org.openide.util.LookupListener;
  import org.openide.util.RequestProcessor;
  import org.openide.util.Utilities;
  import org.openide.windows.TopComponent;
  import org.openide.windows.WindowManager;
  
  /**
   *
   * @author albilu
   */
  public final class PythonStatusBarPanel extends javax.swing.JPanel {
  
      private static final long serialVersionUID = 1L;
      static Lookup.Result<DataObject> res;
      RequestProcessor requestProcessor = new RequestProcessor(this.getClass().getName(), 1);
      public static String currentPyPath = "";
  
      /**
       * Creates new form PythonStatusBarPanel
       */
      public PythonStatusBarPanel() {
          initComponents();
          res = Utilities.actionsGlobalContext().lookupResult(DataObject.class);
          res.addLookupListener(lookupListener);
          try {
              String platformPythonExe = PythonUtility.getPlatformPythonExe();
              statusLabel.setText(PythonUtility.getVersion(platformPythonExe));
              statusLabel.setToolTipText(platformPythonExe);
          } catch (IOException ex) {
              Exceptions.printStackTrace(ex);
          }
      }
  
      /**
       * This method is called from within the constructor to initialize the form.
       * WARNING: Do NOT modify this code. The content of this method is always
       * regenerated by the Form Editor.
       */
      @SuppressWarnings("unchecked")
      // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
      private void initComponents() {
  
          statusLabel = new javax.swing.JLabel();
          statusSeparator = new javax.swing.JSeparator();
  
          statusLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/netbeans/modules/python/python-2.png"))); // NOI18N
          org.openide.awt.Mnemonics.setLocalizedText(statusLabel, org.openide.util.NbBundle.getMessage(PythonStatusBarPanel.class, "PythonStatusBarPanel.statusLabel.text")); // NOI18N
          statusLabel.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
          statusLabel.addMouseListener(new java.awt.event.MouseAdapter() {
              public void mouseClicked(java.awt.event.MouseEvent evt) {
                  statusLabelMouseClicked(evt);
              }
          });
  
          statusSeparator.setOrientation(javax.swing.SwingConstants.VERTICAL);
  
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
          this.setLayout(layout);
          layout.setHorizontalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
                  .addGap(0, 0, 0)
                  .addComponent(statusSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addComponent(statusLabel)
                  .addContainerGap())
          );
          layout.setVerticalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(statusLabel)
              .addComponent(statusSeparator)
          );
      }// </editor-fold>//GEN-END:initComponents
  
      private void statusLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_statusLabelMouseClicked
          OptionsDisplayer.getDefault().open("PythonOptions/PyPlatform");
      }//GEN-LAST:event_statusLabelMouseClicked
  
      LookupListener lookupListener = new LookupListener() {
          @Override
          public void resultChanged(LookupEvent ev) {
              requestProcessor.execute(() -> {
                  Collection c = ((Lookup.Result) ev.getSource()).allInstances();
                  if (!c.isEmpty() && c.size() == 1/*to control multi project selection*/) {
                      try {
                          DataObject dataO = (DataObject) c.iterator().next();
                          FileObject primaryFile = dataO.getPrimaryFile();
                          boolean isPythonFile = !primaryFile.getMIMEType()
                                  .equals(PythonUtility.PYTHON_MIME_TYPE);
                          Project owner = FileOwnerQuery.getOwner(primaryFile);
                          if ((!primaryFile.isFolder() && isPythonFile)
                                  || owner == null || !(owner instanceof PythonProject)) {
                              if (isPythonFile) {
                                  setVisible(false);
                                  return;
                              }
                          }
                          setVisible(true);
                          if (owner != null && owner instanceof PythonProject) {
                              String projectPythonExe = PythonUtility.getProjectPythonExe(owner
                                      .getProjectDirectory());
                              statusLabel.setText((projectPythonExe.contains(".venv")
                                      ? "(.venv) " : "") + PythonUtility.getVersion(projectPythonExe));
                              statusLabel.setToolTipText(projectPythonExe);
                              currentPyPath = projectPythonExe;
                              statusLabel.setHorizontalTextPosition(JLabel.RIGHT);
                          } else {
                              String platformPythonExe = PythonUtility.getPlatformPythonExe();
                              statusLabel.setText(PythonUtility.getVersion(platformPythonExe));
                              statusLabel.setToolTipText(platformPythonExe);
                              currentPyPath = platformPythonExe;
                              statusLabel.setHorizontalTextPosition(JLabel.RIGHT);
                          }
                          EventQueue.invokeLater(() -> {
                              TopComponent findTopComponent = WindowManager.getDefault().findTopComponent("PythonPackageManagerTopComponent");
                              if (findTopComponent != null && findTopComponent.isOpened()) {
                                  try {
                                      PythonPackagesModel.loadInstalled(currentPyPath.isEmpty() ? PythonUtility.getPlatformPythonExe()
                                              : currentPyPath);
                                  } catch (IOException ex) {
                                      Exceptions.printStackTrace(ex);
                                  }
                              }
  
                          });
  
                      } catch (IOException ex) {
                          Exceptions.printStackTrace(ex);
                      }
                  }
              });
          }
      };
  
      // Variables declaration - do not modify//GEN-BEGIN:variables
      private static javax.swing.JLabel statusLabel;
      private javax.swing.JSeparator statusSeparator;
      // End of variables declaration//GEN-END:variables
  
  }
  