📄 jfpage.java
字号:
Object obj =super.clone();
if (obj==null){
return null;
}
JFPage page =(JFPage)obj;
page.m_layerList =(ObjectList)m_layerList.clone();
page.m_pageFormat =(JFPageFormat)m_pageFormat.clone();
page.m_canvasFormat.setValue(m_canvasFormat);
page.m_version =m_version;
page.m_producer =m_producer;
page.m_author =m_author;
page.m_hidePorts =m_hidePorts;
page.m_disablePortSnapping=m_disablePortSnapping;
page.m_scaleValue =m_scaleValue;
page.m_scaleUnit =m_scaleUnit;
page.m_screenUnit =m_screenUnit;
return page;
}catch(Exception e){
throw new CloneNotSupportedException(e.getMessage());
}
}
/**
* Returns the hashcode for this Object.
*
* @return hash code for this Point2D.
*
*/
public int hashCode(){
long scaleValue =Double.doubleToLongBits(m_scaleValue);
return super.hashCode() ^
m_layerList.hashCode()^
m_pageFormat.hashCode() ^
m_canvasFormat.hashCode() ^
m_version.hashCode() ^
m_producer.hashCode() ^
m_author.hashCode() ^
(m_hidePorts?1:0) ^
(m_disablePortSnapping?1:0) ^
(int)(scaleValue ^ (scaleValue >>> 32)) ^
m_scaleUnit ^
m_screenUnit
;
}
/**
* Determines whether or not two objects are equal.
*
* @param obj an object to be compared with this object
*
* @return true if the object to be compared is an instance of Port and has the same values; false otherwise.
*
*/
public boolean equals(Object obj){
if (!super.equals(obj))
return false;
if (obj == this)
return true;
if (!(obj instanceof JFPage))
return false;
JFPage page= (JFPage)obj;
return page.m_layerList.equals(m_layerList) &&
page.m_pageFormat.equals(m_pageFormat) &&
page.m_canvasFormat.equals(m_canvasFormat) &&
page.m_version.equals(m_version) &&
page.m_producer.equals(m_producer) &&
page.m_author.equals(m_author)&&
page.m_hidePorts==m_hidePorts&&
page.m_disablePortSnapping==m_disablePortSnapping &&
(int)(page.m_scaleValue*100)==(int)(m_scaleValue*100) &&
page.m_scaleUnit==m_scaleUnit &&
page.m_screenUnit==m_screenUnit;
}
/**
* Save this page to an XML file.
*
* @param fileName A file name.
*
*/
public boolean saveToXML(String fileName){
try{
//top element
Element drawElement = new Element(XML_JFDRAW);
//document
Document doc = new Document(drawElement);
//save this page to dom.
toDOM(drawElement,null);
XMLOutput output =new XMLOutput();
output.saveDocument(fileName,doc);
setModified(false);
return true;
}catch(Exception e){
m_logger.error("saveToXML:"+e);
return false;
}
}
/**
* Save this page to an XML string.
*
* @return Not null if successfully return a XML string document, null otherwise.
*
*/
public String saveToXMLString(){
try{
//top element
Element drawElement = new Element(XML_JFDRAW);
//document
Document doc = new Document(drawElement);
//save this page to dom.
toDOM(drawElement,null);
setModified(false);
XMLOutput out =new XMLOutput();
return out.getDocumentText(doc);
}catch(Exception e){
m_logger.error("saveToXMLString:"+e);
return null;
}
}
/**
* Append necessary xml child for current element,
* this method will be called internally by toDOM.
*
* @param version A file version notification so this object can obey the rules to save data.
*
*/
protected void appendChildToDOM(Element element,JFVersion version){
if (element==null)
return;
super.appendChildToDOM(element,version);
//always store as current version.
element.addChild(new Element(JFVersion.XML_VERSION,JFVersion.getCurrentVersion()));
element.addChild(new Element(JFVersion.XML_PRODUCER,m_producer));
element.addChild(new Element(JFVersion.XML_AUTHOR,m_author));
element.addChild(new Element(XML_HIDEPORTS,m_hidePorts));
element.addChild(new Element(XML_DISABLEPORTSNAPPING,m_disablePortSnapping));
element.addChild(new Element(XML_SCALEVALUE,m_scaleValue));
element.addChild(new Element(XML_SCALEUNIT,m_scaleUnit));
element.addChild(new Element(XML_SCREENUNIT,m_screenUnit));
m_pageFormat.toDOM(element,version);
m_canvasFormat.toDOM(element,version);
m_layerList.toDOM(element,version);
}
/**
* Load this page from an XML file.
*
* @param fileName A file name.
* @param c A component used to help loading some special objects, e.g. image.
*
*/
public boolean loadFromXML(String fileName,Component c){
try{
XMLInput input =new XMLInput();
Document doc =input.loadDocument(fileName);
if (doc==null){
m_logger.error("loadFromXML: Invalid xml document!");
return false;
}
Element root = doc.getRoot();
Element page = root.getChild(getXMLTag());
if (page==null){
m_logger.error("loadFromXML: No necessary xml tag!");
return false;
}
fromDOM(page,null);
if (c!=null)
finishLoadingImages(c);
return true;
}catch(Exception e){
m_logger.error("loadFromXML:"+e);
return false;
}
}
/**
* Load this page from an XML string.Attention: this xml string is already an xml document,
* but not the file name of an xml file.
*
* @param xmlString An xml string.
* @param c A component used to help loading some special objects, e.g. image.
*
*/
public boolean loadFromXMLString(String xmlString,Component c){
try{
XMLInput input =new XMLInput();
Document doc =input.loadDocumentFromText(xmlString);
if (doc==null){
m_logger.error("loadFromXMLString: Invalid xml document!");
return false;
}
Element root =doc.getRoot();
Element page =root.getChild(getXMLTag());
if (page==null){
m_logger.error("loadFromXMLString:No necessary xml tag!");
return false;
}
fromDOM(page,null);
if (c!=null)
finishLoadingImages(c);
return true;
}catch(Exception e){
m_logger.error("loadFromXMLString:"+e);
return false;
}
}
/**
* Extract needed xml child from current element,
* this method will be called internally by fromDOM.
*
* @param element An element used to extract needed xml child
* @param version A file version notification so this object can obey the rules to fetch data.
*
*/
protected void extractChildFromDOM(Element element,JFVersion version){
if (element==null)
return;
m_version =Element.getStringValue(element.getChild(JFVersion.XML_VERSION));
m_producer =Element.getStringValue(element.getChild(JFVersion.XML_PRODUCER));
m_author =Element.getStringValue(element.getChild(JFVersion.XML_AUTHOR));
m_hidePorts =Element.getBooleanValue(element.getChild(XML_HIDEPORTS));
m_disablePortSnapping =Element.getBooleanValue(element.getChild(XML_DISABLEPORTSNAPPING));
m_scaleValue =Element.getDoubleValue(element.getChild(XML_SCALEVALUE));
m_scaleUnit =Element.getIntValue(element.getChild(XML_SCALEUNIT));
m_screenUnit =Element.getIntValue(element.getChild(XML_SCREENUNIT));
/** create a JFVersion object by current version,for version control on file formats*/
version =new JFVersion(m_version);
super.extractChildFromDOM(element,version);
m_pageFormat.fromDOM(element.getChild(m_pageFormat.getXMLTag()),version);
if (!version.lowerVersionOf(JFVersion.VERSIONID_141))
m_canvasFormat.fromDOM(element.getChild(m_pageFormat.getXMLTag()),version);
m_layerList.fromDOM(element.getChild(m_layerList.getXMLTag()),version);
setDefaultLayerIndex();
}
/**
* Save this page to a binary file.
*
* @param fileName A file name.
*
*/
public boolean saveToBinary(String fileName){
try{
//binary JFDraw file
DataOutputStream out =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(fileName)));
com.jfimagine.utils.io.JFWriter writer =new com.jfimagine.utils.io.JFBinaryWriter(out);
saveToStream(writer,null);
out.close();
out =null;
setModified(false);
return true;
}catch(Exception e){
m_logger.error("saveToBinary:"+e);
return false;
}
}
/**
* Save this object to a binary stream
*
* @param stream An binary output stream
*
* @param version A file version notification so this object can obey the rules to save data.
* @exception java.io.IOException
*
*/
public void saveToStream(com.jfimagine.utils.io.JFWriter stream,JFVersion version) throws IOException{
//write a leading key here, to indicate current page version is equal or great than 1.8.0.
stream.writeInt(JFVersion.LEADINGKEY_180);
//write super attributes
super.saveToStream(stream,version);
//write current object attributes
stream.writeUTF(JFVersion.getCurrentVersion());
stream.writeUTF(m_producer);
stream.writeUTF(m_author);
stream.writeBoolean(m_hidePorts);
stream.writeBoolean(m_disablePortSnapping);
stream.writeDouble(m_scaleValue);
stream.writeInt(m_scaleUnit);
stream.writeInt(m_screenUnit);
m_pageFormat.saveToStream(stream,version);
m_canvasFormat.saveToStream(stream,version);
m_layerList.saveToStream(stream,version);
}
/**
* Load this page from a binary file.
*
* @param fileName A file name.
* @param c A component used to help loading some special objects, e.g. image.
*
*/
public boolean loadFromBinary(String fileName,Component c){
try{
InputStream in;
if (CommonUtil.isURLFile(fileName)){
//URL url =new URL(fileName);
//in =url.openStream();
in =com.jfimagine.utils.io.DownloadURLFile.openDownloadStream(fileName);
}else{
in=new FileInputStream(fileName);
}
DataInputStream dataInput=new DataInputStream(new BufferedInputStream(in));
com.jfimagine.utils.io.JFReader reader =new com.jfimagine.utils.io.JFBinaryReader(dataInput);
loadFromStream(reader,false,null);
if (c!=null)
finishLoadingImages(c);
dataInput.close();
in.close();
dataInput =null;
in =null;
return true;
}catch(Exception e){
m_logger.error("loadFromBinary:"+e);
return false;
}
}
/**
* Load object data from a binary stream <br>
*
* @param stream An binary input stream
*
* @param skipHead Skip head 'TYPE' check, an shape object should always
* has its own shape-type stored, if this shape-type has already been readed,
* this loadFromStream should/could not read the type anymore.
*
* @param version A file version notification so this object can obey the rules to fetch data.
*
* @exception java.io.IOException
*
*/
public void loadFromStream(com.jfimagine.utils.io.JFReader stream,boolean skipHead,JFVersion version) throws IOException{
if (!skipHead){
//detect if a leading key is stored at the top of the page.
int leadingKey =stream.readInt();
if (leadingKey==JFVersion.LEADINGKEY_180){
//the leading key is assigned, so create a temporary version object
version =new JFVersion(JFVersion.VERSIONID_180);
}else{
//the leading key is not assigned, so it's actually an object type now:
setObjectType(leadingKey);
skipHead =true;
}
}
//load super attributes
super.loadFromStream(stream,skipHead,version);
//load current attributes
m_version =stream.readUTF();
m_producer =stream.readUTF();
m_author =stream.readUTF();
/** create a JFVersion object by current version,for version control on file formats
* This is a real JFVersion object other than the temporary 1.8.0 JFVersion Object above.
*/
version =new JFVersion(m_version);
//Since version 1.1.1, we started to make properties for page below:
if (!version.lowerVersionOf(JFVersion.VERSIONID_111)){
m_hidePorts =stream.readBoolean();
m_disablePortSnapping =stream.readBoolean();
m_scaleValue =stream.readDouble();
m_scaleUnit =stream.readInt();
m_screenUnit =stream.readInt();
}
m_pageFormat.loadFromStream(stream,false,version);//don't skip head here
//Since version 1.4.1, we started to make properties for page below:
if (!version.lowerVersionOf(JFVersion.VERSIONID_141)){
m_canvasFormat.loadFromStream(stream,false,version);//don't skip head here
}
m_layerList.loadFromStream(stream,false,version);//don't skip head here
setDefaultLayerIndex();
}
/**
* for loading an image, we need to call this finish loading image method to completely create a new image.
*
* @param canvas A component for creating an image object.
*/
public boolean finishLoadingImages(java.awt.Component canvas){
JFLayer layer;
for (int i=0; i<m_layerList.size(); i++){
try{
layer =(JFLayer)m_layerList.getByIndex(i);
layer.finishLoadingImages(canvas);
}catch(Exception e){
}
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -