|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright (c) 2018 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 | +package org.netbeans.modules.php.wordpress.ui.actions; |
| 41 | + |
| 42 | +import java.awt.EventQueue; |
| 43 | +import java.io.IOException; |
| 44 | +import java.util.ArrayList; |
| 45 | +import java.util.HashMap; |
| 46 | +import java.util.List; |
| 47 | +import java.util.Map; |
| 48 | +import java.util.logging.Level; |
| 49 | +import java.util.logging.Logger; |
| 50 | +import javax.swing.event.ChangeEvent; |
| 51 | +import javax.swing.event.ChangeListener; |
| 52 | +import org.netbeans.modules.csl.api.UiUtils; |
| 53 | +import org.netbeans.modules.php.api.phpmodule.PhpModule; |
| 54 | +import org.netbeans.modules.php.spi.framework.actions.BaseAction; |
| 55 | +import org.netbeans.modules.php.wordpress.modules.WordPressModule; |
| 56 | +import org.netbeans.modules.php.wordpress.ui.wizards.CreateMinimumBlankThemePanel; |
| 57 | +import org.netbeans.modules.php.wordpress.util.WPUtils; |
| 58 | +import org.openide.DialogDescriptor; |
| 59 | +import org.openide.DialogDisplayer; |
| 60 | +import org.openide.NotifyDescriptor; |
| 61 | +import org.openide.filesystems.FileObject; |
| 62 | +import org.openide.filesystems.FileUtil; |
| 63 | +import org.openide.loaders.DataFolder; |
| 64 | +import org.openide.loaders.DataObject; |
| 65 | +import org.openide.util.NbBundle; |
| 66 | + |
| 67 | +public class CreateMinimumBlankThemeAction extends BaseAction { |
| 68 | + |
| 69 | + private final List<String> themes = new ArrayList<>(); |
| 70 | + private static final CreateMinimumBlankThemeAction INSTANCE = new CreateMinimumBlankThemeAction(); |
| 71 | + private static final Logger LOGGER = Logger.getLogger(CreateMinimumBlankThemeAction.class.getName()); |
| 72 | + |
| 73 | + private CreateMinimumBlankThemeAction() { |
| 74 | + } |
| 75 | + |
| 76 | + public static CreateMinimumBlankThemeAction getInstance() { |
| 77 | + return INSTANCE; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected String getFullName() { |
| 82 | + return getPureName(); |
| 83 | + } |
| 84 | + |
| 85 | + @NbBundle.Messages("CreateMinimumBlankThemeAction.PureName=Minimum Theme") |
| 86 | + @Override |
| 87 | + protected String getPureName() { |
| 88 | + return Bundle.CreateMinimumBlankThemeAction_PureName(); |
| 89 | + } |
| 90 | + |
| 91 | + @NbBundle.Messages({ |
| 92 | + "CreateMinimumBlankThemeAction.dialog.title=Create Minimum Theme", |
| 93 | + "CreateMinimumBlankThemeAction.existing.directoryName=It already exists." |
| 94 | + }) |
| 95 | + @Override |
| 96 | + protected void actionPerformed(PhpModule phpModule) { |
| 97 | + assert EventQueue.isDispatchThread(); |
| 98 | + if (!WPUtils.isWP(phpModule)) { |
| 99 | + // called via shortcut |
| 100 | + return; |
| 101 | + } |
| 102 | + themes.clear(); |
| 103 | + |
| 104 | + // get themes directory |
| 105 | + WordPressModule wpModule = WordPressModule.Factory.forPhpModule(phpModule); |
| 106 | + if (wpModule == null) { |
| 107 | + return; |
| 108 | + } |
| 109 | + final FileObject themesDirectory = wpModule.getThemesDirectory(); |
| 110 | + if (themesDirectory == null) { |
| 111 | + return; |
| 112 | + } |
| 113 | + for (FileObject child : themesDirectory.getChildren()) { |
| 114 | + if (child.isFolder()) { |
| 115 | + themes.add(child.getNameExt()); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // create a panel & descriptor |
| 120 | + final CreateMinimumBlankThemePanel panel = new CreateMinimumBlankThemePanel(); |
| 121 | + final DialogDescriptor dialogDescriptor = new DialogDescriptor( |
| 122 | + panel, |
| 123 | + Bundle.CreateMinimumBlankThemeAction_dialog_title(), |
| 124 | + true, |
| 125 | + DialogDescriptor.OK_CANCEL_OPTION, |
| 126 | + null, |
| 127 | + null |
| 128 | + ); |
| 129 | + |
| 130 | + // add ChangeListener |
| 131 | + ChangeListener changeListener = new ChangeListener() { |
| 132 | + @Override |
| 133 | + public void stateChanged(ChangeEvent e) { |
| 134 | + String themeDirectoryName = panel.getThemeDirectoryName(); |
| 135 | + boolean existsDirectory = themes.contains(themeDirectoryName); |
| 136 | + dialogDescriptor.setValid(!existsDirectory); |
| 137 | + if (existsDirectory) { |
| 138 | + panel.setErrorMessage(Bundle.CreateMinimumBlankThemeAction_existing_directoryName()); |
| 139 | + } else { |
| 140 | + panel.setErrorMessage(" "); // NOI18N |
| 141 | + } |
| 142 | + } |
| 143 | + }; |
| 144 | + panel.addChangeListener(changeListener); |
| 145 | + |
| 146 | + // show dialog |
| 147 | + Object result = DialogDisplayer.getDefault().notify(dialogDescriptor); |
| 148 | + if (result == NotifyDescriptor.OK_OPTION) { |
| 149 | + try { |
| 150 | + createMinimumTheme(themesDirectory, panel.getThemeDirectoryName()); |
| 151 | + } catch (IOException ex) { |
| 152 | + LOGGER.log(Level.WARNING, null, ex); |
| 153 | + } |
| 154 | + } |
| 155 | + panel.removeChangeListener(changeListener); |
| 156 | + } |
| 157 | + |
| 158 | + @NbBundle.Messages({ |
| 159 | + "CreateMinimumBlankThemePanel.notFound.themeTemplate=Not found : style.css template file", |
| 160 | + "CreateMinimumBlankThemePanel.directoryName.error=Cannot create a directory" |
| 161 | + }) |
| 162 | + private void createMinimumTheme(FileObject themesDirectory, String directoryName) throws IOException { |
| 163 | + // get template |
| 164 | + FileObject template = FileUtil.getConfigFile("Templates/WordPress/style.css"); // NOI18N |
| 165 | + if (template == null) { |
| 166 | + LOGGER.log(Level.WARNING, Bundle.CreateMinimumBlankThemePanel_notFound_themeTemplate()); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + // create a theme directory |
| 171 | + FileObject themeDirectory = themesDirectory.createFolder(directoryName); |
| 172 | + if (themeDirectory == null) { |
| 173 | + LOGGER.log(Level.WARNING, Bundle.CreateMinimumBlankThemePanel_directoryName_error()); |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + // create an empty index.php |
| 178 | + themeDirectory.createData("index", "php"); // NOI18N |
| 179 | + |
| 180 | + // create a style.css |
| 181 | + DataObject templateDataObject = DataObject.find(template); |
| 182 | + DataFolder targetFolder = DataFolder.findFolder(themeDirectory); |
| 183 | + Map<String, String> parameters = new HashMap<>(); |
| 184 | + // TODO add fields as well as child theme? |
| 185 | + DataObject styleCssDataObject = templateDataObject.createFromTemplate(targetFolder, "style.css", parameters); // NOI18N |
| 186 | + if (styleCssDataObject != null) { |
| 187 | + UiUtils.open(styleCssDataObject.getPrimaryFile(), 0); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | +} |
0 commit comments