{
String attr = getSelectedAttribute();
if (attr != null)
{
boolean isAscending = ASCENDING == ascendingOrder.getSelectedItem();
sortOrderModel.addElement(new VLVSortOrder(attr, isAscending));
DefaultComboBoxModel model =
(DefaultComboBoxModel)attributes.getModel();
int i = attributes.getSelectedIndex();
if (i > 0)
{
// To avoid issues, try to figure out first the new selection
int newIndex = -1;
for (int j= i -1; j>0 && (newIndex == -1); j--)
{
CategorizedComboBoxElement o = (CategorizedComboBoxElement)
model.getElementAt(j);
if (o.getType() == CategorizedComboBoxElement.Type.REGULAR)
{
newIndex = j;
}
}
if (newIndex == -1)
{
for (int j= i + 1; j<model.getSize() && (newIndex == -1); j++)
{
CategorizedComboBoxElement o = (CategorizedComboBoxElement)
model.getElementAt(j);
if (o.getType() == CategorizedComboBoxElement.Type.REGULAR)
{
newIndex = j;
}
}
}
if (newIndex != -1)
{
attributes.setSelectedIndex(newIndex);
}
model.removeElementAt(i);
}
}
listListener.valueChanged(null);
}
});
moveUp.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
int[] indexes = sortOrder.getSelectedIndices();
for (int i=0; i<indexes.length; i++)
{
Object o1 = sortOrderModel.elementAt(indexes[i] - 1);
Object o2 = sortOrderModel.elementAt(indexes[i]);
sortOrderModel.set(indexes[i] - 1, o2);
sortOrderModel.set(indexes[i], o1);
indexes[i] = indexes[i] - 1;
}
sortOrder.setSelectedIndices(indexes);
listListener.valueChanged(null);
}
});
moveDown.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
int[] indexes = sortOrder.getSelectedIndices();
for (int i=0; i<indexes.length; i++)
{
Object o1 = sortOrderModel.elementAt(indexes[i] + 1);
Object o2 = sortOrderModel.elementAt(indexes[i]);
sortOrderModel.set(indexes[i] + 1, o2);
sortOrderModel.set(indexes[i], o1);
indexes[i] = indexes[i] + 1;
}
sortOrder.setSelectedIndices(indexes);
listListener.valueChanged(null);
}
});
remove.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
int[] indexes = sortOrder.getSelectedIndices();
synchronized (standardAttrNames)
{
DefaultComboBoxModel model =
(DefaultComboBoxModel)attributes.getModel();
for (int i=0; i<indexes.length; i++)
{
VLVSortOrder sortOrder = (VLVSortOrder)sortOrderModel.getElementAt(
indexes[i]);
String attrName = sortOrder.getAttributeName();
boolean isCustom = customAttrNames.contains(attrName);
boolean dealingWithCustom = true;
for (int j = 0; j < model.getSize(); j++)
{
CategorizedComboBoxElement o = (CategorizedComboBoxElement)
model.getElementAt(j);
if (o.getType() == CategorizedComboBoxElement.Type.REGULAR)
{
if (dealingWithCustom == isCustom)
{
if (attrName.compareTo(o.getValue().toString()) < 0)
{
model.insertElementAt(new CategorizedComboBoxElement(
attrName,
CategorizedComboBoxElement.Type.REGULAR), j);
break;
}
}
}
else if (!o.getValue().equals(CUSTOM_ATTRIBUTES))
{
dealingWithCustom = false;
if (isCustom)
{
model.insertElementAt(new CategorizedComboBoxElement(
attrName,
CategorizedComboBoxElement.Type.REGULAR), j);
break;
}
}
}
}
}
for (int i=indexes.length - 1; i >=0; i--)
{
sortOrderModel.remove(indexes[i]);
}
listListener.valueChanged(null);
}
});
gbc.insets.top = 5;
gbc.gridx = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.EAST;
c.add(add, gbc);
gbc.gridy ++;
gbc.insets.top = 10;
gbc.gridwidth = 1;
gbc.gridheight = 3;
gbc.gridx = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
sortOrderModel = new DefaultListModel();
sortOrder.setModel(sortOrderModel);
sortOrder.setCellRenderer(new VLVSortOrderRenderer(sortOrder));
sortOrder.setVisibleRowCount(6);
sortOrder.setPrototypeCellValue("AjA");
c.add(Utilities.createScrollPane(sortOrder), gbc);
sortOrder.addListSelectionListener(listListener);
gbc.gridx = 2;
gbc.weighty = 0.0;
gbc.weightx = 0.0;
gbc.gridheight = 1;