Skip to content

Commit a845fa2

Browse files
committed
Add notificatoin for upgrade #22
1 parent 9bb1de5 commit a845fa2

15 files changed

Lines changed: 852 additions & 4 deletions

nbproject/genfiles.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
build.xml.data.CRC32=a6abec14
1+
build.xml.data.CRC32=afe7c087
22
build.xml.script.CRC32=f6cbff90
33
build.xml.stylesheet.CRC32=a56c6a5b@2.62.1
44
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
55
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
6-
nbproject/build-impl.xml.data.CRC32=a6abec14
6+
nbproject/build-impl.xml.data.CRC32=afe7c087
77
nbproject/build-impl.xml.script.CRC32=bfa38a5f
88
nbproject/build-impl.xml.stylesheet.CRC32=238281d1@2.62.1

nbproject/project.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@
2424
<specification-version>1.32.1</specification-version>
2525
</run-dependency>
2626
</dependency>
27+
<dependency>
28+
<code-name-base>org.netbeans.libs.json_simple</code-name-base>
29+
<build-prerequisite/>
30+
<compile-dependency/>
31+
<run-dependency>
32+
<release-version>1</release-version>
33+
<specification-version>0.6.1</specification-version>
34+
</run-dependency>
35+
</dependency>
2736
<dependency>
2837
<code-name-base>org.netbeans.modules.csl.api</code-name-base>
2938
<build-prerequisite/>
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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;
43+
44+
import java.awt.event.ActionEvent;
45+
import java.awt.event.ActionListener;
46+
import java.io.IOException;
47+
import java.net.MalformedURLException;
48+
import java.util.Collections;
49+
import java.util.logging.Level;
50+
import java.util.logging.Logger;
51+
import org.netbeans.modules.php.api.executable.InvalidPhpExecutableException;
52+
import org.netbeans.modules.php.api.phpmodule.PhpModule;
53+
import org.netbeans.modules.php.api.util.StringUtils;
54+
import org.netbeans.modules.php.wordpress.wpapis.WordPressVersionCheckApi;
55+
import org.netbeans.modules.php.wordpress.commands.WordPressCli;
56+
import org.netbeans.modules.php.wordpress.ui.options.WordPressOptions;
57+
import org.openide.DialogDisplayer;
58+
import org.openide.NotifyDescriptor;
59+
import org.openide.awt.NotificationDisplayer;
60+
import org.openide.util.Exceptions;
61+
import org.openide.util.ImageUtilities;
62+
import org.openide.util.NbBundle;
63+
import org.openide.util.lookup.ServiceProvider;
64+
65+
/**
66+
*
67+
* @author junichi11
68+
*/
69+
@ServiceProvider(service = WordPressUpgradeChecker.class)
70+
public final class WordPressCoreUpgradeChecker implements WordPressUpgradeChecker {
71+
72+
private String upgradeVersionNumber;
73+
private static final Logger LOGGER = Logger.getLogger(WordPressCoreUpgradeChecker.class.getName());
74+
75+
public WordPressCoreUpgradeChecker() {
76+
}
77+
78+
@Override
79+
public boolean hasUpgrade(PhpModule phpModule) {
80+
try {
81+
if (StringUtils.isEmpty(upgradeVersionNumber)) {
82+
WordPressVersionCheckApi versionCheckApi = new WordPressVersionCheckApi();
83+
versionCheckApi.parse();
84+
upgradeVersionNumber = versionCheckApi.getVersion();
85+
if (StringUtils.isEmpty(upgradeVersionNumber)) {
86+
// XXX throw exception?
87+
return false;
88+
}
89+
}
90+
91+
WordPressVersion version = WordPressVersion.create(phpModule);
92+
return hasUpgrade(version);
93+
} catch (MalformedURLException ex) {
94+
LOGGER.log(Level.WARNING, ex.getLocalizedMessage());
95+
} catch (IOException ex) {
96+
LOGGER.log(Level.WARNING, "Isn't connected to Internet:{0}", ex.getLocalizedMessage());
97+
}
98+
99+
return false;
100+
}
101+
102+
/**
103+
* Compare with current version number.
104+
*
105+
* @param version current version
106+
* @return true new version available, false otherwise
107+
* @throws NumberFormatException
108+
*/
109+
private boolean hasUpgrade(WordPressVersion version) throws NumberFormatException {
110+
// compare version
111+
String[] splitVersion = upgradeVersionNumber.split("[.]"); // NOI18N
112+
int length = splitVersion.length;
113+
if (length >= 2) {
114+
int majorVersion = Integer.parseInt(splitVersion[0]);
115+
int minorVersion = Integer.parseInt(splitVersion[1]);
116+
if (version.getMajor() < majorVersion) {
117+
return true;
118+
} else if (version.isMajor(majorVersion)) {
119+
if (version.getMinor() < minorVersion) {
120+
return true;
121+
}
122+
}
123+
}
124+
if (length >= 3) {
125+
int revisionVersion = Integer.parseInt(splitVersion[2]);
126+
if (version.getRevision() < revisionVersion) {
127+
return true;
128+
}
129+
}
130+
return false;
131+
}
132+
133+
@NbBundle.Messages({
134+
"WordPressUpgradeChecker.core.notify.title=Notification: New version is available",
135+
"# {0} - version number",
136+
"# {1} - display name",
137+
"WordPressUpgradeChecker.core.notify.detail=New version is available: {0} ({1})",})
138+
@Override
139+
public void notifyUpgrade(PhpModule phpModule) {
140+
if (StringUtils.isEmpty(upgradeVersionNumber)) {
141+
return;
142+
}
143+
NotificationDisplayer.getDefault().notify(
144+
Bundle.WordPressUpgradeChecker_core_notify_title(),
145+
ImageUtilities.loadImageIcon(WordPress.WP_ICON_16, false),
146+
Bundle.WordPressUpgradeChecker_core_notify_detail(upgradeVersionNumber, phpModule.getDisplayName()),
147+
new CoreUpdateActionListener(phpModule)
148+
);
149+
}
150+
151+
public String getUpgradeVersionNumber() {
152+
return upgradeVersionNumber;
153+
}
154+
155+
//~ Inner classes
156+
private static class CoreUpdateActionListener implements ActionListener {
157+
158+
private final PhpModule phpModule;
159+
160+
public CoreUpdateActionListener(PhpModule phpModule) {
161+
this.phpModule = phpModule;
162+
}
163+
164+
@NbBundle.Messages("CoreUpdateActionListener.comfirmation=Do you want to update? (run wp core update)")
165+
@Override
166+
public void actionPerformed(ActionEvent e) {
167+
if (StringUtils.isEmpty(WordPressOptions.getInstance().getWpCliPath())) {
168+
return;
169+
}
170+
171+
// confirmation
172+
NotifyDescriptor.Confirmation comfirmation = new NotifyDescriptor.Confirmation(
173+
Bundle.CoreUpdateActionListener_comfirmation(),
174+
NotifyDescriptor.OK_CANCEL_OPTION
175+
);
176+
if (DialogDisplayer.getDefault().notify(comfirmation) != NotifyDescriptor.OK_OPTION) {
177+
return;
178+
}
179+
try {
180+
WordPressCli wpCli = WordPressCli.getDefault(true);
181+
wpCli.coreUpdate(phpModule, Collections.<String>emptyList());
182+
} catch (InvalidPhpExecutableException ex) {
183+
Exceptions.printStackTrace(ex);
184+
}
185+
}
186+
}
187+
188+
}

src/org/netbeans/modules/php/wordpress/WordPressPhpProvider.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
package org.netbeans.modules.php.wordpress;
4343

4444
import java.io.File;
45+
import java.util.Collection;
4546
import java.util.HashSet;
4647
import java.util.LinkedList;
4748
import java.util.List;
@@ -63,6 +64,7 @@
6364
import org.openide.filesystems.FileObject;
6465
import org.openide.filesystems.FileUtil;
6566
import org.openide.util.ImageUtilities;
67+
import org.openide.util.Lookup;
6668
import org.openide.util.NbBundle;
6769

6870
/**
@@ -173,4 +175,16 @@ public PhpModuleCustomizerExtender createPhpModuleCustomizerExtender(PhpModule p
173175
public EditorExtender getEditorExtender(PhpModule pm) {
174176
return new WordPressEditorExtender();
175177
}
178+
179+
@Override
180+
public void phpModuleOpened(PhpModule phpModule) {
181+
// check new version
182+
Collection<? extends WordPressUpgradeChecker> checkers = Lookup.getDefault().lookupAll(WordPressUpgradeChecker.class);
183+
for (WordPressUpgradeChecker checker : checkers) {
184+
if (checker.hasUpgrade(phpModule)) {
185+
checker.notifyUpgrade(phpModule);
186+
}
187+
}
188+
}
189+
176190
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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;
43+
44+
import org.netbeans.modules.php.api.phpmodule.PhpModule;
45+
46+
/**
47+
*
48+
* @author junichi11
49+
*/
50+
public interface WordPressUpgradeChecker {
51+
52+
public boolean hasUpgrade(PhpModule phpModule);
53+
54+
public void notifyUpgrade(PhpModule phpModule);
55+
56+
}

0 commit comments

Comments
 (0)