Suggest a feature
×

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 12:55 PM
Modified: Nov 1, 2018 12:57 PM
Views: 2
Tags: no tags
  1. package com.tagmycode.netbeans;
  2.  
  3.  
  4. import com.tagmycode.plugin.Framework;
  5. import com.tagmycode.sdk.model.Snippet;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import javax.swing.text.JTextComponent;
  9. import org.netbeans.modules.editor.NbEditorUtilities;
  10. import org.openide.awt.ActionID;
  11. import org.openide.awt.ActionReference;
  12. import org.openide.awt.ActionReferences;
  13. import org.openide.awt.ActionRegistration;
  14. import org.openide.cookies.EditorCookie;
  15. import org.openide.util.NbBundle.Messages;
  16.  
  17. @ActionID(
  18.         category = "Edit",
  19.         id = "com.tagmycode.netbeans.CreateSnippetAction"
  20. )
  21. @ActionRegistration(
  22.         iconBase = "com/tagmycode/netbeans/resources/create.png",
  23.         displayName = "#CTL_CreateSnippetAction"
  24. )
  25. @ActionReferences({
  26.     @ActionReference(path = "Menu/Edit", position = 2250),
  27.     @ActionReference(path = "Toolbars/TagMyCode", position = 0),
  28.     @ActionReference(path = "Editors/Popup", position = 4040, separatorBefore = 4035, separatorAfter = 4045),
  29.     @ActionReference(path = "Shortcuts", name = "DOS-T")
  30. })
  31. @Messages("CTL_CreateSnippetAction=Add Snippet on TagMyCode...")
  32. public final class CreateSnippetAction implements ActionListener {
  33.  
  34.     private final EditorCookie context;
  35.     private final Framework framework;
  36.     private final TagMyCodeTopComponent topcomponent;
  37.  
  38.     public CreateSnippetAction(EditorCookie context) {
  39.         this.context = context;
  40.         this.topcomponent = TagMyCodeTopComponent.getInstance();
  41.         this.framework = topcomponent.getFramework();
  42.     }
  43.  
  44.     @Override
  45.     public void actionPerformed(ActionEvent ev) {
  46.         if (!framework.canOperate()) {
  47.             return;
  48.         }
  49.  
  50.         JTextComponent component = context.getOpenedPanes()[0];
  51.         org.openide.loaders.DataObject dataObject = NbEditorUtilities.getDataObject(context.getDocument());
  52.         final String displayName = dataObject.getNodeDelegate().getDisplayName();
  53.        
  54.         Snippet snippet = new Snippet();
  55.         snippet.setTitle(displayName);
  56.         snippet.setCode(getCode(component));
  57.         snippet.setLanguage(framework.getData().getLanguages().findByFileName(displayName));
  58.         framework.showSnippetDialog(snippet);
  59.     }
  60.  
  61.     private String getCode(JTextComponent component) {
  62.         String code = component.getSelectedText();
  63.         if (code == null || code.isEmpty()) {
  64.             code = component.getText();
  65.         }
  66.         return code;
  67.     }
  68. }
  69.