Skip to content

Commit 4257189

Browse files
committed
Improve the zip action #55
1 parent 8ae198e commit 4257189

2 files changed

Lines changed: 41 additions & 12 deletions

File tree

src/org/netbeans/modules/php/wordpress/ui/actions/ZipAction.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,41 @@ public ZipAction(DataObject context) {
8888

8989
@Messages({
9090
"# {0} - file name",
91-
"ZipAction.error.file.already.exist=Zip file ({0}) already exists."
91+
"ZipAction.error.file.already.exist=Zip file ({0}) already exists.\nDo you really want to overwrite it?",
92+
"ZipAction.error.not.directory=It must be selected a directory.",
93+
"ZipAction.error.invalid.parent.directory=The parent directory doesn't exist."
9294
})
9395
@Override
9496
public void actionPerformed(ActionEvent ev) {
9597
if (!isValidDirectory(getFileObject())) {
9698
return;
9799
}
98100

99-
// zip compress
100101
FileObject target = getFileObject();
102+
if (target == null || !target.isFolder()) {
103+
showErrorDialog(Bundle.ZipAction_error_not_directory());
104+
return;
105+
}
106+
107+
FileObject parent = target.getParent();
108+
if (parent == null) {
109+
showErrorDialog(Bundle.ZipAction_error_invalid_parent_directory());
110+
return;
111+
}
112+
113+
FileObject targetZip = parent.getFileObject(target.getName() + ".zip"); // NOI18N
114+
if (targetZip != null) {
115+
if (!showConfirmationDialog(Bundle.ZipAction_error_file_already_exist(target.getName() + ".zip"))) { // NOI18N
116+
return;
117+
}
118+
}
119+
120+
// zip compress
101121
try {
102122
WPFileUtils.zip(target);
103123
} catch (IOException ex) {
104124
// #36
105125
LOGGER.log(Level.WARNING, ex.getMessage());
106-
showErrorDialog(Bundle.ZipAction_error_file_already_exist(target.getName() + ".zip")); // NOI18N
107126
}
108127
}
109128

@@ -130,13 +149,6 @@ private boolean isValidDirectory(FileObject target) {
130149
String name = parent.getNameExt();
131150
if (parent.isFolder()) {
132151
if (name.equals("plugins") || name.equals("themes")) { // NOI18N
133-
// #36
134-
String zipFileName = target.getName() + ".zip"; // NOI18N
135-
FileObject zipFile = parent.getFileObject(zipFileName);
136-
if (zipFile != null) {
137-
showErrorDialog(Bundle.ZipAction_error_file_already_exist(zipFileName));
138-
return false;
139-
}
140152
return true;
141153
}
142154
}
@@ -153,6 +165,17 @@ private void showErrorDialog(String errorMessage) {
153165
DialogDisplayer.getDefault().notify(message);
154166
}
155167

168+
/**
169+
* Show confirmation message dialog.
170+
*
171+
* @param meesage the confirmation message
172+
* @return {@code true} if OK is pressed, otherwise {@code false}
173+
*/
174+
private boolean showConfirmationDialog(String meesage) {
175+
NotifyDescriptor.Confirmation message = new NotifyDescriptor.Confirmation(meesage, NotifyDescriptor.OK_CANCEL_OPTION, NotifyDescriptor.QUESTION_MESSAGE);
176+
return DialogDisplayer.getDefault().notify(message) == NotifyDescriptor.OK_OPTION;
177+
}
178+
156179
@Override
157180
public Action createContextAwareInstance(Lookup actionContext) {
158181
DataObject dataObject = actionContext.lookup(DataObject.class);

src/org/netbeans/modules/php/wordpress/util/WPFileUtils.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,14 @@ public static void zip(FileObject target) throws IOException {
8282
}
8383
// create zip file
8484
FileObject parent = target.getParent();
85-
FileObject zipFile = parent.createData(target.getName() + ".zip"); // NOI18N
86-
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(FileUtil.toFile(zipFile)));
85+
if (parent == null) {
86+
return;
87+
}
88+
FileObject targetZip = parent.getFileObject(target.getName() + ".zip"); // NOI18N
89+
if (targetZip == null) {
90+
targetZip = parent.createData(target.getName() + ".zip"); // NOI18N
91+
}
92+
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(FileUtil.toFile(targetZip)));
8793

8894
// compress
8995
File targetDirectory = FileUtil.toFile(target);

0 commit comments

Comments
 (0)