|
| 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.util.ArrayList; |
| 46 | +import java.util.Collections; |
| 47 | +import java.util.HashMap; |
| 48 | +import java.util.List; |
| 49 | +import java.util.Map; |
| 50 | +import java.util.logging.Level; |
| 51 | +import java.util.logging.Logger; |
| 52 | +import javax.swing.SwingUtilities; |
| 53 | +import javax.swing.event.ChangeEvent; |
| 54 | +import javax.swing.event.ChangeListener; |
| 55 | +import org.netbeans.modules.csl.api.UiUtils; |
| 56 | +import org.netbeans.modules.php.api.phpmodule.PhpModule; |
| 57 | +import org.netbeans.modules.php.api.validation.ValidationResult; |
| 58 | +import org.netbeans.modules.php.spi.framework.actions.BaseAction; |
| 59 | +import org.netbeans.modules.php.wordpress.modules.WordPressModule; |
| 60 | +import org.netbeans.modules.php.wordpress.ui.wizards.CreateChildThemePanel; |
| 61 | +import org.netbeans.modules.php.wordpress.util.WPUtils; |
| 62 | +import org.netbeans.modules.php.wordpress.validators.WordPressDirectoryNameValidator; |
| 63 | +import org.openide.DialogDescriptor; |
| 64 | +import org.openide.DialogDisplayer; |
| 65 | +import org.openide.filesystems.FileObject; |
| 66 | +import org.openide.filesystems.FileUtil; |
| 67 | +import org.openide.loaders.DataFolder; |
| 68 | +import org.openide.loaders.DataObject; |
| 69 | +import org.openide.loaders.DataObjectNotFoundException; |
| 70 | +import org.openide.util.Exceptions; |
| 71 | +import org.openide.util.NbBundle; |
| 72 | + |
| 73 | +/** |
| 74 | + * |
| 75 | + * @author junichi11 |
| 76 | + */ |
| 77 | +public final class CreateChildThemeAction extends BaseAction implements ChangeListener { |
| 78 | + |
| 79 | + private CreateChildThemePanel panel; |
| 80 | + private DialogDescriptor descriptor; |
| 81 | + private final List<String> themes = new ArrayList<String>(); |
| 82 | + private static final CreateChildThemeAction INSTANCE = new CreateChildThemeAction(); |
| 83 | + private static final Logger LOGGER = Logger.getLogger(CreateChildThemeAction.class.getName()); |
| 84 | + private static final long serialVersionUID = -2231810352652363996L; |
| 85 | + |
| 86 | + private CreateChildThemeAction() { |
| 87 | + } |
| 88 | + |
| 89 | + public static CreateChildThemeAction getInstance() { |
| 90 | + return INSTANCE; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected String getFullName() { |
| 95 | + return getPureName(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + @NbBundle.Messages("LBL_CreateChildThemeBaseAction=Create Child Theme") |
| 100 | + protected String getPureName() { |
| 101 | + return Bundle.LBL_CreateChildThemeBaseAction(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + @NbBundle.Messages({ |
| 106 | + "CreateChildThemeAction.notFound.template=Not found : style.css template file for child theme", |
| 107 | + "CreateChildThemeAction.childFolder.error=Can't create a child theme folder" |
| 108 | + }) |
| 109 | + protected void actionPerformed(PhpModule phpModule) { |
| 110 | + if (!WPUtils.isWP(phpModule)) { |
| 111 | + // called via shortcut |
| 112 | + return; |
| 113 | + } |
| 114 | + themes.clear(); |
| 115 | + |
| 116 | + // get themes directory |
| 117 | + WordPressModule wpModule = WordPressModule.Factory.forPhpModule(phpModule); |
| 118 | + if (wpModule == null) { |
| 119 | + return; |
| 120 | + } |
| 121 | + final FileObject themesDirectory = wpModule.getThemesDirectory(); |
| 122 | + if (themesDirectory == null) { |
| 123 | + return; |
| 124 | + } |
| 125 | + for (FileObject child : themesDirectory.getChildren()) { |
| 126 | + if (child.isFolder()) { |
| 127 | + themes.add(child.getNameExt()); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (themes.isEmpty()) { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + Collections.sort(themes); |
| 136 | + |
| 137 | + // show dialog |
| 138 | + SwingUtilities.invokeLater(new Runnable() { |
| 139 | + |
| 140 | + @Override |
| 141 | + public void run() { |
| 142 | + getDescriptor().setValid(false); |
| 143 | + DialogDisplayer displayer = DialogDisplayer.getDefault(); |
| 144 | + if (displayer.notify(getDescriptor()) == DialogDescriptor.OK_OPTION) { |
| 145 | + try { |
| 146 | + createChildTheme(themesDirectory); |
| 147 | + } catch (IOException ex) { |
| 148 | + Exceptions.printStackTrace(ex); |
| 149 | + } |
| 150 | + } |
| 151 | + clear(); |
| 152 | + } |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + private void createChildTheme(FileObject themesDirectory) throws IOException, DataObjectNotFoundException { |
| 157 | + // get template |
| 158 | + FileObject template = FileUtil.getConfigFile("Templates/WordPress/child-style.css"); // NOI18N |
| 159 | + if (template == null) { |
| 160 | + LOGGER.log(Level.WARNING, Bundle.CreateChildThemeAction_notFound_template()); |
| 161 | + return; |
| 162 | + } |
| 163 | + // create child theme directory and style.css |
| 164 | + FileObject childThemeDirectory = themesDirectory.createFolder(getPanel().getChildDirectoryName()); |
| 165 | + if (childThemeDirectory == null) { |
| 166 | + LOGGER.log(Level.WARNING, Bundle.CreateChildThemeAction_childFolder_error()); |
| 167 | + return; |
| 168 | + } |
| 169 | + DataObject templateDataObject = DataObject.find(template); |
| 170 | + DataFolder targetFolder = DataFolder.findFolder(childThemeDirectory); |
| 171 | + Map<String, String> parameters = new HashMap<String, String>(); |
| 172 | + parameters.put("name", getPanel().getChildThemeName()); // NOI18N |
| 173 | + parameters.put("parent", getPanel().getParentThemeName()); // NOI18N |
| 174 | + parameters.put("uri", getPanel().getChildThemeUri()); // NOI18N |
| 175 | + parameters.put("description", getPanel().getDescription()); // NOI18N |
| 176 | + parameters.put("version", getPanel().getVersion()); // NOI18N |
| 177 | + parameters.put("author", getPanel().getAuthor()); // NOI18N |
| 178 | + parameters.put("authorUri", getPanel().getAuthorUri()); // NOI18N |
| 179 | + parameters.put("tags", getPanel().getTags()); // NOI18N |
| 180 | + parameters.put("textDomain", getPanel().getTextDomain()); // NOI18N |
| 181 | + DataObject styleCssDataObject = templateDataObject.createFromTemplate(targetFolder, "style.css", parameters); // NOI18N |
| 182 | + if (styleCssDataObject != null) { |
| 183 | + UiUtils.open(styleCssDataObject.getPrimaryFile(), 0); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + private void clear() { |
| 188 | + if (panel != null) { |
| 189 | + panel.removeChangeListener(this); |
| 190 | + } |
| 191 | + panel = null; |
| 192 | + descriptor = null; |
| 193 | + themes.clear(); |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public void stateChanged(ChangeEvent e) { |
| 198 | + if (descriptor == null || panel == null) { |
| 199 | + return; |
| 200 | + } |
| 201 | + String childThemeName = getPanel().getChildDirectoryName(); |
| 202 | + WordPressDirectoryNameValidator validator = new WordPressDirectoryNameValidator(); |
| 203 | + ValidationResult result = validator.validateName(childThemeName) |
| 204 | + .validateExistingName(childThemeName, themes) |
| 205 | + .getResult(); |
| 206 | + |
| 207 | + // errors |
| 208 | + List<ValidationResult.Message> errors = result.getErrors(); |
| 209 | + if (!errors.isEmpty()) { |
| 210 | + getDescriptor().setValid(false); |
| 211 | + getPanel().setError(result.getErrors().get(0).getMessage()); |
| 212 | + return; |
| 213 | + } |
| 214 | + |
| 215 | + // warnings |
| 216 | + List<ValidationResult.Message> warnings = result.getWarnings(); |
| 217 | + if (!warnings.isEmpty()) { |
| 218 | + getDescriptor().setValid(false); |
| 219 | + getPanel().setError(result.getWarnings().get(0).getMessage()); |
| 220 | + return; |
| 221 | + } |
| 222 | + |
| 223 | + // everything ok |
| 224 | + getDescriptor().setValid(true); |
| 225 | + getPanel().setError(" "); // NOI18N |
| 226 | + } |
| 227 | + |
| 228 | + private CreateChildThemePanel getPanel() { |
| 229 | + if (panel == null) { |
| 230 | + panel = new CreateChildThemePanel(themes); |
| 231 | + panel.addChangeListener(this); |
| 232 | + } |
| 233 | + return panel; |
| 234 | + } |
| 235 | + |
| 236 | + private DialogDescriptor getDescriptor() { |
| 237 | + if (descriptor == null) { |
| 238 | + descriptor = new DialogDescriptor(getPanel(), Bundle.LBL_CreateChildThemeBaseAction(), true, DialogDescriptor.OK_CANCEL_OPTION, null, null); |
| 239 | + } |
| 240 | + return descriptor; |
| 241 | + } |
| 242 | + |
| 243 | +} |
0 commit comments