log.debug("Putting PanelInstance " + id + " to region " + region + ", position " + position);
final WorkspaceImpl workspace = NavigationManager.lookup().getCurrentWorkspace();
final Section section = NavigationManager.lookup().getCurrentSection();
final PanelProvider provider = UIServices.lookup().getPanelsProvidersManager().getProvider(id != null ? id.trim() : null);
if (provider == null) { //Create new panel, instance is given in id
final PanelInstance instance = workspace.getPanelInstance(id);
if (instance != null) {
final Panel newPanel = new Panel();
HibernateTxFragment txFragment = new HibernateTxFragment() {
protected void txFragment(Session session) throws Exception {
newPanel.setInstance(instance);
newPanel.setSection(section);
Layout layout = section.getLayout();
if (layout != null)
newPanel.getProvider().getDriver().fireBeforePanelPlacedInRegion(newPanel, layout.getRegion(region));
newPanel.setLayoutRegionId(region);
UIServices.lookup().getPanelsManager().store(newPanel);
// Assign panel & save changes
section.assignPanel(newPanel, region);
if (position != -1) {
SectionRegion sr = newPanel.getSection().getSectionRegion(region);
int maxLoops = 30;
while (newPanel.getPosition() != position && (maxLoops--) > 0) {
if (newPanel.getPosition() > position)
sr.moveBackInRegion(newPanel);
else
sr.moveForwardInRegion(newPanel);
}
}
UIServices.lookup().getSectionsManager().store(section);
newPanel.getProvider().getDriver().fireAfterPanelPlacedInRegion(newPanel, null);
hideModalDialog();
}
};
txFragment.execute();
} else {
log.error("Instance with id <" + id + "> not found in current workspace. Cannot put panel to region " + region);
log.error("Also, provider with id <" + id + "> is not found in current installation.");
}
} else {//Create new instance and new panel, provider is given in id.
final PanelInstance newInstance = new PanelInstance();
HibernateTxFragment txFragment = new HibernateTxFragment() {
protected void txFragment(Session session) throws Exception {
newInstance.setProvider(provider);
newInstance.setWorkspace(workspace);
Locale[] locales = (LocaleManager.lookup()).getPlatformAvailableLocales();
for (int i = 0; i < locales.length; i++) {
Locale locale = locales[i];
String panelTitle = provider.getResource(provider.getDescription(), locale);
if (panelTitle != null)
newInstance.setTitle(StringEscapeUtils.unescapeHtml(panelTitle), locale.toString());
}
// Add instance to workspace
workspace.addPanelInstance(newInstance);