34
34
import java .beans .PropertyChangeEvent ;
35
35
import java .beans .PropertyChangeListener ;
36
36
import java .beans .PropertyChangeSupport ;
37
+ import org .graalvm .visualvm .core .datasupport .Stateful ;
38
+ import org .graalvm .visualvm .core .datasupport .Utils ;
37
39
38
40
/**
39
41
* DataSourceDescriptor for Application.
40
42
*
41
43
* @author Jiri Sedlacek
42
44
*/
43
- public class ApplicationDescriptor extends DataSourceDescriptor <Application > {
45
+ public class ApplicationDescriptor extends DataSourceDescriptor <Application > implements PropertyChangeListener {
44
46
45
47
private static final String DISPLAY_NAME_PROPERTY = "-Dvisualvm.display.name=" ; // NOI18N
46
48
@@ -49,6 +51,9 @@ public class ApplicationDescriptor extends DataSourceDescriptor<Application> {
49
51
50
52
51
53
private String name ;
54
+
55
+ private ApplicationType type ;
56
+
52
57
53
58
/**
54
59
* Creates new instance of Application Descriptor.
@@ -73,38 +78,13 @@ protected ApplicationDescriptor(Application application, int preferredPosition)
73
78
preferredPosition );
74
79
}
75
80
76
- private ApplicationDescriptor (final Application application , final ApplicationType type ,
77
- int preferredPosition ) {
78
- super ( application , resolveApplicationName (application , type ), type . getDescription (),
79
- type . getIcon (), preferredPosition , EXPAND_ON_EACH_FIRST_CHILD );
81
+ protected ApplicationDescriptor (final Application application , final ApplicationType type , int preferredPosition ) {
82
+ super ( application , resolveApplicationName ( application , type ), resolveApplicationDescription ( application , type ),
83
+ resolveApplicationIcon (application , type ), preferredPosition , EXPAND_ON_EACH_FIRST_CHILD );
84
+
80
85
name = super .getName ();
81
- type .addPropertyChangeListener (new PropertyChangeListener () {
82
- public void propertyChange (PropertyChangeEvent evt ) {
83
- String propertyName = evt .getPropertyName ();
84
- if (ApplicationType .PROPERTY_NAME .equals (propertyName )) {
85
- // Name already customized by the user, do not change it
86
- if (resolveName (application , null ) != null ) return ;
87
-
88
- if (supportsRename ()) {
89
- // Descriptor supports renaming, use setName(), sync name
90
- setName ((String )evt .getNewValue ());
91
- name = ApplicationDescriptor .super .getName ();
92
- } else {
93
- // Descriptor doesn't support renaming, set name for overriden getName()
94
- String oldName = name ;
95
- name = formatName (createGenericName (application , type .getName ()));
96
- PropertyChangeSupport pcs = ApplicationDescriptor .this .getChangeSupport ();
97
- pcs .firePropertyChange (PROPERTY_NAME , oldName , name );
98
- }
99
- } else if (ApplicationType .PROPERTY_ICON .equals (propertyName )) {
100
- setIcon ((Image )evt .getNewValue ());
101
- } else if (ApplicationType .PROPERTY_DESCRIPTION .equals (propertyName )) {
102
- setDescription ((String )evt .getNewValue ());
103
- } else if (ApplicationType .PROPERTY_VERSION .equals (propertyName )) {
104
- // Not supported by ApplicationDescriptor
105
- }
106
- }
107
- });
86
+
87
+ setApplicationType (type );
108
88
}
109
89
110
90
public String getName () {
@@ -115,6 +95,50 @@ public String getName() {
115
95
public boolean providesProperties () {
116
96
return true ;
117
97
}
98
+
99
+
100
+ protected void setApplicationType (ApplicationType type ) {
101
+ if (this .type != null ) this .type .removePropertyChangeListener (this );
102
+
103
+ this .type = type ;
104
+
105
+ this .type .addPropertyChangeListener (this );
106
+ }
107
+
108
+ protected ApplicationType getApplicationType () {
109
+ return type ;
110
+ }
111
+
112
+
113
+ @ Override
114
+ public void propertyChange (PropertyChangeEvent evt ) {
115
+ String propertyName = evt .getPropertyName ();
116
+ if (ApplicationType .PROPERTY_NAME .equals (propertyName )) {
117
+ Application application = getDataSource ();
118
+
119
+ // Name already customized by the user, do not change it
120
+ if (resolveName (application , null ) != null ) return ;
121
+
122
+ if (supportsRename ()) {
123
+ // Descriptor supports renaming, use setName(), sync name
124
+ setName ((String )evt .getNewValue ());
125
+ name = ApplicationDescriptor .super .getName ();
126
+ } else {
127
+ // Descriptor doesn't support renaming, set name for overriden getName()
128
+ String oldName = name ;
129
+ name = formatName (createGenericName (application , type .getName ()));
130
+ PropertyChangeSupport pcs = ApplicationDescriptor .this .getChangeSupport ();
131
+ pcs .firePropertyChange (PROPERTY_NAME , oldName , name );
132
+ }
133
+ } else if (ApplicationType .PROPERTY_ICON .equals (propertyName )) {
134
+ setIcon ((Image )evt .getNewValue ());
135
+ } else if (ApplicationType .PROPERTY_DESCRIPTION .equals (propertyName )) {
136
+ setDescription ((String )evt .getNewValue ());
137
+ } else if (ApplicationType .PROPERTY_VERSION .equals (propertyName )) {
138
+ // Not supported by ApplicationDescriptor
139
+ }
140
+ }
141
+
118
142
119
143
/**
120
144
* Returns Application name if available in Snapshot Storage as PROPERTY_NAME
@@ -140,9 +164,9 @@ protected static String resolveApplicationName(Application application, Applicat
140
164
return createGenericName (application , type .getName ());
141
165
}
142
166
143
- private static String resolveCustomName (Application application ) {
144
- Jvm jvm = JvmFactory .getJVMFor (application );
145
- if (jvm .isBasicInfoSupported ()) {
167
+ protected static String resolveCustomName (Application application ) {
168
+ Jvm jvm = application . getState () == Stateful . STATE_AVAILABLE ? JvmFactory .getJVMFor (application ) : null ;
169
+ if (jvm != null && jvm .isBasicInfoSupported ()) {
146
170
String args = jvm .getJvmArgs ();
147
171
int propIndex = args .indexOf (DISPLAY_NAME_PROPERTY );
148
172
@@ -171,11 +195,29 @@ protected String formatName(String namePattern) {
171
195
return formatted ;
172
196
}
173
197
174
- private static String createGenericName (Application application , String nameBase ) {
198
+ protected static String createGenericName (Application application , String nameBase ) {
175
199
int pid = application .getPid ();
176
200
String id = Application .CURRENT_APPLICATION .getPid () == pid ||
177
- pid == Application .UNKNOWN_PID ? "" : " (pid " + pid + ")" ; // NOI18N
201
+ pid == Application .UNKNOWN_PID ? "" : PID_PARAM ; // NOI18N
178
202
return nameBase + id ;
179
203
}
180
204
205
+
206
+ protected static String resolveApplicationDescription (Application application , ApplicationType type ) {
207
+ String persistedDescription = application .getStorage ().getCustomProperty (PROPERTY_DESCRIPTION );
208
+ if (persistedDescription != null ) return persistedDescription ;
209
+
210
+ return type .getDescription ();
211
+ }
212
+
213
+ protected static Image resolveApplicationIcon (Application application , ApplicationType type ) {
214
+ String persistedIconString = application .getStorage ().getCustomProperty (PROPERTY_ICON );
215
+ if (persistedIconString != null ) {
216
+ Image persistedIcon = Utils .stringToImage (persistedIconString );
217
+ if (persistedIcon != null ) return persistedIcon ;
218
+ }
219
+
220
+ return type .getIcon ();
221
+ }
222
+
181
223
}
0 commit comments