×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Andro Max
Added: Nov 1, 2018 1:00 PM
Views: 3523
Tags: no tags
  1. package com.tagmycode.netbeans;
  2.  
  3. import com.tagmycode.plugin.Framework;
  4. import com.tagmycode.plugin.FrameworkConfig;
  5. import com.tagmycode.sdk.DbService;
  6. import com.tagmycode.sdk.SaveFilePath;
  7. import com.tagmycode.sdk.authentication.TagMyCodeApiProduction;
  8. import com.tagmycode.sdk.exception.TagMyCodeException;
  9. import java.awt.BorderLayout;
  10. import java.awt.Frame;
  11. import java.io.IOException;
  12. import java.math.BigInteger;
  13. import java.security.SecureRandom;
  14. import java.sql.SQLException;
  15. import org.netbeans.api.settings.ConvertAsProperties;
  16. import org.openide.awt.ActionID;
  17. import org.openide.awt.ActionReference;
  18. import org.openide.util.NbBundle.Messages;
  19. import org.openide.windows.TopComponent;
  20. import org.openide.windows.WindowManager;
  21.  
  22. @ConvertAsProperties(
  23.         dtd = "-//com.tagmycode.netbeans//TagMyCode//EN",
  24.         autostore = false
  25. )
  26. @TopComponent.Description(
  27.         preferredID = "TagMyCodeTopComponent",
  28.         iconBase = "com/tagmycode/netbeans/resources/tagmycode.png",
  29.         persistenceType = TopComponent.PERSISTENCE_ALWAYS
  30. )
  31. @TopComponent.Registration(mode = "output", openAtStartup = true)
  32. @ActionID(category = "Window", id = "com.tagmycode.netbeans.TagMyCodeTopComponent")
  33. @ActionReference(path = "Menu/Window" /*, position = 333 */)
  34. @TopComponent.OpenActionRegistration(
  35.         displayName = "#CTL_TagMyCodeAction",
  36.         preferredID = "TagMyCodeTopComponent"
  37. )
  38. @Messages({
  39.     "CTL_TagMyCodeAction=TagMyCode",
  40.     "CTL_TagMyCodeTopComponent=TagMyCode",
  41.     "HINT_TagMyCodeTopComponent=This is a TagMyCode window"
  42. })
  43. public final class TagMyCodeTopComponent extends TopComponent {
  44.  
  45.     private Framework framework;
  46.  
  47.     public TagMyCodeTopComponent() {
  48.         initTagMyCode();
  49.         initComponents();
  50.         add(framework.getMainWindow().getMainComponent(), BorderLayout.CENTER);
  51.  
  52.         revalidate();
  53.         repaint();
  54.  
  55.         setName(Bundle.CTL_TagMyCodeTopComponent());
  56.         setToolTipText(Bundle.HINT_TagMyCodeTopComponent());
  57.     }
  58.  
  59.     private void initTagMyCode() {
  60.         try {
  61.             DbService dbService = new DbService(new SaveFilePath(getOrCreateNamespace()));
  62.    
  63.             FrameworkConfig frameworkConfig = new FrameworkConfig(new PasswordKeyChain(), dbService, new MessageManager(), new TaskFactory(), new NetBeansVersion(), getMainFrame());
  64.             framework = new Framework(new TagMyCodeApiProduction(), frameworkConfig, new Secret());
  65.             framework.start();
  66.         } catch (IOException | SQLException | TagMyCodeException e) {
  67.             throw new RuntimeException(e);
  68.         }
  69.     }
  70.  
  71.     public Framework getFramework() {
  72.         if (framework == null) {
  73.             initTagMyCode();
  74.         }
  75.         return framework;
  76.     }
  77.  
  78.     public static TagMyCodeTopComponent getInstance() {
  79.         TopComponent comp = WindowManager.getDefault()
  80.                 .findTopComponent("TagMyCodeTopComponent");
  81.         return TagMyCodeTopComponent.class.cast(comp);
  82.     }
  83.  
  84.     /**
  85.      * This method is called from within the constructor to initialize the form.
  86.      * WARNING: Do NOT modify this code. The content of this method is always
  87.      * regenerated by the Form Editor.
  88.      */
  89.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  90.     private void initComponents() {
  91.  
  92.         setLayout(new java.awt.BorderLayout());
  93.     }// </editor-fold>//GEN-END:initComponents
  94.  
  95.     // Variables declaration - do not modify//GEN-BEGIN:variables
  96.     // End of variables declaration//GEN-END:variables
  97.     @Override
  98.     public void componentOpened() {
  99.         // TODO add custom code on component opening
  100.     }
  101.  
  102.     @Override
  103.     public void componentClosed() {
  104.     }
  105.  
  106.     void writeProperties(java.util.Properties p) {
  107.         // better to version settings since initial version as advocated at
  108.         // http://wiki.apidesign.org/wiki/PropertyFiles
  109.         p.setProperty("version", "1.0");
  110.     }
  111.  
  112.     void readProperties(java.util.Properties p) {
  113.         String version = p.getProperty("version");
  114.     }
  115.  
  116.     public Frame getMainFrame() {
  117.         return WindowManager.getDefault().getMainWindow();
  118.     }
  119.  
  120.     private String getOrCreateNamespace() {
  121.         NetbeansPreferences preferences = new NetbeansPreferences();
  122.         String profile = preferences.read("profile");
  123.         if (profile.length() == 0) {
  124.             // TODO move to framework
  125.             SecureRandom random = new SecureRandom();
  126.             profile = new BigInteger(130, random).toString(32);
  127.             preferences.write("profile", profile);
  128.             Framework.LOGGER.info("profile id: " + profile);
  129.         }
  130.         return "netbeans-" + profile;
  131.     }
  132. }
  133.