Saturday, November 17, 2007

Tips for Intellij IDEA plugins developers (my bloody experience)

1. How to get project with new API

Old fashion:


Project project = (Project)dataContext.getData(DataConstants.PROJECT);


New fashion:


Project project = DataKeys.PROJECT.getData(dataContext);


2. How to register tool window with new API


// Get toolwindow manager
ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);

// Register your tool window at desired location
ToolWindow toolWindow =
        toolWindowManager.registerToolWindow("your_toolwindow_id", true, ToolWindowAnchor.RIGHT);

// Prepare content panel of your tool window (all your controls)
JPanel yourContentPanel = ...

// Get peer factory
PeerFactory peerFactory = PeerFactory.getInstance();

// Create content
Content content = peerFactory.getContentFactory().createContent(yourContentPanel, "", false);

// Add created content
toolWindow.getContentManager().addContent(content);


3. How to persist plugin properties with new API

If you did it for previous versions of IDEA, you probably have the following code:


public class YourPluginConfiguration
       implements ProjectComponent, Configurable, JDOMExternalizable {
  private final String COMPONENT_NAME = ...;
 
  publuc String yourProperty; // property to be persisted

  public String getComponentName() {
    return COMPONENT_NAME;
  }

  public void readExternal(Element element) throws InvalidDataException {
    DefaultJDOMExternalizer.readExternal(this, element); // load
  }

  public void writeExternal(Element element) throws WriteExternalException {
    DefaultJDOMExternalizer.writeExternal(this, element); // save
  }

}


With new API you have to do something like this:


@State(
    name = YourPluginConfiguration.COMPONENT_NAME,
    storages = {@Storage(id = "your_id", file = "$PROJECT_FILE$")}
)
public final class YourPluginConfiguration
          implements ProjectComponent, Configurable, PersistentStateComponent<YourPluginConfiguration> {
  public static final String COMPONENT_NAME = ...;

  publuc String yourProperty; // property to be persisted

  public String getComponentName() {
    return COMPONENT_NAME;
  }

  public YourPluginConfiguration getState() {
    return this; // load
  }

  public void loadState(YourPluginConfigurationstate) {
    XmlSerializerUtil.copyBean(state, this); // save
  }

}


In this example we use $PROJECT_FILE$ macro. You have to use appropriates macros according to component level.

There are 3 types of component level. Each of them has unique, only for this level macros:

- application-level components (ApplicationComponent): $APP_CONFIG$, $OPTIONS$;
- project-level components (ProjectComponent): $PROJECT_FILE$, $WORKSPACE_FILE$, $PROJECT_CONFIG_DIR$;
- module-level components (ModuleComponent): $MODULE_FILE$


4. How to register intention action

Usually intention action should be assoсiated with some analytical tool that recognizes important situation and indicates about it the user. We also can assoсiate intention action with changes in the editor.

First, your class should implement IntentionAction. Additionally, if you want to listen to editor changes,it should extend EditorAction:


public class YourIntentionAction extends EditorAction implements IntentionAction {

  public YourIntentionAction() {
    super(new YourEditorActionHandler());
  }

  private static class YourEditorActionHandler extends EditorActionHandler /* or EditorWriteActionHandler */ {

    // This method is executed every time you do modifications in the editor.
    // Wev are trying to look for selections
    public void execute(Editor editor, DataContext dataContext) {
      String searchText = null;

      SelectionModel selection = editor.getSelectionModel();
      if (selection.hasSelection()) {
        searchText = selection.getSelectedText();
      }

      if (searchText != null && searchText.trim().length() > 0) {
        // Displays pop up

        Project project = DataKeys.PROJECT.getData(dataContext);

        showPopup(project, editor, searchText);
      }
    }
  }

 /**
   * Returns text to be shown in the list of available actions, if this action
   * is available.
   */
  @NotNull
  public String getText() {
    return "The text to show in the intention popup.";
  }

  /**
   * Returns the identifier of the family of intentions. This id is used to externalize
   * "auto-show" state of intentions. When user clicks on a lightbulb in intention list,
   * all intentions with the same family name get enabled/disabled. The identifier
   * is also used to locate the description and preview text for the intention.
   */
  @NotNull
  public String getFamilyName() {
    return "popup";
  }

  /**
   * Checks whether this intention is available at a caret offset in file.
   * If this method returns true, a light bulb for this intention is shown.
   */
  public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    return editor.getSelectionModel().hasSelection();
  }

  /**
   * Called when user invokes intention. This method is called inside command.
   * If {@link #startInWriteAction()} returns true, this method is also called
   * inside write action.
   */
  public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    String searchText = null;

    SelectionModel selection = editor.getSelectionModel();

    if (selection.hasSelection()) {
      searchText = selection.getSelectedText();
    }

    showPopup(project, editor, searchText);
  }

  /**
   * Indicate whether this action should be invoked inside write action.
   * Should return false if e.g. modal dialog is shown inside the action.
   * If false is returned the action itself is responsible for starting write action
   * when needed, by calling {@link com.intellij.openapi.application.Application#runWriteAction(Runnable)}.
   */
  public boolean startInWriteAction() {
    return false;
  }

 private static void showPopup(Project project, Editor editor, String searchText) {
    JPanel panel = ... // Create your content here

    JBPopup jbPopup = JBPopupFactory.getInstance()
        .createComponentPopupBuilder(panel, panel)
        .setDimensionServiceKey(project, "popup", false)
        .setRequestFocus(true)
        .setResizable(true)
        .setMovable(true)
        .setTitle("Your title")
        .createPopup();

    jbPopup.showInBestPositionFor(editor);
  }

}


You have to register your intention action. If you have project component, you can do it
in projectOpened() method:


  public void projectOpened() {
    IntentionManager.getInstance().addAction(new YourIntentionAction());
  }


Useful links

1. Tutorial

2. Plugin Development FAQ

3. The Basics of Plugin Development for IntelliJ IDEA

4. IntelliJ IDEA 7.x Developers Documentation

5. IntelliJ Developing Custom Language Plugins for IntelliJ IDEA 5.0

28 comments:

Jacek Jaroczynski said...

Great article! There is very little information about new API so that short article is really helpful!!! Thanks!

Guy said...

Your post was a great help to me! Thanks man!

Anonymous said...

Do you like playing the game where you need to use flyff penya, when you do not have flyff money, you must borrow flyff gold from friends, or you buy flyff penya. If you get cheap penya, you can continue this game.
Do you like playing the game where you need to use priston tale Gold, when you do not have priston tale Money, you must borrow it from friends, or you buy priston tale Gold. If you getcheap priston tale Gold, you can continue this game.

Anonymous said...

Now do you worried about that in the game do not had enough second life linden to play the game, now you can not worried, my friend told me a website, in here you can buy a lot linden dollars and only spend a little money, do not hesitate, it was really, in here we had much secondlife money, we can sure that you will get the cheap linden, quick to come here to buy lindens.

Now do you worried about that in the game do not had enough requiem gold to play the game, now you can not worried, my friend told me a website, in here you can buy a lot requiem lant and only spend a little money, do not hesitate, it was really, in here we had much requiem money, we can sure that you will get the cheap requiem lant, quick to come here to buy requiem online gold.

Adi said...

Find Internet Marketing resource hare Online Marketing Strategy | Internet Marketing Tools | Online Marketing Campaign | Online Marketing Business | Online Marketing System | Online Business | Online Home Business | Online Business Tips | Internet Marketing Online

Anonymous said...

It was extremely interesting for me to read that blog. Thanx for it. I like such topics and everything that is connected to them. I definitely want to read a bit more soon.

Daniela said...

Interesting post. I have been wondering about this issue,so thanks for posting. I’ll likely be coming back to your blog. Keep up great writing. Find your great Travel News and sing the songs at Free Song Lyric or you can watch the drama at Korea Drama Online one of great korea drama is A Love to Kill if you go to travel to Indonesia learn Learn Indonesia Language first! And find your home cari rumah or make a blog Belajar membuat Blog find your home again rumah dijual and again at jual rumah then if you want buy a new laptop see the Laptop Price List or you can buy a New Blackberry and then take care your Health & Jewerly.

Anonymous said...

Hello everyone! Who knows where to upload the film Avatar?
I even bought the film Avatar for a SMS to http://rsskino.ru/kinofilm/avatar.html , the link was, but download fails, the system will boot quite strange cocoa something.
Men, advise where to normal as quickly download film avatar?

Anonymous said...

Don't stop posting such articles. I love to read blogs like this. BTW add some pics :)

Anonymous said...

[url=http://vegasonlines.net/casino-royal-lloret-de-mar.html]comercio digital escasino bonus [/url]
[url=http://vegasonlines.net/mystic-lake-casino.html]eurobet casino [/url]
[url=http://vegasonlines.net/casino-careers.html]casino terribles vegas [/url]
[url=http://vegasonlines.net/mount-airy-casino.html]casino resort rio vegas [/url]
[url=http://vegasonlines.net/casino-slot-games.html]mirage vegas casino [/url]
[url=http://www.netknowledgenow.com/members/casino_2D00_gamblings_2D00_online.aspx]pop 9 casino game [/url]
[url=http://vegasonlines.net/fort-mcdowell-casino.html]banda sonora casino royal [/url]
[url=http://vegasonlines.net/argosy-casino-kansas.html]bingos y casinos [/url]
[url=http://vegasonlines.net/south-point-casino.html]stardust casino vegas [/url]
[url=http://vegasonlines.net/oregon-casino.html]san juan intercontinental resort casino san juan [/url]
[url=http://vegasonlines.net/casino-st-louis-mo.html]best online casino directory [/url]
[url=http://vegasonlines.net/beau-rivage-resort-casino.html]casino streep poker [/url]
[url=http://vegasonlines.net/casino-in-louisiana.html]casino agua caliente tijuana [/url]
[url=http://vegasonlines.net/accommodation-casino.html]sabadell casino sevilla [/url]
[url=http://vegasonlines.net/riviera-casino.html]juego casino internet ruleta [/url]
linea casino
[b]fallsview casino niagara fall canada[/b]
descarga juego on line casino gratis
casinos online en espana
[b]black jack casino game[/b]
jeu casino
[u]add link online casino[/u]
sistemas casino
casinos en montevideo
[b]online casino[/b]
casino virtual pagina web
trucos casino

Anonymous said...

I sell a boat-program which will help you to outwit auction and to win, initially the boat was created for the Scandinavian auction http://internet-aukcion.ru/ but now the program can work with similar auctions: gagen ru, vezetmne ru and with ten.
The program-boat stakes for you, i.e. for this purpose it is not necessary to sit constantly at the monitor. The boat can set time when it is necessary to stake, thus you as much as possible will lower expenses for rates, and as much as possible increase the chances of a victory.

The price of the program a boat for the Scandinavian auctions 20$

For the first 10 clients the price 15$

To all clients free updating and support.

Behind purchases I ask in icq: 588889590 Max.

Anonymous said...

[b]Set software LoveBots v 5.2[/b]

All for a mass mailing dating http://24lux.ru/

The script is written in php5

Features:

[i]registration, account activation
manual input captures, or the solution through antikapchu
filling data accounts:
- Gulf desired photo
- Инфы about yourself
- Diary
- Sexual preference[/i]

gulyalka on questionnaires spammer on lichku
- Randomization Posts: replacement of Russian letters in Latin analogues

optimized to work in a continuous loop
check-activation-filling-spam check ..

Updates and support free of charge.

Price per set 100 wmz

For the first 10 buyers price 70 wmz (your feedback on the software).

For shopping I ask in icq: 588889590 Max.

Scrin program:

[IMG]http://i066.radikal.ru/1002/9d/a7a68e8c96ee.jpg[/IMG]

[IMG]http://i054.radikal.ru/1002/19/9db76967c0e5.jpg[/IMG]

[IMG]http://s003.radikal.ru/i202/1002/24/20716e86512e.jpg[/IMG]

Flooding in the subject no! Write to feedback after the purchase.

Anonymous said...

[url=http://seghan.ru/go.php?sid=35][img]http://s003.radikal.ru/i203/1001/17/1008f12c7936.jpg[/img][/url]












[url=http://members.multimania.co.uk/fjctuke/]buy cigarette cards [/url]
buy cigarettes online from the states buy cigarette tubes aqua filter cigarette holders buy online
[url=http://members.multimania.co.uk/laqoaom/]buy cigarette filters [/url]
buying cigarettes over the enternet buy carlton menthol 100 cigarettes where can you buy rockets cigarettes
[url=http://members.multimania.co.uk/eomuefm/]order newport cigarettes [/url]
were to buy charter menthol cigarettes to buy e cigarette buying cigarette receptacles calgary alberta
[url=http://members.multimania.co.uk/goxiucl/]buy cigarettes with check account [/url]
buying cigarettes online buy marlboro menthol cigarettes buying cigarettes on line
[url=http://members.multimania.co.uk/motluka/]buy malboro cigarettes [/url]
cigarettes native indian mail order alternative cigaretts buy buy barclay 100 cigarettes
[url=http://mitglied.multimania.de/mfoyjqw/]buy cigarettes online new york [/url]
safe site to buy cigarettes online buy some candy and cigarettes lyrics buy cigarettes in kentucky
[url=http://mitglied.multimania.de/hnlgiui/]how to order cigarettes online [/url]
cannabis cigarettes buy buy smokeless cigarettes uk law buying cigarette lighter

Anonymous said...

These online sports betting become widely patronized especially among the global masses. Since the globe contains a huge number of masses compared to the other two social statuses, the online sports betting is pouring a lot of money compared to Las Vegas - the betting capital of the world. [url=http://www.pulsebet.com/bg/]bet online[/url] [url=http://www.pulsebet.com/da/]bet online[/url] [url=http://www.pulsebet.com/de/]bet online[/url] [url=http://www.pulsebet.com/es]apuestas[/url] [url=http://www.pulsebet.com/fr/]jouer en ligne[/url] [url=http://www.pulsebet.com/gr]bet online[/url] [url=http://www.pulsebet.com/jp]bet online[/url] [url=http://www.pulsebet.com/nl]bet online[/url] [url=http://www.pulsebet.com/pl]bet online[/url] [url=http://www.pulsebet.com/pt]bet online[/url] [url=http://www.pulsebet.com/ro]bet online[/url] [url=http://www.pulsebet.com/ru]bet online[/url] [url=http://www.pulsebet.com/se]bet online[/url] [url=http://www.pulsebet.com/it]bet online[/url] [url=http://www.pulsebet.com/cn]bet online[/url] When it comes time to place your wager, you should know how to bet the two most common types of wagers which is against the spread and betting the over/under. http://www.pulsebet.com/bg/ http://www.pulsebet.com/da/ http://www.pulsebet.com/de/ http://www.pulsebet.com/es/ http://www.pulsebet.com/fr/ http://www.pulsebet.com/gr/ http://www.pulsebet.com/jp/ http://www.pulsebet.com/nl/ http://www.pulsebet.com/pl/ http://www.pulsebet.com/pt/ http://www.pulsebet.com/ro/ http://www.pulsebet.com/ru/ http://www.pulsebet.com/se/ http://www.pulsebet.com/it/ http://www.pulsebet.com/cn/

dfadf said...

Microsoft Office 2010
Office 2010
Office 2007
Microsoft Office
Microsoft Office 2007
Office 2007 key
Office 2007 download
Office 2007 Professional
Windows 7
Microsoft outlook 2010

power2balance said...

power balance
winter boots
Movado Bold
Baby Carriers
ecco shoes
Bakugan Toys
coach outlet store online purses 
Coach Outlet Store
2011 Coach Outlet Store Online
Coach Purses
coach outlet 
Coach Leather Hangbags
Cheap Coach Purses Wholesale 
Cheap Coach Bags Store
Coach Shoulder bags Outlet 
Coach Crossbody leather bags 
Coach Wallets Outlet
Coach Business Bags
Cheap Coach Backpacks 
Coach Sling Bags
Coach Hobos Bags 

Invertir en oro said...

hello, i think that this post is the best that i have read.

buy Crestor said...

greatest post I have seen.

hosted virtual call center said...

Thanks for the list, just submitted to them. Keep up the good work.

Anonymous said...


Viagra Online[url=http://www.viagra.gd] Viagra Online Without Prescription [/url]http://www.viagra.gd
Cialis[url=http://www.cialis.gd]Cialis[/url]http://www.cialis.gd
Cheap Viagra[url=http://www.viagra-cheap.us]Viagra [/url]http://www.viagra-cheap.us
Cialis [url=http://www.nonprescriptioncialis.com] Non Prescription Cialis[/url]http://www.nonprescriptioncialis.com

Anonymous said...


Klonopin[url=http://www.klonopin.name]Klonopin[/url]http://www.klonopin.name
Accutane[url=http://www.accutane.ws]Accutane[/url]http://www.accutane.ws
Viagra Online Without Prescription[url=http://www.viagra-online-without-prescription.biz]Viagra Online Without Prescription[/url]http://www.viagra-online-without-prescription.biz
Valium[url=http://www.valium.name]Valium[/url]http://www.valium.name

Anonymous said...


Viagra [url=http://www.viagra.gd]Viagra [/url]http://www.viagra.gd
Cialis[url=http://www.cialis.gd]Cialis[/url]http://www.cialis.gd
Viagra[url=http://www.viagra-no-prescription.com]Viagra[/url]http://www.viagra-no-prescription.com
Cialis [url=http://www.buycialis.name] Buy Cialis[/url]http://www.buycialis.name

Anonymous said...


valium, [url=http://www.purevolume.com/drawcrib85/posts/3034376/Magnolias+-+Better+Than+Valium]valium[/url],

http://www.purevolume.com/drawcrib85/posts/3034376/Magnolias+-+Better+Than+Valium

valium, [url=http://www.iamsport.org/pg/blog/georgemeter55/read/12629278/prescription-pill-slang-every-parent-should-know]valium[/url],

http://www.iamsport.org/pg/blog/georgemeter55/read/12629278/prescription-pill-slang-every-parent-should-know

valium, [url=http://bonefarm00.posterous.com/anti-anxiety-pills-uses-and-applications-in-t]valium[/url],

http://bonefarm00.posterous.com/anti-anxiety-pills-uses-and-applications-in-t

valium, [url=http://www.zootoo.com/profile/bulbrayon35/blog/entry/eyeexamtestshouldyoustudyortak10]valium[/url],

http://www.zootoo.com/profile/bulbrayon35/blog/entry/eyeexamtestshouldyoustudyortak10

valium, [url=http://www.lagbook.com/blogs/3176073]valium[/url],

http://www.lagbook.com/blogs/3176073

Anonymous said...


Adderall Adderall Online http://www.adderall-online.org
Adderall Online Adderall Online http://www.adderallonline.biz
Cheap Viagraviagra http://www.cheapviagra.cc
Viagra Without Prescription viagrahttp://www.viagra-without-prescription.ws

Anonymous said...

Epgtqniwf [url=http://www.oakleymaxjp.com/]オークリー サングラス[/url] Vfwmjlgcu Xcwtqijhs [url=http://www.oakleymaxjp.com/]オークリー アウトレット[/url] Itstpilxq Rxjyukdwy [url=http://www.oakleymaxjp.com/]オークリー ゴルフ[/url] Ynlkcudyi Eapwvdusa [url=http://www.oakleymaxjp.com/]オークリー メガネ[/url] Vyrgfqhym Jjcgiqygo http://www.oakleymaxjp.com/ Jymyvywgx

Anonymous said...


Cheap Cialis Cheap Cialishttp://www.cheap-cialis.name

Anonymous said...

Duagegealiare Nike Justin Tucker Jersey
Duagegealiare Ray Lewis Jersey Super Bowl
usargoruist Jacoby Jones Super Bowl Jersey

Anonymous said...

Duagegealiare Ahmad Brooks Super Bowl Jersey
Duagegealiare Randy Moss Women's Jersey
usargoruist Haloti Ngata Super Bowl Jersey