Skip to content

Commit 0c6edd5

Browse files
committed
Fixed reloading DataSource views for re-enabled applications
- listener issues still not resolved
1 parent ec2f87d commit 0c6edd5

File tree

5 files changed

+110
-32
lines changed

5 files changed

+110
-32
lines changed

visualvm/core/src/org/graalvm/visualvm/core/ui/DataSourceCaption.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ final class DataSourceCaption<X extends DataSource> extends JPanel implements Pr
6565
private static final String APPLICATION_PID_PREFIX = "(pid"; // NOI18N
6666
private static final String APPLICATION_PID_SUFFIX = ")"; // NOI18N
6767

68-
private DataSource dataSource;
69-
private DataSourceDescriptor<X> dataSourceDescriptor;
68+
private final DataSource dataSourceMaster;
69+
private final DataSourceDescriptor<X> dataSourceMasterDescriptor;
7070

71+
private final boolean tracksChanges;
7172
private boolean isAvailable;
7273
private boolean isDirty = false;
7374
private String name;
@@ -78,21 +79,23 @@ final class DataSourceCaption<X extends DataSource> extends JPanel implements Pr
7879
public DataSourceCaption(X dataSource) {
7980
initComponents();
8081

81-
this.dataSource = dataSource;
82-
dataSource.addPropertyChangeListener(this);
82+
this.dataSourceMaster = DataSourceWindowManager.getViewMaster(dataSource);
8383

84-
dataSourceDescriptor = DataSourceDescriptorFactory.getDescriptor(dataSource);
85-
dataSourceDescriptor.addPropertyChangeListener(this);
84+
tracksChanges = dataSource == dataSourceMaster;
85+
dataSourceMaster.addPropertyChangeListener(this);
86+
87+
dataSourceMasterDescriptor = DataSourceDescriptorFactory.getDescriptor(dataSourceMaster);
88+
dataSourceMasterDescriptor.addPropertyChangeListener(this);
8689

8790
initAvailable();
88-
name = dataSourceDescriptor.getName();
89-
description = dataSourceDescriptor.getDescription();
91+
name = dataSourceMasterDescriptor.getName();
92+
description = dataSourceMasterDescriptor.getDescription();
9093

9194
updateCaption();
9295
updateDescription();
9396
updateAvailable();
9497

95-
dataSource.notifyWhenRemoved(this);
98+
dataSourceMaster.notifyWhenRemoved(this);
9699
}
97100

98101

@@ -101,7 +104,7 @@ public void propertyChange(PropertyChangeEvent evt) {
101104
if (Stateful.PROPERTY_STATE.equals(propertyName)) {
102105
int state = (Integer)evt.getNewValue();
103106
isAvailable = state == Stateful.STATE_AVAILABLE;
104-
if (!isDirty && isAvailable && (Integer)evt.getOldValue() == Stateful.STATE_UNAVAILABLE) isDirty = true;
107+
if (tracksChanges && !isDirty && isAvailable && (Integer)evt.getOldValue() == Stateful.STATE_UNAVAILABLE) isDirty = true;
105108
updateAvailable();
106109
updateCaption();
107110
} else if (DataSourceDescriptor.PROPERTY_NAME.equals(propertyName)) {
@@ -131,8 +134,8 @@ public void run() {
131134
public synchronized void finish() {
132135
if (finished) return;
133136
finished = true;
134-
dataSource.removePropertyChangeListener(this);
135-
dataSourceDescriptor.removePropertyChangeListener(this);
137+
dataSourceMaster.removePropertyChangeListener(this);
138+
dataSourceMasterDescriptor.removePropertyChangeListener(this);
136139
}
137140

138141

@@ -152,7 +155,7 @@ private void updateAvailable() {
152155

153156
if (!SwingUtilities.isEventDispatchThread()) Thread.dumpStack();
154157

155-
if (!isOpaque() && isDirty) {
158+
if (isDirty && !isOpaque()) {
156159
JLabel l = new JLabel(NbBundle.getMessage(DataSourceCaption.class, "DataSourceCaption_LBL_Reload")) { // NOI18N
157160
public Dimension getMinimumSize() {
158161
Dimension dim = super.getMinimumSize();
@@ -172,7 +175,7 @@ public Dimension getMinimumSize() {
172175
JButton b = new JButton(NbBundle.getMessage(DataSourceCaption.class, "DataSourceCaption_BTN_Reload")) { // NOI18N
173176
protected void fireActionPerformed(ActionEvent e) {
174177
super.fireActionPerformed(e);
175-
DataSourceWindowManager.sharedInstance().reopenDataSource(dataSource);
178+
DataSourceWindowManager.sharedInstance().reopenDataSource(dataSourceMaster);
176179
}
177180
public Dimension getMinimumSize() {
178181
Dimension dim = super.getMinimumSize();
@@ -249,8 +252,8 @@ public void actionPerformed(ActionEvent e) {
249252

250253

251254
private void initAvailable() {
252-
if (dataSource instanceof Stateful) {
253-
Stateful statefulDataSource = (Stateful)dataSource;
255+
if (dataSourceMaster instanceof Stateful) {
256+
Stateful statefulDataSource = (Stateful)dataSourceMaster;
254257
isAvailable = statefulDataSource.getState() == Stateful.STATE_AVAILABLE;
255258
} else {
256259
isAvailable = true;

visualvm/core/src/org/graalvm/visualvm/core/ui/DataSourceWindow.java

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.beans.PropertyChangeListener;
3838
import java.util.Collections;
3939
import java.util.List;
40+
import java.util.Objects;
4041
import javax.swing.JPanel;
4142
import javax.swing.SwingUtilities;
4243
import org.openide.util.RequestProcessor;
@@ -72,7 +73,7 @@ public DataSource getDataSource() {
7273

7374
public void addView(DataSourceView view) {
7475
if (viewsCount == 0) {
75-
singleViewContainer = new DataSourceWindowTabbedPane.ViewContainer(new DataSourceCaption(dataSource), view);
76+
singleViewContainer = new DataSourceWindowTabbedPane.ViewContainer(new DataSourceCaption(view.getDataSource()), view);
7677
add(singleViewContainer, BorderLayout.CENTER);
7778
doLayout();
7879
alertListener = new AlertListener();
@@ -92,6 +93,28 @@ public void addView(DataSourceView view) {
9293
view.addPropertyChangeListener(WeakListeners.propertyChange(alertListener,view));
9394
}
9495

96+
private void insertView(DataSourceView view, int index) {
97+
if (viewsCount == 0) {
98+
singleViewContainer = new DataSourceWindowTabbedPane.ViewContainer(new DataSourceCaption(view.getDataSource()), view);
99+
add(singleViewContainer, BorderLayout.CENTER);
100+
doLayout();
101+
alertListener = new AlertListener();
102+
} else if (viewsCount == 1) {
103+
remove(singleViewContainer);
104+
105+
add(multiViewContainer, BorderLayout.CENTER);
106+
tabbedContainer.addView(dataSource, singleViewContainer.getView());
107+
tabbedContainer.insertView(dataSource, view, index);
108+
doLayout();
109+
singleViewContainer.getCaption().finish();
110+
singleViewContainer = null;
111+
} else {
112+
tabbedContainer.insertView(dataSource, view, index);
113+
}
114+
viewsCount++;
115+
view.addPropertyChangeListener(WeakListeners.propertyChange(alertListener,view));
116+
}
117+
95118
public void selectView(DataSourceView view) {
96119
if (viewsCount > 1) {
97120
int viewIndex = indexOf(view);
@@ -112,7 +135,8 @@ public void removeView(final DataSourceView view) {
112135
else tabbedContainer.removeView(viewIndex);
113136

114137
if (viewsCount == 2) {
115-
singleViewContainer = new DataSourceWindowTabbedPane.ViewContainer(new DataSourceCaption(dataSource), tabbedContainer.getViews().get(0));
138+
DataSourceView remaining = tabbedContainer.getViews().get(0);
139+
singleViewContainer = new DataSourceWindowTabbedPane.ViewContainer(new DataSourceCaption(remaining.getDataSource()), remaining);
116140
remove(multiViewContainer);
117141
tabbedContainer.removeView(0);
118142
add(singleViewContainer, BorderLayout.CENTER);
@@ -130,7 +154,7 @@ public void removeView(final DataSourceView view) {
130154
}
131155

132156
void clearView(final DataSourceView view) {
133-
if (viewsCount == 1) {
157+
if (viewsCount == 1 && Objects.equals(singleViewContainer.getName(), view.getName())) {
134158
singleViewContainer.removeAll();
135159
if (singleViewContainer.getCaption() != null) singleViewContainer.getCaption().finish();
136160
singleViewContainer.setReloading();
@@ -148,8 +172,8 @@ void clearView(final DataSourceView view) {
148172
});
149173
}
150174

151-
void setView(final DataSourceView view) {
152-
if (viewsCount == 1) {
175+
void updateView(final DataSourceView view, int index) {
176+
if (viewsCount == 1 && Objects.equals(singleViewContainer.getName(), view.getName())) {
153177
singleViewContainer.removeAll();
154178
singleViewContainer.setCaption(new DataSourceCaption(view.getDataSource()));
155179
singleViewContainer.setView(view);
@@ -163,6 +187,8 @@ void setView(final DataSourceView view) {
163187
container.setView(view);
164188
container.doLayout();
165189
container.repaint();
190+
} else {
191+
insertView(view, index);
166192
}
167193
}
168194

@@ -171,6 +197,31 @@ void setView(final DataSourceView view) {
171197
});
172198
}
173199

200+
void closeUnregisteredView(final DataSourceView view) {
201+
if (viewsCount == 1) {
202+
if (view != singleViewContainer.getView()) throw new RuntimeException("View " + view + " not present in DataSourceWindow " + this); // NOI18N
203+
remove(singleViewContainer);
204+
singleViewContainer.getCaption().finish();
205+
singleViewContainer = null;
206+
} else {
207+
int viewIndex = indexOf(view);
208+
if (viewIndex == -1) throw new RuntimeException("View " + view + " not present in DataSourceWindow " + this); // NOI18N
209+
else tabbedContainer.removeView(viewIndex);
210+
211+
if (viewsCount == 2) {
212+
DataSourceView remaining = tabbedContainer.getViews().get(0);
213+
singleViewContainer = new DataSourceWindowTabbedPane.ViewContainer(new DataSourceCaption(remaining.getDataSource()), remaining);
214+
remove(multiViewContainer);
215+
tabbedContainer.removeView(0);
216+
add(singleViewContainer, BorderLayout.CENTER);
217+
doLayout();
218+
}
219+
}
220+
221+
viewsCount--;
222+
if (viewsCount == 0 && isOpened()) close();
223+
}
224+
174225
public void removeAllViews() {
175226
List<DataSourceView> views = getViews();
176227
for (DataSourceView view : views) removeView(view);

visualvm/core/src/org/graalvm/visualvm/core/ui/DataSourceWindowManager.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public void run() {
308308
}
309309

310310

311-
private DataSource getViewMaster(DataSource dataSource) {
311+
static DataSource getViewMaster(DataSource dataSource) {
312312
DataSource master = dataSource.getMaster();
313313
while (master != null && master != DataSource.ROOT) {
314314
dataSource = master;
@@ -381,11 +381,12 @@ public void run() {
381381
Set<DataSourceView> _views = openedViews.get(dataSource);
382382
if (_views == null) return;
383383

384-
final Set<DataSourceView> oldViews = new HashSet(_views);
384+
final Map<String, DataSourceView> oldViews = new HashMap();
385+
for (DataSourceView view : _views) oldViews.put(view.getName(), view);
385386
SwingUtilities.invokeLater(new Runnable () {
386387
public void run() {
387388
final Set<DataSourceView> opened = openedViews.get(dataSource);
388-
for (DataSourceView view : oldViews) {
389+
for (DataSourceView view : oldViews.values()) {
389390
window.clearView(view);
390391
opened.remove(view);
391392
}
@@ -396,11 +397,14 @@ public void run() {
396397
for (DataSourceView view : newViews) {
397398
opened.add(view);
398399
view.viewWillBeAdded();
400+
oldViews.remove(view.getName());
399401
}
402+
if (opened.isEmpty()) openedViews.remove(dataSource);
400403

401404
SwingUtilities.invokeLater(new Runnable() {
402405
public void run() {
403-
for (DataSourceView view : newViews) window.setView(view);
406+
for (DataSourceView view : oldViews.values()) window.closeUnregisteredView(view);
407+
for (int i = 0; i < newViews.size(); i++) window.updateView(newViews.get(i), i);
404408
}
405409
});
406410
}

visualvm/core/src/org/graalvm/visualvm/core/ui/DataSourceWindowTabbedPane.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,15 @@ public final boolean requestFocusInWindow() {
118118
}
119119

120120
public void addView(DataSource dataSource, DataSourceView view) {
121-
ViewContainer container = new ViewContainer(new DataSourceCaption(dataSource), view);
122-
// String viewName = view.getName();
123-
// if (view.isClosable()) {
124-
// if (viewName.indexOf("</html>") == -1) viewName += " "; // NOI18N
125-
// else viewName.replace("</html>", "&nbsp;</html>"); // NOI18N
126-
// }
127-
// tabpane.addTab(viewName, new ImageIcon(view.getImage()), container);
121+
ViewContainer container = new ViewContainer(new DataSourceCaption(view.getDataSource()), view);
128122
tabpane.addTab(view.getName(), new ImageIcon(view.getImage()), container, null, view.isClosable());
129123
}
130124

125+
public void insertView(DataSource dataSource, DataSourceView view, int index) {
126+
ViewContainer container = new ViewContainer(new DataSourceCaption(view.getDataSource()), view);
127+
tabpane.insertTab(view.getName(), new ImageIcon(view.getImage()), container, null, view.isClosable(), index);
128+
}
129+
131130
public void removeView(int index) {
132131
ViewContainer container = (ViewContainer)tabpane.getComponentAt(index);
133132
tabpane.removeTabAt(index);
@@ -230,6 +229,7 @@ final void setView(DataSourceView view) {
230229
this.view = view;
231230
this.viewComponent = view.getView();
232231
add(viewComponent, BorderLayout.CENTER);
232+
setName(view.getName());
233233
}
234234

235235
public DataSourceView getView() { return view; }

visualvm/uisupport/src/org/graalvm/visualvm/uisupport/ProfilerTabbedPane.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,26 @@ public void run() {
110110
setTabComponentAt(tabCount, new TabCaption(title, icon, closer));
111111
}
112112

113+
public void insertTab(String title, Icon icon, final Component component, String tip, boolean closable, int index) {
114+
if (component.getMouseWheelListeners().length == 0 && UIUtils.isAquaLookAndFeel()) {
115+
component.addMouseWheelListener(new MouseWheelListener() {
116+
@Override
117+
public void mouseWheelMoved(MouseWheelEvent e) {
118+
// GH-122
119+
}
120+
});
121+
}
122+
super.insertTab(title, icon, component, tip, index);
123+
124+
Runnable closer = closable ? new Runnable() {
125+
public void run() {
126+
closeTab(component);
127+
}
128+
} : null;
129+
130+
setTabComponentAt(index, new TabCaption(title, icon, closer));
131+
}
132+
113133

114134
@Override
115135
public void setTitleAt(int index, String title) {

0 commit comments

Comments
 (0)