Source Code Cross Referenced for JaWEController.java in  » Workflow-Engines » JaWE » org » enhydra » jawe » base » controller » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Workflow Engines » JaWE » org.enhydra.jawe.base.controller 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        package org.enhydra.jawe.base.controller;
0002:
0003:        import java.io.ByteArrayOutputStream;
0004:        import java.io.File;
0005:        import java.io.FileInputStream;
0006:        import java.io.FileOutputStream;
0007:        import java.io.IOException;
0008:        import java.io.OutputStream;
0009:        import java.io.RandomAccessFile;
0010:        import java.lang.reflect.Constructor;
0011:        import java.nio.channels.FileLock;
0012:        import java.nio.channels.NonWritableChannelException;
0013:        import java.util.ArrayList;
0014:        import java.util.Arrays;
0015:        import java.util.Collection;
0016:        import java.util.Collections;
0017:        import java.util.HashMap;
0018:        import java.util.HashSet;
0019:        import java.util.Iterator;
0020:        import java.util.List;
0021:        import java.util.Map;
0022:        import java.util.Observable;
0023:        import java.util.Observer;
0024:        import java.util.Properties;
0025:        import java.util.Set;
0026:
0027:        import javax.swing.JComponent;
0028:        import javax.swing.JOptionPane;
0029:        import javax.xml.parsers.DocumentBuilder;
0030:        import javax.xml.parsers.DocumentBuilderFactory;
0031:        import javax.xml.transform.Transformer;
0032:        import javax.xml.transform.TransformerFactory;
0033:        import javax.xml.transform.dom.DOMSource;
0034:        import javax.xml.transform.stream.StreamResult;
0035:
0036:        import org.enhydra.jawe.BarFactory;
0037:        import org.enhydra.jawe.ChoiceButton;
0038:        import org.enhydra.jawe.ChoiceButtonListener;
0039:        import org.enhydra.jawe.JaWEComponent;
0040:        import org.enhydra.jawe.JaWEComponentSettings;
0041:        import org.enhydra.jawe.JaWEComponentView;
0042:        import org.enhydra.jawe.JaWEConstants;
0043:        import org.enhydra.jawe.JaWEEAHandler;
0044:        import org.enhydra.jawe.JaWEManager;
0045:        import org.enhydra.jawe.JaWEXMLUtil;
0046:        import org.enhydra.jawe.UndoHistoryManager;
0047:        import org.enhydra.jawe.Utils;
0048:        import org.enhydra.jawe.WaitScreen;
0049:        import org.enhydra.jawe.XPDLElementChangeInfo;
0050:        import org.enhydra.jawe.XPDLListenerAndObservable;
0051:        import org.enhydra.jawe.base.xpdlhandler.XPDLHandler;
0052:        import org.enhydra.jawe.base.xpdlobjectfactory.XPDLObjectFactory;
0053:        import org.enhydra.jawe.base.xpdlvalidator.ValidationError;
0054:        import org.enhydra.shark.utilities.SequencedHashMap;
0055:        import org.enhydra.shark.xpdl.Path;
0056:        import org.enhydra.shark.xpdl.StandardPackageValidator;
0057:        import org.enhydra.shark.xpdl.XMLAttribute;
0058:        import org.enhydra.shark.xpdl.XMLCollection;
0059:        import org.enhydra.shark.xpdl.XMLCollectionElement;
0060:        import org.enhydra.shark.xpdl.XMLComplexElement;
0061:        import org.enhydra.shark.xpdl.XMLElement;
0062:        import org.enhydra.shark.xpdl.XMLElementChangeInfo;
0063:        import org.enhydra.shark.xpdl.XMLUtil;
0064:        import org.enhydra.shark.xpdl.XMLValidationError;
0065:        import org.enhydra.shark.xpdl.elements.Activities;
0066:        import org.enhydra.shark.xpdl.elements.Activity;
0067:        import org.enhydra.shark.xpdl.elements.ActivitySet;
0068:        import org.enhydra.shark.xpdl.elements.ActivitySets;
0069:        import org.enhydra.shark.xpdl.elements.Application;
0070:        import org.enhydra.shark.xpdl.elements.Applications;
0071:        import org.enhydra.shark.xpdl.elements.DataField;
0072:        import org.enhydra.shark.xpdl.elements.DataFields;
0073:        import org.enhydra.shark.xpdl.elements.EnumerationValue;
0074:        import org.enhydra.shark.xpdl.elements.ExtendedAttribute;
0075:        import org.enhydra.shark.xpdl.elements.ExtendedAttributes;
0076:        import org.enhydra.shark.xpdl.elements.ExternalPackage;
0077:        import org.enhydra.shark.xpdl.elements.ExternalPackages;
0078:        import org.enhydra.shark.xpdl.elements.FormalParameter;
0079:        import org.enhydra.shark.xpdl.elements.FormalParameters;
0080:        import org.enhydra.shark.xpdl.elements.Join;
0081:        import org.enhydra.shark.xpdl.elements.Member;
0082:        import org.enhydra.shark.xpdl.elements.Namespace;
0083:        import org.enhydra.shark.xpdl.elements.Namespaces;
0084:        import org.enhydra.shark.xpdl.elements.Package;
0085:        import org.enhydra.shark.xpdl.elements.Participant;
0086:        import org.enhydra.shark.xpdl.elements.Participants;
0087:        import org.enhydra.shark.xpdl.elements.Responsible;
0088:        import org.enhydra.shark.xpdl.elements.Responsibles;
0089:        import org.enhydra.shark.xpdl.elements.Split;
0090:        import org.enhydra.shark.xpdl.elements.Tools;
0091:        import org.enhydra.shark.xpdl.elements.Transition;
0092:        import org.enhydra.shark.xpdl.elements.TransitionRef;
0093:        import org.enhydra.shark.xpdl.elements.TransitionRefs;
0094:        import org.enhydra.shark.xpdl.elements.TransitionRestriction;
0095:        import org.enhydra.shark.xpdl.elements.TransitionRestrictions;
0096:        import org.enhydra.shark.xpdl.elements.Transitions;
0097:        import org.enhydra.shark.xpdl.elements.TypeDeclaration;
0098:        import org.enhydra.shark.xpdl.elements.TypeDeclarations;
0099:        import org.enhydra.shark.xpdl.elements.WorkflowProcess;
0100:        import org.enhydra.shark.xpdl.elements.WorkflowProcesses;
0101:        import org.w3c.dom.Document;
0102:
0103:        /**
0104:         * Used to handle JaWE.
0105:         * 
0106:         * @author Sasa Bojanic
0107:         * @author Miroslav Popov
0108:         */
0109:        public class JaWEController extends Observable implements  Observer,
0110:                JaWEComponent, ChoiceButtonListener {
0111:
0112:            protected ControllerSettings settings;
0113:
0114:            protected boolean undoableChangeInProgress = false;
0115:
0116:            protected boolean updateInProgress = false;
0117:
0118:            protected boolean updateSpecialInProgress = false;
0119:
0120:            protected JaWEEdit edit = new JaWEEdit();
0121:
0122:            protected JaWETypes jtypes;
0123:
0124:            protected JaWETypeResolver jtypeResolver;
0125:
0126:            protected List xpdlInfoList = new ArrayList();
0127:
0128:            protected List xpdlListenerObservables = new ArrayList();
0129:
0130:            protected JaWESelectionManager selectionMng;
0131:
0132:            protected JaWEActions defaultJaWEActions;
0133:
0134:            protected UndoHistoryManager undoHistoryManager;
0135:
0136:            protected JaWEFrame frame;
0137:
0138:            protected boolean jaweFrameShown = false;
0139:
0140:            protected XPDLElementChangeInfo undoSelectionEvent = null;
0141:
0142:            protected String currentConfig = null;
0143:
0144:            protected Map configInfo;
0145:
0146:            public JaWEController(ControllerSettings settings) {
0147:                this .settings = settings;
0148:                this .settings.init(this );
0149:
0150:                jtypes = new JaWETypes();
0151:                jtypes.setPropertyMgr(settings.getPropertyMgr());
0152:                jtypes.init(this );
0153:            }
0154:
0155:            // ********************** Observable
0156:            public void addObserver(Observer obs) {
0157:                super .addObserver(obs);
0158:                if (obs instanceof  Observable) {
0159:                    ((Observable) obs).addObserver(this );
0160:                }
0161:            }
0162:
0163:            // **********************
0164:
0165:            // ********************** Observer
0166:            public void update(Observable o, Object arg) {
0167:                if (updateInProgress) {
0168:                    JaWEManager
0169:                            .getInstance()
0170:                            .getLoggingManager()
0171:                            .warn(
0172:                                    "JaWEController -> this is nested event dispatch, because another one is in progress!");
0173:                    Thread.dumpStack();
0174:                    return;
0175:                }
0176:                if (!(arg instanceof  XMLElementChangeInfo)
0177:                        || ((XMLElementChangeInfo) arg).getChangedElement() == null) {
0178:                    JaWEManager.getInstance().getLoggingManager().error(
0179:                            "JaWEController -> Invalid arg " + arg
0180:                                    + " or element");
0181:                    Thread.dumpStack();
0182:                    return;
0183:                }
0184:                XPDLElementChangeInfo info;
0185:                if (!(arg instanceof  XPDLElementChangeInfo)) {
0186:                    info = new XPDLElementChangeInfo(this ,
0187:                            (XMLElementChangeInfo) arg);
0188:                } else {
0189:                    info = (XPDLElementChangeInfo) arg;
0190:                }
0191:                if (arg instanceof  XPDLElementChangeInfo
0192:                        && info.getSource() == this ) {
0193:                    JaWEManager
0194:                            .getInstance()
0195:                            .getLoggingManager()
0196:                            .error(
0197:                                    "JaWEController -> Aborting update because JaWEController is a source!");
0198:                    Thread.dumpStack();
0199:                    return;
0200:                }
0201:
0202:                int action = info.getAction();
0203:
0204:                XMLElement chel = info.getChangedElement();
0205:                if (chel != null && chel.getParent() instanceof  Package) {
0206:                    if (action == XMLElementChangeInfo.UPDATED
0207:                            && chel instanceof  XMLAttribute
0208:                            && chel.toName().equals("Id")
0209:                            || chel.toName().equals("Name")) {
0210:                        if (chel.toName().equals("Id")) {
0211:                            changePackageId((Package) chel.getParent(),
0212:                                    (String) info.getOldValue(), (String) info
0213:                                            .getNewValue());
0214:                        }
0215:                        updateTitle();
0216:                    }
0217:                }
0218:
0219:                if (isUndoOrRedoInProgress()) {
0220:                    JaWEManager
0221:                            .getInstance()
0222:                            .getLoggingManager()
0223:                            .debug(
0224:                                    "JaWEController -> event "
0225:                                            + info
0226:                                            + " won't be taken into account while processing undo/redo actions!");
0227:                    return;
0228:                }
0229:                if (undoableChangeInProgress) {
0230:                    if (!(action == XMLElementChangeInfo.INSERTED
0231:                            || action == XMLElementChangeInfo.REMOVED
0232:                            || action == XMLElementChangeInfo.UPDATED || action == XMLElementChangeInfo.REPOSITIONED)) {
0233:                        JaWEManager
0234:                                .getInstance()
0235:                                .getLoggingManager()
0236:                                .error(
0237:                                        "JaWEController -> event "
0238:                                                + info
0239:                                                + " won't be taken into account while processing undoable change action!");
0240:                        return;
0241:                    }
0242:
0243:                    xpdlInfoList.add(info);
0244:                    return;
0245:                }
0246:
0247:                if (updateSpecialInProgress) {
0248:                    JaWEManager
0249:                            .getInstance()
0250:                            .getLoggingManager()
0251:                            .warn(
0252:                                    "JaWEController -> this is nested event dispatch, because another special update is in progress!");
0253:                    Thread.dumpStack();
0254:                    return;
0255:                }
0256:
0257:                updateInProgress = true;
0258:                JaWEManager.getInstance().getLoggingManager().info(
0259:                        "JaWEController -> normal update for event " + arg
0260:                                + " started ...");
0261:
0262:                if (action == XPDLElementChangeInfo.SELECTED) {
0263:                    JaWEManager
0264:                            .getInstance()
0265:                            .getLoggingManager()
0266:                            .error(
0267:                                    "JaWEController -> SELECTION event not sent from JaWEController! ");
0268:                    Thread.dumpStack();
0269:                    return;
0270:                }
0271:                clearHistory();
0272:
0273:                setChanged();
0274:                notifyObservers(info);
0275:
0276:                JaWEManager.getInstance().getLoggingManager().info(
0277:                        "JaWEController -> normal update ended");
0278:                updateTitle();
0279:                adjustActions();
0280:                updateInProgress = false;
0281:
0282:                if (settings.isDesingTimeValidationEnabled()) {
0283:                    if (getMainPackage() != null
0284:                            && (action == XMLElementChangeInfo.INSERTED
0285:                                    || action == XMLElementChangeInfo.REMOVED
0286:                                    || action == XMLElementChangeInfo.UPDATED || action == XMLElementChangeInfo.REPOSITIONED)) {
0287:                        checkValidity(getMainPackage(), true, false, true);
0288:                    }
0289:                }
0290:
0291:                handleEvent(info);
0292:            }
0293:
0294:            // **********************
0295:
0296:            // ********************** JaWEComponent
0297:            public JaWEComponentSettings getSettings() {
0298:                return settings;
0299:            }
0300:
0301:            public JaWEComponentView getView() {
0302:                return getJaWEFrame();
0303:            }
0304:
0305:            public String getName() {
0306:                return "TogWE";
0307:            }
0308:
0309:            public String getType() {
0310:                return JaWEComponent.MAIN_COMPONENT;
0311:            }
0312:
0313:            public void setType(String type) {
0314:            }
0315:
0316:            public boolean adjustXPDL(Package pkg) {
0317:                boolean changed = JaWEManager.getInstance().getXPDLUtils()
0318:                        .correctSplitsAndJoins(pkg);
0319:
0320:                if (JaWEManager.getInstance().getXPDLHandler()
0321:                        .getXPDLRepositoryHandler().isXPDLPrefixEnabled()) {
0322:                    List l = pkg.getNamespaces().toElements();
0323:                    boolean hasxpdlns = false;
0324:                    for (int i = 0; i < l.size(); i++) {
0325:                        Namespace ns = (Namespace) l.get(i);
0326:                        String nsn = ns.getName();
0327:                        if (nsn.equals("xpdl")) {
0328:                            hasxpdlns = true;
0329:                            break;
0330:                        }
0331:                    }
0332:                    if (!hasxpdlns) {
0333:                        Namespaces nss = pkg.getNamespaces();
0334:                        XPDLObjectFactory xpdlof = JaWEManager.getInstance()
0335:                                .getXPDLObjectFactory();
0336:                        Namespace ns = xpdlof.createXPDLObject(nss, "", false);
0337:                        ns.setName("xpdl");
0338:                        ns.setLocation("http://www.wfmc.org/2002/XPDL1.0");
0339:                        nss.add(ns);
0340:                        xpdlof.adjustXPDLObject(pkg, null);
0341:                        changed = true;
0342:                    }
0343:                }
0344:                Iterator comps = JaWEManager.getInstance()
0345:                        .getComponentManager().getComponents().iterator();
0346:                while (comps.hasNext()) {
0347:                    JaWEComponent jc = (JaWEComponent) comps.next();
0348:                    if (jc != this ) {
0349:                        changed = jc.adjustXPDL(pkg) || changed;
0350:                    }
0351:                }
0352:
0353:                // configuration ext. attribute
0354:                if (!JaWEEAHandler.getJaWEConfig(pkg)
0355:                        .equals(getCurrentConfig())) {
0356:                    JaWEEAHandler.setJaWEConfig(pkg, getCurrentConfig());
0357:                    changed = true;
0358:                }
0359:
0360:                String verInfo = JaWEManager.getVersion() + "-"
0361:                        + JaWEManager.getRelease() + "-"
0362:                        + JaWEManager.getBuildEdition()
0363:                        + JaWEManager.getBuildEditionSuffix() + "-"
0364:                        + JaWEManager.getBuildNo();
0365:                if (!JaWEEAHandler.getEditingToolVersion(pkg).equals(verInfo)) {
0366:                    JaWEEAHandler.setEditingToolVersion(pkg, verInfo);
0367:                    changed = true;
0368:                }
0369:                if (!JaWEEAHandler.getEditingTool(pkg).equals(
0370:                        JaWEManager.getInstance().getName())) {
0371:                    JaWEEAHandler.setEditingTool(pkg, JaWEManager.getInstance()
0372:                            .getName());
0373:                    changed = true;
0374:                }
0375:
0376:                if (JaWEEAHandler.removeOldPackageEAs(pkg)) {
0377:                    changed = true;
0378:                }
0379:
0380:                // add external package's ext. attrib. if it does not exist
0381:                if (JaWEEAHandler.adjustExternalPackageEAs(pkg)) {
0382:                    changed = true;
0383:                }
0384:
0385:                return changed;
0386:            }
0387:
0388:            public boolean checkValidity(Package el, boolean fullCheck,
0389:                    boolean specNotif, boolean initialOrDesignTimeValidation) {
0390:                // configure validator
0391:                // System.err.println("Checking validity for
0392:                // "+((XMLComplexElement)el).get("Id").toValue());
0393:                StandardPackageValidator xpdlValidator = JaWEManager
0394:                        .getInstance().getXPDLValidator();
0395:                xpdlValidator.init(JaWEManager.getInstance().getXPDLHandler(),
0396:                        XMLUtil.getPackage(el), !specNotif, settings
0397:                                .getEncoding(), JaWEManager.getInstance()
0398:                                .getStartingLocale());
0399:                List l = checkValidity(el, fullCheck);
0400:                XPDLElementChangeInfo info = createInfo(el, l,
0401:                        XPDLElementChangeInfo.VALIDATION_ERRORS);
0402:                info.setNewValue(new Boolean(specNotif));
0403:                info.setOldValue(new Boolean(initialOrDesignTimeValidation));
0404:                sendEvent(info);
0405:                return (l == null || l.size() == 0);
0406:            }
0407:
0408:            public List checkValidity(XMLElement el, boolean fullCheck) {
0409:                StandardPackageValidator xpdlValidator = JaWEManager
0410:                        .getInstance().getXPDLValidator();
0411:
0412:                if (el == null)
0413:                    return null;
0414:
0415:                List existingErrors = new ArrayList();
0416:                xpdlValidator.validateElement(el, existingErrors, fullCheck);
0417:                List verrors = new ArrayList();
0418:                for (int i = 0; i < existingErrors.size(); i++) {
0419:                    ValidationError verr = new ValidationError(
0420:                            (XMLValidationError) existingErrors.get(i));
0421:                    verrors.add(verr);
0422:                }
0423:                existingErrors.clear();
0424:                boolean isValid = verrors.size() == 0;
0425:
0426:                List vers = new ArrayList();
0427:                if (isValid || fullCheck) {
0428:                    Iterator comps = JaWEManager.getInstance()
0429:                            .getComponentManager().getComponents().iterator();
0430:                    while (comps.hasNext()) {
0431:                        JaWEComponent jc = (JaWEComponent) comps.next();
0432:                        if (jc != this ) {
0433:                            List l = jc.checkValidity(el, fullCheck);
0434:                            isValid = (l == null || l.size() == 0) && isValid;
0435:                            if (l != null) {
0436:                                vers.addAll(l);
0437:                            }
0438:                            if (!(isValid || fullCheck)) {
0439:                                break;
0440:                            }
0441:                        }
0442:                    }
0443:                }
0444:
0445:                verrors.addAll(vers);
0446:                verrors = Utils.sortValidationErrorList(verrors, XMLUtil
0447:                        .getPackage(el));
0448:                return verrors;
0449:            }
0450:
0451:            public boolean canCreateElement(XMLCollection col) {
0452:                return canCreateElement(col, true);
0453:            }
0454:
0455:            public boolean canInsertElement(XMLCollection col, XMLElement el) {
0456:                return canInsertElement(col, el, true);
0457:            }
0458:
0459:            public boolean canModifyElement(XMLElement el) {
0460:                return canModifyElement(el, true);
0461:            }
0462:
0463:            public boolean canRemoveElement(XMLCollection col, XMLElement el) {
0464:                return canRemoveElement(col, el, true);
0465:            }
0466:
0467:            public boolean canDuplicateElement(XMLCollection col, XMLElement el) {
0468:                return canDuplicateElement(col, el, true);
0469:            }
0470:
0471:            public boolean canRepositionElement(XMLCollection col, XMLElement el) {
0472:                return canRepositionElement(col, el, true);
0473:            }
0474:
0475:            // **********************
0476:
0477:            // ********************** ChoiceButtonListener
0478:            public void selectionChanged(ChoiceButton cbutton, Object change) {
0479:                if (cbutton instanceof  JaWETypeChoiceButton
0480:                        && ((JaWETypeChoiceButton) cbutton).getXPDLChoiceType() != null
0481:                        && cbutton.getChoiceType().equals(JaWEType.class)) {
0482:                    JaWEType jtype = (JaWEType) change;
0483:
0484:                    String typeId = null;
0485:                    if (jtype != null) {
0486:                        typeId = jtype.getTypeId();
0487:                    }
0488:
0489:                    Class xpdlClass = ((JaWETypeChoiceButton) cbutton)
0490:                            .getXPDLChoiceType();
0491:                    if (xpdlClass == Package.class) {
0492:                        if (tryToClosePackage(getMainPackageId(), false)) {
0493:                            newPackage(typeId);
0494:                        }
0495:                    } else {
0496:                        startUndouableChange();
0497:                        List temp = new ArrayList();
0498:                        XMLElement newEl = null;
0499:                        if (xpdlClass == Activities.class
0500:                                || xpdlClass == Activity.class) {
0501:                            ActivitySet as = selectionMng
0502:                                    .getWorkingActivitySet();
0503:                            WorkflowProcess wp = selectionMng
0504:                                    .getWorkingProcess();
0505:                            if (as != null && !as.isReadOnly()) {
0506:                                newEl = JaWEManager.getInstance()
0507:                                        .getXPDLObjectFactory()
0508:                                        .createXPDLObject(as.getActivities(),
0509:                                                typeId, true);
0510:                            } else if (wp != null && !wp.isReadOnly()) {
0511:                                newEl = JaWEManager.getInstance()
0512:                                        .getXPDLObjectFactory()
0513:                                        .createXPDLObject(wp.getActivities(),
0514:                                                typeId, true);
0515:                            }
0516:                        } else if (xpdlClass == ActivitySet.class
0517:                                || xpdlClass == ActivitySets.class) {
0518:                            WorkflowProcess wp = selectionMng
0519:                                    .getWorkingProcess();
0520:                            if (wp != null && !wp.isReadOnly()) {
0521:                                newEl = JaWEManager.getInstance()
0522:                                        .getXPDLObjectFactory()
0523:                                        .createXPDLObject(wp.getActivitySets(),
0524:                                                typeId, true);
0525:                            }
0526:                        } else if (xpdlClass == Application.class
0527:                                || xpdlClass == Applications.class) {
0528:                            Package workingPkg = selectionMng.getWorkingPKG();
0529:                            if (workingPkg != null && !workingPkg.isReadOnly()) {
0530:                                newEl = JaWEManager.getInstance()
0531:                                        .getXPDLObjectFactory()
0532:                                        .createXPDLObject(
0533:                                                workingPkg.getApplications(),
0534:                                                typeId, true);
0535:                            }
0536:                        } else if (xpdlClass == DataField.class
0537:                                || xpdlClass == DataFields.class) {
0538:                            Package workingPkg = selectionMng.getWorkingPKG();
0539:                            if (workingPkg != null && !workingPkg.isReadOnly()) {
0540:                                newEl = JaWEManager.getInstance()
0541:                                        .getXPDLObjectFactory()
0542:                                        .createXPDLObject(
0543:                                                workingPkg.getDataFields(),
0544:                                                typeId, true);
0545:                            }
0546:                        } else if (xpdlClass == ExtendedAttribute.class
0547:                                || xpdlClass == ExtendedAttributes.class) {
0548:                            Package workingPkg = selectionMng.getWorkingPKG();
0549:                            if (workingPkg != null && !workingPkg.isReadOnly()) {
0550:                                newEl = JaWEManager
0551:                                        .getInstance()
0552:                                        .getXPDLObjectFactory()
0553:                                        .createXPDLObject(
0554:                                                workingPkg
0555:                                                        .getExtendedAttributes(),
0556:                                                typeId, true);
0557:                            }
0558:                        } else if (xpdlClass == FormalParameter.class
0559:                                || xpdlClass == FormalParameters.class) {
0560:                            WorkflowProcess wp = selectionMng
0561:                                    .getWorkingProcess();
0562:                            if (wp != null && !wp.isReadOnly()) {
0563:                                newEl = JaWEManager.getInstance()
0564:                                        .getXPDLObjectFactory()
0565:                                        .createXPDLObject(
0566:                                                wp.getFormalParameters(),
0567:                                                typeId, true);
0568:                            }
0569:                        } else if (xpdlClass == Namespace.class
0570:                                || xpdlClass == Namespaces.class) {
0571:                            Package workingPkg = selectionMng.getWorkingPKG();
0572:                            if (workingPkg != null && !workingPkg.isReadOnly()) {
0573:                                newEl = JaWEManager.getInstance()
0574:                                        .getXPDLObjectFactory()
0575:                                        .createXPDLObject(
0576:                                                workingPkg.getNamespaces(),
0577:                                                typeId, true);
0578:                            }
0579:                        } else if (xpdlClass == Participant.class
0580:                                || xpdlClass == Participants.class) {
0581:                            Package workingPkg = selectionMng.getWorkingPKG();
0582:                            if (workingPkg != null && !workingPkg.isReadOnly()) {
0583:                                newEl = JaWEManager.getInstance()
0584:                                        .getXPDLObjectFactory()
0585:                                        .createXPDLObject(
0586:                                                workingPkg.getParticipants(),
0587:                                                typeId, true);
0588:                            }
0589:                        } else if (xpdlClass == Responsible.class
0590:                                || xpdlClass == Responsibles.class) {
0591:                            Package workingPkg = selectionMng.getWorkingPKG();
0592:                            if (workingPkg != null && !workingPkg.isReadOnly()) {
0593:                                newEl = JaWEManager.getInstance()
0594:                                        .getXPDLObjectFactory()
0595:                                        .createXPDLObject(
0596:                                                workingPkg
0597:                                                        .getRedefinableHeader()
0598:                                                        .getResponsibles(),
0599:                                                typeId, true);
0600:                            }
0601:                        } else if (xpdlClass == Transition.class
0602:                                || xpdlClass == Transitions.class) {
0603:                            ActivitySet as = selectionMng
0604:                                    .getWorkingActivitySet();
0605:                            WorkflowProcess wp = selectionMng
0606:                                    .getWorkingProcess();
0607:                            if (as != null && !as.isReadOnly()) {
0608:                                newEl = JaWEManager.getInstance()
0609:                                        .getXPDLObjectFactory()
0610:                                        .createXPDLObject(as.getTransitions(),
0611:                                                typeId, true);
0612:                            } else if (wp != null && !wp.isReadOnly()) {
0613:                                newEl = JaWEManager.getInstance()
0614:                                        .getXPDLObjectFactory()
0615:                                        .createXPDLObject(wp.getTransitions(),
0616:                                                typeId, true);
0617:                            }
0618:                        } else if (xpdlClass == TypeDeclaration.class
0619:                                || xpdlClass == TypeDeclarations.class) {
0620:                            Package workingPkg = selectionMng.getWorkingPKG();
0621:                            if (workingPkg != null && !workingPkg.isReadOnly()) {
0622:                                newEl = JaWEManager.getInstance()
0623:                                        .getXPDLObjectFactory()
0624:                                        .createXPDLObject(
0625:                                                workingPkg
0626:                                                        .getTypeDeclarations(),
0627:                                                typeId, true);
0628:                            }
0629:                        } else if (xpdlClass == WorkflowProcess.class
0630:                                || xpdlClass == WorkflowProcesses.class) {
0631:                            Package workingPkg = selectionMng.getWorkingPKG();
0632:                            if (workingPkg != null && !workingPkg.isReadOnly()) {
0633:                                newEl = JaWEManager
0634:                                        .getInstance()
0635:                                        .getXPDLObjectFactory()
0636:                                        .createXPDLObject(
0637:                                                workingPkg
0638:                                                        .getWorkflowProcesses(),
0639:                                                typeId, true);
0640:                            }
0641:                        }
0642:                        if (newEl != null) {
0643:                            temp.add(newEl);
0644:                        }
0645:                        endUndouableChange(temp);
0646:                    }
0647:                } else {
0648:                    selectionMng.setSelection((XMLElement) change, true);
0649:                    updateTitle();
0650:                    adjustActions();
0651:                }
0652:            }
0653:
0654:            public Object getSelectedObject(ChoiceButton cbutton) {
0655:                return null;
0656:            }
0657:
0658:            public List getChoices(ChoiceButton cbutton) {
0659:                ArrayList toRet = new ArrayList();
0660:                if (cbutton instanceof  JaWETypeChoiceButton) {
0661:                    Class xpdlElType = ((JaWETypeChoiceButton) cbutton)
0662:                            .getXPDLChoiceType();
0663:                    toRet.addAll(jtypes.getTypes(xpdlElType));
0664:                }
0665:
0666:                return toRet;
0667:            }
0668:
0669:            // **********************
0670:
0671:            public void init() {
0672:                selectionMng = new JaWESelectionManager(this );
0673:                try {
0674:                    String uhmc = getControllerSettings()
0675:                            .undoHistoryManagerClass();
0676:                    if (uhmc != null && !uhmc.equals("")) {
0677:                        undoHistoryManager = (UndoHistoryManager) Class
0678:                                .forName(uhmc).newInstance();
0679:                        undoHistoryManager.init(getControllerSettings()
0680:                                .undoHistorySize());
0681:                    }
0682:                } catch (Exception ex) {
0683:                    System.err
0684:                            .println("Failed to instantiate undo history manager!");
0685:                }
0686:
0687:                // try {
0688:                // jtypes.init(this);
0689:                // } catch (Exception e) {
0690:                // // only default types will be available
0691:                // }
0692:
0693:                try {
0694:                    String className = settings.getResolverTypeClassName();
0695:
0696:                    Constructor c = Class.forName(className).getConstructor(
0697:                            new Class[] { JaWEController.class });
0698:                    jtypeResolver = (JaWETypeResolver) c
0699:                            .newInstance(new Object[] { this  });
0700:                    JaWEManager.getInstance().getLoggingManager().info(
0701:                            "JaWEController -> Working with '" + className
0702:                                    + "' as type resolver");
0703:                } catch (Throwable ex) {
0704:                    ex.printStackTrace();
0705:                    JaWEManager
0706:                            .getInstance()
0707:                            .getLoggingManager()
0708:                            .info(
0709:                                    "JaweManager -> Problems while instantiating type resolver! Using default!");
0710:                    jtypeResolver = new JaWETypeResolver(this );
0711:                }
0712:
0713:                defaultJaWEActions = new JaWEActions(this );
0714:                defaultJaWEActions.init();
0715:
0716:                settings.adjustActions();
0717:
0718:                createJaWEFrame();
0719:                updateTitle();
0720:                clearAll();
0721:            }
0722:
0723:            public void registerJaWEComponent(JaWEComponent jaweComponent) {
0724:                if (getJaWEFrame() != null) {
0725:                    JComponent display = jaweComponent.getView().getDisplay();
0726:                    String name = jaweComponent.getName();
0727:                    String type = jaweComponent.getType();
0728:                    if (type.equals(JaWEComponent.MAIN_COMPONENT)) {
0729:                        getJaWEFrame().addMainComponent(name, display);
0730:                    } else if (type.equals(JaWEComponent.SPECIAL_COMPONENT)) {
0731:                        getJaWEFrame().addToSpecialComponents(name, display);
0732:                    } else if (type.equals(JaWEComponent.TREE_COMPONENT)) {
0733:                        getJaWEFrame().addToTreeComponents(name, display);
0734:                    } else if (type.equals(JaWEComponent.OTHER_COMPONENT)) {
0735:                        getJaWEFrame().addToOtherComponents(name, display);
0736:                    } else if (type
0737:                            .equals(JaWEComponent.UPPER_STATUS_COMPONENT)) {
0738:                        getJaWEFrame().addUpperStatusComponent(name, display);
0739:                    } else if (type
0740:                            .equals(JaWEComponent.LOWER_STATUS_COMPONENT)) {
0741:                        getJaWEFrame().addLowerStatusComponent(name, display);
0742:                    }
0743:                }
0744:            }
0745:
0746:            public void removeJaWEComonent(JaWEComponent comp) {
0747:                if (getJaWEFrame() != null) {
0748:                    String type = comp.getType();
0749:                    JComponent display = comp.getView().getDisplay();
0750:                    if (type.equals(JaWEComponent.MAIN_COMPONENT)) {
0751:                        getJaWEFrame().removeMainComponent(display);
0752:                    } else if (type.equals(JaWEComponent.SPECIAL_COMPONENT)) {
0753:                        getJaWEFrame().removeSpecialComponent(display);
0754:                    } else if (type.equals(JaWEComponent.TREE_COMPONENT)) {
0755:                        getJaWEFrame().removeTreeComponent(display);
0756:                    } else if (type.equals(JaWEComponent.OTHER_COMPONENT)) {
0757:                        getJaWEFrame().removeOtherComponent(display);
0758:                    } else if (type
0759:                            .equals(JaWEComponent.UPPER_STATUS_COMPONENT)) {
0760:                        getJaWEFrame().removeUpperStatusComponent(display);
0761:                    } else if (type
0762:                            .equals(JaWEComponent.LOWER_STATUS_COMPONENT)) {
0763:                        getJaWEFrame().removeLowerStatusComponent(display);
0764:                    }
0765:                }
0766:            }
0767:
0768:            public void changeJaWEComponentType(JaWEComponent comp,
0769:                    String newType) {
0770:                if (getJaWEFrame() != null) {
0771:                    removeJaWEComonent(comp);
0772:                    comp.setType(newType);
0773:                    registerJaWEComponent(comp);
0774:                }
0775:            }
0776:
0777:            protected void createJaWEFrame() {
0778:                frame = new JaWEFrame(this );
0779:                frame.configure();
0780:            }
0781:
0782:            public void showJaWEFrame() {
0783:                if (frame != null) {
0784:                    // frame.init();
0785:                    frame.setVisible(true);
0786:                    jaweFrameShown = true;
0787:                }
0788:            }
0789:
0790:            public boolean isPackageModified(String xpdlId) {
0791:                if (xpdlId == null)
0792:                    xpdlId = getMainPackageId();
0793:                Iterator it = xpdlListenerObservables.iterator();
0794:                while (it.hasNext()) {
0795:                    XPDLListenerAndObservable xpdl = (XPDLListenerAndObservable) it
0796:                            .next();
0797:                    if (xpdl.getPackage() != null
0798:                            && xpdl.getPackage().getId().equals(xpdlId)) {
0799:                        return xpdl.isModified();
0800:                    }
0801:                }
0802:                return false;
0803:            }
0804:
0805:            public Package getMainPackage() {
0806:                return JaWEManager.getInstance().getXPDLHandler()
0807:                        .getPackageById(getMainPackageId());
0808:            }
0809:
0810:            public String getMainPackageId() {
0811:                XPDLHandler xpdlhandler = JaWEManager.getInstance()
0812:                        .getXPDLHandler();
0813:                String xpdlId = xpdlhandler.getMainPackageId();
0814:                if (xpdlId == null && xpdlhandler.getAllPackageIds().size() > 0) {
0815:                    Iterator it = xpdlhandler.getAllPackages().iterator();
0816:                    while (it.hasNext()) {
0817:                        Package p = (Package) it.next();
0818:                        if (!p.isTransient() && !p.isReadOnly()) {
0819:                            xpdlId = p.getId();
0820:                            break;
0821:                        }
0822:                    }
0823:                }
0824:                return xpdlId;
0825:            }
0826:
0827:            // ///////////////////////////// XPDL File Handling /////////////////////////
0828:            public String getPackageFilename(String xpdlId) {
0829:                XPDLHandler xpdlhandler = JaWEManager.getInstance()
0830:                        .getXPDLHandler();
0831:                if (xpdlId == null)
0832:                    xpdlId = getMainPackageId();
0833:                Package pkg = xpdlhandler.getPackageById(xpdlId);
0834:                return xpdlhandler.getAbsoluteFilePath(pkg);
0835:            }
0836:
0837:            public void changePackageFileName(Package pkg, String newFileName) {
0838:                XPDLHandler xpdlhandler = JaWEManager.getInstance()
0839:                        .getXPDLHandler();
0840:                xpdlhandler.registerPackageFilename(newFileName, pkg);
0841:            }
0842:
0843:            public void newPackage(String type) {
0844:                clearAll();
0845:
0846:                XPDLHandler xpdlhandler = JaWEManager.getInstance()
0847:                        .getXPDLHandler();
0848:                JaWEManager.getInstance().getLoggingManager().info(
0849:                        "JaWEController -> creating new XPDL, type=" + type);
0850:
0851:                Package pkg = JaWEManager.getInstance().getXPDLObjectFactory()
0852:                        .createPackage(type);
0853:                xpdlhandler.registerPackage(pkg);
0854:                adjustXPDL(pkg);
0855:                createNewXPDLListenerObservable(pkg, true, true);
0856:                setChanged();
0857:                notifyObservers(createInfo(pkg, XMLElementChangeInfo.INSERTED));
0858:                selectionMng.setSelection(pkg, true);
0859:
0860:                JaWEManager.getInstance().getLoggingManager().info(
0861:                        "JaWEController -> new package with Id " + pkg.getId()
0862:                                + " is created");
0863:                // xpdlhandler.printDebug();
0864:                updateTitle();
0865:                adjustActions();
0866:            }
0867:
0868:            public Package openPackageFromFile(String filename) {
0869:                JaWEManager.getInstance().getLoggingManager().info(
0870:                        "JaWEController -> opening package from file "
0871:                                + filename);
0872:                return openPackage(filename, null);
0873:            }
0874:
0875:            public Package openPackageFromStream(byte[] xpdlStream) {
0876:                JaWEManager.getInstance().getLoggingManager().info(
0877:                        "JaWEController -> opening package from stream ");
0878:                return openPackage(null, xpdlStream);
0879:            }
0880:
0881:            protected Package openPackage(String filename, byte[] xpdlStream) {
0882:                WaitScreen ws = new WaitScreen(frame);
0883:                XPDLHandler xpdlh = null;
0884:                try {
0885:                    Package pkg = null;
0886:                    clearAll();
0887:                    XPDLHandler xpdlhandler = JaWEManager.getInstance()
0888:                            .getXPDLHandler();
0889:                    if (jaweFrameShown && filename != null
0890:                            && xpdlStream == null) {
0891:                        ws.show(null, "", settings
0892:                                .getLanguageDependentString("OpeningFile"));
0893:                    }
0894:                    xpdlh = JaWEManager.getInstance().createXPDLHandler(
0895:                            xpdlhandler.getXPDLRepositoryHandler());
0896:
0897:                    try {
0898:                        if (filename != null) {
0899:                            pkg = xpdlh.openPackage(filename, true);
0900:                        } else {
0901:                            pkg = xpdlh.openPackageFromStream(xpdlStream, true);
0902:                        }
0903:                    } catch (Exception ex) {
0904:                        ex.printStackTrace();
0905:                        clearAll();
0906:                        xpdlh.closeAllPackages();
0907:                        ws.setVisible(false);
0908:                        getJaWEFrame()
0909:                                .message(
0910:                                        settings
0911:                                                .getLanguageDependentString("ErrorCannotOpenXPDL"
0912:                                                        + "\n"
0913:                                                        + ex.getMessage()),
0914:                                        JOptionPane.INFORMATION_MESSAGE);
0915:                    }
0916:
0917:                    if (pkg != null) {
0918:                        // do not allow insertion if package has the same Id as the one already
0919:                        // inserted
0920:                        Set allpkgids = new HashSet(xpdlhandler
0921:                                .getAllPackageIds());
0922:                        boolean canInsert = true;
0923:                        if (allpkgids.contains(pkg.getId())) {
0924:                            canInsert = false;
0925:                        }
0926:                        if (canInsert) {
0927:                            xpdlhandler.synchronizePackages(xpdlh);
0928:
0929:                            boolean mainChanged = false;
0930:                            Iterator it = xpdlhandler.getAllPackages()
0931:                                    .iterator();
0932:                            while (it.hasNext()) {
0933:                                Package p = (Package) it.next();
0934:                                boolean changed = adjustXPDL(p);
0935:
0936:                                XPDLListenerAndObservable xpdl;
0937:                                if (p == pkg) {
0938:                                    xpdl = createNewXPDLListenerObservable(pkg,
0939:                                            true, false);
0940:                                    if (changed) {
0941:                                        mainChanged = true;
0942:                                    }
0943:                                } else {
0944:                                    xpdl = createNewXPDLListenerObservable(p,
0945:                                            false, false);
0946:                                    p.setReadOnly(true);
0947:                                }
0948:                                xpdl.setModified(changed);
0949:                            }
0950:
0951:                            List inserted = new ArrayList();
0952:                            inserted.add(pkg);
0953:
0954:                            it = xpdlhandler.getAllPackages().iterator();
0955:                            while (it.hasNext()) {
0956:                                Package pkgext = (Package) it.next();
0957:                                if (pkgext == pkg)
0958:                                    continue;
0959:                                inserted.add(pkgext);
0960:                            }
0961:
0962:                            setChanged();
0963:                            notifyObservers(createInfo(pkg, inserted,
0964:                                    XMLElementChangeInfo.INSERTED));
0965:
0966:                            if (pkg.getWorkflowProcesses().size() > 0) {
0967:                                selectionMng.setSelection(pkg
0968:                                        .getWorkflowProcesses().get(0), true);
0969:                            } else {
0970:                                selectionMng.setSelection(pkg, true);
0971:                            }
0972:
0973:                            JaWEManager.getInstance().getLoggingManager().info(
0974:                                    "JaWEController -> opened package "
0975:                                            + pkg.getId());
0976:                            if (settings.isInitialXPDLValidationEnabled()) {
0977:                                checkValidity(pkg, true, true, true);
0978:                            }
0979:
0980:                            if ((mainChanged) && jaweFrameShown) {
0981:                                if (mainChanged) {
0982:                                    ws.setVisible(false);
0983:                                    frame
0984:                                            .message(
0985:                                                    settings
0986:                                                            .getLanguageDependentString("InformationTogWEHasAutomaticallyAdjustedSomeXPDLParts"),
0987:                                                    JOptionPane.INFORMATION_MESSAGE);
0988:                                }
0989:                            }
0990:
0991:                        } else {
0992:                            getJaWEFrame()
0993:                                    .message(
0994:                                            settings
0995:                                                    .getLanguageDependentString("InformationPackageCannotBeOpened"),
0996:                                            JOptionPane.INFORMATION_MESSAGE);
0997:                        }
0998:                    } else {
0999:                        clearAll();
1000:                        Map pems = xpdlhandler.getParsingErrorMessages();
1001:                        String msg = null;
1002:                        if (pems != null && pems.size() > 0) {
1003:                            Set s = (Set) pems.values().toArray()[0];
1004:                            if (s != null && s.size() > 0) {
1005:                                msg = (String) s.toArray()[0];
1006:                            }
1007:                        }
1008:                        Iterator it = xpdlhandler.getAllPackages().iterator();
1009:                        while (it.hasNext()) {
1010:                            Package p = (Package) it.next();
1011:                            if (!p.isTransient()) {
1012:                                xpdlhandler.closePackageVersion(p.getId(), p
1013:                                        .getInternalVersion());
1014:                            }
1015:                        }
1016:                        ws.setVisible(false);
1017:                        getJaWEFrame()
1018:                                .message(
1019:                                        settings
1020:                                                .getLanguageDependentString("ErrorCannotOpenXPDL")
1021:                                                + ((msg != null) ? "\n" + msg
1022:                                                        : ""),
1023:                                        JOptionPane.INFORMATION_MESSAGE);
1024:                    }
1025:                    // xpdlhandler.printDebug();
1026:                    updateTitle();
1027:                    adjustActions();
1028:
1029:                    return pkg;
1030:                } finally {
1031:                    ws.setVisible(false);
1032:                }
1033:            }
1034:
1035:            public void addExternalPackage() {
1036:                Package mainPkg = getMainPackage();
1037:                XPDLHandler xpdlhmain = JaWEManager.getInstance()
1038:                        .getXPDLHandler();
1039:                getSelectionManager().setSelection(
1040:                        mainPkg.getExternalPackages(), false);
1041:
1042:                String filename = "";
1043:                String message = settings
1044:                        .getLanguageDependentString("DialogChooseFile");
1045:                filename = JaWEXMLUtil.dialog(getJaWEFrame(), message, 0, 0,
1046:                        null);
1047:                XPDLHandler xpdlh = null;
1048:                if (filename != null && filename.length() > 0) {
1049:                    try {
1050:                        xpdlh = JaWEManager.getInstance().createXPDLHandler(
1051:                                xpdlhmain.getXPDLRepositoryHandler());
1052:                        Package pkg = xpdlh.openPackage(filename, true);
1053:                        // do not allow insertion if package has the same Id as the main one,
1054:                        // or as some of its external packages
1055:                        Set mainPkgExtPkgIds = new HashSet(mainPkg
1056:                                .getExternalPackageIds());
1057:                        boolean canInsert = true;
1058:                        if (mainPkgExtPkgIds.contains(pkg.getId())) {
1059:                            canInsert = false;
1060:                        }
1061:                        // System.err.println("CI1="+canInsert);
1062:                        if (canInsert) {
1063:                            canInsert = checkInsertion(xpdlhmain, xpdlh,
1064:                                    mainPkg.getId());
1065:                        }
1066:                        if (canInsert) {
1067:                            List l = XMLUtil.getAllExternalPackageIds(
1068:                                    xpdlhmain, mainPkg, new HashSet());
1069:                            if (!l.contains(pkg.getId())
1070:                                    && xpdlhmain.getPackageById(pkg.getId()) != null) {
1071:                                canInsert = false;
1072:                            }
1073:                        }
1074:                        // System.err.println("CI2="+canInsert);
1075:                        if (canInsert) {
1076:                            Set pkgIdsToInsert = new HashSet(xpdlh
1077:                                    .getAllPackageIds());
1078:                            Set otherEPIds = new HashSet(mainPkg
1079:                                    .getExternalPackageIds());
1080:                            Set allOtherEPIds = new HashSet(otherEPIds);
1081:                            List l = XMLUtil.getAllExternalPackageIds(
1082:                                    xpdlhmain, mainPkg, new HashSet());
1083:                            List ids = new ArrayList(xpdlhmain
1084:                                    .getAllPackageIds());
1085:                            ids.removeAll(l);
1086:                            allOtherEPIds.addAll(ids);
1087:                            Iterator it = otherEPIds.iterator();
1088:                            while (it.hasNext()) {
1089:                                String pkgId = (String) it.next();
1090:                                Package p = xpdlhmain.getPackageById(pkgId);
1091:                                allOtherEPIds.addAll(XMLUtil
1092:                                        .getAllExternalPackageIds(xpdlhmain, p,
1093:                                                new HashSet()));
1094:                            }
1095:                            pkgIdsToInsert.removeAll(allOtherEPIds);
1096:
1097:                            Set pkgsToInsert = new HashSet();
1098:                            it = pkgIdsToInsert.iterator();
1099:                            while (it.hasNext()) {
1100:                                String pkgId = (String) it.next();
1101:                                Package tAdd = xpdlh.getPackageById(pkgId);
1102:                                pkgsToInsert.add(tAdd);
1103:                            }
1104:
1105:                            xpdlhmain.synchronizePackages(xpdlh);
1106:
1107:                            Package realPkg = xpdlhmain.getPackageById(pkg
1108:                                    .getId());
1109:                            ExternalPackage ep = null;
1110:                            // insert ExternalPackage
1111:                            ExternalPackages eps = mainPkg
1112:                                    .getExternalPackages();
1113:                            File f = new File(xpdlhmain
1114:                                    .getAbsoluteFilePath(mainPkg));
1115:                            String parentF = f.getParent();
1116:                            Path newPath = new Path(parentF);
1117:                            String eppath = xpdlhmain
1118:                                    .getAbsoluteFilePath(realPkg);
1119:                            String relativePath = Path.getRelativePath(
1120:                                    new Path(eppath), newPath);
1121:                            ep = JaWEManager.getInstance()
1122:                                    .getXPDLObjectFactory().createXPDLObject(
1123:                                            eps, "", false);
1124:                            ep.setHref(relativePath);
1125:                            JaWEEAHandler.setExternalPackageEA(ep, realPkg
1126:                                    .getId());
1127:                            mainPkg.addExternalPackageMapping(relativePath,
1128:                                    realPkg.getId());
1129:                            eps.add(ep);
1130:                            // List inserted=new ArrayList();
1131:                            // inserted.add(ep);
1132:                            // setChanged();
1133:                            // notifyObservers(createInfo(eps,inserted,
1134:                            // XPDLElementChangeInfo.INSERTED));
1135:
1136:                            it = pkgsToInsert.iterator();
1137:                            while (it.hasNext()) {
1138:                                Package p = (Package) it.next();
1139:                                boolean changed = adjustXPDL(p);
1140:                                XPDLListenerAndObservable xpdl;
1141:                                xpdl = createNewXPDLListenerObservable(p,
1142:                                        false, false);
1143:                                p.setReadOnly(true);
1144:                                xpdl.setModified(changed);
1145:                            }
1146:
1147:                            setChanged();
1148:                            notifyObservers(createInfo(mainPkg, new ArrayList(
1149:                                    pkgsToInsert),
1150:                                    XMLElementChangeInfo.INSERTED));
1151:
1152:                            if (ep != null) {
1153:                                getSelectionManager().setSelection(ep, true);
1154:                            }
1155:
1156:                            // if (settings.isDesingTimeValidationEnabled()) {
1157:                            // it = pkgsToInsert.iterator();
1158:                            // while (it.hasNext()) {
1159:                            // Package p = (Package) it.next();
1160:                            // JaWEManager.getInstance()
1161:                            // .getXPDLValidator()
1162:                            // .reCheckExternalPackage(p);
1163:                            // }
1164:                            //
1165:                            // checkValidity(mainPkg, true, true);
1166:                            // }
1167:
1168:                        } else {
1169:                            getJaWEFrame()
1170:                                    .message(
1171:                                            settings
1172:                                                    .getLanguageDependentString("InformationExternalPackageCannotBeInserted"),
1173:                                            JOptionPane.INFORMATION_MESSAGE);
1174:                            xpdlh.closeAllPackages();
1175:                        }
1176:                    } catch (Exception ex) {
1177:                        ex.printStackTrace();
1178:                        getJaWEFrame()
1179:                                .message(
1180:                                        settings
1181:                                                .getLanguageDependentString("InformationExternalPackageCannotBeInserted"),
1182:                                        JOptionPane.INFORMATION_MESSAGE);
1183:                        if (xpdlh != null) {
1184:                            xpdlh.closeAllPackages();
1185:                        }
1186:                    }
1187:                }
1188:                // xpdlhmain.printDebug();
1189:            }
1190:
1191:            public void removeExternalPackage() {
1192:                Package mainPkg = getMainPackage();
1193:                XPDLHandler xpdlh = JaWEManager.getInstance().getXPDLHandler();
1194:                Package toRemove = (Package) getSelectionManager()
1195:                        .getSelectedElement();
1196:
1197:                Set pkgIdsToRemove = new HashSet();
1198:                pkgIdsToRemove.add(toRemove.getId());
1199:                pkgIdsToRemove.addAll(XMLUtil.getAllExternalPackageIds(xpdlh,
1200:                        toRemove, new HashSet()));
1201:
1202:                Set otherEPIds = new HashSet(mainPkg.getExternalPackageIds());
1203:                otherEPIds.remove(toRemove.getId());
1204:
1205:                Set allOtherEPIds = new HashSet(otherEPIds);
1206:                Iterator it = otherEPIds.iterator();
1207:                while (it.hasNext()) {
1208:                    String pkgId = (String) it.next();
1209:                    Package p = xpdlh.getPackageById(pkgId);
1210:                    allOtherEPIds.addAll(XMLUtil.getAllExternalPackageIds(
1211:                            xpdlh, p, new HashSet()));
1212:                }
1213:
1214:                pkgIdsToRemove.removeAll(allOtherEPIds);
1215:
1216:                Set pkgsToRemove = new HashSet();
1217:                it = pkgIdsToRemove.iterator();
1218:                while (it.hasNext()) {
1219:                    String pkgId = (String) it.next();
1220:                    Package tRem = xpdlh.getPackageById(pkgId);
1221:                    if (tRem != mainPkg && !tRem.isTransient()) {
1222:                        pkgsToRemove.add(tRem);
1223:                    }
1224:                }
1225:
1226:                ExternalPackages eps = mainPkg.getExternalPackages();
1227:                ExternalPackage ep = mainPkg.getExternalPackage(toRemove
1228:                        .getId());
1229:
1230:                if (pkgsToRemove.size() > 0) {
1231:                    boolean warningMessage = false;
1232:                    it = pkgsToRemove.iterator();
1233:                    while (it.hasNext()) {
1234:                        Package tRem = (Package) it.next();
1235:                        List refs = JaWEManager.getInstance().getXPDLUtils()
1236:                                .getReferences(mainPkg, tRem);
1237:                        if (refs.size() > 0) {
1238:                            warningMessage = true;
1239:                            break;
1240:                        }
1241:                    }
1242:                    if (warningMessage) {
1243:                        int yn = JOptionPane
1244:                                .showConfirmDialog(
1245:                                        getJaWEFrame(),
1246:                                        settings
1247:                                                .getLanguageDependentString("MessageReferencedDoYouReallyWantToDeleteSelectedItem"),
1248:                                        settings
1249:                                                .getLanguageDependentString("DeletingKey"),
1250:                                        JOptionPane.YES_NO_OPTION);
1251:                        if (yn != JOptionPane.YES_OPTION) {
1252:                            return;
1253:                        }
1254:                    }
1255:                }
1256:
1257:                try {
1258:                    if (ep != null) {
1259:                        mainPkg.removeExternalPackageMapping(ep.getHref());
1260:                    }
1261:                    if (pkgsToRemove.size() > 0) {
1262:                        it = pkgsToRemove.iterator();
1263:                        while (it.hasNext()) {
1264:                            Package pkg = (Package) it.next();
1265:                            XPDLListenerAndObservable xpdllo = getXPDLListenerObservable(pkg);
1266:                            xpdllo.unregisterFromXPDL();
1267:                            xpdlListenerObservables.remove(xpdllo);
1268:                            xpdlh.closePackageVersion(pkg.getId(), pkg
1269:                                    .getInternalVersion());
1270:                        }
1271:                        setChanged();
1272:                        notifyObservers(createInfo(mainPkg, new ArrayList(
1273:                                pkgsToRemove), XMLElementChangeInfo.REMOVED));
1274:                    }
1275:                    if (ep != null) {
1276:                        eps.remove(ep);
1277:                        List remove = new ArrayList();
1278:                        remove.add(ep);
1279:                        setChanged();
1280:                        notifyObservers(createInfo(eps, remove,
1281:                                XMLElementChangeInfo.REMOVED));
1282:                    }
1283:                    getSelectionManager().setSelection(eps, true);
1284:
1285:                    if (pkgsToRemove.size() > 0) {
1286:                        if (settings.isDesingTimeValidationEnabled()) {
1287:                            it = pkgsToRemove.iterator();
1288:                            while (it.hasNext()) {
1289:                                Package pkg = (Package) it.next();
1290:                                JaWEManager.getInstance().getXPDLValidator()
1291:                                        .clearCache(pkg);
1292:                            }
1293:                            checkValidity(mainPkg, true, true, true);
1294:                        }
1295:                    }
1296:
1297:                } catch (Exception ex) {
1298:                    ex.printStackTrace();
1299:                    getJaWEFrame()
1300:                            .message(
1301:                                    settings
1302:                                            .getLanguageDependentString("ErrorCannotRemoveExternalPackage"),
1303:                                    JOptionPane.INFORMATION_MESSAGE);
1304:                }
1305:                // xpdlh.printDebug();
1306:            }
1307:
1308:            public void addTransientPackage() {
1309:                getSelectionManager().setSelection((XMLElement) null, true);
1310:
1311:                String filename = "";
1312:                String message = settings
1313:                        .getLanguageDependentString("DialogChooseFile");
1314:                filename = JaWEXMLUtil.dialog(getJaWEFrame(), message, 0, 0,
1315:                        null);
1316:                if (filename != null && filename.length() > 0) {
1317:                    boolean added = addTransientPackage(filename);
1318:                    if (!added) {
1319:                        getJaWEFrame()
1320:                                .message(
1321:                                        settings
1322:                                                .getLanguageDependentString("InformationTransientPackageCannotBeInserted"),
1323:                                        JOptionPane.INFORMATION_MESSAGE);
1324:                    }
1325:                }
1326:            }
1327:
1328:            public boolean addTransientPackage(String filename) {
1329:                XPDLHandler xpdlhmain = JaWEManager.getInstance()
1330:                        .getXPDLHandler();
1331:                XPDLHandler xpdlh = null;
1332:                if (filename != null && filename.length() > 0) {
1333:                    try {
1334:                        xpdlh = JaWEManager.getInstance().createXPDLHandler(
1335:                                xpdlhmain.getXPDLRepositoryHandler());
1336:                        Package pkg = xpdlh.openPackage(filename, false);
1337:                        // do not allow insertion if package has the same Id as the main one,
1338:                        // or as some of its external packages
1339:                        Set allpkgids = new HashSet(xpdlhmain
1340:                                .getAllPackageIds());
1341:                        boolean canInsert = true;
1342:                        if (allpkgids.contains(pkg.getId())) {
1343:                            canInsert = false;
1344:                        }
1345:                        // System.err.println("CI1="+canInsert);
1346:                        if (canInsert) {
1347:                            Package mainPkg = getMainPackage();
1348:                            if (mainPkg != null) {
1349:                                canInsert = checkInsertion(xpdlhmain, xpdlh,
1350:                                        mainPkg.getId());
1351:                            }
1352:                        }
1353:                        // System.err.println("CI2="+canInsert);
1354:                        if (canInsert) {
1355:                            xpdlhmain.synchronizePackages(xpdlh);
1356:
1357:                            Package realPkg = xpdlhmain.getPackageById(pkg
1358:                                    .getId());
1359:                            adjustXPDL(realPkg);
1360:                            XPDLListenerAndObservable xpdl = createNewXPDLListenerObservable(
1361:                                    realPkg, false, false);
1362:                            realPkg.setReadOnly(true);
1363:                            xpdl.setModified(false);
1364:
1365:                            List pkgsToInsert = new ArrayList();
1366:                            pkgsToInsert.add(realPkg);
1367:                            setChanged();
1368:                            notifyObservers(createInfo(realPkg, new ArrayList(
1369:                                    pkgsToInsert),
1370:                                    XMLElementChangeInfo.INSERTED));
1371:
1372:                            return true;
1373:                        }
1374:                        xpdlh.closeAllPackages();
1375:                        return false;
1376:                    } catch (Exception ex) {
1377:                        ex.printStackTrace();
1378:                        if (xpdlh != null) {
1379:                            xpdlh.closeAllPackages();
1380:                        }
1381:                    }
1382:                }
1383:                return false;
1384:            }
1385:
1386:            public void removeTransientPackage() {
1387:                XPDLHandler xpdlh = JaWEManager.getInstance().getXPDLHandler();
1388:                Package toRemove = XMLUtil.getPackage(getSelectionManager()
1389:                        .getSelectedElement());
1390:
1391:                try {
1392:                    XPDLListenerAndObservable xpdllo = getXPDLListenerObservable(toRemove);
1393:                    xpdllo.unregisterFromXPDL();
1394:                    xpdlListenerObservables.remove(xpdllo);
1395:                    xpdlh.closePackageVersion(toRemove.getId(), toRemove
1396:                            .getInternalVersion());
1397:                    setChanged();
1398:                    List pkgsToRemove = new ArrayList();
1399:                    pkgsToRemove.add(toRemove);
1400:                    notifyObservers(createInfo(toRemove, pkgsToRemove,
1401:                            XMLElementChangeInfo.REMOVED));
1402:                    getSelectionManager().setSelection(getMainPackage(), true);
1403:                } catch (Exception ex) {
1404:                    ex.printStackTrace();
1405:                    getJaWEFrame()
1406:                            .message(
1407:                                    settings
1408:                                            .getLanguageDependentString("ErrorCannotRemoveTransientPackage"),
1409:                                    JOptionPane.INFORMATION_MESSAGE);
1410:                }
1411:                // xpdlh.printDebug();
1412:            }
1413:
1414:            protected boolean checkInsertion(XPDLHandler xpdlhmain,
1415:                    XPDLHandler xpdlh, String mainPkgId) {
1416:                boolean canInsert = true;
1417:                Collection allPackages = xpdlh.getAllPackages();
1418:                Iterator it = allPackages.iterator();
1419:                while (it.hasNext()) {
1420:                    // do not allow insertion if package has the same path as the one of the main
1421:                    // package or some of the packages that are already referenced
1422:                    Package pkg = (Package) it.next();
1423:                    // System.err.println("Checking pkg "+pkg.getId()+" agains mp "+mainPkgId);
1424:                    if (pkg.getId().equals(mainPkgId)) {
1425:                        canInsert = false;
1426:                        // System.err.println("CI11="+canInsert);
1427:                        break;
1428:                    }
1429:
1430:                    String filename = xpdlh.getAbsoluteFilePath(pkg);
1431:                    Package pkgbfn = xpdlhmain.getPackageByFilename(filename);
1432:                    // System.err.println("Checking filename "+filename+": found pkg "+pkgbfn);
1433:                    if (pkgbfn != null && !pkgbfn.getId().equals(pkg.getId())) {
1434:                        canInsert = false;
1435:                        // System.err.println("CI12="+canInsert);
1436:                        break;
1437:                    }
1438:                }
1439:                return canInsert;
1440:            }
1441:
1442:            public void closePackage(String xpdlId, boolean closeTransient) {
1443:                clearAll();
1444:                XPDLHandler xpdlhandler = JaWEManager.getInstance()
1445:                        .getXPDLHandler();
1446:                Package mainPackage = getMainPackage();
1447:                if (xpdlId == null && mainPackage != null) {
1448:                    xpdlId = mainPackage.getId();
1449:                }
1450:                JaWEManager.getInstance().getLoggingManager().info(
1451:                        "JaWEController -> closing package " + xpdlId);
1452:                if (mainPackage != null && mainPackage.getId().equals(xpdlId)) {
1453:                    String filePath = JaWEManager.getInstance()
1454:                            .getXPDLHandler().getAbsoluteFilePath(mainPackage);
1455:                    List allPackages = new ArrayList(xpdlhandler
1456:                            .getAllPackages());
1457:                    List notToClose = new ArrayList();
1458:                    for (int i = 0; i < allPackages.size(); i++) {
1459:                        Package pkg = (Package) allPackages.get(i);
1460:                        if (!pkg.isTransient() || closeTransient) {
1461:                            xpdlhandler.closePackageVersion(pkg.getId(), pkg
1462:                                    .getInternalVersion());
1463:                        } else {
1464:                            notToClose.add(pkg);
1465:                        }
1466:                    }
1467:                    allPackages.removeAll(notToClose);
1468:                    setChanged();
1469:                    XPDLElementChangeInfo info = createInfo(mainPackage,
1470:                            allPackages, XMLElementChangeInfo.REMOVED);
1471:                    info.setOldValue(filePath);
1472:                    notifyObservers(info);
1473:                    clearXPDLListenerObservables(closeTransient);
1474:                }
1475:                JaWEManager.getInstance().getLoggingManager().info(
1476:                        "JaWEController -> package " + xpdlId + " closed");
1477:                // xpdlhandler.printDebug();
1478:                updateTitle();
1479:                frame.repaint();
1480:                adjustActions();
1481:                System.gc();
1482:            }
1483:
1484:            public boolean isSaveEnabled(boolean isSaveAs) {
1485:                if (getMainPackageId() != null) {
1486:                    if (isSaveAs)
1487:                        return true;
1488:
1489:                    return isPackageModified(getMainPackageId());
1490:                }
1491:
1492:                return false;
1493:            }
1494:
1495:            public void savePackage(String xpdlId, String filename) {
1496:                XPDLHandler xpdlhandler = JaWEManager.getInstance()
1497:                        .getXPDLHandler();
1498:                Package pkg = xpdlhandler.getPackageById(xpdlId);
1499:                String oldFilename = xpdlhandler.getAbsoluteFilePath(pkg);
1500:                try {
1501:
1502:                    // if SaveAs was performed and the document was previously saved,
1503:                    // change ExternalPackage's relative paths
1504:                    boolean isNewFile = !filename.equals(oldFilename);
1505:                    if (oldFilename != null && isNewFile) {
1506:                        boolean crossRefs = JaWEManager.getInstance()
1507:                                .getXPDLUtils().doesCrossreferenceExist(pkg);
1508:                        int r = JOptionPane.YES_OPTION;
1509:                        if (crossRefs) {
1510:                            r = JOptionPane
1511:                                    .showConfirmDialog(
1512:                                            getJaWEFrame(),
1513:                                            settings
1514:                                                    .getLanguageDependentString("MessageCrossReferenceExistDoYouWantToProceed"),
1515:                                            getJaWEFrame().getAppTitle(),
1516:                                            JOptionPane.YES_NO_OPTION);
1517:                        }
1518:                        if (r == JOptionPane.YES_OPTION) {
1519:                            updateExternalPackagesRelativePaths(pkg, filename);
1520:                        } else {
1521:                            // JaWEManager.getInstance().getJaWEController().setFilename(null);
1522:                            return;
1523:                        }
1524:                    }
1525:
1526:                    Document document = null;
1527:
1528:                    DocumentBuilderFactory dbf = DocumentBuilderFactory
1529:                            .newInstance();
1530:                    DocumentBuilder dbuilder = dbf.newDocumentBuilder();
1531:                    document = dbuilder.newDocument();
1532:
1533:                    // retrieve the file writter
1534:                    RandomAccessFile raf = xpdlhandler.getRaf(pkg);
1535:
1536:                    // output stream will either be the FileOutputStream in the
1537:                    // case of save as, or the ByteArrayOutputStream if we are
1538:                    // saving an existing file
1539:                    OutputStream os;
1540:                    if (isNewFile) {
1541:                        // try to open random access file as rw, if it fails
1542:                        // the saving shouldn't occur
1543:                        try {
1544:                            File f = new File(filename);
1545:                            RandomAccessFile r = new RandomAccessFile(f, "rw");
1546:                            FileLock flck = r.getChannel().tryLock();
1547:                            flck.release();
1548:                            r.close(); // Harald Meister
1549:                            // this exception happens when using jdk1.4 under Linux
1550:                            // if it happens, just catch it and proceed with saving
1551:                            // because Linux with jdk1.4.0 doesn't support locking
1552:                        } catch (IOException ioe) {
1553:                            // ioe.printStackTrace();
1554:                            // this happens when the locking fails, and null is returned,
1555:                            // and after that release method is called on the null;
1556:                            // This means that the file we want to save the given
1557:                            // package as, is already locked, so we do not allow saving
1558:                        } catch (NullPointerException npe) {
1559:                            // npe.printStackTrace();
1560:                            throw new Exception();
1561:                        }
1562:                        // if we are at this point, this means either the locking
1563:                        // succeeded, or we use jdk1.4 under Linux that does not
1564:                        // support locking
1565:                        os = new FileOutputStream(filename);
1566:                    } else {
1567:                        os = new ByteArrayOutputStream();
1568:                    }
1569:
1570:                    // Here we get all document elements set
1571:                    JaWEManager.getInstance().getXPDLHandler()
1572:                            .getXPDLRepositoryHandler().toXML(document, pkg);
1573:
1574:                    // Use a Transformer for output
1575:                    TransformerFactory tFactory = TransformerFactory
1576:                            .newInstance();
1577:                    Transformer transformer = tFactory.newTransformer();
1578:                    transformer.setOutputProperty("indent", "yes");
1579:                    transformer.setOutputProperty(
1580:                            "{http://xml.apache.org/xslt}indent-amount", "4");
1581:                    transformer.setOutputProperty("encoding", settings
1582:                            .getEncoding());
1583:                    DOMSource source = new DOMSource(document);
1584:                    StreamResult result = new StreamResult(os);
1585:                    transformer.transform(source, result);
1586:
1587:                    if (!isNewFile && raf != null
1588:                            && os instanceof  ByteArrayOutputStream) {
1589:                        if (raf != null) {
1590:                            // must go to the beginning - otherwise, it will not
1591:                            // truncate the file correctly in some Java-OS combination
1592:                            raf.seek(0);
1593:                            raf.getChannel().truncate(0);
1594:                            raf.write(((ByteArrayOutputStream) os)
1595:                                    .toByteArray());
1596:                        }
1597:                    }
1598:
1599:                    os.close();
1600:
1601:                    XPDLListenerAndObservable xpdl = getXPDLListenerObservable(pkg);
1602:                    if (xpdl != null) {
1603:                        xpdl.setModified(false);
1604:                    }
1605:
1606:                    if (isNewFile) {
1607:                        xpdlhandler.registerPackageFilename(filename, pkg);
1608:                    }
1609:                    try {
1610:                        System.setProperty("user.dir", xpdlhandler
1611:                                .getParentDirectory(pkg));
1612:                    } catch (Exception ex) {
1613:                        ex.printStackTrace();
1614:                    }
1615:
1616:                    // try {
1617:                    // JaWE.getInstance().addToRecentFiles(filename);
1618:                    // } catch (Exception ex) {}
1619:                } catch (NonWritableChannelException nwcex) {
1620:                    nwcex.printStackTrace();
1621:                    getJaWEFrame()
1622:                            .message(
1623:                                    settings
1624:                                            .getLanguageDependentString("ErrorCannotSaveReadOnlyFile"),
1625:                                    JOptionPane.ERROR_MESSAGE);
1626:                } catch (Exception ex) {
1627:                    ex.printStackTrace();
1628:                    getJaWEFrame()
1629:                            .message(
1630:                                    settings
1631:                                            .getLanguageDependentString("ErrorCannotSaveDocument"),
1632:                                    JOptionPane.ERROR_MESSAGE);
1633:                    // ex.printStackTrace();
1634:                }
1635:                // xpdlhandler.printDebug();
1636:                // XPDLElementChangeInfo info = createInfo(pkg, XPDLElementChangeInfo.SELECTED);
1637:                // sendEvent(info);
1638:                updateTitle();
1639:                adjustActions();
1640:            }
1641:
1642:            /*
1643:             * Method for closing document. Returns true if the user really wants to close. Gives
1644:             * chance to save work.
1645:             */
1646:            public boolean tryToClosePackage(String xpdlId,
1647:                    boolean closeTransient) {
1648:                if (xpdlId == null)
1649:                    xpdlId = getMainPackageId();
1650:                if (xpdlId == null)
1651:                    return true;
1652:                int r = JOptionPane.NO_OPTION;
1653:                boolean modified = isPackageModified(xpdlId);
1654:                String filename = getPackageFilename(xpdlId);
1655:                if (modified || filename == null) {
1656:                    r = JOptionPane.showConfirmDialog(getJaWEFrame(), settings
1657:                            .getLanguageDependentString("DialogSaveChanges"),
1658:                            getJaWEFrame().getAppTitle(),
1659:                            JOptionPane.YES_NO_CANCEL_OPTION);
1660:                }
1661:                if (r == JOptionPane.YES_OPTION) {
1662:                    String dialogTitle = settings
1663:                            .getLanguageDependentString("Save"
1664:                                    + BarFactory.LABEL_POSTFIX);
1665:                    if (filename == null) {
1666:                        dialogTitle = settings
1667:                                .getLanguageDependentString("SaveAs"
1668:                                        + BarFactory.LABEL_POSTFIX);
1669:                    }
1670:
1671:                    if (filename == null) {
1672:                        filename = getJaWEFrame().saveDialog(dialogTitle, 0,
1673:                                xpdlId);
1674:                    }
1675:
1676:                    savePackage(xpdlId, filename);
1677:                    if (isPackageModified(xpdlId)) {
1678:                        r = JOptionPane.CANCEL_OPTION;
1679:                    }
1680:                }
1681:                if (r == JOptionPane.CANCEL_OPTION) {
1682:                    // getWindow().dispose();
1683:                    return false;
1684:                }
1685:                closePackage(xpdlId, closeTransient);
1686:                return true;
1687:            }
1688:
1689:            protected XPDLListenerAndObservable createNewXPDLListenerObservable(
1690:                    Package pkg, boolean receiveEvents, boolean modified) {
1691:                XPDLListenerAndObservable xpdl = new XPDLListenerAndObservable(
1692:                        pkg, receiveEvents);
1693:                xpdl.setModified(modified);
1694:                xpdlListenerObservables.add(xpdl);
1695:                xpdl.addObserver(this );
1696:                return xpdl;
1697:            }
1698:
1699:            protected XPDLListenerAndObservable getXPDLListenerObservable(
1700:                    Package pkg) {
1701:                Iterator it = xpdlListenerObservables.iterator();
1702:                while (it.hasNext()) {
1703:                    XPDLListenerAndObservable xpdl = (XPDLListenerAndObservable) it
1704:                            .next();
1705:                    if (xpdl.getPackage() == pkg) {
1706:                        return xpdl;
1707:                    }
1708:                }
1709:                return null;
1710:            }
1711:
1712:            protected XPDLListenerAndObservable getWorkingXPDLListenerObservable() {
1713:                Iterator it = xpdlListenerObservables.iterator();
1714:                while (it.hasNext()) {
1715:                    XPDLListenerAndObservable xpdl = (XPDLListenerAndObservable) it
1716:                            .next();
1717:                    if (xpdl.getPackage() == selectionMng.getWorkingPKG()) {
1718:                        return xpdl;
1719:                    }
1720:                }
1721:                return null;
1722:            }
1723:
1724:            protected void changePackageId(Package pkg, String oldId,
1725:                    String newId) {
1726:                XPDLHandler xpdlhandler = JaWEManager.getInstance()
1727:                        .getXPDLHandler();
1728:                xpdlhandler.changePackageId(pkg, oldId, newId);
1729:            }
1730:
1731:            protected void updateExternalPackagesRelativePaths(Package pkg,
1732:                    String newFilename) {
1733:                XPDLHandler xpdlhandler = JaWEManager.getInstance()
1734:                        .getXPDLHandler();
1735:                File f = new File(newFilename);
1736:                String parentF = f.getParent();
1737:                Path newPath = new Path(parentF);
1738:
1739:                Iterator eps = pkg.getExternalPackages().toElements()
1740:                        .iterator();
1741:                while (eps.hasNext()) {
1742:                    ExternalPackage ep = (ExternalPackage) eps.next();
1743:                    String oldRelativePath = ep.getHref();
1744:                    try {
1745:                        Package extP = xpdlhandler
1746:                                .getExternalPackageByRelativeFilePath(
1747:                                        oldRelativePath, pkg);
1748:                        String oldFullPath = xpdlhandler
1749:                                .getAbsoluteFilePath(extP);
1750:                        String relativePath = Path.getRelativePath(new Path(
1751:                                oldFullPath), newPath);
1752:                        // System.out.println("RP="+relativePath);
1753:                        ep.setHref(XMLUtil
1754:                                .replaceBackslashesWithSlashes(relativePath));
1755:                    } catch (Exception ex) {
1756:                        System.err
1757:                                .println("Failed to update old external package's relative path "
1758:                                        + oldRelativePath
1759:                                        + " for main package "
1760:                                        + pkg.getId()
1761:                                        + " with a new filename " + newFilename);
1762:                        // ex.printStackTrace();
1763:                    }
1764:                }
1765:            }
1766:
1767:            // ////////////////////////// END Of XPDL File Handling /////////////////////
1768:
1769:            public XPDLElementChangeInfo createInfo(XMLElement el, int action) {
1770:                XPDLElementChangeInfo info = new XPDLElementChangeInfo();
1771:                info.setChangedElement(el);
1772:                info.setAction(action);
1773:                info.setSource(this );
1774:                return info;
1775:            }
1776:
1777:            public XPDLElementChangeInfo createInfo(XMLElement main,
1778:                    List elements, int action) {
1779:                XPDLElementChangeInfo info = new XPDLElementChangeInfo();
1780:                info.setChangedElement(main);
1781:                info.setChangedSubElements(elements);
1782:                info.setAction(action);
1783:                info.setSource(this );
1784:                return info;
1785:            }
1786:
1787:            public boolean canCreateElement(XMLCollection col,
1788:                    boolean checkReadOnly) {
1789:                if (checkReadOnly && col.isReadOnly()) {
1790:                    return false;
1791:                }
1792:                if (col instanceof  TransitionRestrictions
1793:                        || col instanceof  TransitionRefs
1794:                        || col instanceof  ExternalPackages) {
1795:                    return false;
1796:                }
1797:
1798:                if (col instanceof  Responsibles) {
1799:                    Map m = JaWEManager.getInstance().getXPDLUtils()
1800:                            .getPossibleResponsibles((Responsibles) col, null);
1801:                    if (m.size() == 0) {
1802:                        return false;
1803:                    }
1804:                }
1805:
1806:                if (col instanceof  Tools) {
1807:                    Map m = XMLUtil.getPossibleApplications(XMLUtil
1808:                            .getWorkflowProcess(col), JaWEManager.getInstance()
1809:                            .getXPDLHandler());
1810:                    if (m.size() == 0) {
1811:                        return false;
1812:                    }
1813:                }
1814:
1815:                Iterator comps = JaWEManager.getInstance()
1816:                        .getComponentManager().getComponents().iterator();
1817:                while (comps.hasNext()) {
1818:                    JaWEComponent jc = (JaWEComponent) comps.next();
1819:                    if (jc != this ) {
1820:                        boolean approved = jc.canCreateElement(col);
1821:                        if (approved == false) {
1822:                            return false;
1823:                        }
1824:                    }
1825:                }
1826:                return true;
1827:            }
1828:
1829:            public boolean canInsertElement(XMLCollection col, XMLElement el,
1830:                    boolean checkReadOnly) {
1831:                if (checkReadOnly && col.isReadOnly()) {
1832:                    return false;
1833:                }
1834:                if (col instanceof  TransitionRestrictions
1835:                        || col instanceof  TransitionRefs
1836:                        || col instanceof  ExternalPackages) {
1837:                    return false;
1838:                } else if (col instanceof  ExtendedAttributes
1839:                        && col.getParent() instanceof  ExternalPackage) {
1840:                    if (el != null
1841:                            && ((ExtendedAttribute) el).getName().equals(
1842:                                    JaWEEAHandler.EA_JAWE_EXTERNAL_PACKAGE_ID)) {
1843:                        return false;
1844:                    }
1845:                }
1846:
1847:                Iterator comps = JaWEManager.getInstance()
1848:                        .getComponentManager().getComponents().iterator();
1849:                while (comps.hasNext()) {
1850:                    JaWEComponent jc = (JaWEComponent) comps.next();
1851:                    if (jc != this ) {
1852:                        boolean approved = jc.canInsertElement(col, el);
1853:                        if (approved == false) {
1854:                            return false;
1855:                        }
1856:                    }
1857:                }
1858:                return true;
1859:            }
1860:
1861:            public boolean canModifyElement(XMLElement el, boolean checkReadOnly) {
1862:                if (checkReadOnly && el.isReadOnly()) {
1863:                    return false;
1864:                }
1865:                if (el instanceof  TransitionRef
1866:                        || el.getParent() instanceof  TransitionRef) {
1867:                    return false;
1868:                } else if (XMLUtil.getParentElement(ExternalPackage.class, el) != null) {
1869:                    if (el.toName().equals("href")) {
1870:                        return false;
1871:                    }
1872:                    ExtendedAttribute ea = (ExtendedAttribute) XMLUtil
1873:                            .getParentElement(ExtendedAttribute.class, el);
1874:                    if (ea != null) {
1875:                        if (ea.getName().equals(
1876:                                JaWEEAHandler.EA_JAWE_EXTERNAL_PACKAGE_ID)) {
1877:                            return false;
1878:                        }
1879:                    }
1880:                }
1881:
1882:                if ((el.getParent() instanceof  Split || el.getParent() instanceof  Join)
1883:                        && el.toName().equals("Type")) {
1884:                    if (el.getParent() instanceof  Split) {
1885:                        Set ogt = XMLUtil.getOutgoingTransitions(XMLUtil
1886:                                .getActivity(el));
1887:                        if (ogt.size() <= 1) {
1888:                            return false;
1889:                        }
1890:                    } else {
1891:                        Set inct = XMLUtil.getIncomingTransitions(XMLUtil
1892:                                .getActivity(el));
1893:                        if (inct.size() <= 1) {
1894:                            return false;
1895:                        }
1896:                    }
1897:                }
1898:
1899:                Iterator comps = JaWEManager.getInstance()
1900:                        .getComponentManager().getComponents().iterator();
1901:                while (comps.hasNext()) {
1902:                    JaWEComponent jc = (JaWEComponent) comps.next();
1903:                    if (jc != this ) {
1904:                        boolean approved = jc.canModifyElement(el);
1905:                        if (approved == false) {
1906:                            return false;
1907:                        }
1908:                    }
1909:                }
1910:                return true;
1911:            }
1912:
1913:            public boolean canRemoveElement(XMLCollection col, XMLElement el,
1914:                    boolean checkReadOnly) {
1915:                if (checkReadOnly && col.isReadOnly()) {
1916:                    return false;
1917:                }
1918:                if (col instanceof  TransitionRestrictions
1919:                        || col instanceof  TransitionRefs) {
1920:                    return false;
1921:                } else if (col instanceof  ExternalPackages) {
1922:                    if (el != null) {
1923:                        String href = ((ExternalPackage) el).getHref();
1924:                        Package pkg = XMLUtil.getPackage(col);
1925:                        String epId = pkg.getExternalPackageId(href);
1926:                        if (epId != null) {
1927:                            return false;
1928:                        }
1929:                    }
1930:                } else if (col instanceof  ExtendedAttributes
1931:                        && col.getParent() instanceof  ExternalPackage) {
1932:                    if (el != null
1933:                            && ((ExtendedAttribute) el).getName().equals(
1934:                                    JaWEEAHandler.EA_JAWE_EXTERNAL_PACKAGE_ID)) {
1935:                        return false;
1936:                    }
1937:                }
1938:
1939:                Iterator comps = JaWEManager.getInstance()
1940:                        .getComponentManager().getComponents().iterator();
1941:                while (comps.hasNext()) {
1942:                    JaWEComponent jc = (JaWEComponent) comps.next();
1943:                    if (jc != this ) {
1944:                        boolean approved = jc.canRemoveElement(col, el);
1945:                        if (approved == false) {
1946:                            return false;
1947:                        }
1948:                    }
1949:                }
1950:                return true;
1951:            }
1952:
1953:            public boolean canDuplicateElement(XMLCollection col,
1954:                    XMLElement el, boolean checkReadOnly) {
1955:                if (checkReadOnly && col.isReadOnly()) {
1956:                    return false;
1957:                }
1958:                if (el instanceof  TransitionRestriction
1959:                        || el instanceof  TransitionRef || el instanceof  Member
1960:                        || el instanceof  EnumerationValue
1961:                        || el instanceof  ExternalPackage
1962:                        || el instanceof  Responsible
1963:                        || el instanceof  Transition) {
1964:                    return false;
1965:                } else if (col instanceof  ExtendedAttributes
1966:                        && col.getParent() instanceof  ExternalPackage) {
1967:                    if (el != null
1968:                            && ((ExtendedAttribute) el).getName().equals(
1969:                                    JaWEEAHandler.EA_JAWE_EXTERNAL_PACKAGE_ID)) {
1970:                        return false;
1971:                    }
1972:                }
1973:
1974:                Iterator comps = JaWEManager.getInstance()
1975:                        .getComponentManager().getComponents().iterator();
1976:                while (comps.hasNext()) {
1977:                    JaWEComponent jc = (JaWEComponent) comps.next();
1978:                    if (jc != this ) {
1979:                        boolean approved = jc.canDuplicateElement(col, el);
1980:                        if (approved == false) {
1981:                            return false;
1982:                        }
1983:                    }
1984:                }
1985:                return true;
1986:            }
1987:
1988:            public boolean canRepositionElement(XMLCollection col,
1989:                    XMLElement el, boolean checkReadOnly) {
1990:                if (checkReadOnly && col.isReadOnly()) {
1991:                    return false;
1992:                } else if (col instanceof  ExtendedAttributes
1993:                        && col.getParent() instanceof  ExternalPackage) {
1994:                    if (el != null
1995:                            && ((ExtendedAttribute) el).getName().equals(
1996:                                    JaWEEAHandler.EA_JAWE_EXTERNAL_PACKAGE_ID)) {
1997:                        return false;
1998:                    }
1999:                }
2000:
2001:                Iterator comps = JaWEManager.getInstance()
2002:                        .getComponentManager().getComponents().iterator();
2003:                while (comps.hasNext()) {
2004:                    JaWEComponent jc = (JaWEComponent) comps.next();
2005:                    if (jc != this ) {
2006:                        boolean approved = jc.canRepositionElement(col, el);
2007:                        if (approved == false) {
2008:                            return false;
2009:                        }
2010:                    }
2011:                }
2012:                return true;
2013:            }
2014:
2015:            public void startUndouableChange() {
2016:                Package mainPkg = getMainPackage();
2017:                xpdlInfoList.clear();
2018:                undoableChangeInProgress = true;
2019:                undoSelectionEvent = getCurrentSelectionEvent();
2020:                // System.err.println("SELECTION EVENT AFTER UNDO/REDO size is
2021:                // "+undoSelectionEvent.getChangedSubElements().size()+",
2022:                // sel="+undoSelectionEvent.getChangedSubElements()+",
2023:                // main="+undoSelectionEvent.getChangedElement());
2024:                XPDLElementChangeInfo ucInfo = createInfo(mainPkg,
2025:                        XPDLElementChangeInfo.UNDOABLE_ACTION_STARTED);
2026:                ucInfo.setChangedSubElements(xpdlInfoList);
2027:                setChanged();
2028:                notifyObservers(ucInfo);
2029:            }
2030:
2031:            public void endUndouableChange(List elementsToSelect) {
2032:                Package mainPkg = getMainPackage();
2033:                List infoList = new ArrayList(xpdlInfoList);
2034:
2035:                Iterator it = infoList.iterator();
2036:                while (it.hasNext()) {
2037:                    XPDLElementChangeInfo info = (XPDLElementChangeInfo) it
2038:                            .next();
2039:                    handleEvent(info);
2040:                }
2041:
2042:                XPDLElementChangeInfo ucInfo = createInfo(mainPkg,
2043:                        XPDLElementChangeInfo.ADJUST_UNDOABLE_ACTION);
2044:                ucInfo.setChangedSubElements(xpdlInfoList);
2045:                setChanged();
2046:                notifyObservers(ucInfo);
2047:
2048:                ucInfo = createInfo(mainPkg,
2049:                        XPDLElementChangeInfo.UNDOABLE_ACTION_ENDED);
2050:
2051:                if (elementsToSelect != null && elementsToSelect.size() > 0) {
2052:                    if (!selectionMng.checkSelection(elementsToSelect)) {
2053:                        XMLElement firstToSelect = (XMLElement) elementsToSelect
2054:                                .get(0);
2055:                        elementsToSelect.clear();
2056:                        elementsToSelect.add(firstToSelect);
2057:                    }
2058:                } else {
2059:                    elementsToSelect = new ArrayList();
2060:                    elementsToSelect.add(mainPkg);
2061:                }
2062:
2063:                ucInfo.setChangedSubElements(xpdlInfoList);
2064:
2065:                updateInProgress = true;
2066:                setChanged();
2067:                notifyObservers(ucInfo);
2068:                updateInProgress = false;
2069:
2070:                if (undoHistoryManager != null) {
2071:                    undoHistoryManager.registerEvents(xpdlInfoList,
2072:                            undoSelectionEvent);
2073:                }
2074:                getSettings().adjustActions();
2075:                JaWEManager.getInstance().getLoggingManager().debug(
2076:                        "All events after undoable change:\n" + xpdlInfoList);
2077:                xpdlInfoList.clear();
2078:                undoableChangeInProgress = false;
2079:
2080:                // System.err.println("Elements to select after undoable change are
2081:                // "+elementsToSelect);
2082:                selectionMng.setSelection(elementsToSelect, true);
2083:                if (settings.isDesingTimeValidationEnabled()) {
2084:                    checkValidity(mainPkg, true, false, true);
2085:                }
2086:            }
2087:
2088:            public boolean isUndoableChangeInProgress() {
2089:                return undoableChangeInProgress;
2090:            }
2091:
2092:            public boolean isUndoOrRedoInProgress() {
2093:                if (undoHistoryManager != null) {
2094:                    return undoHistoryManager.isUndoOrRedoInProgress();
2095:                }
2096:                return false;
2097:            }
2098:
2099:            public void handleEvent(XPDLElementChangeInfo info) {
2100:                XMLElement changedElement = info.getChangedElement();
2101:
2102:                JaWEManager.getInstance().getLoggingManager().debug(
2103:                        "INFO: handleEvenet(): " + info);
2104:
2105:                int action = info.getAction();
2106:
2107:                if (action == XPDLElementChangeInfo.SELECTED) {
2108:                    // should never happen
2109:                } else {
2110:                    if (changedElement instanceof  WorkflowProcesses) {
2111:                        if (action == XMLElementChangeInfo.REMOVED) {
2112:                            if (info.getChangedSubElements() != null) {
2113:                                Iterator it = info.getChangedSubElements()
2114:                                        .iterator();
2115:                                while (it.hasNext()) {
2116:                                    WorkflowProcess wp = (WorkflowProcess) it
2117:                                            .next();
2118:                                    if (wp == selectionMng.getWorkingProcess()) {
2119:                                        // select package
2120:                                        selectionMng.setSelection(wp
2121:                                                .getParent().getParent(), true);
2122:                                        break;
2123:                                    }
2124:                                }
2125:                            }
2126:                        }
2127:                    } else if (changedElement instanceof  ActivitySet) {
2128:                        if (action == XMLElementChangeInfo.REMOVED) {
2129:                            if (info.getChangedSubElements() != null) {
2130:                                Iterator it = info.getChangedSubElements()
2131:                                        .iterator();
2132:                                while (it.hasNext()) {
2133:                                    ActivitySet as = (ActivitySet) it.next();
2134:                                    if (as == selectionMng
2135:                                            .getWorkingActivitySet()) {
2136:                                        // select process
2137:                                        selectionMng.setSelection(as
2138:                                                .getParent().getParent(), true);
2139:                                        break;
2140:                                    }
2141:                                }
2142:                            }
2143:                        }
2144:                    }
2145:                }
2146:
2147:                if (action == XMLElementChangeInfo.INSERTED
2148:                        && (changedElement instanceof  WorkflowProcesses || changedElement instanceof  ActivitySets)) {
2149:                    if (info.getChangedSubElements() != null) {
2150:                        selectionMng.setSelection(info.getChangedSubElements(),
2151:                                true);
2152:                    }
2153:                }
2154:
2155:                updateTitle();
2156:                adjustActions();
2157:
2158:                if (action == XMLElementChangeInfo.REMOVED
2159:                        || action == XMLElementChangeInfo.INSERTED) {
2160:                    JaWEManager
2161:                            .getInstance()
2162:                            .getLoggingManager()
2163:                            .info(
2164:                                    "JaWEController -> performing appropriate actions on inserting/removing");
2165:                    if (changedElement instanceof  Activities
2166:                            && action == XMLElementChangeInfo.REMOVED) {
2167:                        // for removing connected Transitions
2168:                        updateSpecialInProgress = true;
2169:                        JaWEManager.getInstance().getXPDLUtils()
2170:                                .removeTransitionsForActivities(
2171:                                        info.getChangedSubElements());
2172:                        selectionMng.removeFromSelection(info
2173:                                .getChangedSubElements());
2174:                        updateSpecialInProgress = false;
2175:                    } else if (changedElement instanceof  Transitions) {
2176:                        // Iterator it = info.getChangedSubElements().iterator();
2177:                        Activities acts = (Activities) ((XMLCollectionElement) changedElement
2178:                                .getParent()).get("Activities");
2179:
2180:                        updateSpecialInProgress = true;
2181:                        JaWEManager.getInstance().getXPDLUtils()
2182:                                .correctSplitsAndJoins(acts.toElements());
2183:                        updateSpecialInProgress = false;
2184:                        if (action == XMLElementChangeInfo.REMOVED)
2185:                            selectionMng.removeFromSelection(info
2186:                                    .getChangedSubElements());
2187:                    }
2188:                    JaWEManager
2189:                            .getInstance()
2190:                            .getLoggingManager()
2191:                            .info(
2192:                                    "JaWEController -> finished performing appropriate actions on inserting/removing");
2193:                } else if (action == XMLElementChangeInfo.UPDATED) {
2194:                    XMLElement parent = changedElement.getParent();
2195:                    if ((changedElement.toName().equals("Id") && (parent instanceof  WorkflowProcess
2196:                            || parent instanceof  ActivitySet
2197:                            || parent instanceof  Application
2198:                            || parent instanceof  Participant
2199:                            || parent instanceof  DataField
2200:                            || parent instanceof  FormalParameter
2201:                            || parent instanceof  Activity
2202:                            || parent instanceof  Transition || parent instanceof  TypeDeclaration))
2203:                            || ((changedElement.toName().equals("From") || changedElement
2204:                                    .toName().equals("To")) && parent instanceof  Transition)
2205:                            || ((parent instanceof  Split || parent instanceof  Join) && changedElement instanceof  XMLAttribute)) {
2206:
2207:                        if (parent instanceof  Activity
2208:                                || parent instanceof  Transition
2209:                                || parent instanceof  Split
2210:                                || parent instanceof  Join) {
2211:                            XMLCollectionElement wpOrAs = XMLUtil
2212:                                    .getActivitySet(parent);
2213:                            if (wpOrAs == null) {
2214:                                wpOrAs = XMLUtil.getWorkflowProcess(parent);
2215:                            }
2216:                            if (parent instanceof  Activity) {
2217:                                updateSpecialInProgress = true;
2218:                                JaWEManager.getInstance().getXPDLUtils()
2219:                                        .updateTransitionsOnActivityIdChange(
2220:                                                (Transitions) wpOrAs
2221:                                                        .get("Transitions"),
2222:                                                (String) info.getOldValue(),
2223:                                                (String) info.getNewValue());
2224:                                updateSpecialInProgress = false;
2225:                            } else if (parent instanceof  Transition) {
2226:                                if (changedElement.toName().equals("Id")) {
2227:                                    updateSpecialInProgress = true;
2228:                                    JaWEManager
2229:                                            .getInstance()
2230:                                            .getXPDLUtils()
2231:                                            .updateActivityOnTransitionIdChange(
2232:                                                    (Activities) wpOrAs
2233:                                                            .get("Activities"),
2234:                                                    ((Transition) parent)
2235:                                                            .getFrom(),
2236:                                                    (String) info.getOldValue(),
2237:                                                    (String) info.getNewValue());
2238:                                    updateSpecialInProgress = false;
2239:                                } else if (changedElement.toName().equals(
2240:                                        "From")) {
2241:                                    updateSpecialInProgress = true;
2242:                                    JaWEManager
2243:                                            .getInstance()
2244:                                            .getXPDLUtils()
2245:                                            .updateActivitiesOnTransitionFromChange(
2246:                                                    (Activities) wpOrAs
2247:                                                            .get("Activities"),
2248:                                                    ((Transition) parent)
2249:                                                            .getId(),
2250:                                                    (String) info.getOldValue(),
2251:                                                    (String) info.getNewValue());
2252:                                    updateSpecialInProgress = false;
2253:                                } else {
2254:                                    updateSpecialInProgress = true;
2255:                                    JaWEManager
2256:                                            .getInstance()
2257:                                            .getXPDLUtils()
2258:                                            .updateActivitiesOnTransitionToChange(
2259:                                                    (Activities) wpOrAs
2260:                                                            .get("Activities"),
2261:                                                    ((Transition) parent)
2262:                                                            .getId(),
2263:                                                    (String) info.getOldValue(),
2264:                                                    (String) info.getNewValue());
2265:                                    updateSpecialInProgress = false;
2266:                                }
2267:                            } else if (parent instanceof  Split
2268:                                    || parent instanceof  Join) {
2269:                                updateSpecialInProgress = true;
2270:                                Activity act = XMLUtil.getActivity(parent);
2271:                                JaWEManager.getInstance().getXPDLUtils()
2272:                                        .correctSplitAndJoin(act);
2273:                                updateSpecialInProgress = false;
2274:                            }
2275:                        } else if (parent instanceof  TypeDeclaration) {
2276:                            updateSpecialInProgress = true;
2277:                            JaWEManager
2278:                                    .getInstance()
2279:                                    .getXPDLUtils()
2280:                                    .updateTypeDeclarationReferences(
2281:                                            JaWEManager
2282:                                                    .getInstance()
2283:                                                    .getXPDLUtils()
2284:                                                    .getTypeDeclarationReferences(
2285:                                                            XMLUtil
2286:                                                                    .getPackage(parent),
2287:                                                            (String) info
2288:                                                                    .getOldValue()),
2289:                                            (String) info.getNewValue());
2290:                            updateSpecialInProgress = false;
2291:                        } else if (parent instanceof  WorkflowProcess) {
2292:                            updateSpecialInProgress = true;
2293:                            JaWEManager
2294:                                    .getInstance()
2295:                                    .getXPDLUtils()
2296:                                    .updateWorkflowProcessReferences(
2297:                                            JaWEManager
2298:                                                    .getInstance()
2299:                                                    .getXPDLUtils()
2300:                                                    .getWorkflowProcessReferences(
2301:                                                            XMLUtil
2302:                                                                    .getPackage(parent),
2303:                                                            (String) info
2304:                                                                    .getOldValue()),
2305:                                            (String) info.getNewValue());
2306:                            updateSpecialInProgress = false;
2307:                        } else if (parent instanceof  ActivitySet) {
2308:                            updateSpecialInProgress = true;
2309:                            JaWEManager
2310:                                    .getInstance()
2311:                                    .getXPDLUtils()
2312:                                    .updateActivitySetReferences(
2313:                                            JaWEManager
2314:                                                    .getInstance()
2315:                                                    .getXPDLUtils()
2316:                                                    .getActivitySetReferences(
2317:                                                            XMLUtil
2318:                                                                    .getWorkflowProcess(parent),
2319:                                                            (String) info
2320:                                                                    .getOldValue()),
2321:                                            (String) info.getNewValue());
2322:                            updateSpecialInProgress = false;
2323:                        } else if (parent instanceof  Application) {
2324:                            updateSpecialInProgress = true;
2325:                            JaWEManager
2326:                                    .getInstance()
2327:                                    .getXPDLUtils()
2328:                                    .updateApplicationReferences(
2329:                                            JaWEManager
2330:                                                    .getInstance()
2331:                                                    .getXPDLUtils()
2332:                                                    .getApplicationReferences(
2333:                                                            (XMLComplexElement) parent
2334:                                                                    .getParent()
2335:                                                                    .getParent(),
2336:                                                            (String) info
2337:                                                                    .getOldValue()),
2338:                                            (String) info.getNewValue());
2339:                            updateSpecialInProgress = false;
2340:                        } else if (parent instanceof  Participant) {
2341:                            updateSpecialInProgress = true;
2342:                            JaWEManager
2343:                                    .getInstance()
2344:                                    .getXPDLUtils()
2345:                                    .updateParticipantReferences(
2346:                                            JaWEManager
2347:                                                    .getInstance()
2348:                                                    .getXPDLUtils()
2349:                                                    .getParticipantReferences(
2350:                                                            (XMLComplexElement) parent
2351:                                                                    .getParent()
2352:                                                                    .getParent(),
2353:                                                            (String) info
2354:                                                                    .getOldValue()),
2355:                                            (String) info.getNewValue());
2356:                            updateSpecialInProgress = false;
2357:                        } else if (parent instanceof  DataField
2358:                                || (parent instanceof  FormalParameter && parent
2359:                                        .getParent().getParent() instanceof  WorkflowProcess)) {
2360:                            updateSpecialInProgress = true;
2361:                            XMLComplexElement pkgOrWp = (XMLComplexElement) parent
2362:                                    .getParent().getParent();
2363:                            List refs = null;
2364:                            if (parent instanceof  DataField) {
2365:                                refs = JaWEManager.getInstance().getXPDLUtils()
2366:                                        .getDataFieldReferences(pkgOrWp,
2367:                                                (String) info.getOldValue());
2368:                            } else {
2369:                                refs = JaWEManager.getInstance().getXPDLUtils()
2370:                                        .getFormalParameterReferences(
2371:                                                (WorkflowProcess) pkgOrWp,
2372:                                                (String) info.getOldValue());
2373:                            }
2374:                            JaWEManager.getInstance().getXPDLUtils()
2375:                                    .updateVariableReferences(refs,
2376:                                            (String) info.getOldValue(),
2377:                                            (String) info.getNewValue());
2378:                            updateSpecialInProgress = false;
2379:                        }
2380:                    }
2381:                }
2382:
2383:            }
2384:
2385:            protected void clearHistory() {
2386:                if (undoHistoryManager != null) {
2387:                    undoHistoryManager.cleanHistory();
2388:                    adjustActions();
2389:                }
2390:                undoSelectionEvent = null;
2391:                System.gc();
2392:            }
2393:
2394:            protected void clearAll() {
2395:                xpdlInfoList.clear();
2396:                clearHistory();
2397:                selectionMng.clear();
2398:                edit.clear();
2399:                undoSelectionEvent = null;
2400:                StandardPackageValidator val = JaWEManager.getInstance()
2401:                        .getXPDLValidator();
2402:                if (val != null) {
2403:                    val.clearCache();
2404:                }
2405:                if (JaWEManager.getInstance().getXPDLElementEditor() != null
2406:                        && JaWEManager.getInstance().getXPDLElementEditor()
2407:                                .getWindow() != null) {
2408:                    JaWEManager.getInstance().getXPDLElementEditor().close();
2409:                }
2410:                XPDLElementChangeInfo info = createInfo(null, new ArrayList(),
2411:                        XPDLElementChangeInfo.VALIDATION_ERRORS);
2412:                info.setNewValue(new Boolean(false));
2413:                sendEvent(info);
2414:                info = createInfo(null, new ArrayList(),
2415:                        XPDLElementChangeInfo.REFERENCES);
2416:                sendEvent(info);
2417:                if (JaWEManager.getInstance().getTableEditor() != null) {
2418:                    JaWEManager.getInstance().getTableEditor().close();
2419:                }
2420:
2421:                System.gc();
2422:            }
2423:
2424:            protected void clearXPDLListenerObservables(boolean clearTransient) {
2425:                List retain = new ArrayList();
2426:                for (int i = 0; i < xpdlListenerObservables.size(); i++) {
2427:                    XPDLListenerAndObservable xpdllo = (XPDLListenerAndObservable) xpdlListenerObservables
2428:                            .get(i);
2429:                    if (clearTransient || !xpdllo.getPackage().isTransient()) {
2430:                        xpdllo.unregisterFromXPDL();
2431:                    } else {
2432:                        retain.add(xpdllo);
2433:                    }
2434:                }
2435:                xpdlListenerObservables.retainAll(retain);
2436:                System.gc();
2437:            }
2438:
2439:            public void undo() {
2440:                if (undoHistoryManager != null) {
2441:                    undoHistoryManager.undo();
2442:                    if (settings.isDesingTimeValidationEnabled()) {
2443:                        checkValidity(getMainPackage(), true, false, true);
2444:                    }
2445:                    getSettings().adjustActions();
2446:                }
2447:            }
2448:
2449:            public void redo() {
2450:                if (undoHistoryManager != null) {
2451:                    undoHistoryManager.redo();
2452:                    if (settings.isDesingTimeValidationEnabled()) {
2453:                        checkValidity(getMainPackage(), true, false, true);
2454:                    }
2455:                    getSettings().adjustActions();
2456:                }
2457:            }
2458:
2459:            public void sendEvent(XPDLElementChangeInfo info) {
2460:                setChanged();
2461:                JaWEManager.getInstance().getLoggingManager().debug(
2462:                        "Controller sending event: " + info);
2463:                notifyObservers(info);
2464:                updateTitle();
2465:                adjustActions();
2466:            }
2467:
2468:            public XPDLElementChangeInfo getCurrentSelectionEvent() {
2469:                // add will be selected event at the end
2470:                List currentSelection = selectionMng.getSelectedElements();
2471:                XMLElement selectionOwner = getMainPackage();
2472:                if (currentSelection != null && currentSelection.size() > 0) {
2473:                    selectionOwner = (XMLElement) currentSelection.get(0);
2474:                    if (currentSelection.size() == 1) {
2475:                        currentSelection.clear();
2476:                    } else {
2477:                        selectionOwner = selectionOwner.getParent();
2478:                    }
2479:                }
2480:                XPDLElementChangeInfo selectionEvent = createInfo(
2481:                        selectionOwner, currentSelection,
2482:                        XPDLElementChangeInfo.SELECTED);
2483:                return selectionEvent;
2484:            }
2485:
2486:            public boolean confirmDelete(List sel, XMLElement firstSelected) {
2487:                XMLComplexElement pkgOrWP = getMainPackage();
2488:                WorkflowProcess wp = XMLUtil.getWorkflowProcess(firstSelected);
2489:                if (wp != null && wp != firstSelected) {
2490:                    pkgOrWP = wp;
2491:                }
2492:
2493:                String doNotAskOnDeletionOfReferencedElements = getControllerSettings()
2494:                        .doNotAskOnDeletionOfReferencedElementTypes();
2495:                Set notToAsk = refElsNotToAskOnDeletion(doNotAskOnDeletionOfReferencedElements);
2496:                List refs = new ArrayList();
2497:                if (getControllerSettings()
2498:                        .shouldAskOnDeletionOfReferencedElements()) {
2499:                    for (int i = 0; i < sel.size(); i++) {
2500:                        XMLElement el = (XMLElement) sel.get(i);
2501:                        if (el instanceof  Activity) {
2502:                            if (!notToAsk.contains(el.toName())) {
2503:                                refs.addAll(JaWEManager.getInstance()
2504:                                        .getXPDLUtils().getReferences(
2505:                                                (Activity) el));
2506:                            }
2507:                        } else if (el instanceof  Transition) {
2508:                            if (!notToAsk.contains(el.toName())) {
2509:                                refs.addAll(JaWEManager.getInstance()
2510:                                        .getXPDLUtils().getReferences(
2511:                                                (Transition) el));
2512:                            }
2513:                        } else if (el instanceof  XMLComplexElement) {
2514:                            if (!notToAsk.contains(el.toName())) {
2515:                                refs.addAll(JaWEManager.getInstance()
2516:                                        .getXPDLUtils().getReferences(pkgOrWP,
2517:                                                (XMLComplexElement) el));
2518:                            }
2519:                        }
2520:                    }
2521:                }
2522:
2523:                int yn = JOptionPane.YES_OPTION;
2524:                if (getControllerSettings().shoudAskOnDeletion()
2525:                        || getControllerSettings()
2526:                                .shouldAskOnDeletionOfReferencedElements()) {
2527:                    if (refs.size() == 0) {
2528:                        if (getControllerSettings().shoudAskOnDeletion()) {
2529:                            yn = JOptionPane
2530:                                    .showConfirmDialog(
2531:                                            getJaWEFrame(),
2532:                                            getSettings()
2533:                                                    .getLanguageDependentString(
2534:                                                            "MessageDoYouReallyWantToRemoveSelectedItem"),
2535:                                            getSettings()
2536:                                                    .getLanguageDependentString(
2537:                                                            "DeletingKey"),
2538:                                            JOptionPane.YES_NO_OPTION);
2539:                        }
2540:                    } else {
2541:                        if (getControllerSettings()
2542:                                .shouldAskOnDeletionOfReferencedElements()) {
2543:
2544:                            yn = JOptionPane
2545:                                    .showConfirmDialog(
2546:                                            getJaWEFrame(),
2547:                                            getSettings()
2548:                                                    .getLanguageDependentString(
2549:                                                            "MessageReferencedDoYouReallyWantToDeleteSelectedItem"),
2550:                                            getSettings()
2551:                                                    .getLanguageDependentString(
2552:                                                            "DeletingKey"),
2553:                                            JOptionPane.YES_NO_OPTION);
2554:
2555:                        }
2556:                    }
2557:                }
2558:                if (yn == JOptionPane.YES_OPTION) {
2559:                    return true;
2560:                }
2561:                return false;
2562:            }
2563:
2564:            protected static Set refElsNotToAskOnDeletion(String hstr) {
2565:                Set s = new HashSet();
2566:
2567:                String[] hstra = Utils.tokenize(hstr, " ");
2568:                if (hstra.length > 0) {
2569:                    s.addAll(Arrays.asList(hstra));
2570:                }
2571:
2572:                return s;
2573:            }
2574:
2575:            // Controller help classes
2576:            public JaWEEdit getEdit() {
2577:                return edit;
2578:            }
2579:
2580:            public JaWESelectionManager getSelectionManager() {
2581:                return selectionMng;
2582:            }
2583:
2584:            public JaWEActions getJaWEActions() {
2585:                return defaultJaWEActions;
2586:            }
2587:
2588:            public JaWEFrame getJaWEFrame() {
2589:                return frame;
2590:            }
2591:
2592:            public void adjustActions() {
2593:                settings.adjustActions();
2594:                defaultJaWEActions.enableDisableActions();
2595:            }
2596:
2597:            protected void updateTitle() {
2598:                String title = "";
2599:                if (getMainPackage() != null) {
2600:                    XPDLListenerAndObservable xpdllo = getXPDLListenerObservable(getMainPackage());
2601:                    if (xpdllo != null && xpdllo.isModified()) {
2602:                        // System.out.println("WXPLD=" + xpdllo.getPackage().getId());
2603:                        title = "*";
2604:                    }
2605:                }
2606:
2607:                title += JaWEManager.getInstance().getName();
2608:
2609:                String ccn = getCurrentConfigName();
2610:                if (ccn == null) {
2611:                    ccn = "Default";
2612:                }
2613:                title += " ("
2614:                        + ccn
2615:                        + " "
2616:                        + getSettings().getLanguageDependentString(
2617:                                "ConfigurationKey") + ")";
2618:
2619:                if (getMainPackage() != null) {
2620:                    title += " - ";
2621:                    XPDLHandler xpdlh = JaWEManager.getInstance()
2622:                            .getXPDLHandler();
2623:                    String s = xpdlh.getAbsoluteFilePath(getMainPackage());
2624:                    if (s == null || s.equals("")) {
2625:                        title += getSettings().getLanguageDependentString(
2626:                                "NotSavedKey");
2627:                    } else {
2628:                        title += s;
2629:                    }
2630:                }
2631:                getJaWEFrame().setTitle(title);
2632:
2633:            }
2634:
2635:            public JaWETypes getJaWETypes() {
2636:                return jtypes;
2637:            }
2638:
2639:            public JaWETypeResolver getTypeResolver() {
2640:                return jtypeResolver;
2641:            }
2642:
2643:            public ControllerSettings getControllerSettings() {
2644:                return settings;
2645:            }
2646:
2647:            public void setUpdateInProgress(boolean inProgress) {
2648:                updateInProgress = inProgress;
2649:            }
2650:
2651:            public boolean isUpdateInProgress() {
2652:                return updateInProgress;
2653:            }
2654:
2655:            public UndoHistoryManager getUndoHistoryManager() {
2656:                return undoHistoryManager;
2657:            }
2658:
2659:            public String getCurrentConfig() {
2660:                if (currentConfig == null) {
2661:                    fillConfigInfo();
2662:                    currentConfig = "default";
2663:                    String cch = System
2664:                            .getProperty(JaWEConstants.JAWE_CURRENT_CONFIG_HOME);
2665:                    if (cch != null) {
2666:                        int indofs = cch.lastIndexOf("/");
2667:                        int indofbs = cch.lastIndexOf("\\");
2668:                        int ind = Math.max(indofs, indofbs);
2669:                        if (ind >= 0) {
2670:                            cch = cch.substring(ind + 1);
2671:                        }
2672:                        currentConfig = cch;
2673:                    }
2674:                }
2675:                return currentConfig;
2676:            }
2677:
2678:            public String getCurrentConfigName() {
2679:                String cc = JaWEManager.getInstance().getJaWEController()
2680:                        .getCurrentConfig();
2681:                return (String) configInfo.get(cc);
2682:            }
2683:
2684:            public String getConfigId(String cfgName) {
2685:                if (configInfo != null) {
2686:                    return getConfigId(configInfo, cfgName);
2687:                }
2688:                return cfgName;
2689:            }
2690:
2691:            protected String getConfigId(Map ci, String cfgName) {
2692:                Iterator it = ci.entrySet().iterator();
2693:                while (it.hasNext()) {
2694:                    Map.Entry me = (Map.Entry) it.next();
2695:                    if (me.getValue().equals(cfgName)) {
2696:                        return (String) me.getKey();
2697:                    }
2698:                }
2699:                return cfgName;
2700:            }
2701:
2702:            public Map getConfigInfo() {
2703:                if (configInfo == null) {
2704:                    fillConfigInfo();
2705:                }
2706:                return Collections.unmodifiableMap(configInfo);
2707:            }
2708:
2709:            protected void fillConfigInfo() {
2710:                Map m = new HashMap();
2711:                if (JaWEConstants.JAWE_CONF_HOME != null) {
2712:                    File mainConfFolder = new File(JaWEConstants.JAWE_CONF_HOME);
2713:                    if (mainConfFolder.exists()) {
2714:                        File[] confs = mainConfFolder.listFiles();
2715:                        for (int i = 0; i < confs.length; i++) {
2716:                            if (confs[i].isDirectory()) {
2717:                                m.put(confs[i].getName(),
2718:                                        readConfigName(confs[i].getName()));
2719:                            }
2720:                        }
2721:                    }
2722:                }
2723:                List l = new ArrayList(m.values());
2724:                Collections.sort(l);
2725:
2726:                configInfo = new SequencedHashMap();
2727:                for (int i = 0; i < l.size(); i++) {
2728:                    String cfgName = (String) l.get(i);
2729:                    configInfo.put(getConfigId(m, cfgName), cfgName);
2730:                }
2731:            }
2732:
2733:            protected String readConfigName(String configFolder) {
2734:                String fn = JaWEConstants.JAWE_CONF_HOME + "/" + configFolder
2735:                        + "/jaweconfigname";
2736:                File file = new File(fn);
2737:                if (file.exists()) {
2738:                    Properties props = new Properties();
2739:                    FileInputStream fis = null;
2740:                    try {
2741:                        fis = new FileInputStream(fn);
2742:                        props.load(fis);
2743:                        return props.getProperty(
2744:                                JaWEConstants.JAWE_CONFIG_NAME, configFolder);
2745:                    } catch (Exception ex) {
2746:                        throw new Error(
2747:                                "Something went wrong while reading external component properties !!!",
2748:                                ex);
2749:                    } finally {
2750:                        try {
2751:                            fis.close();
2752:                        } catch (Exception ex) {
2753:                        }
2754:                    }
2755:                }
2756:
2757:                return configFolder;
2758:            }
2759:
2760:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.