Skip to content

Commit b9bbe20

Browse files
jisedlacthurka
authored andcommitted
Snapshot file can become null in some special cases (snapshot deleted during opening)
- handle it for snapshot node - handle it for Save As action - handle it for snapshot viewer
1 parent 494f968 commit b9bbe20

File tree

10 files changed

+50
-39
lines changed

10 files changed

+50
-39
lines changed

visualvm/application/src/com/sun/tools/visualvm/application/snapshot/ApplicationSnapshot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void save(File directory) {
6666
}
6767

6868
public boolean supportsSaveAs() {
69-
return true;
69+
return getFile() != null;
7070
}
7171

7272
public void saveAs() {

visualvm/core/src/com/sun/tools/visualvm/core/snapshot/Bundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
LBL_Snapshots=Snapshots
2626

2727
LBL_Saving=Saving {0}...
28+
LBL_CannotSave=Snapshot cannot be saved.
2829

2930
LBL_SourceProperties=Source
3031
DESCR_SourceProperties=File or directory of the snapshot

visualvm/core/src/com/sun/tools/visualvm/core/snapshot/SnapshotDescriptor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.sun.tools.visualvm.core.datasource.descriptor.DataSourceDescriptor;
2929
import java.awt.Image;
3030
import java.io.File;
31+
import org.openide.util.NbBundle;
3132

3233
/**
3334
* Abstract implementation of DataSourceDescriptor for snapshots.
@@ -92,13 +93,12 @@ protected static String resolveSnapshotName(Snapshot snapshot) {
9293
if (persistedName != null) return persistedName;
9394

9495
File file = snapshot.getFile();
95-
if (file == null) return snapshot.toString();
96-
97-
String fileName = file.getName();
96+
String fileName = file != null ? file.getName() :
97+
NbBundle.getMessage(SnapshotDescriptor.class, "LBL_NoFile"); // NOI18N
9898
SnapshotCategory category = snapshot.getCategory();
9999
String name = "[" + category.getPrefix() + "] " + fileName; // NOI18N
100100

101-
if (category.isSnapshot(file)) {
101+
if (file != null && category.isSnapshot(file)) {
102102
String timeStamp = category.getTimeStamp(fileName);
103103
if (timeStamp != null) name = "[" + category.getPrefix() + "] " + timeStamp; // NOI18N
104104
}

visualvm/core/src/com/sun/tools/visualvm/core/snapshot/SnapshotsSupport.java

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import javax.swing.SwingUtilities;
3636
import org.netbeans.api.progress.ProgressHandle;
3737
import org.netbeans.api.progress.ProgressHandleFactory;
38+
import org.netbeans.modules.profiler.api.ProfilerDialogs;
3839
import org.openide.util.ImageUtilities;
3940
import org.openide.util.NbBundle;
4041
import org.openide.util.RequestProcessor;
@@ -71,33 +72,37 @@ public static synchronized SnapshotsSupport getInstance() {
7172
*/
7273
public void saveAs(final Snapshot snapshot, String dialogTitle) {
7374
final File file = snapshot.getFile();
74-
JFileChooser chooser = new JFileChooser();
75-
chooser.setDialogTitle(dialogTitle);
76-
chooser.setSelectedFile(new File(snapshot.getFile().getName()));
77-
chooser.setAcceptAllFileFilterUsed(false);
78-
chooser.setFileFilter(snapshot.getCategory().getFileFilter());
79-
// chooser.setFileView(category.getFileView());
80-
if (chooser.showSaveDialog(WindowManager.getDefault().getMainWindow()) == JFileChooser.APPROVE_OPTION) {
81-
String categorySuffix = snapshot.getCategory().getSuffix();
82-
String filePath = chooser.getSelectedFile().getAbsolutePath();
83-
if (!filePath.endsWith(categorySuffix)) filePath += categorySuffix;
84-
final File copy = new File(filePath);
85-
RequestProcessor.getDefault().post(new Runnable() {
86-
public void run() {
87-
ProgressHandle pHandle = null;
88-
try {
89-
pHandle = ProgressHandleFactory.createHandle(NbBundle.getMessage(SnapshotsSupport.class, "LBL_Saving",DataSourceDescriptorFactory.getDescriptor(snapshot).getName())); // NOI18N
90-
pHandle.setInitialDelay(0);
91-
pHandle.start();
92-
Utils.copyFile(file, copy);
93-
} finally {
94-
final ProgressHandle pHandleF = pHandle;
95-
SwingUtilities.invokeLater(new Runnable() {
96-
public void run() { if (pHandleF != null) pHandleF.finish(); }
97-
});
75+
if (file == null) {
76+
ProfilerDialogs.displayError(NbBundle.getMessage(SnapshotsSupport.class, "LBL_CannotSave")); // NOI18N
77+
} else {
78+
JFileChooser chooser = new JFileChooser();
79+
chooser.setDialogTitle(dialogTitle);
80+
chooser.setSelectedFile(new File(snapshot.getFile().getName()));
81+
chooser.setAcceptAllFileFilterUsed(false);
82+
chooser.setFileFilter(snapshot.getCategory().getFileFilter());
83+
// chooser.setFileView(category.getFileView());
84+
if (chooser.showSaveDialog(WindowManager.getDefault().getMainWindow()) == JFileChooser.APPROVE_OPTION) {
85+
String categorySuffix = snapshot.getCategory().getSuffix();
86+
String filePath = chooser.getSelectedFile().getAbsolutePath();
87+
if (!filePath.endsWith(categorySuffix)) filePath += categorySuffix;
88+
final File copy = new File(filePath);
89+
RequestProcessor.getDefault().post(new Runnable() {
90+
public void run() {
91+
ProgressHandle pHandle = null;
92+
try {
93+
pHandle = ProgressHandleFactory.createHandle(NbBundle.getMessage(SnapshotsSupport.class, "LBL_Saving",DataSourceDescriptorFactory.getDescriptor(snapshot).getName())); // NOI18N
94+
pHandle.setInitialDelay(0);
95+
pHandle.start();
96+
Utils.copyFile(file, copy);
97+
} finally {
98+
final ProgressHandle pHandleF = pHandle;
99+
SwingUtilities.invokeLater(new Runnable() {
100+
public void run() { if (pHandleF != null) pHandleF.finish(); }
101+
});
102+
}
98103
}
99-
}
100-
});
104+
});
105+
}
101106
}
102107
}
103108

visualvm/coredump/src/com/sun/tools/visualvm/coredump/CoreDump.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public final String getJDKHome() {
105105
}
106106

107107
public boolean supportsSaveAs() {
108-
return true;
108+
return getFile() != null;
109109
}
110110

111111
public void saveAs() {

visualvm/heapdump/src/com/sun/tools/visualvm/heapdump/HeapDump.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public HeapDump(File file, DataSource master) {
5959
}
6060

6161
public boolean supportsSaveAs() {
62-
return true;
62+
return getFile() != null;
6363
}
6464

6565
public void saveAs() {

visualvm/heapdump/src/com/sun/tools/visualvm/heapdump/impl/Bundle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ LBL_Creating_Heap_Dump=Creating Heap Dump...
4343

4444
LBL_Loading_Heap_Dump=Loading Heap Dump...
4545

46+
LBL_Loading_Heap_Dump_failed=Loading Heap Dump failed.
47+
4648
CAPTION_Remote_heap_dump=Remote Heap Dump
4749

4850
MSG_Remote_heap_dump=&Heap dump file to be created on the remote system\:

visualvm/heapdump/src/com/sun/tools/visualvm/heapdump/impl/HeapDumpView.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ private static class MasterViewSupport extends JPanel {
107107
private JPanel contentsPanel;
108108

109109
public MasterViewSupport(HeapDump heapDump) {
110-
initComponents();
111-
loadHeap(heapDump.getFile());
110+
File file = heapDump.getFile();
111+
initComponents(file != null);
112+
if (file != null) loadHeap(file);
112113
}
113114

114115

@@ -117,10 +118,12 @@ public DataViewComponent.MasterView getMasterView() {
117118
}
118119

119120

120-
private void initComponents() {
121+
private void initComponents(boolean hasDump) {
121122
setLayout(new BorderLayout());
122123

123-
progressLabel = new JLabel(NbBundle.getMessage(HeapDumpView.class, "LBL_Loading_Heap_Dump"), SwingConstants.CENTER); // NOI18N
124+
String label = hasDump ? NbBundle.getMessage(HeapDumpView.class, "LBL_Loading_Heap_Dump") : // NOI18N
125+
NbBundle.getMessage(HeapDumpView.class, "LBL_Loading_Heap_Dump_failed"); // NOI18N
126+
progressLabel = new JLabel(label, SwingConstants.CENTER);
124127

125128
contentsPanel = new JPanel(new BorderLayout());
126129
contentsPanel.add(progressLabel, BorderLayout.CENTER);

visualvm/profiling/src/com/sun/tools/visualvm/profiling/snapshot/ProfilerSnapshot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public ProfilerSnapshot(File file, DataSource master) {
6767

6868
@Override
6969
public boolean supportsSaveAs() {
70-
return true;
70+
return getFile() != null;
7171
}
7272

7373
@Override

visualvm/threaddump/src/com/sun/tools/visualvm/threaddump/ThreadDump.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public ThreadDump(File file, DataSource master) {
5959
}
6060

6161
public boolean supportsSaveAs() {
62-
return true;
62+
return getFile() != null;
6363
}
6464

6565
public void saveAs() {

0 commit comments

Comments
 (0)