mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-28 01:07:49 +08:00
205 lines
15 KiB
HTML
205 lines
15 KiB
HTML
|
|
|
|
|
|
<!doctype html>
|
|
<html lang="en-US">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Creating an action / IntelliJ Platform SDK DevGuide</title>
|
|
<link rel="stylesheet" href="/intellij/sdk/docs/app/css/styles.min.css">
|
|
|
|
<!-- non-retina iPad pre iOS 7 -->
|
|
<link rel="apple-touch-icon" href="/intellij/sdk/docs/apple-touch-icon-72x72.png" sizes="72x72">
|
|
|
|
<!-- retina iPhone pre iOS 7 -->
|
|
<link rel="apple-touch-icon" href="/intellij/sdk/docs/apple-touch-icon-114x114.png" sizes="114x114">
|
|
|
|
<!-- retina iPad pre iOS 7 -->
|
|
<link rel="apple-touch-icon" href="/intellij/sdk/docs/apple-touch-icon-144x144.png" sizes="144x144">
|
|
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/intellij/sdk/docs/apple-touch-icon-precomposed.png">
|
|
|
|
<!-- normal favicon -->
|
|
<link rel="shortcut icon" type="image/x-icon" href="/intellij/sdk/docs/favicon.ico">
|
|
<link rel="icon" type="image/png" href="/intellij/sdk/docs/favicon.png">
|
|
<link rel="stylesheet" href="/intellij/sdk/docs/styles/styles.css"></head>
|
|
<body data-id="basics/getting_started/creating_an_action">
|
|
<div class="wrapper">
|
|
<section class="panel _nav">
|
|
<header class="panel__header">
|
|
<div class="container">
|
|
<form class="search-box">
|
|
<label for="search-box__input" class="search-box__label">
|
|
<input type="text" class="search-box__input" id="search-box__input" placeholder="Search IntelliJ Platform SDK DevGuide">
|
|
</label>
|
|
<div class="search-box__clear" title="Clear"></div>
|
|
</form>
|
|
</div>
|
|
</header>
|
|
<nav class="panel__content">
|
|
<div class="container _nav">
|
|
<menu class="nav-tree"></menu>
|
|
</div>
|
|
<div class="container _footer panel__footer">
|
|
<p><a href="https://youtrack.jetbrains.com/issues/IJSDK">Send feedback</a></p>
|
|
<p>© 2000–2016 <a href="//www.jetbrains.com">JetBrains</a> s.r.o.<br>
|
|
All rights reserved.</p>
|
|
</div>
|
|
</nav>
|
|
</section>
|
|
|
|
<main class="panel _main" role="main">
|
|
<header class="panel__header">
|
|
<div class="container">
|
|
<h3>IntelliJ Platform SDK DevGuide</h3>
|
|
|
|
<div class="shortcuts-switcher"><label for="switch-shortcuts">Keymap:</label><select id="switch-shortcuts" class="select _shortcuts" height="1">
|
|
<option data-group="primary" value="default" selected>Default</option>
|
|
<option data-group="primary" value="default_for_gnome">GNOME</option>
|
|
<option data-group="primary" value="default_for_kde">KDE</option>
|
|
<option data-group="primary" value="default_for_xwin">XWindow</option>
|
|
<option data-group="primary" value="emacs">Emacs</option>
|
|
<option data-group="primary" value="jbuilder">JBuilder</option>
|
|
<option data-group="primary" value="visual_studio">Visual Studio</option>
|
|
<option data-group="primary" value="netbeans_6.5">NetBeans 6.5</option>
|
|
<option data-group="primary" value="eclipse">Eclipse</option>
|
|
<option data-group="secondary" value="mac_os_x_10.5_">OS X 10.5+</option>
|
|
<option data-group="secondary" value="mac_os_x">OS X</option>
|
|
<option data-group="secondary" value="eclipse_mac_os_x">OS X Eclipse</option></select>
|
|
</div>
|
|
|
|
<div class="panel-trigger"></div>
|
|
</div>
|
|
</header>
|
|
<section class="panel__content">
|
|
<div class="container">
|
|
<article class="article" data-shortcut-switcher="false">
|
|
|
|
<h1>Creating an action</h1>
|
|
<p>Your plugins can customize the IntelliJ Platform UI by adding new items to the menus and toolbars. The IntelliJ Platform provides the class <a href="https://upsource.jetbrains.com/idea-ce/file/idea-ce-1731d054af4ca27aa827c03929e27eeb0e6a8366/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnAction.java" data-bypass="yes" target="_blank"><span>AnAction.java</span></a> whose <code class="code highlight language-text">actionPerformed</code> method is called each time you select a menu item or click a toolbar button.</p>
|
|
|
|
<p>To create custom actions in the <em>IntelliJ Platform</em>, you should perform two basic steps:</p>
|
|
|
|
<ol>
|
|
<li>In your plugin, define an action or a system of actions that add their own items to menus and toolbars.</li>
|
|
<li>Register your actions.</li>
|
|
</ol>
|
|
|
|
<p>This topic outlines the above steps. For detailed information and samples, refer to <a href="/intellij/sdk/docs/basics/action_system.html"><span>IntelliJ Platform Action System</span></a>.</p>
|
|
|
|
<a name="defining-actions" class="elem-anchor"></a>
|
|
<h3>Defining actions<a href="#defining-actions" class="anchor-link"><span></span></a></h3>
|
|
|
|
<p>An action is a class derived from the <code class="code highlight language-text">AnAction</code> class. To define your action, in your plugin, create a Java class derived from the <code class="code highlight language-text">AnAction</code> class. In this class, override the <code class="code highlight language-text">actionPerformed</code> method to be called when a menu item or a toolbar button is selected.</p>
|
|
|
|
<p>To clarify this procedure, consider the following code snippet that defines the <code class="code highlight language-text">TextBoxes</code> class derived from the <code class="code highlight language-text">AnAction</code> class:</p>
|
|
|
|
<pre><code class="code-block__wrapper code-block _highlighted lang_java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">TextBoxes</span> <span class="kd">extends</span> <span class="n">AnAction</span> <span class="o">{</span>
|
|
<span class="c1">// If you register the action from Java code, this constructor is used to set the menu item name</span>
|
|
<span class="c1">// (optionally, you can specify the menu description and an icon to display next to the menu item).</span>
|
|
<span class="c1">// You can omit this constructor when registering the action in the plugin.xml file.</span>
|
|
<span class="kd">public</span> <span class="nf">TextBoxes</span><span class="o">()</span> <span class="o">{</span>
|
|
<span class="c1">// Set the menu item name.</span>
|
|
<span class="kd">super</span><span class="o">(</span><span class="s">"Text _Boxes"</span><span class="o">);</span>
|
|
<span class="c1">// Set the menu item name, description and icon.</span>
|
|
<span class="c1">// super("Text _Boxes","Item description",IconLoader.getIcon("/Mypackage/icon.png"));</span>
|
|
<span class="o">}</span>
|
|
|
|
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">actionPerformed</span><span class="o">(</span><span class="n">AnActionEvent</span> <span class="n">event</span><span class="o">)</span> <span class="o">{</span>
|
|
<span class="n">Project</span> <span class="n">project</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="na">getData</span><span class="o">(</span><span class="n">PlatformDataKeys</span><span class="o">.</span><span class="na">PROJECT</span><span class="o">);</span>
|
|
<span class="n">String</span> <span class="n">txt</span><span class="o">=</span> <span class="n">Messages</span><span class="o">.</span><span class="na">showInputDialog</span><span class="o">(</span><span class="n">project</span><span class="o">,</span> <span class="s">"What is your name?"</span><span class="o">,</span> <span class="s">"Input your name"</span><span class="o">,</span> <span class="n">Messages</span><span class="o">.</span><span class="na">getQuestionIcon</span><span class="o">());</span>
|
|
<span class="n">Messages</span><span class="o">.</span><span class="na">showMessageDialog</span><span class="o">(</span><span class="n">project</span><span class="o">,</span> <span class="s">"Hello, "</span> <span class="o">+</span> <span class="n">txt</span> <span class="o">+</span> <span class="s">"!\n I am glad to see you."</span><span class="o">,</span> <span class="s">"Information"</span><span class="o">,</span> <span class="n">Messages</span><span class="o">.</span><span class="na">getInformationIcon</span><span class="o">());</span>
|
|
<span class="o">}</span>
|
|
<span class="o">}</span>
|
|
</code></pre>
|
|
|
|
<p>Note that optionally, you can define a set of classes derived from the <code class="code highlight language-text">AnAction</code> class. In this case, your plugin will define a system of actions.</p>
|
|
|
|
<a name="registering-actions" class="elem-anchor"></a>
|
|
<h3>Registering actions<a href="#registering-actions" class="anchor-link"><span></span></a></h3>
|
|
|
|
<p>Once you have defined an action or a system of actions, you must register them to specify the menu items or toolbar buttons associated with actions. You can register actions in one of the following ways:</p>
|
|
|
|
<ul>
|
|
<li>Register actions in the <code class="code highlight language-text"><actions></code> section of the <code class="code highlight language-text">plugin.xml</code> file.</li>
|
|
<li>Register actions from Java code.</li>
|
|
</ul>
|
|
|
|
<p>This section provides some examples that illustrate how to register actions. For more information, refer to <a href="/intellij/sdk/docs/basics/action_system.html"><span>IntelliJ Platform Action System</span></a>.</p>
|
|
|
|
<a name="registering-actions-in-the-pluginxml-file" class="elem-anchor"></a>
|
|
<h4>Registering actions in the plugin.xml file</h4>
|
|
|
|
<p>To register your actions, make appropriate changes to the <code class="code highlight language-text"><actions></code> section of the plugin.xml file for your IDEA project. The following fragment of the plugin.xml file adds the Sample Menu group (item) to the main menu. Clicking this item allows you to access <strong>Sample Menu | Text Boxes and Sample Menu | Show Dialog</strong> menu commands:</p>
|
|
|
|
<p><img src="img/sample_menu.jpg" alt="Sample Menu" /></p>
|
|
|
|
<pre><code class="code-block__wrapper code-block _highlighted lang_xml"><span class="nt"><actions></span>
|
|
<span class="nt"><group</span> <span class="na">id=</span><span class="s">"MyPlugin.SampleMenu"</span> <span class="na">text=</span><span class="s">"_Sample Menu"</span> <span class="na">description=</span><span class="s">"Sample menu"</span><span class="nt">></span>
|
|
<span class="nt"><add-to-group</span> <span class="na">group-id=</span><span class="s">"MainMenu"</span> <span class="na">anchor=</span><span class="s">"last"</span> <span class="nt">/></span>
|
|
<span class="nt"><action</span> <span class="na">id=</span><span class="s">"Myplugin.Textboxes"</span> <span class="na">class=</span><span class="s">"Mypackage.TextBoxes"</span> <span class="na">text=</span><span class="s">"Text _Boxes"</span> <span class="na">description=</span><span class="s">"A test menu item"</span> <span class="nt">/></span>
|
|
<span class="nt"><action</span> <span class="na">id=</span><span class="s">"Myplugin.Dialogs"</span> <span class="na">class=</span><span class="s">"Mypackage.MyShowDialog"</span> <span class="na">text=</span><span class="s">"Show _Dialog"</span> <span class="na">description=</span><span class="s">"A test menu item"</span> <span class="nt">/></span>
|
|
<span class="nt"></group></span>
|
|
<span class="nt"></actions></span>
|
|
</code></pre>
|
|
|
|
<p>This fragment of the plugin.xml file demonstrates only some elements you can use in the <code class="code highlight language-text"><actions></code> section to register your actions. For information about all elements designed to register your actions, refer to <a href="/intellij/sdk/docs/basics/action_system.html"><span>IntelliJ Platform Action System</span></a>.</p>
|
|
|
|
<a name="registering-actions-from-java-code" class="elem-anchor"></a>
|
|
<h4>Registering actions from Java code</h4>
|
|
|
|
<p>Alternatively, you can register your actions from Java code. For more information and samples that illustrate how to register actions from Java code, see <a href="/intellij/sdk/docs/basics/action_system.html"><span>IntelliJ Platform Action System</span></a>.</p>
|
|
|
|
<a name="quick-creation-of-actions" class="elem-anchor"></a>
|
|
<h3>Quick creation of actions<a href="#quick-creation-of-actions" class="anchor-link"><span></span></a></h3>
|
|
|
|
<p>The IntelliJ Platform provides the <strong>New Action</strong> wizard that suggests a simplified way of creating actions, with all the required infrastructure. The wizard helps you declare the action class and automatically makes appropriate changes to the <code class="code highlight language-text"><actions></code> section of the plugin.xml file.</p>
|
|
|
|
<p>Note that you can use this wizard only to add a new action to an existing action group on the main menu or toolbar. If you want to create a new action group, and then add an action to this group, follow instructions earlier in this document.</p>
|
|
|
|
<p><strong>To create and register an action with the New Action wizard</strong></p>
|
|
|
|
<ol>
|
|
<li>In your project, on the context menu of the destination package click <strong>New</strong> or press <strong>Alt + Insert</strong>.</li>
|
|
<li>On the <strong>New</strong> menu, click <strong>Action</strong>.</li>
|
|
</ol>
|
|
|
|
<p><img src="img/new_action_template.png" alt="New Action Template" /></p>
|
|
|
|
<ol>
|
|
<li>On the <strong>New Action</strong> page that opens, fill in the following fields, and then click <strong>OK</strong>:</li>
|
|
</ol>
|
|
|
|
<ul>
|
|
<li><strong>Action ID</strong>: Enter the unique ID of the action. Recommended format: <code class="code highlight language-text">PluginName.ID</code></li>
|
|
<li><strong>Class Name</strong>: Enter the name of the action class to be created.</li>
|
|
<li><strong>Name</strong>: Enter the name of the menu item or tooltip for toolbar button associated with action.</li>
|
|
<li><strong>Description</strong>: Optionally, enter the action description. The IDEA status bar indicates this description when focusing the action.</li>
|
|
<li>In the <strong>Add to Group</strong> area, under <strong>Groups</strong>, <strong>Actions</strong> and <strong>Anchor</strong>, specify the action group to which to add a newly created action, and the position of the newly created action relative to other existing actions.</li>
|
|
<li>In the <strong>Keyboard Shortcuts</strong> area, optionally, specify the first and second keystrokes of the action.</li>
|
|
</ul>
|
|
|
|
<p><img src="img/new_action_page.png" alt="New Action Page" /></p>
|
|
|
|
<p>The IntelliJ Platform generates a <code class="code highlight language-text">.java</code> file with the specified class name, registers the newly created action in the plugin.xml file, adds a node to the module tree view, and opens the created action class file in the editor.</p>
|
|
|
|
|
|
<div class="last-modified">
|
|
Last modified: 30 November 2015
|
|
</div>
|
|
</article>
|
|
|
|
<section class="disqus">
|
|
<div id="disqus_thread"></div>
|
|
</section>
|
|
</div>
|
|
</section>
|
|
</main>
|
|
</div>
|
|
|
|
|
|
<script data-main="/intellij/sdk/docs/app/js/main.build" data-baseurl="/intellij/sdk/docs/" src="/intellij/sdk/docs/app/js/vendor/requirejs/require.js"></script>
|
|
|
|
</body>
|
|
</html>
|
|
|