//END CUSTOM
}
public void addExternalPackage(String filename) {
Package mainPkg = getMainPackage();
XPDLHandler xpdlhmain = JaWEManager.getInstance().getXPDLHandler();
XPDLHandler xpdlh = null;
if (filename != null && filename.length() > 0) {
try {
xpdlh = new XPDLHandler(new DefaultXPDLHandlerSettings());
Package pkg = xpdlh.openPackage(filename, true);
// do not allow insertion if package has the same Id as the main one,
// or as some of its external packages
Set mainPkgExtPkgIds = new HashSet(mainPkg.getExternalPackageIds());
boolean canInsert = true;
if (mainPkgExtPkgIds.contains(pkg.getId())) {
canInsert = false;
}
if (canInsert) {
canInsert = checkInsertion(xpdlhmain, xpdlh, mainPkg.getId());
}
if (canInsert) {
List l = XMLUtil.getAllExternalPackageIds(xpdlhmain,
mainPkg,
new HashSet());
if (!l.contains(pkg.getId()) && xpdlhmain.getPackageById(pkg.getId()) != null) {
canInsert = false;
}
}
if (canInsert) {
Set pkgIdsToInsert = new HashSet(xpdlh.getAllPackageIds());
Set otherEPIds = new HashSet(mainPkg.getExternalPackageIds());
Set allOtherEPIds = new HashSet(otherEPIds);
List l = XMLUtil.getAllExternalPackageIds(xpdlhmain,
mainPkg,
new HashSet());
List ids = new ArrayList(xpdlhmain.getAllPackageIds());
ids.removeAll(l);
allOtherEPIds.addAll(ids);
Iterator it = otherEPIds.iterator();
while (it.hasNext()) {
String pkgId = (String) it.next();
Package p = xpdlhmain.getPackageById(pkgId);
allOtherEPIds.addAll(XMLUtil.getAllExternalPackageIds(xpdlhmain,
p,
new HashSet()));
}
pkgIdsToInsert.removeAll(allOtherEPIds);
Set pkgsToInsert = new HashSet();
it = pkgIdsToInsert.iterator();
while (it.hasNext()) {
String pkgId = (String) it.next();
Package tAdd = xpdlh.getPackageById(pkgId);
pkgsToInsert.add(tAdd);
}
xpdlhmain.synchronizePackages(xpdlh);
Package realPkg = xpdlhmain.getPackageById(pkg.getId());
ExternalPackage ep = null;
// insert ExternalPackage
ExternalPackages eps = mainPkg.getExternalPackages();
File f = new File(xpdlhmain.getAbsoluteFilePath(mainPkg));
String parentF = f.getParent();
Path newPath = new Path(parentF);
String eppath = xpdlhmain.getAbsoluteFilePath(realPkg);
String relativePath = Path.getRelativePath(new Path(eppath), newPath);
ep = JaWEManager.getInstance().getXPDLObjectFactory().createXPDLObject(eps, "", false);
ep.setHref(relativePath);
JaWEEAHandler.setExternalPackageEA(ep, realPkg.getId());
mainPkg.addExternalPackageMapping(relativePath, realPkg.getId());
eps.add(ep);
it = pkgsToInsert.iterator();
while (it.hasNext()) {
Package p = (Package) it.next();
boolean changed = adjustXPDL(p);
XPDLListenerAndObservable xpdl;
xpdl = createNewXPDLListenerObservable(p, false, false);
p.setReadOnly(true);
xpdl.setModified(changed);
}
setChanged();
notifyObservers(createInfo(mainPkg,
new ArrayList(pkgsToInsert),
XMLElementChangeInfo.INSERTED));
if (ep != null) {
getSelectionManager().setSelection(ep, true);
}
} else {
getJaWEFrame().message(settings.getLanguageDependentString("InformationExternalPackageCannotBeInserted"),
JOptionPane.INFORMATION_MESSAGE);
xpdlh.closeAllPackages();
}
} catch (Exception ex) {
ex.printStackTrace();
getJaWEFrame().message(settings.getLanguageDependentString("InformationExternalPackageCannotBeInserted"),
JOptionPane.INFORMATION_MESSAGE);
if (xpdlh != null) {
xpdlh.closeAllPackages();
}
}
}
}