From b9cc1a71298792a7771050ee2ca1df8ea2bca7da Mon Sep 17 00:00:00 2001 From: junichi11 Date: Wed, 24 Jan 2018 19:54:01 +0900 Subject: [PATCH] Add CreateMinimumBlankThemeAction #59 - Just create a style.css and an empty index.php --- README.md | 8 +- .../CreateMinimumBlankThemeAction.java | 191 ++++++++++++++++++ .../ui/actions/CreateThemeAction.java | 1 + .../wordpress/ui/wizards/Bundle.properties | 4 + .../wizards/CreateMinimumBlankThemePanel.form | 86 ++++++++ .../wizards/CreateMinimumBlankThemePanel.java | 176 ++++++++++++++++ 6 files changed, 464 insertions(+), 2 deletions(-) create mode 100644 src/org/netbeans/modules/php/wordpress/ui/actions/CreateMinimumBlankThemeAction.java create mode 100644 src/org/netbeans/modules/php/wordpress/ui/wizards/CreateMinimumBlankThemePanel.form create mode 100644 src/org/netbeans/modules/php/wordpress/ui/wizards/CreateMinimumBlankThemePanel.java diff --git a/README.md b/README.md index 019d475..8388584 100644 --- a/README.md +++ b/README.md @@ -79,18 +79,22 @@ add_filter('the_content', 'w[Ctrl + Space]'); // e.g. start with 'w' ``` ### Display And Change Debug Status -WP_DEBUG value(wp-config.php) is displayed on bottome-right of IDE. +WP_DEBUG value(wp-config.php) is displayed on bottom-right of IDE. If you click there, you can change WP_DEBUG value. WordPress version number is also displayed. ### Create New Theme Action Right-click Project > WordPress > Create Theme +#### Minimum Theme + +Just create a style.css and an empty index.php. + #### Underscores Create theme from [Underscores | A Starter Theme for WordPress](http://underscores.me/). Underscores is awesome! This plugin uses [Automattic/_s · GitHub](https://github.com/automattic/_s). -**Please notice that license of created theme is GPLv2** +**Please note that license of created theme is GPLv2** #### Barebones Create theme form [welcomebrand/Barebones · GitHub](https://github.com/welcomebrand/Barebones). diff --git a/src/org/netbeans/modules/php/wordpress/ui/actions/CreateMinimumBlankThemeAction.java b/src/org/netbeans/modules/php/wordpress/ui/actions/CreateMinimumBlankThemeAction.java new file mode 100644 index 0000000..a5cdbf3 --- /dev/null +++ b/src/org/netbeans/modules/php/wordpress/ui/actions/CreateMinimumBlankThemeAction.java @@ -0,0 +1,191 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + */ +package org.netbeans.modules.php.wordpress.ui.actions; + +import java.awt.EventQueue; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import org.netbeans.modules.csl.api.UiUtils; +import org.netbeans.modules.php.api.phpmodule.PhpModule; +import org.netbeans.modules.php.spi.framework.actions.BaseAction; +import org.netbeans.modules.php.wordpress.modules.WordPressModule; +import org.netbeans.modules.php.wordpress.ui.wizards.CreateMinimumBlankThemePanel; +import org.netbeans.modules.php.wordpress.util.WPUtils; +import org.openide.DialogDescriptor; +import org.openide.DialogDisplayer; +import org.openide.NotifyDescriptor; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.loaders.DataFolder; +import org.openide.loaders.DataObject; +import org.openide.util.NbBundle; + +public class CreateMinimumBlankThemeAction extends BaseAction { + + private final List themes = new ArrayList<>(); + private static final CreateMinimumBlankThemeAction INSTANCE = new CreateMinimumBlankThemeAction(); + private static final Logger LOGGER = Logger.getLogger(CreateMinimumBlankThemeAction.class.getName()); + + private CreateMinimumBlankThemeAction() { + } + + public static CreateMinimumBlankThemeAction getInstance() { + return INSTANCE; + } + + @Override + protected String getFullName() { + return getPureName(); + } + + @NbBundle.Messages("CreateMinimumBlankThemeAction.PureName=Minimum Theme") + @Override + protected String getPureName() { + return Bundle.CreateMinimumBlankThemeAction_PureName(); + } + + @NbBundle.Messages({ + "CreateMinimumBlankThemeAction.dialog.title=Create Minimum Theme", + "CreateMinimumBlankThemeAction.existing.directoryName=It already exists." + }) + @Override + protected void actionPerformed(PhpModule phpModule) { + assert EventQueue.isDispatchThread(); + if (!WPUtils.isWP(phpModule)) { + // called via shortcut + return; + } + themes.clear(); + + // get themes directory + WordPressModule wpModule = WordPressModule.Factory.forPhpModule(phpModule); + if (wpModule == null) { + return; + } + final FileObject themesDirectory = wpModule.getThemesDirectory(); + if (themesDirectory == null) { + return; + } + for (FileObject child : themesDirectory.getChildren()) { + if (child.isFolder()) { + themes.add(child.getNameExt()); + } + } + + // create a panel & descriptor + final CreateMinimumBlankThemePanel panel = new CreateMinimumBlankThemePanel(); + final DialogDescriptor dialogDescriptor = new DialogDescriptor( + panel, + Bundle.CreateMinimumBlankThemeAction_dialog_title(), + true, + DialogDescriptor.OK_CANCEL_OPTION, + null, + null + ); + + // add ChangeListener + ChangeListener changeListener = new ChangeListener() { + @Override + public void stateChanged(ChangeEvent e) { + String themeDirectoryName = panel.getThemeDirectoryName(); + boolean existsDirectory = themes.contains(themeDirectoryName); + dialogDescriptor.setValid(!existsDirectory); + if (existsDirectory) { + panel.setErrorMessage(Bundle.CreateMinimumBlankThemeAction_existing_directoryName()); + } else { + panel.setErrorMessage(" "); // NOI18N + } + } + }; + panel.addChangeListener(changeListener); + + // show dialog + Object result = DialogDisplayer.getDefault().notify(dialogDescriptor); + if (result == NotifyDescriptor.OK_OPTION) { + try { + createMinimumTheme(themesDirectory, panel.getThemeDirectoryName()); + } catch (IOException ex) { + LOGGER.log(Level.WARNING, null, ex); + } + } + panel.removeChangeListener(changeListener); + } + + @NbBundle.Messages({ + "CreateMinimumBlankThemePanel.notFound.themeTemplate=Not found : style.css template file", + "CreateMinimumBlankThemePanel.directoryName.error=Cannot create a directory" + }) + private void createMinimumTheme(FileObject themesDirectory, String directoryName) throws IOException { + // get template + FileObject template = FileUtil.getConfigFile("Templates/WordPress/style.css"); // NOI18N + if (template == null) { + LOGGER.log(Level.WARNING, Bundle.CreateMinimumBlankThemePanel_notFound_themeTemplate()); + return; + } + + // create a theme directory + FileObject themeDirectory = themesDirectory.createFolder(directoryName); + if (themeDirectory == null) { + LOGGER.log(Level.WARNING, Bundle.CreateMinimumBlankThemePanel_directoryName_error()); + return; + } + + // create an empty index.php + themeDirectory.createData("index", "php"); // NOI18N + + // create a style.css + DataObject templateDataObject = DataObject.find(template); + DataFolder targetFolder = DataFolder.findFolder(themeDirectory); + Map parameters = new HashMap<>(); + // TODO add fields as well as child theme? + DataObject styleCssDataObject = templateDataObject.createFromTemplate(targetFolder, "style.css", parameters); // NOI18N + if (styleCssDataObject != null) { + UiUtils.open(styleCssDataObject.getPrimaryFile(), 0); + } + } + +} diff --git a/src/org/netbeans/modules/php/wordpress/ui/actions/CreateThemeAction.java b/src/org/netbeans/modules/php/wordpress/ui/actions/CreateThemeAction.java index a3165d1..5daaefc 100644 --- a/src/org/netbeans/modules/php/wordpress/ui/actions/CreateThemeAction.java +++ b/src/org/netbeans/modules/php/wordpress/ui/actions/CreateThemeAction.java @@ -94,6 +94,7 @@ private JMenuItem createMenuItem() { JMenu menu = new JMenu(getPureName()); JMenuItem underscores = new JMenuItem(CreateUnderscoresThemeAction.getInstance()); JMenuItem barebones = new JMenuItem(CreateBarebonesThemeAction.getInstance()); + menu.add(CreateMinimumBlankThemeAction.getInstance()); menu.add(underscores); menu.add(barebones); return menu; diff --git a/src/org/netbeans/modules/php/wordpress/ui/wizards/Bundle.properties b/src/org/netbeans/modules/php/wordpress/ui/wizards/Bundle.properties index 63e3b0c..fd15013 100644 --- a/src/org/netbeans/modules/php/wordpress/ui/wizards/Bundle.properties +++ b/src/org/netbeans/modules/php/wordpress/ui/wizards/Bundle.properties @@ -55,3 +55,7 @@ CreateChildThemePanel.childDirectoryNameTextField.text= CreateChildThemePanel.childThemeNameLabel.text=Child Theme Name: CreateChildThemePanel.childThemeNameTextField.text= NewProjectConfigurationPanel.dbNameLabel.text=DB_NAME: +CreateMinimumBlankThemePanel.errorMessageLabel.text=ERROR +CreateMinimumBlankThemePanel.themeDirectoryNameTextField.text= +CreateMinimumBlankThemePanel.themeDirectoryNameLabel.text=Theme Directory Name: +CreateMinimumBlankThemePanel.descriptionLabel.text=Just create a style.css and an empty index.php diff --git a/src/org/netbeans/modules/php/wordpress/ui/wizards/CreateMinimumBlankThemePanel.form b/src/org/netbeans/modules/php/wordpress/ui/wizards/CreateMinimumBlankThemePanel.form new file mode 100644 index 0000000..4c3c592 --- /dev/null +++ b/src/org/netbeans/modules/php/wordpress/ui/wizards/CreateMinimumBlankThemePanel.form @@ -0,0 +1,86 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/org/netbeans/modules/php/wordpress/ui/wizards/CreateMinimumBlankThemePanel.java b/src/org/netbeans/modules/php/wordpress/ui/wizards/CreateMinimumBlankThemePanel.java new file mode 100644 index 0000000..9338c97 --- /dev/null +++ b/src/org/netbeans/modules/php/wordpress/ui/wizards/CreateMinimumBlankThemePanel.java @@ -0,0 +1,176 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + */ +package org.netbeans.modules.php.wordpress.ui.wizards; + +import javax.swing.UIManager; +import javax.swing.event.ChangeListener; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import org.openide.util.ChangeSupport; + +/** + * + * @author junichi11 + */ +public class CreateMinimumBlankThemePanel extends javax.swing.JPanel { + + private static final long serialVersionUID = -2551047100022407877L; + + private final ChangeSupport changeSupport = new ChangeSupport(this); + + /** + * Creates new form CreateMinimumThemePanel + */ + public CreateMinimumBlankThemePanel() { + initComponents(); + init(); + } + + private void init() { + errorMessageLabel.setText(" "); // NOI18N + errorMessageLabel.setForeground(UIManager.getColor("nb.errorForeground")); // NOI18N + themeDirectoryNameTextField.getDocument().addDocumentListener(new DocumentListener() { + @Override + public void insertUpdate(DocumentEvent e) { + processUpdate(); + } + + @Override + public void removeUpdate(DocumentEvent e) { + processUpdate(); + } + + @Override + public void changedUpdate(DocumentEvent e) { + processUpdate(); + } + + private void processUpdate() { + fireChange(); + } + }); + } + + public void addChangeListener(ChangeListener changeListener) { + changeSupport.addChangeListener(changeListener); + } + + public void removeChangeListener(ChangeListener changeListener) { + changeSupport.removeChangeListener(changeListener); + } + + public String getThemeDirectoryName() { + return themeDirectoryNameTextField.getText().trim(); + } + + public void setErrorMessage(String message) { + if (message == null) { + errorMessageLabel.setText(""); // NOI18N + } else { + errorMessageLabel.setText(message); + } + } + + void fireChange() { + changeSupport.fireChange(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + themeDirectoryNameLabel = new javax.swing.JLabel(); + themeDirectoryNameTextField = new javax.swing.JTextField(); + errorMessageLabel = new javax.swing.JLabel(); + descriptionLabel = new javax.swing.JLabel(); + + org.openide.awt.Mnemonics.setLocalizedText(themeDirectoryNameLabel, org.openide.util.NbBundle.getMessage(CreateMinimumBlankThemePanel.class, "CreateMinimumBlankThemePanel.themeDirectoryNameLabel.text")); // NOI18N + + themeDirectoryNameTextField.setText(org.openide.util.NbBundle.getMessage(CreateMinimumBlankThemePanel.class, "CreateMinimumBlankThemePanel.themeDirectoryNameTextField.text")); // NOI18N + + org.openide.awt.Mnemonics.setLocalizedText(errorMessageLabel, org.openide.util.NbBundle.getMessage(CreateMinimumBlankThemePanel.class, "CreateMinimumBlankThemePanel.errorMessageLabel.text")); // NOI18N + + org.openide.awt.Mnemonics.setLocalizedText(descriptionLabel, org.openide.util.NbBundle.getMessage(CreateMinimumBlankThemePanel.class, "CreateMinimumBlankThemePanel.descriptionLabel.text")); // NOI18N + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); + this.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(themeDirectoryNameLabel) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(themeDirectoryNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(errorMessageLabel) + .addComponent(descriptionLabel)) + .addGap(0, 0, Short.MAX_VALUE))) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(themeDirectoryNameLabel) + .addComponent(themeDirectoryNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(descriptionLabel) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(errorMessageLabel) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + }// //GEN-END:initComponents + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JLabel descriptionLabel; + private javax.swing.JLabel errorMessageLabel; + private javax.swing.JLabel themeDirectoryNameLabel; + private javax.swing.JTextField themeDirectoryNameTextField; + // End of variables declaration//GEN-END:variables +}