Custom UI class
Dimitri Pissarenko <[email protected]>
| Newsgroups | gmane.comp.embedded.carlsbad-cubes |
|---|---|
| Message-ID | <[email protected]> |
Hello!
I have a dialog, which consists, among other things, of an instance of
JPanel. That JPanel instance is generated from a SwiXML file by means
of a class PropertiesPanel (method getPanel).
Event-handling is also implemented inside PropertiesPanel (full code
of PropertiesPanel see below).
Now I want to know whether it is possible to instruct SwiXML to
a) create an instance of PropertiesPanel
b) invoke PropertiesPanel.getPanel()
when creating a UI from XML?
In other words, can I write into SwiXML UI descriptor something like
Sample (start)
<dialog title="Import from database" modal="true" resizable="true"
layout="GridBagLayout">
<panel id="wannaBeCustomPanel"
generatorClass="net.sourceforge.demetrix.ui.PropertiesPanel">
<gridbagconstraints insets="5,5,5,5" gridx="0"
gridy="0" anchor="GridBagConstraints.WEST" weightx="1.0"
weighty="1.0" fill="GridBagConstraints.BOTH"/>
</panel>
</dialog>
Sample (end)
?
Has someone done this or a similar thing?
Are there any alternatives apart from manually inserting
PropertiesPanel in Java?
TIA
dap
Code of PropertiesPanel (start)
package net.sourceforge.demetrix.ui;
import java.awt.event.ActionEvent;
import java.util.*;
import javax.swing.*;
import net.sourceforge.demetrix.Demetrix;
import net.sourceforge.demetrix.properties.*;
import net.sourceforge.demetrix.properties.swixml.*;
import org.apache.log4j.Logger;
import org.swixml.SwingEngine;
import org.swixml.XVBox;
/**
* @author Dimitri Pissarenko
*
*/
public class PropertiesPanel {
public Action applyButtonAction = new AbstractAction() {
public void actionPerformed(ActionEvent event) {
applyButtonAction();
}
};
private Logger logger = Logger.getLogger(getClass());
public JButton newPropertyButton;
public Action newPropertyButtonAction = new AbstractAction() {
public void actionPerformed(ActionEvent event) {
addNewProperty();
}
};
public JButton applyButton;
public JLabel nodeLabel;
private JPanel panel;
private DemetrixPropertiesHolder propertiesHolder;
private DemetrixPropertyRenderer propertyRenderer;
public XVBox propertyRowsPanel;
private Hashtable representationsByPropertyNames;
private final String UI_DEF_FILE_NAME =
"net/sourceforge/demetrix/ui/PropertiesPanel.xml";
public PropertiesPanel() {
try {
this.panel =
(JPanel) (new
SwingEngine(this)).render(
ClassLoader.getSystemClassLoader().getResource(UI_DEF_FILE_NAME));
} catch (Exception exception) {
this.logger.error("", exception);
}
this.propertyRenderer = new SwixMLPropertyRenderer();
}
private void addNewProperty() {
DemetrixPropertyCreationDialog dialog = null;
DemetrixProperty newProperty = null;
String newPropertyName = null;
dialog =
new DemetrixPropertyCreationDialog(
this.representationsByPropertyNames);
dialog.show();
if (dialog.isClosedWithOk()) {
newPropertyName = dialog.getPropertyName();
newProperty = dialog.getProperty();
newProperty.setName(newPropertyName);
this.propertiesHolder.addProperty(newProperty);
this.updatePropertiesPanel();
}
}
public JPanel getPanel() {
return this.panel;
}
protected void updatePropertiesPanel() {
Iterator propertiesIterator = null;
DemetrixProperty currentProperty = null;
SwixMLDemetrixPropertyRepresentation representation =
null;
this.representationsByPropertyNames = new Hashtable();
this.propertyRowsPanel.removeAll();
if (this.propertiesHolder!=null)
{
propertiesIterator =
this.propertiesHolder.getAllProperties();
while (propertiesIterator.hasNext()) {
currentProperty = (DemetrixProperty)
propertiesIterator.next();
representation =
(
SwixMLDemetrixPropertyRepresentation) this
.propertyRenderer
.renderProperty(
currentProperty);
if (representation != null) {
this.propertyRowsPanel.add(representation.getPanel());
representation.updateRepresentation(currentProperty);
this.representationsByPropertyNames.put(
currentProperty.getName(),
representation);
}
}
}
}
private void updatePropertyData() {
Iterator propertiesIterator = null;
DemetrixProperty currentProperty = null;
DemetrixPropertyRepresentation representation = null;
if (this.propertiesHolder != null) {
propertiesIterator =
this.propertiesHolder.getAllProperties();
while (propertiesIterator.hasNext()) {
currentProperty = (DemetrixProperty)
propertiesIterator.next();
representation =
(
DemetrixPropertyRepresentation) this
.representationsByPropertyNames
.get(
currentProperty.getName());
this.logger.debug("representation=" +
representation);
this.logger.debug("currentProperty=" +
currentProperty);
representation.updatePropertyData(currentProperty);
}
}
}
public String getInvalidPropertyName() {
Enumeration enumeration = null;
SwixMLDemetrixPropertyRepresentation representation =
null;
String propertyName = null;
boolean everythingValid = true;
enumeration =
this.representationsByPropertyNames.keys();
while (enumeration.hasMoreElements() &&
everythingValid) {
propertyName = (String)
enumeration.nextElement();
representation =
(
SwixMLDemetrixPropertyRepresentation) this
.representationsByPropertyNames
.get(
propertyName);
if (!representation.isEnteredDataValid()) {
everythingValid = false;
}
}
if (everythingValid) {
return null;
} else {
return propertyName;
}
}
public boolean updateDataObject() {
String invalidPropertyName = null;
boolean everythingOk = false;
invalidPropertyName = this.getInvalidPropertyName();
if (invalidPropertyName == null) {
updatePropertyData();
everythingOk = true;
} else {
JOptionPane.showMessageDialog(
this.panel,
"Property \""
+ invalidPropertyName
+ "\" can't be changed, the
value you entered is not valid.",
Demetrix.PRODUCT_NAME,
JOptionPane.ERROR_MESSAGE);
everythingOk = false;
}
return everythingOk;
}
protected void applyButtonAction() {
if (this.updateDataObject()) {
ProcessChainEditor.getInstance().redrawGraph();
}
}
/**
* @return
*/
public DemetrixPropertiesHolder getPropertiesHolder() {
return propertiesHolder;
}
/**
* @param holder
*/
public void setPropertiesHolder(DemetrixPropertiesHolder
holder) {
propertiesHolder = holder;
this.updatePropertiesPanel();
}
/**
* @return
*/
protected Hashtable getRepresentationsByPropertyNames() {
return representationsByPropertyNames;
}
}
Code of PropertiesPanel (end)