X

Geertjan's Blog

  • August 19, 2015

Complex Properties Windows on the NetBeans Platform (Part 2)

Geertjan Wielenga
Product Manager


In part 1
, you ended up removing the UI components from the custom Properties window whenever the Lookup changes. That's kind of messy and here's an alternative approach, where you repopulate the custom Properties window instead of recreating its UI components.

public class PlanetPropertiesTopComponent extends TopComponent
implements LookupListener {
private Lookup.Result<PlanetNode> planetNodeResult;
private final JPanel mainPanel;
private boolean isInitialized = false;
public PlanetPropertiesTopComponent() {
setName(Bundle.CTL_PlanetPropertiesTopComponent());
setLayout(new BorderLayout());
mainPanel = new JPanel(new MigLayout());
JLabel title = new JLabel();
add(title, BorderLayout.NORTH);
add(mainPanel, BorderLayout.CENTER);
}
@Override
public void resultChanged(LookupEvent le) {
if (!planetNodeResult.allInstances().isEmpty()) {
PlanetNode planetNode
= planetNodeResult.allInstances().iterator().next();
if (!isInitialized) {
initializeContent(planetNode);
} else {
repopulateContent(planetNode);
}
}
}
private void initializeContent(PlanetNode planetNode) {
writeTabDisplayName(planetNode);
List<Property> booleanProperties = new ArrayList<Property>();
PropertySet ps = planetNode.getPropertySets()[0];
Property<?>[] props = ps.getProperties();
for (Node.Property property : props) {
SinglePropertyJPanel ppJPanel
= new SinglePropertyJPanel(property);
if (property.getValueType().equals(boolean.class)) {
booleanProperties.add(property);
} else {
mainPanel.add(
ppJPanel,
"dock north, wrap, growx, pushx");
}
}
mainPanel.add(
new MultiPropertyJPanel(booleanProperties),
"wrap, growx, pushx");
revalidate();
isInitialized = true;
}
private void repopulateContent(PlanetNode planetNode) {
writeTabDisplayName(planetNode);
PropertySet ps = planetNode.getPropertySets()[0];
Property<?>[] props = ps.getProperties();
for (Node.Property property : props) {
Component[] components = mainPanel.getComponents();
for (Component component : components) {
if (component instanceof SinglePropertyJPanel) {
SinglePropertyJPanel spjp = (SinglePropertyJPanel) component;
PropertyPanel pp = spjp.getPropertyPanel().get(0);
String ppDisplayName = pp.getProperty().getDisplayName();
if (ppDisplayName.equals(property.getDisplayName())) {
pp.setProperty(property);
}
} else
if (component instanceof MultiPropertyJPanel) {
MultiPropertyJPanel mpjp = (MultiPropertyJPanel) component;
List<PropertyPanel> ppList = mpjp.getPropertyPanel();
for (PropertyPanel pp : ppList) {
String ppDisplayName = pp.getProperty().getDisplayName();
if (ppDisplayName.equals(property.getDisplayName())) {
pp.setProperty(property);
}
}
}
}
}
}
private void writeTabDisplayName(PlanetNode planetNode) {
String displayName = planetNode.getDisplayName();
setDisplayName(displayName + " - Properties");
}
@Override
public void componentOpened() {
planetNodeResult
= Utilities.actionsGlobalContext().lookupResult(PlanetNode.class);
planetNodeResult.addLookupListener(
this);
}
@Override
public void componentClosed() {
planetNodeResult.removeLookupListener(this);
}
}

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.