|
41 | 41 | */ |
42 | 42 | package org.netbeans.modules.php.wordpress; |
43 | 43 |
|
44 | | -import org.netbeans.modules.php.wordpress.update.WordPressUpgradeChecker; |
| 44 | +import java.awt.event.ActionEvent; |
| 45 | +import java.awt.event.ActionListener; |
| 46 | +import java.beans.PropertyChangeEvent; |
45 | 47 | import java.io.File; |
46 | 48 | import java.util.Collection; |
47 | 49 | import java.util.HashSet; |
48 | 50 | import java.util.LinkedList; |
49 | 51 | import java.util.List; |
50 | 52 | import java.util.Set; |
| 53 | +import java.util.concurrent.TimeUnit; |
51 | 54 | import org.netbeans.modules.php.api.framework.BadgeIcon; |
52 | 55 | import org.netbeans.modules.php.api.phpmodule.PhpModule; |
53 | 56 | import org.netbeans.modules.php.api.phpmodule.PhpModuleProperties; |
| 57 | +import org.netbeans.modules.php.api.validation.ValidationResult; |
54 | 58 | import org.netbeans.modules.php.spi.editor.EditorExtender; |
55 | 59 | import org.netbeans.modules.php.spi.framework.PhpFrameworkProvider; |
56 | 60 | import org.netbeans.modules.php.spi.framework.PhpModuleActionsExtender; |
|
61 | 65 | import org.netbeans.modules.php.wordpress.commands.WordPressCommandSupport; |
62 | 66 | import org.netbeans.modules.php.wordpress.customizer.WordPressCustomizerExtender; |
63 | 67 | import org.netbeans.modules.php.wordpress.editor.WordPressEditorExtender; |
| 68 | +import org.netbeans.modules.php.wordpress.modules.WordPressModule; |
64 | 69 | import org.netbeans.modules.php.wordpress.preferences.WordPressPreferences; |
| 70 | +import org.netbeans.modules.php.wordpress.update.WordPressUpgradeChecker; |
| 71 | +import org.netbeans.modules.php.wordpress.validators.WordPressModuleValidator; |
| 72 | +import org.openide.awt.Notification; |
| 73 | +import org.openide.awt.NotificationDisplayer; |
65 | 74 | import org.openide.filesystems.FileObject; |
66 | 75 | import org.openide.filesystems.FileUtil; |
67 | 76 | import org.openide.util.ImageUtilities; |
68 | 77 | import org.openide.util.Lookup; |
69 | 78 | import org.openide.util.NbBundle; |
| 79 | +import org.openide.util.RequestProcessor; |
70 | 80 |
|
71 | 81 | /** |
72 | 82 | * |
73 | 83 | * @author junichi11 |
74 | 84 | */ |
75 | 85 | public class WordPressPhpProvider extends PhpFrameworkProvider { |
76 | 86 |
|
| 87 | + private static final RequestProcessor RP = new RequestProcessor(WordPressPhpProvider.class); |
77 | 88 | private static final WordPressPhpProvider INSTANCE = new WordPressPhpProvider(); |
78 | 89 | private static final String ICON_PATH = "org/netbeans/modules/php/wordpress/resources/wordpress_badge_8.png"; // NOI18N |
79 | 90 | private final BadgeIcon badgeIcon; |
80 | | - private static final Set<String> WP_DIRS = new HashSet<String>(); |
| 91 | + public static final Set<String> WP_DIRS = new HashSet<String>(); |
81 | 92 |
|
82 | 93 | static { |
83 | 94 | WP_DIRS.add("wp-admin"); // NOI18N |
@@ -108,30 +119,16 @@ private WordPressPhpProvider() { |
108 | 119 |
|
109 | 120 | @Override |
110 | 121 | public boolean isInPhpModule(PhpModule pm) { |
111 | | - FileObject sourceDirectory = pm.getSourceDirectory(); |
112 | | - if (sourceDirectory != null) { |
113 | | - for (String dir : WP_DIRS) { |
114 | | - FileObject fileObject = sourceDirectory.getFileObject(dir); |
115 | | - if (fileObject == null) { |
116 | | - return false; |
117 | | - } |
118 | | - } |
119 | | - |
120 | | - // content name |
121 | | - FileObject content = sourceDirectory.getFileObject(WordPressPreferences.getCustomContentName(pm)); |
122 | | - if (content == null) { |
123 | | - return false; |
124 | | - } |
125 | | - } |
126 | | - return true; |
| 122 | + return WordPressPreferences.isEnabled(pm); |
127 | 123 | } |
128 | 124 |
|
129 | 125 | @Override |
130 | 126 | public File[] getConfigurationFiles(PhpModule pm) { |
131 | 127 | List<File> files = new LinkedList<File>(); |
132 | | - FileObject sourceDirectory = pm.getSourceDirectory(); |
133 | | - if (sourceDirectory != null) { |
134 | | - FileObject config = sourceDirectory.getFileObject("wp-config.php"); // NOI18N |
| 128 | + WordPressModule wpModuel = WordPressModule.Factory.forPhpModule(pm); |
| 129 | + FileObject wordPressRoot = wpModuel.getWordPressRootDirecotry(); |
| 130 | + if (wordPressRoot != null) { |
| 131 | + FileObject config = wordPressRoot.getFileObject("wp-config.php"); // NOI18N |
135 | 132 | if (config != null) { |
136 | 133 | files.add(FileUtil.toFile(config)); |
137 | 134 | } |
@@ -177,13 +174,81 @@ public EditorExtender getEditorExtender(PhpModule pm) { |
177 | 174 | return new WordPressEditorExtender(); |
178 | 175 | } |
179 | 176 |
|
| 177 | + @Override |
| 178 | + public void phpModuleClosed(PhpModule phpModule) { |
| 179 | + WordPressModule.Factory.remove(phpModule); |
| 180 | + } |
| 181 | + |
| 182 | + @NbBundle.Messages({ |
| 183 | + "# {0} - name", |
| 184 | + "WordPressPhpProvider.autoditection=WordPress autoditection : {0}", |
| 185 | + "WordPressPhpProvider.autoditection.action=If you want to enable as WordPress project, please click here." |
| 186 | + }) |
180 | 187 | @Override |
181 | 188 | public void phpModuleOpened(PhpModule phpModule) { |
182 | | - // check new version |
183 | | - Collection<? extends WordPressUpgradeChecker> checkers = Lookup.getDefault().lookupAll(WordPressUpgradeChecker.class); |
184 | | - for (WordPressUpgradeChecker checker : checkers) { |
185 | | - if (checker.hasUpgrade(phpModule)) { |
186 | | - checker.notifyUpgrade(phpModule); |
| 189 | + // this method is run for not only WordPress projects but also all php projects, |
| 190 | + // so need check i.e. avoid NPE |
| 191 | + if (isInPhpModule(phpModule)) { |
| 192 | + // check new version |
| 193 | + Collection<? extends WordPressUpgradeChecker> checkers = Lookup.getDefault().lookupAll(WordPressUpgradeChecker.class); |
| 194 | + for (WordPressUpgradeChecker checker : checkers) { |
| 195 | + if (checker.hasUpgrade(phpModule)) { |
| 196 | + checker.notifyUpgrade(phpModule); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + RP.schedule(new WordPressAutoDetectionTask(phpModule), 1, TimeUnit.MINUTES); |
| 201 | + } |
| 202 | + |
| 203 | + //~ Inner class |
| 204 | + private static class WordPressAutoDetectionTask implements Runnable { |
| 205 | + |
| 206 | + private final PhpModule phpModule; |
| 207 | + private Notification notification; |
| 208 | + |
| 209 | + public WordPressAutoDetectionTask(PhpModule phpModule) { |
| 210 | + this.phpModule = phpModule; |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public void run() { |
| 215 | + // auto detection |
| 216 | + FileObject wordPressRoot = phpModule.getSourceDirectory(); |
| 217 | + if (wordPressRoot == null) { |
| 218 | + return; |
| 219 | + } |
| 220 | + ValidationResult result = new WordPressModuleValidator() |
| 221 | + .validateWordPressDirectories(wordPressRoot, WordPressPreferences.getCustomContentName(phpModule)) |
| 222 | + .getResult(); |
| 223 | + if (result.hasWarnings()) { |
| 224 | + return; |
| 225 | + } |
| 226 | + |
| 227 | + // show notification displayer |
| 228 | + if (!WordPressPreferences.isEnabled(phpModule)) { |
| 229 | + NotificationDisplayer notificationDisplayer = NotificationDisplayer.getDefault(); |
| 230 | + notification = notificationDisplayer.notify( |
| 231 | + Bundle.WordPressPhpProvider_autoditection(phpModule.getDisplayName()), // title |
| 232 | + NotificationDisplayer.Priority.LOW.getIcon(), // icon |
| 233 | + Bundle.WordPressPhpProvider_autoditection_action(), |
| 234 | + new WordPressAutoDetectionActionListener(), // action |
| 235 | + NotificationDisplayer.Priority.LOW); // priority |
| 236 | + } |
| 237 | + |
| 238 | + } |
| 239 | + |
| 240 | + private class WordPressAutoDetectionActionListener implements ActionListener { |
| 241 | + |
| 242 | + public WordPressAutoDetectionActionListener() { |
| 243 | + } |
| 244 | + |
| 245 | + @Override |
| 246 | + public void actionPerformed(ActionEvent e) { |
| 247 | + WordPressPreferences.setEnabled(phpModule, true); |
| 248 | + WordPressModule module = WordPressModule.Factory.forPhpModule(phpModule); |
| 249 | + module.notifyPropertyChanged(new PropertyChangeEvent(this, WordPressModule.PROPERTY_CHANGE_WP, null, null)); |
| 250 | + phpModule.notifyPropertyChanged(new PropertyChangeEvent(this, PhpModule.PROPERTY_FRAMEWORKS, null, null)); |
| 251 | + notification.clear(); |
187 | 252 | } |
188 | 253 | } |
189 | 254 | } |
|
0 commit comments