Merge branch '37' into 'master'
Implement custon combo box item renderer fixes #37 See merge request !2
This commit is contained in:
commit
fd95772fc8
@ -67,9 +67,7 @@ public class OptimizationEditorPanel extends JPanel implements ItemListener {
|
|||||||
* Creates the GUI editor component
|
* Creates the GUI editor component
|
||||||
*/
|
*/
|
||||||
private GenericObjectEditor genericObjectEditor = null;
|
private GenericObjectEditor genericObjectEditor = null;
|
||||||
private boolean withComboBoxToolTips = true; // should tool tips for the combo box be created?
|
|
||||||
private int tipMaxLen = 100; // maximum length of tool tip
|
private int tipMaxLen = 100; // maximum length of tool tip
|
||||||
private HashMap<String, String> classNameMap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -96,7 +94,7 @@ public class OptimizationEditorPanel extends JPanel implements ItemListener {
|
|||||||
System.gc();
|
System.gc();
|
||||||
System.err.println("Could not create backup object: not enough memory (OptimizationEditorPanel backup of " + object + ")");
|
System.err.println("Could not create backup object: not enough memory (OptimizationEditorPanel backup of " + object + ")");
|
||||||
}
|
}
|
||||||
comboBoxModel = new DefaultComboBoxModel(new String[0]);
|
comboBoxModel = new DefaultComboBoxModel(new Vector<Item>());
|
||||||
objectChooser = new JComboBox(comboBoxModel);
|
objectChooser = new JComboBox(comboBoxModel);
|
||||||
objectChooser.setEditable(false);
|
objectChooser.setEditable(false);
|
||||||
propertySheetPanel = new PropertySheetPanel();
|
propertySheetPanel = new PropertySheetPanel();
|
||||||
@ -318,15 +316,16 @@ public class OptimizationEditorPanel extends JPanel implements ItemListener {
|
|||||||
classesLongNames = GenericObjectEditor.getClassesFromProperties(genericObjectEditor.getClassType().getName(), instances);
|
classesLongNames = GenericObjectEditor.getClassesFromProperties(genericObjectEditor.getClassType().getName(), instances);
|
||||||
LOGGER.finest("Selected type for OptimizationEditorPanel: " + genericObjectEditor.getClassType().getName());
|
LOGGER.finest("Selected type for OptimizationEditorPanel: " + genericObjectEditor.getClassType().getName());
|
||||||
if (classesLongNames.size() > 1) {
|
if (classesLongNames.size() > 1) {
|
||||||
classNameMap = new HashMap<>();
|
Vector<Item> classesList = new Vector<>();
|
||||||
|
String[] toolTips = collectComboToolTips(instances, tipMaxLen);
|
||||||
|
int i = 0;
|
||||||
for (String className : classesLongNames) {
|
for (String className : classesLongNames) {
|
||||||
classNameMap.put(StringTools.cutClassName(className), className);
|
String displayName = StringTools.cutClassName(className);
|
||||||
|
|
||||||
|
classesList.add(new Item(className, displayName, toolTips[i++]));
|
||||||
}
|
}
|
||||||
Vector<String> classesList = new Vector<>(classesLongNames);
|
|
||||||
objectChooser.setModel(new DefaultComboBoxModel(classesList));
|
objectChooser.setModel(new DefaultComboBoxModel(classesList));
|
||||||
if (withComboBoxToolTips) {
|
objectChooser.setRenderer(new ToolTipComboBoxRenderer());
|
||||||
objectChooser.setRenderer(new ToolTipComboBoxRenderer(collectComboToolTips(instances, tipMaxLen)));
|
|
||||||
}
|
|
||||||
GridBagConstraints gbConstraints = new GridBagConstraints();
|
GridBagConstraints gbConstraints = new GridBagConstraints();
|
||||||
gbConstraints.fill = GridBagConstraints.HORIZONTAL;
|
gbConstraints.fill = GridBagConstraints.HORIZONTAL;
|
||||||
gbConstraints.gridx = 0;
|
gbConstraints.gridx = 0;
|
||||||
@ -342,7 +341,6 @@ public class OptimizationEditorPanel extends JPanel implements ItemListener {
|
|||||||
|
|
||||||
for (int i = 0; i < tips.length; i++) {
|
for (int i = 0; i < tips.length; i++) {
|
||||||
tips[i] = null;
|
tips[i] = null;
|
||||||
Class[] classParams = new Class[]{};
|
|
||||||
|
|
||||||
String tip = null;
|
String tip = null;
|
||||||
|
|
||||||
@ -363,20 +361,16 @@ public class OptimizationEditorPanel extends JPanel implements ItemListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void updateChooser() {
|
public void updateChooser() {
|
||||||
String objectName = /*
|
String objectName = genericObjectEditor.getValue().getClass().getName();
|
||||||
* EVAHELP.cutClassName
|
|
||||||
*/ (genericObjectEditor.getValue().getClass().getName());
|
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
for (int i = 0; i < comboBoxModel.getSize(); i++) {
|
for (int i = 0; i < comboBoxModel.getSize(); i++) {
|
||||||
if (objectName.equals(comboBoxModel.getElementAt(i))) {
|
Item element = (Item)comboBoxModel.getElementAt(i);
|
||||||
|
if (objectName.equals(element.getId())) {
|
||||||
found = true;
|
found = true;
|
||||||
|
objectChooser.getModel().setSelectedItem(element);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!found) {
|
|
||||||
comboBoxModel.addElement(objectName);
|
|
||||||
}
|
|
||||||
objectChooser.getModel().setSelectedItem(objectName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -398,7 +392,7 @@ public class OptimizationEditorPanel extends JPanel implements ItemListener {
|
|||||||
String className;
|
String className;
|
||||||
|
|
||||||
if ((e.getSource() == objectChooser) && (e.getStateChange() == ItemEvent.SELECTED)) {
|
if ((e.getSource() == objectChooser) && (e.getStateChange() == ItemEvent.SELECTED)) {
|
||||||
className = (String) objectChooser.getSelectedItem();
|
className = ((Item)objectChooser.getSelectedItem()).getId();
|
||||||
try {
|
try {
|
||||||
Object n = Class.forName(className).newInstance();
|
Object n = Class.forName(className).newInstance();
|
||||||
genericObjectEditor.setValue(n);
|
genericObjectEditor.setValue(n);
|
||||||
@ -423,30 +417,73 @@ public class OptimizationEditorPanel extends JPanel implements ItemListener {
|
|||||||
class ToolTipComboBoxRenderer extends BasicComboBoxRenderer {
|
class ToolTipComboBoxRenderer extends BasicComboBoxRenderer {
|
||||||
|
|
||||||
private static final long serialVersionUID = -5781643352198561208L;
|
private static final long serialVersionUID = -5781643352198561208L;
|
||||||
String[] toolTips = null;
|
|
||||||
|
|
||||||
public ToolTipComboBoxRenderer(String[] tips) {
|
public ToolTipComboBoxRenderer() {
|
||||||
super();
|
super();
|
||||||
toolTips = tips;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Component getListCellRendererComponent(JList list, Object value,
|
public Component getListCellRendererComponent(JList list, Object value,
|
||||||
int index, boolean isSelected, boolean cellHasFocus) {
|
int index, boolean isSelected, boolean cellHasFocus) {
|
||||||
if (isSelected) {
|
|
||||||
setBackground(list.getSelectionBackground());
|
super.getListCellRendererComponent(list, value, index,
|
||||||
setForeground(list.getSelectionForeground());
|
isSelected, cellHasFocus);
|
||||||
if ((toolTips != null) && (index >= 0)) {
|
|
||||||
if (toolTips[index] != null) {
|
if (value != null)
|
||||||
list.setToolTipText(toolTips[index]);
|
{
|
||||||
}
|
Item item = (Item)value;
|
||||||
|
setText(item.getDisplayName());
|
||||||
|
|
||||||
|
if (isSelected) {
|
||||||
|
setBackground(list.getSelectionBackground());
|
||||||
|
setForeground(list.getSelectionForeground());
|
||||||
|
list.setToolTipText(item.getDescription());
|
||||||
|
} else {
|
||||||
|
setBackground(list.getBackground());
|
||||||
|
setForeground(list.getForeground());
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
setBackground(list.getBackground());
|
|
||||||
setForeground(list.getForeground());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (index == -1)
|
||||||
|
{
|
||||||
|
Item item = (Item)value;
|
||||||
|
setText(item.getDisplayName());
|
||||||
|
}
|
||||||
|
|
||||||
setFont(list.getFont());
|
setFont(list.getFont());
|
||||||
setText((value == null) ? "" : value.toString());
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Item
|
||||||
|
{
|
||||||
|
private String id;
|
||||||
|
private String displayName;
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
public Item(String id, String displayName, String description)
|
||||||
|
{
|
||||||
|
this.id = id;
|
||||||
|
this.displayName = displayName;
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId()
|
||||||
|
{
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription()
|
||||||
|
{
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDisplayName() {
|
||||||
|
return displayName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString()
|
||||||
|
{
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user