|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright 2014 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 2014 Sun Microsystems, Inc. |
| 41 | + */ |
| 42 | +package org.netbeans.modules.php.wordpress.ui.actions; |
| 43 | + |
| 44 | +import java.io.IOException; |
| 45 | +import java.util.logging.Level; |
| 46 | +import java.util.logging.Logger; |
| 47 | +import org.netbeans.modules.php.api.phpmodule.PhpModule; |
| 48 | +import org.netbeans.modules.php.spi.framework.actions.BaseAction; |
| 49 | +import org.netbeans.modules.php.wordpress.modules.WordPressModule; |
| 50 | +import org.netbeans.modules.php.wordpress.ui.wizards.CreatePermalinkHtaccessPanel; |
| 51 | +import org.netbeans.modules.php.wordpress.util.WPUtils; |
| 52 | +import org.openide.DialogDescriptor; |
| 53 | +import org.openide.DialogDisplayer; |
| 54 | +import org.openide.NotifyDescriptor; |
| 55 | +import org.openide.filesystems.FileObject; |
| 56 | +import org.openide.filesystems.FileUtil; |
| 57 | +import org.openide.util.Exceptions; |
| 58 | +import org.openide.util.NbBundle; |
| 59 | + |
| 60 | +/** |
| 61 | + * |
| 62 | + * @author junichi11 |
| 63 | + */ |
| 64 | +public class CreatePermalinkHtaccessAction extends BaseAction { |
| 65 | + |
| 66 | + private static final long serialVersionUID = -9023009833256287021L; |
| 67 | + private static final CreatePermalinkHtaccessAction INSTANCE = new CreatePermalinkHtaccessAction(); |
| 68 | + private static final Logger LOGGER = Logger.getLogger(CreatePermalinkHtaccessAction.class.getName()); |
| 69 | + |
| 70 | + public static CreatePermalinkHtaccessAction getInstance() { |
| 71 | + return INSTANCE; |
| 72 | + } |
| 73 | + |
| 74 | + private CreatePermalinkHtaccessAction() { |
| 75 | + } |
| 76 | + |
| 77 | + @NbBundle.Messages("WordPressAction.name=WordPress: ") |
| 78 | + @Override |
| 79 | + protected String getFullName() { |
| 80 | + return Bundle.WordPressAction_name() + getPureName(); |
| 81 | + } |
| 82 | + |
| 83 | + @NbBundle.Messages("CreatePermalinkHtaccessAction.name=Create .htaccess for permalink") |
| 84 | + @Override |
| 85 | + protected String getPureName() { |
| 86 | + return Bundle.CreatePermalinkHtaccessAction_name(); |
| 87 | + } |
| 88 | + |
| 89 | + @NbBundle.Messages({ |
| 90 | + "CreatePermalinkHtaccessAction.panel.message=<html>.htaccess file already exists. <br>Do you really want to overwrite it?", |
| 91 | + "# {0} - description", |
| 92 | + "CreatePermalinkHtaccessAction.error=Create permalink .htaccess action: {0}", |
| 93 | + "CreatePermalinkHtaccessAction.error.wproot=Not fond WordPress root", |
| 94 | + "CreatePermalinkHtaccessAction.error.template=Not fond template", |
| 95 | + "CreatePermalinkHtaccessAction.error.template.text=Can't get template text", |
| 96 | + "CreatePermalinkHtaccessAction.error.copy=Can't copy tempate file"}) |
| 97 | + @Override |
| 98 | + protected void actionPerformed(PhpModule pm) { |
| 99 | + // called via shortcut |
| 100 | + if (!WPUtils.isWP(pm)) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + WordPressModule wpModule = WordPressModule.Factory.forPhpModule(pm); |
| 105 | + FileObject wordPressRootDirecotry = wpModule.getWordPressRootDirecotry(); |
| 106 | + if (wordPressRootDirecotry == null) { |
| 107 | + LOGGER.log(Level.WARNING, Bundle.CreatePermalinkHtaccessAction_error(Bundle.CreatePermalinkHtaccessAction_error_wproot())); |
| 108 | + return; |
| 109 | + } |
| 110 | + FileObject template = FileUtil.getConfigFile("Templates/WordPress/.htaccess"); // NOI18N |
| 111 | + if (template == null) { |
| 112 | + LOGGER.log(Level.WARNING, Bundle.CreatePermalinkHtaccessAction_error(Bundle.CreatePermalinkHtaccessAction_error_template())); |
| 113 | + return; |
| 114 | + } |
| 115 | + String contents = null; |
| 116 | + try { |
| 117 | + contents = template.asText("UTF-8"); // NOI18N |
| 118 | + } catch (IOException ex) { |
| 119 | + Exceptions.printStackTrace(ex); |
| 120 | + } |
| 121 | + if (contents == null || contents.isEmpty()) { |
| 122 | + LOGGER.log(Level.WARNING, Bundle.CreatePermalinkHtaccessAction_error(Bundle.CreatePermalinkHtaccessAction_error_template_text())); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + // check whether file already exits |
| 127 | + FileObject htaccessFile = wordPressRootDirecotry.getFileObject(".htaccess"); |
| 128 | + if (htaccessFile != null) { |
| 129 | + CreatePermalinkHtaccessPanel panel = new CreatePermalinkHtaccessPanel() |
| 130 | + .setMessage(Bundle.CreatePermalinkHtaccessAction_panel_message()) |
| 131 | + .setContents(contents); |
| 132 | + DialogDescriptor dialogDescriptor = new DialogDescriptor(panel, "Confirmation", true, DialogDescriptor.YES_NO_OPTION, null, null); |
| 133 | + if (DialogDisplayer.getDefault().notify(dialogDescriptor) != NotifyDescriptor.YES_OPTION) { |
| 134 | + return; |
| 135 | + } |
| 136 | + } |
| 137 | + try { |
| 138 | + if (htaccessFile != null) { |
| 139 | + htaccessFile.delete(); |
| 140 | + } |
| 141 | + template.copy(wordPressRootDirecotry, template.getName(), template.getExt()); |
| 142 | + } catch (IOException ex) { |
| 143 | + LOGGER.log(Level.WARNING, Bundle.CreatePermalinkHtaccessAction_error(Bundle.CreatePermalinkHtaccessAction_error_copy())); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments