Skip to content

Commit db59ba6

Browse files
committed
Merge branch 'create-theme-action'
2 parents 9e3a538 + 63aa1aa commit db59ba6

9 files changed

Lines changed: 670 additions & 194 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public String getMenuName() {
6464
@Override
6565
public List<? extends Action> getActions() {
6666
List<Action> actions = new ArrayList<Action>();
67+
actions.add(CreateThemeAction.getInstance());
6768
actions.add(new CodeCompletionRefreshAction());
68-
actions.add(new CreateThemeAction());
6969
return actions;
7070
}
7171
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.util.Collections;
45+
import org.netbeans.modules.php.wordpress.util.GithubZipEntryFilter;
46+
import org.netbeans.modules.php.wordpress.util.ZipEntryFilter;
47+
import org.openide.util.NbBundle;
48+
49+
/**
50+
* Create Barebones theme.
51+
*
52+
* @see https://github.com/welcomebrand/Barebones
53+
* @author junichi11
54+
*/
55+
public class CreateBarebonesThemeAction extends CreateThemeBaseAction {
56+
57+
private static final long serialVersionUID = -2839579492132022777L;
58+
private static final String BAREBONES_GITHUB_ZIP_URL = "https://github.com/welcomebrand/Barebones/archive/master.zip"; // NOI18N
59+
private static final CreateBarebonesThemeAction INSTANCE = new CreateBarebonesThemeAction();
60+
61+
private CreateBarebonesThemeAction() {
62+
}
63+
64+
public static CreateBarebonesThemeAction getInstance() {
65+
return INSTANCE;
66+
}
67+
68+
@Override
69+
@NbBundle.Messages("LBL_Barebones=Barebones")
70+
protected String getName() {
71+
return Bundle.LBL_Barebones();
72+
}
73+
74+
@Override
75+
protected String getUrl() {
76+
return BAREBONES_GITHUB_ZIP_URL;
77+
}
78+
79+
@Override
80+
protected ZipEntryFilter getZipEntryFilter() {
81+
return new GithubZipEntryFilter(Collections.singleton("assets")); // NOI18N
82+
}
83+
}

src/org/netbeans/modules/php/wordpress/ui/actions/CreateThemeAction.java

Lines changed: 24 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -41,191 +41,51 @@
4141
*/
4242
package org.netbeans.modules.php.wordpress.ui.actions;
4343

44-
import java.io.IOException;
45-
import java.io.OutputStreamWriter;
46-
import java.io.PrintWriter;
47-
import java.net.MalformedURLException;
48-
import java.util.List;
49-
import java.util.logging.Level;
50-
import java.util.logging.Logger;
51-
import org.netbeans.api.progress.ProgressHandle;
52-
import org.netbeans.api.progress.ProgressHandleFactory;
44+
import javax.swing.JMenu;
45+
import javax.swing.JMenuItem;
5346
import org.netbeans.modules.php.api.phpmodule.PhpModule;
5447
import org.netbeans.modules.php.spi.framework.actions.BaseAction;
55-
import org.netbeans.modules.php.wordpress.ui.wizards.CreateThemePanel;
56-
import org.netbeans.modules.php.wordpress.util.Charset;
57-
import org.netbeans.modules.php.wordpress.util.UnderscoresUtils;
58-
import org.netbeans.modules.php.wordpress.util.UnderscoresZipEntryFilter;
59-
import org.netbeans.modules.php.wordpress.util.WPFileUtils;
60-
import org.openide.filesystems.FileAlreadyLockedException;
61-
import org.openide.filesystems.FileObject;
62-
import org.openide.filesystems.FileUtil;
63-
import org.openide.util.Cancellable;
64-
import org.openide.util.Exceptions;
6548
import org.openide.util.NbBundle;
66-
import org.openide.util.RequestProcessor;
49+
import org.openide.util.actions.Presenter;
6750

6851
/**
69-
* Use underscores(_s) for creating theme.
7052
*
71-
* @see <a href="https://github.com/Automattic/_s">github:underscores</a>
72-
* @see <a href="http://underscores.me/">underscores.me</a>
7353
* @author junichi11
7454
*/
75-
public class CreateThemeAction extends BaseAction {
55+
public class CreateThemeAction extends BaseAction implements Presenter.Menu {
7656

77-
private static final long serialVersionUID = -5290582852489607026L;
78-
private static final Logger LOGGER = Logger.getLogger(CreateThemeAction.class.getName());
79-
private static final String UNDERSCORES_ZIP_URL = "https://github.com/Automattic/_s/archive/master.zip"; // NOI18N
80-
private String _s;
81-
private String _s_;
82-
private String themeName;
83-
private String author;
84-
private String authorUri;
85-
private String description;
57+
private static final long serialVersionUID = -1533566813298547558L;
58+
private static final CreateThemeAction INSTANCE = new CreateThemeAction();
59+
60+
private CreateThemeAction() {
61+
}
62+
63+
public static CreateThemeAction getInstance() {
64+
return INSTANCE;
65+
}
8666

8767
@Override
8868
protected String getFullName() {
89-
return Bundle.LBL_WordPressAction(getPureName());
69+
return getPureName();
9070
}
9171

92-
@NbBundle.Messages("LBL_CreateThemeAction=Create Theme (_s)")
9372
@Override
73+
@NbBundle.Messages("LBL_CreateThemeBaseAction=Create Theme")
9474
protected String getPureName() {
95-
return Bundle.LBL_CreateThemeAction();
75+
return Bundle.LBL_CreateThemeBaseAction();
9676
}
9777

9878
@Override
9979
protected void actionPerformed(PhpModule pm) {
100-
// create dialog
101-
CreateThemePanel panel = new CreateThemePanel();
102-
panel.showDialog();
103-
if (!panel.isOK()) {
104-
return;
105-
}
106-
107-
// click OK
108-
String name = panel.getThemeName().trim();
109-
author = panel.getAuthor().trim();
110-
authorUri = panel.getAuthorUri().trim();
111-
description = panel.getDescription().trim();
112-
113-
// create folder name, function prefix, theme name
114-
String themeFolerName = UnderscoresUtils.toFolderName(name);
115-
_s = UnderscoresUtils.toTextDomain(name);
116-
_s_ = UnderscoresUtils.toFunctionName(name);
117-
themeName = name;
118-
119-
// create folder
120-
FileObject themesDirectory = WPFileUtils.getThemesDirectory(pm);
121-
if (themesDirectory == null) {
122-
LOGGER.log(Level.WARNING, "themes directory don't exist!");
123-
return;
124-
}
125-
FileObject themeFolder = null;
126-
try {
127-
themeFolder = themesDirectory.createFolder(themeFolerName);
128-
} catch (IOException ex) {
129-
LOGGER.log(Level.WARNING, null, ex);
130-
}
131-
132-
if (themeFolder == null) {
133-
return;
134-
}
135-
final FileObject theme = themeFolder;
136-
137-
// display progress bar
138-
RequestProcessor.getDefault().post(new Runnable() {
139-
@Override
140-
public void run() {
141-
ProgressHandle handle = ProgressHandleFactory.createHandle("Createing theme", new Cancellable() {
142-
@Override
143-
public boolean cancel() {
144-
return true;
145-
}
146-
});
147-
try {
148-
handle.start();
149-
if (!unzipAndReplace(theme)) {
150-
LOGGER.log(Level.WARNING, "fail: create wp theme");
151-
}
152-
153-
} finally {
154-
handle.finish();
155-
}
156-
}
157-
});
15880
}
15981

160-
/**
161-
* At first Unzip Underscores from github and replace some values.
162-
*
163-
* @param themeFolder
164-
* @return
165-
*/
166-
private boolean unzipAndReplace(FileObject themeFolder) {
167-
// unzip
168-
try {
169-
WPFileUtils.unzip(UNDERSCORES_ZIP_URL, FileUtil.toFile(themeFolder), new UnderscoresZipEntryFilter());
170-
themeFolder.refresh();
171-
} catch (MalformedURLException ex) {
172-
LOGGER.log(Level.WARNING, null, ex);
173-
} catch (IOException ex) {
174-
LOGGER.log(Level.WARNING, null, ex);
175-
}
176-
FileObject[] children = themeFolder.getChildren();
177-
if (children.length == 0) {
178-
return false;
179-
}
180-
// replace
181-
replace(themeFolder);
182-
return true;
183-
}
184-
185-
/**
186-
* Replace : theme name, function name, e.t.c.
187-
*
188-
* @param directory
189-
*/
190-
private void replace(FileObject directory) {
191-
FileObject[] children = directory.getChildren();
192-
for (FileObject child : children) {
193-
if (child.isFolder()) {
194-
replace(child);
195-
continue;
196-
}
197-
String ext = child.getExt();
198-
if (ext.equals("md") || ext.equals("txt")) { // NOI18N
199-
continue;
200-
}
201-
try {
202-
List<String> lines = child.asLines();
203-
PrintWriter pw = new PrintWriter(new OutputStreamWriter(child.getOutputStream(), Charset.UTF8));
204-
try {
205-
for (String line : lines) {
206-
line = line.replaceAll("_s_", _s_); // NOI18N
207-
line = line.replaceAll(" _s", " " + themeName); // NOI18N
208-
line = line.replaceAll("'_s'", "'" + _s + "'"); // NOI18N
209-
if (child.getNameExt().equals("style.css")) { // NOI18N
210-
if (line.startsWith("Author:")) { // NOI18N
211-
line = "Author: " + author; // NOI18N
212-
} else if (line.startsWith("Author URI:")) { // NOI18N
213-
line = "Author URI: " + authorUri; // NOI18N
214-
} else if (line.startsWith("Description:")) { // NOI18N
215-
line = "Description: " + description; // NOI18N
216-
}
217-
}
218-
pw.println(line);
219-
}
220-
} finally {
221-
pw.close();
222-
}
223-
} catch (FileAlreadyLockedException ex) {
224-
Exceptions.printStackTrace(ex);
225-
} catch (IOException ex) {
226-
Exceptions.printStackTrace(ex);
227-
}
228-
229-
}
82+
@Override
83+
public JMenuItem getMenuPresenter() {
84+
JMenu menu = new JMenu(getPureName());
85+
JMenuItem underscores = new JMenuItem(CreateUnderscoresThemeAction.getInstance());
86+
JMenuItem barebones = new JMenuItem(CreateBarebonesThemeAction.getInstance());
87+
menu.add(underscores);
88+
menu.add(barebones);
89+
return menu;
23090
}
23191
}

0 commit comments

Comments
 (0)