Skip to content

Commit 8f121fa

Browse files
committed
Merge branch 'create-plugin-action' into nb73dev
2 parents db59ba6 + 68c97fa commit 8f121fa

5 files changed

Lines changed: 348 additions & 0 deletions

File tree

src/org/netbeans/modules/php/wordpress/WordPressActionsExtender.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import javax.swing.Action;
4747
import org.netbeans.modules.php.spi.framework.PhpModuleActionsExtender;
4848
import org.netbeans.modules.php.wordpress.ui.actions.CodeCompletionRefreshAction;
49+
import org.netbeans.modules.php.wordpress.ui.actions.CreatePluginAction;
4950
import org.netbeans.modules.php.wordpress.ui.actions.CreateThemeAction;
5051
import org.openide.util.NbBundle;
5152

@@ -65,6 +66,7 @@ public String getMenuName() {
6566
public List<? extends Action> getActions() {
6667
List<Action> actions = new ArrayList<Action>();
6768
actions.add(CreateThemeAction.getInstance());
69+
actions.add(CreatePluginAction.getInstance());
6870
actions.add(new CodeCompletionRefreshAction());
6971
return actions;
7072
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/*
3+
Plugin Name: ${name}
4+
Plugin URI:
5+
Description:
6+
Version: 1.0.0
7+
Author:
8+
Author URI:
9+
License: GPLv2
10+
*/
11+

src/org/netbeans/modules/php/wordpress/resources/layer.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@
1010
</folder>
1111
</folder>
1212
</folder>
13+
<folder name="org-netbeans-modules-php-wordpress">
14+
<file name="WpPlugin.php" url="WpPlugin.php" />
15+
<file name="readme.txt" url="readme.txt" />
16+
</folder>
1317
</filesystem>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== Plugin Name ===
2+
Contributors:
3+
Donate link:
4+
Tags:
5+
Requires at least:
6+
Tested up to:
7+
Stable tag:
8+
License: GPLv2 or later
9+
License URI: http://www.gnu.org/licenses/gpl-2.0.html
10+
11+
12+
== Description ==
13+
14+
15+
== Installation ==
16+
17+
18+
== Changelog ==
19+
20+
= 1.0 =
Lines changed: 311 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7+
* Other names may be trademarks of their respective owners.
8+
*
9+
* The contents of this file are subject to the terms of either the GNU
10+
* General Public License Version 2 only ("GPL") or the Common
11+
* Development and Distribution License("CDDL") (collectively, the
12+
* "License"). You may not use this file except in compliance with the
13+
* License. You can obtain a copy of the License at
14+
* http://www.netbeans.org/cddl-gplv2.html
15+
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16+
* specific language governing permissions and limitations under the
17+
* License. When distributing the software, include this License Header
18+
* Notice in each file and include the License file at
19+
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20+
* particular file as subject to the "Classpath" exception as provided
21+
* by Oracle in the GPL Version 2 section of the License file that
22+
* accompanied this code. If applicable, add the following below the
23+
* License Header, with the fields enclosed by brackets [] replaced by
24+
* your own identifying information:
25+
* "Portions Copyrighted [year] [name of copyright owner]"
26+
*
27+
* If you wish your version of this file to be governed by only the CDDL
28+
* or only the GPL Version 2, indicate your decision by adding
29+
* "[Contributor] elects to include this software in this distribution
30+
* under the [CDDL or GPL Version 2] license." If you do not indicate a
31+
* single choice of license, a recipient has the option to distribute
32+
* your version of this file under either the CDDL, the GPL Version 2 or
33+
* to extend the choice of license to its licensees as provided above.
34+
* However, if you add GPL Version 2 code and therefore, elected the GPL
35+
* Version 2 license, then the option applies only if the new code is
36+
* made subject to such option by the copyright holder.
37+
*
38+
* Contributor(s):
39+
*
40+
* Portions Copyrighted 2013 Sun Microsystems, Inc.
41+
*/
42+
package org.netbeans.modules.php.wordpress.ui.actions;
43+
44+
import java.io.IOException;
45+
import java.io.OutputStream;
46+
import java.io.OutputStreamWriter;
47+
import java.io.PrintWriter;
48+
import java.util.Collections;
49+
import java.util.HashSet;
50+
import java.util.List;
51+
import java.util.Set;
52+
import java.util.logging.Level;
53+
import java.util.logging.Logger;
54+
import javax.swing.event.DocumentEvent;
55+
import javax.swing.event.DocumentListener;
56+
import javax.swing.text.Document;
57+
import org.netbeans.modules.csl.api.UiUtils;
58+
import org.netbeans.modules.php.api.phpmodule.PhpModule;
59+
import org.netbeans.modules.php.api.util.StringUtils;
60+
import org.netbeans.modules.php.spi.framework.actions.BaseAction;
61+
import org.netbeans.modules.php.wordpress.util.Charset;
62+
import org.netbeans.modules.php.wordpress.util.WPFileUtils;
63+
import org.netbeans.modules.php.wordpress.util.WPUtils;
64+
import org.openide.DialogDisplayer;
65+
import org.openide.NotifyDescriptor;
66+
import org.openide.filesystems.FileObject;
67+
import org.openide.filesystems.FileUtil;
68+
import org.openide.util.Exceptions;
69+
import org.openide.util.NbBundle;
70+
71+
/**
72+
*
73+
* @author junichi11
74+
*/
75+
public class CreatePluginAction extends BaseAction {
76+
77+
private static final long serialVersionUID = 4960061081316192725L;
78+
private static final String README = "readme"; // NOI18N
79+
private static final String WP_CONFIG_ROOT = "org-netbeans-modules-php-wordpress"; // NOI18N
80+
private static final String WP_PLUGIN_TEMPLATE_PATH = WP_CONFIG_ROOT + "/WpPlugin.php"; // NOI18N
81+
private static final String WP_PLUGIN_README_TEMPLATE_PATH = WP_CONFIG_ROOT + "/readme.txt"; // NOI18N
82+
private static final String NAME_PLACE = "${name}"; // NOI18N
83+
private Set<String> existingPluignNames;
84+
private FileObject pluginsDirectory;
85+
private static final CreatePluginAction INSTANCE = new CreatePluginAction();
86+
private static final Logger LOGGER = Logger.getLogger(CreatePluginAction.class.getName());
87+
88+
private CreatePluginAction() {
89+
}
90+
91+
public static CreatePluginAction getInstance() {
92+
return INSTANCE;
93+
}
94+
95+
@Override
96+
protected String getFullName() {
97+
return getPureName();
98+
}
99+
100+
@Override
101+
@NbBundle.Messages("LBL_CreatePluginAction=Create Plugin")
102+
protected String getPureName() {
103+
return Bundle.LBL_CreatePluginAction();
104+
}
105+
106+
@Override
107+
@NbBundle.Messages("LBL_PluginName=Plugin (Directory) Name")
108+
protected void actionPerformed(PhpModule phpModule) {
109+
// called via shortcut
110+
if (!WPUtils.isWP(phpModule)) {
111+
return;
112+
}
113+
114+
// get plugins directory
115+
pluginsDirectory = WPFileUtils.getPluginsDirectory(phpModule);
116+
if (pluginsDirectory == null) {
117+
return;
118+
}
119+
120+
// for validation
121+
setExistingPluginNames();
122+
123+
// create dialog
124+
InputLine descriptor = new InputLine(Bundle.LBL_PluginName(), Bundle.LBL_PluginName(),
125+
NotifyDescriptor.OK_CANCEL_OPTION, NotifyDescriptor.PLAIN_MESSAGE);
126+
descriptor.setValid(false);
127+
if (DialogDisplayer.getDefault().notify(descriptor) == NotifyDescriptor.OK_OPTION) {
128+
// get plugin name
129+
String name = descriptor.getInputText().trim();
130+
if (StringUtils.isEmpty(name)) {
131+
LOGGER.log(Level.WARNING, "Not found:{0}", name);
132+
return;
133+
}
134+
135+
// create plugin directory
136+
FileObject pluginDirectory = createPluginDirectory(name);
137+
if (pluginDirectory == null) {
138+
LOGGER.log(Level.WARNING, "Not found:{0}", "plugin directory");
139+
return;
140+
}
141+
142+
// add files pluginName.php & readme.txt
143+
try {
144+
addPluginFiles(pluginDirectory, name);
145+
} catch (IOException ex) {
146+
LOGGER.log(Level.WARNING, null, ex);
147+
}
148+
149+
// open file
150+
FileObject plugin = pluginDirectory.getFileObject(name + ".php"); // NOI18N
151+
if (plugin != null) {
152+
UiUtils.open(plugin, 0);
153+
}
154+
}
155+
}
156+
157+
/**
158+
* Add plugin files. It is so simple two files. main plugin file and readme.
159+
*
160+
* @param pluginDirectory
161+
* @param name plugin name
162+
* @throws IOException
163+
*/
164+
private void addPluginFiles(FileObject pluginDirectory, String name) throws IOException {
165+
createReadme(pluginDirectory);
166+
createPluginFile(pluginDirectory, name);
167+
}
168+
169+
/**
170+
* Create plugin directory.
171+
*
172+
* @param name
173+
* @return
174+
*/
175+
private FileObject createPluginDirectory(String name) {
176+
try {
177+
return pluginsDirectory.createFolder(name);
178+
} catch (IOException ex) {
179+
Exceptions.printStackTrace(ex);
180+
}
181+
return null;
182+
183+
}
184+
185+
/**
186+
* Set existing plugin names.
187+
*/
188+
private void setExistingPluginNames() {
189+
existingPluignNames = new HashSet<String>();
190+
if (pluginsDirectory != null) {
191+
for (FileObject child : pluginsDirectory.getChildren()) {
192+
if (child.isFolder()) {
193+
existingPluignNames.add(child.getName());
194+
}
195+
}
196+
}
197+
198+
}
199+
200+
/**
201+
* Get existing plugin names.
202+
*
203+
* @return
204+
*/
205+
public Set<String> getExistingPluginNames() {
206+
if (existingPluignNames == null) {
207+
return Collections.emptySet();
208+
}
209+
return existingPluignNames;
210+
}
211+
212+
/**
213+
* Create readme.txt.
214+
*
215+
* @param pluginDirectory
216+
* @throws IOException
217+
*/
218+
private void createReadme(FileObject pluginDirectory) throws IOException {
219+
FileObject readmeTemplate = FileUtil.getConfigFile(WP_PLUGIN_README_TEMPLATE_PATH);
220+
if (readmeTemplate == null) {
221+
return;
222+
}
223+
FileUtil.copyFile(readmeTemplate, pluginDirectory, README);
224+
}
225+
226+
/**
227+
* Create file for plugin. It is very simple file.
228+
*
229+
* @param pluginDirectory
230+
* @param name plugin name
231+
* @throws IOException
232+
*/
233+
private void createPluginFile(FileObject pluginDirectory, String name) throws IOException {
234+
FileObject pluginTemplate = FileUtil.getConfigFile(WP_PLUGIN_TEMPLATE_PATH);
235+
if (pluginTemplate == null) {
236+
LOGGER.log(Level.WARNING, "Not found:{0}", "plugin template");
237+
return;
238+
}
239+
OutputStream outputPlugin = pluginDirectory.createAndOpen(name + ".php"); // NOI18N
240+
try {
241+
List<String> lines = pluginTemplate.asLines(Charset.UTF8);
242+
PrintWriter pw = new PrintWriter(new OutputStreamWriter(outputPlugin, Charset.UTF8));
243+
try {
244+
for (String line : lines) {
245+
if (line.contains(NAME_PLACE)) {
246+
line = line.replace(NAME_PLACE, name);
247+
}
248+
pw.println(line);
249+
}
250+
} finally {
251+
pw.close();
252+
}
253+
} finally {
254+
outputPlugin.close();
255+
}
256+
}
257+
258+
//~ inner classes
259+
private class InputLine extends NotifyDescriptor.InputLine {
260+
261+
public InputLine(String text, String title) {
262+
super(text, title);
263+
addDocumentListener();
264+
}
265+
266+
public InputLine(String text, String title, int optionType, int messageType) {
267+
super(text, title, optionType, messageType);
268+
addDocumentListener();
269+
}
270+
271+
private void addDocumentListener() {
272+
Document document = textField.getDocument();
273+
document.addDocumentListener(new CreatePluginAction.DocumentListenerImpl(this));
274+
}
275+
}
276+
277+
private class DocumentListenerImpl implements DocumentListener {
278+
279+
private final InputLine inputLine;
280+
private final Set<String> existingPlugins;
281+
282+
public DocumentListenerImpl(InputLine inputLine) {
283+
this.inputLine = inputLine;
284+
existingPlugins = getExistingPluginNames();
285+
}
286+
287+
@Override
288+
public void insertUpdate(DocumentEvent e) {
289+
fire();
290+
}
291+
292+
@Override
293+
public void removeUpdate(DocumentEvent e) {
294+
fire();
295+
}
296+
297+
@Override
298+
public void changedUpdate(DocumentEvent e) {
299+
fire();
300+
}
301+
302+
private void fire() {
303+
String input = inputLine.getInputText().trim();
304+
if (input.isEmpty() || existingPlugins.contains(input)) {
305+
inputLine.setValid(false);
306+
} else {
307+
inputLine.setValid(true);
308+
}
309+
}
310+
}
311+
}

0 commit comments

Comments
 (0)