Home » Modeling » GMF (Graphical Modeling Framework) » How to change the order in which the properties are listed ?
| | | |
Re: How to change the order in which the properties are listed ? [message #639501 is a reply to message #551604] |
Tue, 16 November 2010 18:47 |
Carlos Messages: 16 Registered: July 2009 |
Junior Member |
|
|
Hi Pantelis and Syed,
Pantelis, I have followed your instructions to implement my own sorting: I have created MyPropertySheetPage, MyPropertyShorter, MyAdvancedPropertySection and made MyPropertySection extend MyAdvancedPropertySection.
I just one to preserve the original order of the properties in the ecore model, don't need to do any additional ordering, but it is not working:
The "sort" method of MyPropertySorter is empty (in order to keep the original order), and it is correctly called, but it seems that properties have already been alphabetically ordered somewhere before. So I would need to reorder the properties of each object individually...
Any ideas?
By the way Pantelis, I cannot see the attachments you put on your messages... Could you please reattach them.
Thank you very much!
|
|
|
Re: How to change the order in which the properties are listed ? [message #645119 is a reply to message #639501] |
Wed, 15 December 2010 16:16 |
Carlos Messages: 16 Registered: July 2009 |
Junior Member |
|
|
Hi all,
After some time struggling with this problem, I have found a solution to preserve the order of the attributes in the properties view in a GMF generated diagram editor, as they are defined in the ecore model.
It is a bit long, but it is working fine. I leave it here just in case it is useful for anybody.
The key is that the alphabetical ordering is done not only in the PropertySheetSorter, but also in two places inside the PropertySheetEntry. So I have modified the code to remove this sortings.
So, to achieve this, it is necessary to use custom PropertySheetPage, PropertySheetSorter, and also PropertySheetEntry.
To do so, follow the next 5 steps:
1. Go to package: xxx.sheet inside the diagram plugin.
2. Create class XXXPropertySheetSorter as an extension of PropertySheetSorter, and override method sort(IPropertySheetEntry[] entries) to remove properties ordering, which is alphabetical.
The class code should result in something like this:
public class XXXPropertySheetSorter extends PropertySheetSorter
{
public void sort(IPropertySheetEntry[] entries)
{
}
}
3. Create class XXXPropertySheetPage as an extension of PropertySheetPage, and override the constructor to use the XXXPropertySheetSorter created in the previous step:
public class XXXPropertySheetPage extends PropertySheetPage
{
public XXXPropertySheetPage()
{
super();
setSorter(new XXXPropertySheetSorter());
}
}
4. Create class XXXPropertySheetEntry as an extension of UndoableModelPropertySheetEntry and override methods:
protected List computeMergedPropertyDescriptors();
protected Map computePropertyDescriptorsFor(IPropertySource source);
The code of both methods also does some ordering, so we remove that ordering in order to keep the original order.
The class would result in something like the following piece of code, where the important modifications are between "/////////////////////////////////////////////////////" (I could not find the way to color pieces of code):
public class XXXPropertySheetEntry extends UndoableModelPropertySheetEntry
{
public XXXPropertySheetEntry(IOperationHistory operationHistory)
{
super(operationHistory);
}
/**
* Overrides the super method to avoid ordering attributes, and so keep the
* order defined in the ecore model.
*
* @return sorted <code>List</code> of all the <code>IPropertyDescriptor</code>s for the objects
*/
protected List computeMergedPropertyDescriptors()
{
if (values.length == 0)
return new ArrayList(0);
// get all descriptors from each object
Map[] propertyDescriptorMaps = new Map[values.length];
for (int i = 0; i < values.length; i++)
{
Object object = values[i];
IPropertySource source = getPropertySource(object);
if (source == null)
{
// if one of the selected items is not a property source
// then we show no properties
return new ArrayList(0);
}
// get the property descriptors keyed by id
propertyDescriptorMaps[i] = computePropertyDescriptorsFor(source);
}
// intersect
Map intersection = propertyDescriptorMaps[0];
for (int i = 1; i < propertyDescriptorMaps.length; i++)
{
// get the current ids
Object[] ids = intersection.keySet().toArray();
for (int j = 0; j < ids.length; j++)
{
Object object = propertyDescriptorMaps[i].get(ids[j]);
if (object == null ||
// see if the descriptors (which have the same id) are
// compatible
!((IPropertyDescriptor) intersection.get(ids[j])).isCompatibleWith((IPropertyDescriptor) object))
intersection.remove(ids[j]);
}
}
// Sort the descriptors
List descriptors = new ArrayList(intersection.values());
////////////////////////////////////////////////////////////////////////
// Commented to avoid alphabetically sorting of properties!
// Collections.sort(descriptors, new Comparator() {
// Collator coll = Collator.getInstance(Locale.getDefault());
//
// public int compare(Object a, Object b) {
// IPropertyDescriptor d1, d2;
// String dname1, dname2;
// d1 = (IPropertyDescriptor) a;
// dname1 = d1.getDisplayName();
// d2 = (IPropertyDescriptor) b;
// dname2 = d2.getDisplayName();
// return coll.compare(dname1, dname2);
// }
// });
////////////////////////////////////////////////////////////////////////
return descriptors;
}
/**
* Overrides the super method to avoid ordering attributes, and so keep the
* order defined in the ecore model.
* The difference with the super method is that a LinkedHashMap is used instead of a HashMap,
* in order to keep the properties order.
*
* @param source a property source for which to obtain descriptors
* @return a table of descriptors keyed on their id
*/
protected Map computePropertyDescriptorsFor(IPropertySource source)
{
IPropertyDescriptor[] descriptors = source.getPropertyDescriptors();
////////////////////////////////////////////////////////////////////////
// Use LinkedHashMap instead of HashMap to preserve order
Map result = new LinkedHashMap();
////////////////////////////////////////////////////////////////////////
for (int i = 0; i < descriptors.length; i++)
{
result.put(descriptors[i].getId(), descriptors[i]);
}
return result;
}
}
5. We are almost done! Now, create class XXXAdvancedPropertySection as a copy of AdvancedPropertySection, and modify method createControls to use XXXPropertySheetPage and XXXPropertySheetEntry instead of the originals.
The resulting method is as follows, where the important modification are highlighted:
public void createControls(final Composite parent, TabbedPropertySheetPage aTabbedPropertySheetPage)
{
super.createControls(parent, aTabbedPropertySheetPage);
Composite composite = getWidgetFactory().createFlatFormComposite(parent);
FormData data = null;
String tableLabelStr = getTableLabel();
CLabel tableLabel = null;
if (tableLabelStr != null && tableLabelStr.length() > 0)
{
tableLabel = getWidgetFactory().createCLabel(composite, tableLabelStr);
data = new FormData();
data.left = new FormAttachment(0, 0);
data.top = new FormAttachment(0, 0);
tableLabel.setLayoutData(data);
}
////////////////////////////////////////////////////////////////////////
// Use VadPropertySheetPage to provide custom properties behavior
page = new VadPropertySheetPage();
// Use VadPropertySheetEntry to provide custom properties behavior
UndoableModelPropertySheetEntry root = new VadPropertySheetEntry(OperationHistoryFactory.getOperationHistory());
////////////////////////////////////////////////////////////////////////
root.setPropertySourceProvider(getPropertySourceProvider());
page.setRootEntry(root);
page.createControl(composite);
data = new FormData();
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(100, 0);
if (tableLabel == null)
{
data.top = new FormAttachment(0, 0);
}
else
{
data.top = new FormAttachment(tableLabel, 0, SWT.BOTTOM);
}
data.bottom = new FormAttachment(100, 0);
data.height = 100;
data.width = 100;
page.getControl().setLayoutData(data);
setActionBars(aTabbedPropertySheetPage.getSite().getActionBars());
}
Carlos
|
|
|
Goto Forum:
Current Time: Wed Apr 24 19:18:39 GMT 2024
Powered by FUDForum. Page generated in 0.04721 seconds
|