if (position > -1)
{
int target = -1;
Portlets set = profile.getDocument()
.getPortletsById(customizedSet.getID());
Layout targetLayout = null;
Layout baseLayout = null;
if (set != null)
{
// check if we can possibly move as requested and calculate
// target position
if ( moveUp )
{
if ( (position >= set.getPortletsCount())
&& (position >= set.getEntryCount()) ) return;
target = position + 1;
}
else
{
if (position ==0) return;
target = position - 1;
}
// first find objects at explicit portlets position
for(int i=0; i < set.getPortletsCount(); i++)
{
if ((targetLayout!=null) && (baseLayout!=null)) break;
Portlets p = set.getPortlets(i);
Layout layout = p.getLayout();
if ((layout!=null)&&(layout.getPosition()!=-1))
{
int lpos = (int)layout.getPosition();
if ((baseLayout == null) && (lpos==position))
{
baseLayout = layout;
}
if ((targetLayout == null) && (lpos==target))
{
targetLayout = layout;
}
}
}
// try explicit entry position
for(int i=0; i < set.getEntryCount(); i++)
{
if ((targetLayout!=null) && (baseLayout!=null)) break;
Entry p = set.getEntry(i);
Layout layout = p.getLayout();
if ((layout!=null)&&(layout.getPosition()!=-1))
{
int lpos = (int)layout.getPosition();
if ((baseLayout == null) && (lpos==position))
{
baseLayout = layout;
}
if ((targetLayout == null) && (lpos==target))
{
targetLayout = layout;
}
}
}
//else use implicit position
if (baseLayout == null)
{
if (position < set.getPortletsCount())
{
Portlets p = set.getPortlets(position);
if (p.getLayout()==null)
{
p.setLayout(new PsmlLayout());
}
baseLayout=p.getLayout();
}
if (position < set.getEntryCount())
{
Entry p = set.getEntry(position);
if (p.getLayout()==null)
{
p.setLayout(new PsmlLayout());
}
baseLayout=p.getLayout();
}
}
if (targetLayout == null)
{
if (target < set.getPortletsCount())
{
Portlets p = set.getPortlets(target);
if (p.getLayout()==null)
{
p.setLayout(new PsmlLayout());
}
targetLayout=p.getLayout();
}
if (target < set.getEntryCount())
{
Entry p = set.getEntry(target);
if (p.getLayout()==null)
{
p.setLayout(new PsmlLayout());
}
targetLayout=p.getLayout();
}
}
//we should now have found both baseLayout and targetLayout, swap
//their positions using explicit positioning