KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > xml > elements > ExternalPackages


1 /* ExternalPackages.java
2  *
3  * Authors:
4  * Stefanovic Nenad chupo@iis.ns.ac.yu
5  * Bojanic Sasa sasaboy@neobee.net
6  * Puskas Vladimir vpuskas@eunet.yu
7  * Pilipovic Goran zboniek@uns.ac.yu
8  *
9  */

10
11 package org.enhydra.jawe.xml.elements;
12
13 import org.enhydra.jawe.xml.*;
14 import org.enhydra.jawe.xml.panels.*;
15
16 import java.io.*;
17 import java.net.URL JavaDoc;
18 import java.util.*;
19 import javax.swing.*;
20 import org.w3c.dom.*;
21
22 /**
23 * Represents a WfMC DTD element that has the similar name.
24 * This class is a collection of class <b>ExternalPackage</b> instances.
25 *
26 * @see XML
27 */

28 public class ExternalPackages extends XMLCollection {
29    /**
30    * Creates a new instance of the class.
31    *
32    * @param myOwner The package which member is this class instance.
33    */

34    public ExternalPackages (Package JavaDoc myOwner) {
35       super(myOwner);
36    }
37
38    /**
39    * Overrides super-class method to realize this class specific
40    * reading from XML file.
41    *
42    * @param tagContent The content retrieved from XML file,
43    * that represents tag for this element.
44    */

45    /*public void fromXML(Node node) {
46       super.fromXML(node);
47       // files that are not found
48       Set toDelete=new HashSet();
49       // creating instances of PE for every external package (recursive
50       // implementation of createLocalCopy method)
51       Iterator it=refCollectionElements.iterator();
52       while (it.hasNext()) {
53          ExternalPackage ep=(ExternalPackage)it.next();
54          String xmlFile = ep.toString();
55          if (!XMLInterfaceFactory.getInstance().getXMLInterface().doesPackageFileExists(xmlFile)) {
56             toDelete.add(ep);
57             XMLUtil.message(ep.toString()+": "+XMLUtil.getResourceString(
58                "InformationErrorWhileOpeningFile"),
59                JOptionPane.INFORMATION_MESSAGE);
60             continue;
61          }
62          String ID=XMLInterfaceFactory.getInstance().getXMLInterface().getIDFromFile(xmlFile);
63 System.out.println("EID="+ID);
64          if (ID.length()==0) {
65             toDelete.add(ep);
66             XMLUtil.message(ep.toString()+": "+XMLUtil.getResourceString(
67                "InformationErrorWhileOpeningFile"),
68                JOptionPane.INFORMATION_MESSAGE);
69          }
70       }
71       refCollectionElements.removeAll(toDelete);
72    }*/

73
74    /**
75    * Generates a new element of the class which instances
76    * are members of collection of this class.
77    *
78    * return The generated instance of class that makes collection.
79    */

80    public XMLElement generateNewElement() {
81       ExternalPackage ep=new ExternalPackage(this);
82       ep.setRequired(true);
83       return ep;
84    }
85
86    public boolean canInsertElement (XMLElement el) {
87       boolean canInsert=true;
88       // set the user.dir to the original
89
try {
90          System.setProperty("user.dir",((Package JavaDoc)myOwner).getXMLInterface().getParentDirectory((Package JavaDoc)myOwner));
91       } catch (Exception JavaDoc ex) {}
92
93       try {
94          // get an ID of external package
95
String JavaDoc extID=((Package JavaDoc)myOwner).getXMLInterface().getIDFromFile(el.toString());
96
97          // check if has an valid ID (file must exist)
98
if (extID.length()==0) {
99             canInsert=false;
100          }
101
102          // Check if it has the same ID as the main package file that we are
103
// working at
104
String JavaDoc myOwnerId=myOwner.get("Id").toString().trim();
105          if (canInsert && myOwnerId.equals(extID)) {
106             canInsert=false;
107          }
108
109          // check if package file has already been inserted (or if some file
110
// with the same ID is inserted)
111
if (canInsert) {
112             Iterator it=refCollectionElements.iterator();
113             while (it.hasNext()) {
114                ExternalPackage ep=(ExternalPackage)it.next();
115                String JavaDoc epId=((Package JavaDoc)myOwner).getXMLInterface().getExternalPackageByRelativeFilePath(
116                      ep.toString(),(Package JavaDoc)myOwner).get("Id").toString();
117                if (extID.equals(epId)) {
118                   canInsert=false;
119                   break;
120                }
121             }
122          }
123
124          // check if there are some packages referenced by this package
125
// external packages which have identical Id and diferent URL,
126
// and if so, do not insert this ext. pkg
127
if (canInsert) {
128             String JavaDoc cp=XMLUtil.getCanonicalPath(el.toString(), false);
129             Iterator it=((Package JavaDoc)myOwner).getXMLInterface().getAllPackages().iterator();
130             while (it.hasNext()) {
131                Package JavaDoc p=(Package JavaDoc)it.next();
132                String JavaDoc pId=p.get("Id").toString();
133                if (extID.equals(pId) && !((Package JavaDoc)myOwner).getXMLInterface().getAllPackageFilenames().contains(cp)) {
134                   canInsert=false;
135                   break;
136                }
137             }
138          }
139
140       } catch (Exception JavaDoc ex) {
141          //
142
}
143       if (!canInsert) {
144          XMLUtil.message(XMLUtil.getLanguageDependentString("InformationExternalPackageCannotBeInserted"),
145             JOptionPane.INFORMATION_MESSAGE);
146       }
147       return canInsert;
148    }
149
150
151    // when some external package is referenced, insert their entities to the package
152
/**
153    * Overrides super-class method to implement class specific
154    * actions when element is inserted into this class collection.
155    *
156    * @param el Inserted element - in this case it must be instance
157    * of ExternalPackage class.
158    */

159    public void onElementInserted (XMLElement el) {
160       ExternalPackage ep = (ExternalPackage) el;
161       // the insertFromExternal method of main package is called to
162
// process all newly added packages
163
String JavaDoc xmlFile = XMLUtil.getCanonicalPath(ep.toString(), false);
164       if (((Package JavaDoc)myOwner).getXMLInterface().doesPackageFileExists(xmlFile) && ((Package JavaDoc)myOwner).getXMLInterface().getIDFromFile(xmlFile).length()>0) {
165          ((Package JavaDoc)myOwner).insertFromExternal(ep);
166       } else {
167          XMLUtil.message(XMLUtil.getLanguageDependentString("InformationErrorWhileOpeningFile"),
168             JOptionPane.INFORMATION_MESSAGE);
169          refCollectionElements.remove(ep);
170       }
171    }
172
173    /**
174    * Overrides super-class method to implement class specific
175    * check wether specified element can be removed from
176    * this class collection or not.
177    *
178    * @param el Element that is checked for removal - in this case
179    * it must be instance of ExternalPackage class.
180    * @return <tt>true</tt> if specified element can be removed,
181    * <tt>false</tt> otherwise.
182    */

183    public boolean canRemoveElement (XMLElement el) {
184       return ((Package JavaDoc)myOwner).canRemoveExternalPackage((ExternalPackage)el);
185    }
186
187    /**
188    * Overrides super-class method to implement class specific
189    * actions when element is removed from this class collection.
190    *
191    * @param el Removed element - in this case it must be instance
192    * of ExternalPackage class.
193    */

194    public void onElementRemoved (XMLElement el) {
195       ((Package JavaDoc)myOwner).removeExternal ((ExternalPackage)el);
196
197    }
198
199    /**
200    * Creates panel which allows user to manipulate with instances
201    * of classes that makes a collection that this class represents.
202    * The panel consists of two sections: XMLListPanel - shows
203    * collections' elements, and XMLListControlPanel - display buttons
204    * for manipulating with that elements.
205    * <p>NOTE: The order of creation is relevant, first, the controlled
206    * panel must be created, and then the control panel
207    *
208    * @return DTDPanel to be shown
209    */

210    public XMLPanel getPanel () {
211       controlledPanel=new XMLListPanel(this,"",false,true,false);
212       controlPanel=new XMLListControlPanel(this,"",true,false,true);
213       return new XMLGroupPanel(this,new XMLPanel[]{
214          controlledPanel,controlPanel},toLabel(),XMLPanel.BOX_LAYOUT,
215          false,true);
216    }
217
218    public String JavaDoc getInUseMessageName (XMLComplexElement el) {
219       return "ErrorCannotRemoveExternalPackage";
220    }
221
222
223 }
224
Popular Tags