4242package org .netbeans .modules .php .wordpress .ui .actions ;
4343
4444import java .awt .event .ActionEvent ;
45- import java .awt .event .ActionListener ;
4645import java .io .IOException ;
4746import java .util .logging .Level ;
4847import java .util .logging .Logger ;
49- import org .netbeans .modules .php .wordpress .WordPress ;
48+ import javax .swing .AbstractAction ;
49+ import javax .swing .Action ;
50+ import org .netbeans .modules .php .api .phpmodule .PhpModule ;
5051import org .netbeans .modules .php .wordpress .util .WPFileUtils ;
52+ import org .netbeans .modules .php .wordpress .util .WPUtils ;
5153import org .openide .DialogDisplayer ;
5254import org .openide .NotifyDescriptor ;
5355import org .openide .awt .ActionID ;
5456import org .openide .awt .ActionReference ;
55- import org .openide .awt .ActionReferences ;
5657import org .openide .awt .ActionRegistration ;
5758import org .openide .filesystems .FileObject ;
5859import org .openide .loaders .DataObject ;
60+ import org .openide .util .ContextAwareAction ;
61+ import org .openide .util .Lookup ;
5962import org .openide .util .NbBundle .Messages ;
6063
61- @ ActionID (
62- category = "File" ,
63- id = "org.netbeans.modules.php.wordpress.ui.actions.ZipAction" )
64- @ ActionRegistration (
65- iconBase = WordPress .WP_ICON_16 ,
66- displayName = "#CTL_ZipAction" )
67- @ ActionReferences ({
68- @ ActionReference (path = "Menu/File" , position = 2700 ),
69- @ ActionReference (path = "Loaders/folder/any/Actions" , position = 1650 )
70- })
64+ @ ActionID (category = "File" , id = "org.netbeans.modules.php.wordpress.ui.actions.ZipAction" )
65+ @ ActionRegistration (displayName = "#CTL_ZipAction" , lazy = false )
66+ @ ActionReference (path = "Loaders/folder/any/Actions" , position = 1650 )
7167@ Messages ("CTL_ZipAction=WordPress Zip compress" )
72- public final class ZipAction implements ActionListener {
68+ public final class ZipAction extends AbstractAction implements ContextAwareAction {
7369
74- private final DataObject context ;
70+ private static final long serialVersionUID = 1781415535743693501L ;
71+
72+ private DataObject context ;
7573 private static final Logger LOGGER = Logger .getLogger (ZipAction .class .getName ());
7674
75+ public ZipAction () {
76+ this (null );
77+ }
78+
7779 public ZipAction (DataObject context ) {
7880 this .context = context ;
81+ if (context == null ) {
82+ putValue (Action .NAME , "" ); // NOI18N
83+ } else {
84+ putValue (Action .NAME , Bundle .CTL_ZipAction ()); // NOI18N
85+ }
86+ setEnabled (context != null );
7987 }
8088
8189 @ Messages ({
@@ -84,7 +92,7 @@ public ZipAction(DataObject context) {
8492 })
8593 @ Override
8694 public void actionPerformed (ActionEvent ev ) {
87- if (!isValidDirectory ()) {
95+ if (!isValidDirectory (getFileObject () )) {
8896 return ;
8997 }
9098
@@ -114,8 +122,10 @@ private FileObject getFileObject() {
114122 * @return true if parent directory name is "plugins" or "themes", otherwise
115123 * false.
116124 */
117- private boolean isValidDirectory () {
118- FileObject target = getFileObject ();
125+ private boolean isValidDirectory (FileObject target ) {
126+ if (target == null ) {
127+ return false ;
128+ }
119129 FileObject parent = target .getParent ();
120130 String name = parent .getNameExt ();
121131 if (parent .isFolder ()) {
@@ -142,4 +152,24 @@ private void showErrorDialog(String errorMessage) {
142152 NotifyDescriptor .Message message = new NotifyDescriptor .Message (errorMessage , NotifyDescriptor .ERROR_MESSAGE );
143153 DialogDisplayer .getDefault ().notify (message );
144154 }
155+
156+ @ Override
157+ public Action createContextAwareInstance (Lookup actionContext ) {
158+ DataObject dataObject = actionContext .lookup (DataObject .class );
159+ if (dataObject == null ) {
160+ return this ;
161+ }
162+ FileObject fileObject = dataObject .getPrimaryFile ();
163+ PhpModule phpModule = PhpModule .Factory .forFileObject (fileObject );
164+ if (phpModule == null ) {
165+ return this ;
166+ }
167+ if (!WPUtils .isWP (phpModule )) {
168+ return this ;
169+ }
170+ if (!isValidDirectory (fileObject )) {
171+ return this ;
172+ }
173+ return new ZipAction (dataObject );
174+ }
145175}
0 commit comments