|
41 | 41 | */ |
42 | 42 | package org.netbeans.modules.php.wordpress.ui.actions; |
43 | 43 |
|
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; |
53 | 46 | import org.netbeans.modules.php.api.phpmodule.PhpModule; |
54 | 47 | 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; |
65 | 48 | import org.openide.util.NbBundle; |
66 | | -import org.openide.util.RequestProcessor; |
| 49 | +import org.openide.util.actions.Presenter; |
67 | 50 |
|
68 | 51 | /** |
69 | | - * Use underscores(_s) for creating theme. |
70 | 52 | * |
71 | | - * @see <a href="https://github.com/Automattic/_s">github:underscores</a> |
72 | | - * @see <a href="http://underscores.me/">underscores.me</a> |
73 | 53 | * @author junichi11 |
74 | 54 | */ |
75 | | -public class CreateThemeAction extends BaseAction { |
| 55 | +public class CreateThemeAction extends BaseAction implements Presenter.Menu { |
76 | 56 |
|
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 | + } |
86 | 66 |
|
87 | 67 | @Override |
88 | 68 | protected String getFullName() { |
89 | | - return Bundle.LBL_WordPressAction(getPureName()); |
| 69 | + return getPureName(); |
90 | 70 | } |
91 | 71 |
|
92 | | - @NbBundle.Messages("LBL_CreateThemeAction=Create Theme (_s)") |
93 | 72 | @Override |
| 73 | + @NbBundle.Messages("LBL_CreateThemeBaseAction=Create Theme") |
94 | 74 | protected String getPureName() { |
95 | | - return Bundle.LBL_CreateThemeAction(); |
| 75 | + return Bundle.LBL_CreateThemeBaseAction(); |
96 | 76 | } |
97 | 77 |
|
98 | 78 | @Override |
99 | 79 | 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 | | - }); |
158 | 80 | } |
159 | 81 |
|
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; |
230 | 90 | } |
231 | 91 | } |
0 commit comments