/** * Creates an Element(or Node in XML in Document d) for single component * * @param x the component for which Element will be created * @param d Where the element will be added * @return the Element(for XML) created from component x */ private Element createCmpEle(component x, Document d) { Element cmp_el = d.createElement("comp"); cmp_el.setAttribute("id", String.valueOf(x.getId())); Element type_el = d.createElement("comp_type_id"); Text type_txt = d.createTextNode(String.valueOf(x.getType().getId())); type_el.appendChild(type_txt); cmp_el.appendChild(type_el); Element pkg_el = d.createElement("pkg_name"); Text pkg_txt = d.createTextNode(x.getType().getType_pkg_name()); pkg_el.appendChild(pkg_txt); cmp_el.appendChild(pkg_el); Element pos_el = d.createElement("position"); Element x_el = d.createElement("x"); Text x_txt = d.createTextNode(String.valueOf(x.getPosition().x)); x_el.appendChild(x_txt); pos_el.appendChild(x_el); Element y_el = d.createElement("y"); Text y_txt = d.createTextNode(String.valueOf(x.getPosition().y)); y_el.appendChild(y_txt); pos_el.appendChild(y_el); cmp_el.appendChild(pos_el); return cmp_el; }
private Element generateGraphicsNode(Document doc, ClassGraphics gr) { Element graphicsEl = doc.createElement(EL_GRAPHICS); Element bounds = doc.createElement(EL_BOUNDS); graphicsEl.appendChild(bounds); bounds.setAttribute(ATR_X, Integer.toString(gr.getBoundX())); bounds.setAttribute(ATR_Y, Integer.toString(gr.getBoundY())); bounds.setAttribute(ATR_WIDTH, Integer.toString(gr.getBoundWidth())); bounds.setAttribute(ATR_HEIGHT, Integer.toString(gr.getBoundHeight())); for (Shape shape : gr.getShapes()) { if (shape instanceof Rect) { Rect rect = (Rect) shape; Element rectEl = doc.createElement(EL_RECT); graphicsEl.appendChild(rectEl); rectEl.setAttribute(ATR_X, Integer.toString(rect.getX())); rectEl.setAttribute(ATR_Y, Integer.toString(rect.getY())); rectEl.setAttribute(ATR_WIDTH, Integer.toString(rect.getWidth())); rectEl.setAttribute(ATR_HEIGHT, Integer.toString(rect.getHeight())); rectEl.setAttribute(ATR_COLOUR, Integer.toString(rect.getColor().getRGB())); rectEl.setAttribute(ATR_FILLED, Boolean.toString(rect.isFilled())); rectEl.setAttribute(ATR_FIXED, Boolean.toString(rect.isFixed())); rectEl.setAttribute(ATR_STROKE, Float.toString(rect.getStroke().getLineWidth())); rectEl.setAttribute(ATR_LINETYPE, Float.toString(rect.getLineType())); rectEl.setAttribute(ATR_TRANSPARENCY, Integer.toString(rect.getTransparency())); } else if (shape instanceof Text) { Text text = (Text) shape; Element textEl = doc.createElement(EL_TEXT); textEl.setAttribute(ATR_STRING, text.getText()); textEl.setAttribute(ATR_X, Integer.toString(text.getX())); textEl.setAttribute(ATR_Y, Integer.toString(text.getY())); textEl.setAttribute(ATR_FONTNAME, text.getFont().getName()); textEl.setAttribute(ATR_FONTSIZE, Integer.toString(text.getFont().getSize())); textEl.setAttribute(ATR_FONTSTYLE, Integer.toString(text.getFont().getStyle())); textEl.setAttribute(ATR_TRANSPARENCY, Integer.toString(text.getTransparency())); textEl.setAttribute(ATR_COLOUR, Integer.toString(text.getColor().getRGB())); graphicsEl.appendChild(textEl); } // TODO handle the rest of shapes } return graphicsEl; }
/** * @param path_ret where the file will be saved * @param a getting the information of current status of the editor * @param cnc_a getting the information of current status of the editor * @throws ParserConfigurationException * @throws IOException */ public void save(String tempPath, counts a, connections cnc_a) throws ParserConfigurationException, IOException { try { DocumentBuilderFactory dbf = DocumentBuilderFactoryImpl.newInstance(); // get an instance of builder DocumentBuilder db = dbf.newDocumentBuilder(); // create an instance of DOM Document dom = db.newDocument(); Element el_root = dom.createElement("circuit"); dom.appendChild(el_root); // Root of the components Element cmp_root = dom.createElement("components"); el_root.appendChild(cmp_root); for (component x : a.getComponent_list()) { Element cmpEle = this.createCmpEle(x, dom); cmp_root.appendChild(cmpEle); } // Root of the components Element wire_root = dom.createElement("wires"); el_root.appendChild(wire_root); for (line x : cnc_a.getLine_logs()) { Element wireEle = null; if (x.getId() > 0) { wireEle = this.createWireEle(x, dom); wire_root.appendChild(wireEle); } } OutputFormat format = new OutputFormat(); XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File(tempPath)), format); serializer.serialize(dom); this.changeTitle(new File(tempPath).getName()); this.setSaved(true); this.setPath(tempPath); taskbar.setText("File is saved successfully."); } catch (FileNotFoundException ex) { Logger.getLogger(Pad_Draw.class.getName()).log(Level.SEVERE, null, ex); } }
/** * Carries out preprocessing that makes JEuclid handle the document better. * * @param doc Document */ static void preprocessForJEuclid(Document doc) { // underbrace and overbrace NodeList list = doc.getElementsByTagName("mo"); for (int i = 0; i < list.getLength(); i++) { Element mo = (Element) list.item(i); String parentName = ((Element) mo.getParentNode()).getTagName(); if (parentName == null) { continue; } if (parentName.equals("munder") && isTextChild(mo, "\ufe38")) { mo.setAttribute("stretchy", "true"); mo.removeChild(mo.getFirstChild()); mo.appendChild(doc.createTextNode("\u23df")); } else if (parentName.equals("mover") && isTextChild(mo, "\ufe37")) { mo.setAttribute("stretchy", "true"); mo.removeChild(mo.getFirstChild()); mo.appendChild(doc.createTextNode("\u23de")); } } // menclose for long division doesn't allow enough top padding. Oh, and // <mpadded> isn't implemented. And there isn't enough padding to left of // the bar either. Solve by adding an <mover> with just an <mspace> over# // the longdiv, contained within an mrow that adds a <mspace> before it. list = doc.getElementsByTagName("menclose"); for (int i = 0; i < list.getLength(); i++) { Element menclose = (Element) list.item(i); // Only for longdiv if (!"longdiv".equals(menclose.getAttribute("notation"))) { continue; } Element mrow = doc.createElementNS(WebMathsService.NS, "mrow"); Element mover = doc.createElementNS(WebMathsService.NS, "mover"); Element mspace = doc.createElementNS(WebMathsService.NS, "mspace"); Element mspaceW = doc.createElementNS(WebMathsService.NS, "mspace"); boolean previousElement = false; for (Node previous = menclose.getPreviousSibling(); previous != null; previous = previous.getPreviousSibling()) { if (previous.getNodeType() == Node.ELEMENT_NODE) { previousElement = true; break; } } if (previousElement) { mspaceW.setAttribute("width", "4px"); } menclose.getParentNode().insertBefore(mrow, menclose); menclose.getParentNode().removeChild(menclose); mrow.appendChild(mspaceW); mrow.appendChild(mover); mover.appendChild(menclose); mover.appendChild(mspace); } }
public void build_bricks() { ImagePlus imp; ImagePlus orgimp; ImageStack stack; FileInfo finfo; if (lvImgTitle.isEmpty()) return; orgimp = WindowManager.getImage(lvImgTitle.get(0)); imp = orgimp; finfo = imp.getFileInfo(); if (finfo == null) return; int[] dims = imp.getDimensions(); int imageW = dims[0]; int imageH = dims[1]; int nCh = dims[2]; int imageD = dims[3]; int nFrame = dims[4]; int bdepth = imp.getBitDepth(); double xspc = finfo.pixelWidth; double yspc = finfo.pixelHeight; double zspc = finfo.pixelDepth; double z_aspect = Math.max(xspc, yspc) / zspc; int orgW = imageW; int orgH = imageH; int orgD = imageD; double orgxspc = xspc; double orgyspc = yspc; double orgzspc = zspc; lv = lvImgTitle.size(); if (filetype == "JPEG") { for (int l = 0; l < lv; l++) { if (WindowManager.getImage(lvImgTitle.get(l)).getBitDepth() != 8) { IJ.error("A SOURCE IMAGE MUST BE 8BIT GLAYSCALE"); return; } } } // calculate levels /* int baseXY = 256; int baseZ = 256; if (z_aspect < 0.5) baseZ = 128; if (z_aspect > 2.0) baseXY = 128; if (z_aspect >= 0.5 && z_aspect < 1.0) baseZ = (int)(baseZ*z_aspect); if (z_aspect > 1.0 && z_aspect <= 2.0) baseXY = (int)(baseXY/z_aspect); IJ.log("Z_aspect: " + z_aspect); IJ.log("BaseXY: " + baseXY); IJ.log("BaseZ: " + baseZ); */ int baseXY = 256; int baseZ = 128; int dbXY = Math.max(orgW, orgH) / baseXY; if (Math.max(orgW, orgH) % baseXY > 0) dbXY *= 2; int dbZ = orgD / baseZ; if (orgD % baseZ > 0) dbZ *= 2; lv = Math.max(log2(dbXY), log2(dbZ)) + 1; int ww = orgW; int hh = orgH; int dd = orgD; for (int l = 0; l < lv; l++) { int bwnum = ww / baseXY; if (ww % baseXY > 0) bwnum++; int bhnum = hh / baseXY; if (hh % baseXY > 0) bhnum++; int bdnum = dd / baseZ; if (dd % baseZ > 0) bdnum++; if (bwnum % 2 == 0) bwnum++; if (bhnum % 2 == 0) bhnum++; if (bdnum % 2 == 0) bdnum++; int bw = (bwnum <= 1) ? ww : ww / bwnum + 1 + (ww % bwnum > 0 ? 1 : 0); int bh = (bhnum <= 1) ? hh : hh / bhnum + 1 + (hh % bhnum > 0 ? 1 : 0); int bd = (bdnum <= 1) ? dd : dd / bdnum + 1 + (dd % bdnum > 0 ? 1 : 0); bwlist.add(bw); bhlist.add(bh); bdlist.add(bd); IJ.log("LEVEL: " + l); IJ.log(" width: " + ww); IJ.log(" hight: " + hh); IJ.log(" depth: " + dd); IJ.log(" bw: " + bw); IJ.log(" bh: " + bh); IJ.log(" bd: " + bd); int xyl2 = Math.max(ww, hh) / baseXY; if (Math.max(ww, hh) % baseXY > 0) xyl2 *= 2; if (lv - 1 - log2(xyl2) <= l) { ww /= 2; hh /= 2; } IJ.log(" xyl2: " + (lv - 1 - log2(xyl2))); int zl2 = dd / baseZ; if (dd % baseZ > 0) zl2 *= 2; if (lv - 1 - log2(zl2) <= l) dd /= 2; IJ.log(" zl2: " + (lv - 1 - log2(zl2))); if (l < lv - 1) { lvImgTitle.add(lvImgTitle.get(0) + "_level" + (l + 1)); IJ.selectWindow(lvImgTitle.get(0)); IJ.run( "Scale...", "x=- y=- z=- width=" + ww + " height=" + hh + " depth=" + dd + " interpolation=Bicubic average process create title=" + lvImgTitle.get(l + 1)); } } for (int l = 0; l < lv; l++) { IJ.log(lvImgTitle.get(l)); } Document doc = newXMLDocument(); Element root = doc.createElement("BRK"); root.setAttribute("version", "1.0"); root.setAttribute("nLevel", String.valueOf(lv)); root.setAttribute("nChannel", String.valueOf(nCh)); root.setAttribute("nFrame", String.valueOf(nFrame)); doc.appendChild(root); for (int l = 0; l < lv; l++) { IJ.showProgress(0.0); int[] dims2 = imp.getDimensions(); IJ.log( "W: " + String.valueOf(dims2[0]) + " H: " + String.valueOf(dims2[1]) + " C: " + String.valueOf(dims2[2]) + " D: " + String.valueOf(dims2[3]) + " T: " + String.valueOf(dims2[4]) + " b: " + String.valueOf(bdepth)); bw = bwlist.get(l).intValue(); bh = bhlist.get(l).intValue(); bd = bdlist.get(l).intValue(); boolean force_pow2 = false; /* if(IsPowerOf2(bw) && IsPowerOf2(bh) && IsPowerOf2(bd)) force_pow2 = true; if(force_pow2){ //force pow2 if(Pow2(bw) > bw) bw = Pow2(bw)/2; if(Pow2(bh) > bh) bh = Pow2(bh)/2; if(Pow2(bd) > bd) bd = Pow2(bd)/2; } if(bw > imageW) bw = (Pow2(imageW) == imageW) ? imageW : Pow2(imageW)/2; if(bh > imageH) bh = (Pow2(imageH) == imageH) ? imageH : Pow2(imageH)/2; if(bd > imageD) bd = (Pow2(imageD) == imageD) ? imageD : Pow2(imageD)/2; */ if (bw > imageW) bw = imageW; if (bh > imageH) bh = imageH; if (bd > imageD) bd = imageD; if (bw <= 1 || bh <= 1 || bd <= 1) break; if (filetype == "JPEG" && (bw < 8 || bh < 8)) break; Element lvnode = doc.createElement("Level"); lvnode.setAttribute("lv", String.valueOf(l)); lvnode.setAttribute("imageW", String.valueOf(imageW)); lvnode.setAttribute("imageH", String.valueOf(imageH)); lvnode.setAttribute("imageD", String.valueOf(imageD)); lvnode.setAttribute("xspc", String.valueOf(xspc)); lvnode.setAttribute("yspc", String.valueOf(yspc)); lvnode.setAttribute("zspc", String.valueOf(zspc)); lvnode.setAttribute("bitDepth", String.valueOf(bdepth)); root.appendChild(lvnode); Element brksnode = doc.createElement("Bricks"); brksnode.setAttribute("brick_baseW", String.valueOf(bw)); brksnode.setAttribute("brick_baseH", String.valueOf(bh)); brksnode.setAttribute("brick_baseD", String.valueOf(bd)); lvnode.appendChild(brksnode); ArrayList<Brick> bricks = new ArrayList<Brick>(); int mw, mh, md, mw2, mh2, md2; double tx0, ty0, tz0, tx1, ty1, tz1; double bx0, by0, bz0, bx1, by1, bz1; for (int k = 0; k < imageD; k += bd) { if (k > 0) k--; for (int j = 0; j < imageH; j += bh) { if (j > 0) j--; for (int i = 0; i < imageW; i += bw) { if (i > 0) i--; mw = Math.min(bw, imageW - i); mh = Math.min(bh, imageH - j); md = Math.min(bd, imageD - k); if (force_pow2) { mw2 = Pow2(mw); mh2 = Pow2(mh); md2 = Pow2(md); } else { mw2 = mw; mh2 = mh; md2 = md; } if (filetype == "JPEG") { if (mw2 < 8) mw2 = 8; if (mh2 < 8) mh2 = 8; } tx0 = i == 0 ? 0.0d : ((mw2 - mw + 0.5d) / mw2); ty0 = j == 0 ? 0.0d : ((mh2 - mh + 0.5d) / mh2); tz0 = k == 0 ? 0.0d : ((md2 - md + 0.5d) / md2); tx1 = 1.0d - 0.5d / mw2; if (mw < bw) tx1 = 1.0d; if (imageW - i == bw) tx1 = 1.0d; ty1 = 1.0d - 0.5d / mh2; if (mh < bh) ty1 = 1.0d; if (imageH - j == bh) ty1 = 1.0d; tz1 = 1.0d - 0.5d / md2; if (md < bd) tz1 = 1.0d; if (imageD - k == bd) tz1 = 1.0d; bx0 = i == 0 ? 0.0d : (i + 0.5d) / (double) imageW; by0 = j == 0 ? 0.0d : (j + 0.5d) / (double) imageH; bz0 = k == 0 ? 0.0d : (k + 0.5d) / (double) imageD; bx1 = Math.min((i + bw - 0.5d) / (double) imageW, 1.0d); if (imageW - i == bw) bx1 = 1.0d; by1 = Math.min((j + bh - 0.5d) / (double) imageH, 1.0d); if (imageH - j == bh) by1 = 1.0d; bz1 = Math.min((k + bd - 0.5d) / (double) imageD, 1.0d); if (imageD - k == bd) bz1 = 1.0d; int x, y, z; x = i - (mw2 - mw); y = j - (mh2 - mh); z = k - (md2 - md); bricks.add( new Brick( x, y, z, mw2, mh2, md2, 0, 0, tx0, ty0, tz0, tx1, ty1, tz1, bx0, by0, bz0, bx1, by1, bz1)); } } } Element fsnode = doc.createElement("Files"); lvnode.appendChild(fsnode); stack = imp.getStack(); int totalbricknum = nFrame * nCh * bricks.size(); int curbricknum = 0; for (int f = 0; f < nFrame; f++) { for (int ch = 0; ch < nCh; ch++) { int sizelimit = bdsizelimit * 1024 * 1024; int bytecount = 0; int filecount = 0; int pd_bufsize = Math.max(sizelimit, bw * bh * bd * bdepth / 8); byte[] packed_data = new byte[pd_bufsize]; String base_dataname = basename + "_Lv" + String.valueOf(l) + "_Ch" + String.valueOf(ch) + "_Fr" + String.valueOf(f); String current_dataname = base_dataname + "_data" + filecount; Brick b_first = bricks.get(0); if (b_first.z_ != 0) IJ.log("warning"); int st_z = b_first.z_; int ed_z = b_first.z_ + b_first.d_; LinkedList<ImageProcessor> iplist = new LinkedList<ImageProcessor>(); for (int s = st_z; s < ed_z; s++) iplist.add(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1))); // ImagePlus test; // ImageStack tsst; // test = NewImage.createByteImage("test", imageW, imageH, imageD, // NewImage.FILL_BLACK); // tsst = test.getStack(); for (int i = 0; i < bricks.size(); i++) { Brick b = bricks.get(i); if (ed_z > b.z_ || st_z < b.z_ + b.d_) { if (b.z_ > st_z) { for (int s = 0; s < b.z_ - st_z; s++) iplist.pollFirst(); st_z = b.z_; } else if (b.z_ < st_z) { IJ.log("warning"); for (int s = st_z - 1; s > b.z_; s--) iplist.addFirst(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1))); st_z = b.z_; } if (b.z_ + b.d_ > ed_z) { for (int s = ed_z; s < b.z_ + b.d_; s++) iplist.add(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1))); ed_z = b.z_ + b.d_; } else if (b.z_ + b.d_ < ed_z) { IJ.log("warning"); for (int s = 0; s < ed_z - (b.z_ + b.d_); s++) iplist.pollLast(); ed_z = b.z_ + b.d_; } } else { IJ.log("warning"); iplist.clear(); st_z = b.z_; ed_z = b.z_ + b.d_; for (int s = st_z; s < ed_z; s++) iplist.add(stack.getProcessor(imp.getStackIndex(ch + 1, s + 1, f + 1))); } if (iplist.size() != b.d_) { IJ.log("Stack Error"); return; } // int zz = st_z; int bsize = 0; byte[] bdata = new byte[b.w_ * b.h_ * b.d_ * bdepth / 8]; Iterator<ImageProcessor> ipite = iplist.iterator(); while (ipite.hasNext()) { // ImageProcessor tsip = tsst.getProcessor(zz+1); ImageProcessor ip = ipite.next(); ip.setRoi(b.x_, b.y_, b.w_, b.h_); if (bdepth == 8) { byte[] data = (byte[]) ip.crop().getPixels(); System.arraycopy(data, 0, bdata, bsize, data.length); bsize += data.length; } else if (bdepth == 16) { ByteBuffer buffer = ByteBuffer.allocate(b.w_ * b.h_ * bdepth / 8); buffer.order(ByteOrder.LITTLE_ENDIAN); short[] data = (short[]) ip.crop().getPixels(); for (short e : data) buffer.putShort(e); System.arraycopy(buffer.array(), 0, bdata, bsize, buffer.array().length); bsize += buffer.array().length; } else if (bdepth == 32) { ByteBuffer buffer = ByteBuffer.allocate(b.w_ * b.h_ * bdepth / 8); buffer.order(ByteOrder.LITTLE_ENDIAN); float[] data = (float[]) ip.crop().getPixels(); for (float e : data) buffer.putFloat(e); System.arraycopy(buffer.array(), 0, bdata, bsize, buffer.array().length); bsize += buffer.array().length; } } String filename = basename + "_Lv" + String.valueOf(l) + "_Ch" + String.valueOf(ch) + "_Fr" + String.valueOf(f) + "_ID" + String.valueOf(i); int offset = bytecount; int datasize = bdata.length; if (filetype == "RAW") { int dummy = -1; // do nothing } if (filetype == "JPEG" && bdepth == 8) { try { DataBufferByte db = new DataBufferByte(bdata, datasize); Raster raster = Raster.createPackedRaster(db, b.w_, b.h_ * b.d_, 8, null); BufferedImage img = new BufferedImage(b.w_, b.h_ * b.d_, BufferedImage.TYPE_BYTE_GRAY); img.setData(raster); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageOutputStream ios = ImageIO.createImageOutputStream(baos); String format = "jpg"; Iterator<javax.imageio.ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg"); javax.imageio.ImageWriter writer = iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality((float) jpeg_quality * 0.01f); writer.setOutput(ios); writer.write(null, new IIOImage(img, null, null), iwp); // ImageIO.write(img, format, baos); bdata = baos.toByteArray(); datasize = bdata.length; } catch (IOException e) { e.printStackTrace(); return; } } if (filetype == "ZLIB") { byte[] tmpdata = new byte[b.w_ * b.h_ * b.d_ * bdepth / 8]; Deflater compresser = new Deflater(); compresser.setInput(bdata); compresser.setLevel(Deflater.DEFAULT_COMPRESSION); compresser.setStrategy(Deflater.DEFAULT_STRATEGY); compresser.finish(); datasize = compresser.deflate(tmpdata); bdata = tmpdata; compresser.end(); } if (bytecount + datasize > sizelimit && bytecount > 0) { BufferedOutputStream fis = null; try { File file = new File(directory + current_dataname); fis = new BufferedOutputStream(new FileOutputStream(file)); fis.write(packed_data, 0, bytecount); } catch (IOException e) { e.printStackTrace(); return; } finally { try { if (fis != null) fis.close(); } catch (IOException e) { e.printStackTrace(); return; } } filecount++; current_dataname = base_dataname + "_data" + filecount; bytecount = 0; offset = 0; System.arraycopy(bdata, 0, packed_data, bytecount, datasize); bytecount += datasize; } else { System.arraycopy(bdata, 0, packed_data, bytecount, datasize); bytecount += datasize; } Element filenode = doc.createElement("File"); filenode.setAttribute("filename", current_dataname); filenode.setAttribute("channel", String.valueOf(ch)); filenode.setAttribute("frame", String.valueOf(f)); filenode.setAttribute("brickID", String.valueOf(i)); filenode.setAttribute("offset", String.valueOf(offset)); filenode.setAttribute("datasize", String.valueOf(datasize)); filenode.setAttribute("filetype", String.valueOf(filetype)); fsnode.appendChild(filenode); curbricknum++; IJ.showProgress((double) (curbricknum) / (double) (totalbricknum)); } if (bytecount > 0) { BufferedOutputStream fis = null; try { File file = new File(directory + current_dataname); fis = new BufferedOutputStream(new FileOutputStream(file)); fis.write(packed_data, 0, bytecount); } catch (IOException e) { e.printStackTrace(); return; } finally { try { if (fis != null) fis.close(); } catch (IOException e) { e.printStackTrace(); return; } } } } } for (int i = 0; i < bricks.size(); i++) { Brick b = bricks.get(i); Element bricknode = doc.createElement("Brick"); bricknode.setAttribute("id", String.valueOf(i)); bricknode.setAttribute("st_x", String.valueOf(b.x_)); bricknode.setAttribute("st_y", String.valueOf(b.y_)); bricknode.setAttribute("st_z", String.valueOf(b.z_)); bricknode.setAttribute("width", String.valueOf(b.w_)); bricknode.setAttribute("height", String.valueOf(b.h_)); bricknode.setAttribute("depth", String.valueOf(b.d_)); brksnode.appendChild(bricknode); Element tboxnode = doc.createElement("tbox"); tboxnode.setAttribute("x0", String.valueOf(b.tx0_)); tboxnode.setAttribute("y0", String.valueOf(b.ty0_)); tboxnode.setAttribute("z0", String.valueOf(b.tz0_)); tboxnode.setAttribute("x1", String.valueOf(b.tx1_)); tboxnode.setAttribute("y1", String.valueOf(b.ty1_)); tboxnode.setAttribute("z1", String.valueOf(b.tz1_)); bricknode.appendChild(tboxnode); Element bboxnode = doc.createElement("bbox"); bboxnode.setAttribute("x0", String.valueOf(b.bx0_)); bboxnode.setAttribute("y0", String.valueOf(b.by0_)); bboxnode.setAttribute("z0", String.valueOf(b.bz0_)); bboxnode.setAttribute("x1", String.valueOf(b.bx1_)); bboxnode.setAttribute("y1", String.valueOf(b.by1_)); bboxnode.setAttribute("z1", String.valueOf(b.bz1_)); bricknode.appendChild(bboxnode); } if (l < lv - 1) { imp = WindowManager.getImage(lvImgTitle.get(l + 1)); int[] newdims = imp.getDimensions(); imageW = newdims[0]; imageH = newdims[1]; imageD = newdims[3]; xspc = orgxspc * ((double) orgW / (double) imageW); yspc = orgyspc * ((double) orgH / (double) imageH); zspc = orgzspc * ((double) orgD / (double) imageD); bdepth = imp.getBitDepth(); } } File newXMLfile = new File(directory + basename + ".vvd"); writeXML(newXMLfile, doc); for (int l = 1; l < lv; l++) { imp = WindowManager.getImage(lvImgTitle.get(l)); imp.changes = false; imp.close(); } }
/** * creates a new image * * @param svgHandle a svg handle * @param resourceId the id of the resource from which the image will be created */ protected void createNewImage(SVGHandle svgHandle, String resourceId) { if (svgHandle != null && resourceId != null && !resourceId.equals("")) { Element resourceElement = null; resourceElement = svgHandle.getScrollPane().getSVGCanvas().getDocument().getElementById(resourceId); final String fresourceId = resourceId; if (resourceElement != null) { final SVGHandle fhandle = svgHandle; // creating the canvas and setting its properties final JSVGCanvas canvas = new JSVGCanvas() { @Override public void dispose() { removeKeyListener(listener); removeMouseMotionListener(listener); removeMouseListener(listener); disableInteractions = true; selectableText = false; userAgent = null; bridgeContext.dispose(); super.dispose(); } }; // the element to be added Element elementToAdd = null; canvas.setDocumentState(JSVGComponent.ALWAYS_STATIC); canvas.setDisableInteractions(true); // creating the new document final String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; final SVGDocument doc = (SVGDocument) resourceElement.getOwnerDocument().cloneNode(false); // creating the root element final Element root = (Element) doc.importNode(resourceElement.getOwnerDocument().getDocumentElement(), false); doc.appendChild(root); // removing all the attributes of the root element NamedNodeMap attributes = doc.getDocumentElement().getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { if (attributes.item(i) != null) { doc.getDocumentElement().removeAttribute(attributes.item(i).getNodeName()); } } // adding the new attributes for the root root.setAttributeNS(null, "width", imageSize.width + ""); root.setAttributeNS(null, "height", imageSize.height + ""); root.setAttributeNS(null, "viewBox", "0 0 " + imageSize.width + " " + imageSize.height); // the defs element that will contain the cloned resource node final Element defs = (Element) doc.importNode(resourceElement.getParentNode(), true); root.appendChild(defs); if (resourceElement.getNodeName().equals("linearGradient") || resourceElement.getNodeName().equals("radialGradient") || resourceElement.getNodeName().equals("pattern")) { // the rectangle that will be drawn final Element rect = doc.createElementNS(svgNS, "rect"); rect.setAttributeNS(null, "x", "0"); rect.setAttributeNS(null, "y", "0"); rect.setAttributeNS(null, "width", imageSize.width + ""); rect.setAttributeNS(null, "height", imageSize.height + ""); elementToAdd = rect; // setting that the rectangle uses the resource String id = resourceElement.getAttribute("id"); if (id == null) { id = ""; } rect.setAttributeNS(null, "style", "fill:url(#" + id + ");"); // getting the cloned resource node Node cur = null; Element clonedResourceElement = null; String id2 = ""; for (cur = defs.getFirstChild(); cur != null; cur = cur.getNextSibling()) { if (cur instanceof Element) { id2 = ((Element) cur).getAttribute("id"); if (id2 != null && id.equals(id2)) { clonedResourceElement = (Element) cur; } } } if (clonedResourceElement != null) { // getting the root element of the initial resource // element Element initialRoot = resourceElement.getOwnerDocument().getDocumentElement(); // getting the width and height of the initial root // element double initialWidth = 0, initialHeight = 0; try { initialWidth = EditorToolkit.getPixelledNumber(initialRoot.getAttributeNS(null, "width")); initialHeight = EditorToolkit.getPixelledNumber(initialRoot.getAttributeNS(null, "height")); } catch (DOMException ex) { ex.printStackTrace(); } if (resourceElement.getNodeName().equals("linearGradient")) { if (resourceElement.getAttributeNS(null, "gradientUnits").equals("userSpaceOnUse")) { double x1 = 0, y1 = 0, x2 = 0, y2 = 0; // normalizing the values for the vector to fit // the rectangle try { x1 = Double.parseDouble(resourceElement.getAttributeNS(null, "x1")); y1 = Double.parseDouble(resourceElement.getAttributeNS(null, "y1")); x2 = Double.parseDouble(resourceElement.getAttributeNS(null, "x2")); y2 = Double.parseDouble(resourceElement.getAttributeNS(null, "y2")); x1 = x1 / initialWidth * imageSize.width; y1 = y1 / initialHeight * imageSize.height; x2 = x2 / initialWidth * imageSize.width; y2 = y2 / initialHeight * imageSize.height; } catch (NumberFormatException | DOMException ex) { ex.printStackTrace(); } clonedResourceElement.setAttributeNS(null, "x1", format.format(x1)); clonedResourceElement.setAttributeNS(null, "y1", format.format(y1)); clonedResourceElement.setAttributeNS(null, "x2", format.format(x2)); clonedResourceElement.setAttributeNS(null, "y2", format.format(y2)); } } else if (resourceElement.getNodeName().equals("radialGradient")) { if (resourceElement.getAttributeNS(null, "gradientUnits").equals("userSpaceOnUse")) { double cx = 0, cy = 0, r = 0, fx = 0, fy = 0; // normalizing the values for the circle to fit // the rectangle try { cx = Double.parseDouble(resourceElement.getAttributeNS(null, "cx")); cy = Double.parseDouble(resourceElement.getAttributeNS(null, "cy")); r = Double.parseDouble(resourceElement.getAttributeNS(null, "r")); fx = Double.parseDouble(resourceElement.getAttributeNS(null, "fx")); fy = Double.parseDouble(resourceElement.getAttributeNS(null, "fy")); cx = cx / initialWidth * imageSize.width; cy = cy / initialHeight * imageSize.height; r = r / (Math.abs( Math.sqrt(Math.pow(initialWidth, 2) + Math.pow(initialHeight, 2)))) * Math.abs( Math.sqrt( Math.pow(imageSize.width, 2) + Math.pow(imageSize.width, 2))); fx = fx / initialWidth * imageSize.width; fy = fy / initialHeight * imageSize.height; } catch (NumberFormatException | DOMException ex) { ex.printStackTrace(); } clonedResourceElement.setAttributeNS(null, "cx", format.format(cx)); clonedResourceElement.setAttributeNS(null, "cy", format.format(cy)); clonedResourceElement.setAttributeNS(null, "r", format.format(r)); clonedResourceElement.setAttributeNS(null, "fx", format.format(fx)); clonedResourceElement.setAttributeNS(null, "fy", format.format(fy)); } } else if (resourceElement.getNodeName().equals("pattern")) { if (resourceElement.getAttributeNS(null, "patternUnits").equals("userSpaceOnUse")) { double x = 0, y = 0, w = 0, h = 0; // normalizing the values for the vector to fit // the rectangle try { String xString = resourceElement.getAttributeNS(null, "x"); if (!xString.equals("")) { x = Double.parseDouble(xString); } String yString = resourceElement.getAttributeNS(null, "y"); if (!yString.equals("")) { y = Double.parseDouble(yString); } String wString = resourceElement.getAttributeNS(null, "w"); if (!wString.equals("")) { w = Double.parseDouble(wString); } String hString = resourceElement.getAttributeNS(null, "h"); if (!hString.equals("")) { h = Double.parseDouble(hString); } x = x / initialWidth * imageSize.width; y = y / initialHeight * imageSize.height; w = w / initialWidth * imageSize.width; h = h / initialHeight * imageSize.height; } catch (NumberFormatException | DOMException ex) { ex.printStackTrace(); } clonedResourceElement.setAttributeNS(null, "x", format.format(x)); clonedResourceElement.setAttributeNS(null, "y", format.format(y)); clonedResourceElement.setAttributeNS(null, "width", format.format(w)); clonedResourceElement.setAttributeNS(null, "height", format.format(h)); } } } } else if (resourceElement.getNodeName().equals("marker")) { // the line that will be drawn final Element line = doc.createElementNS(svgNS, "line"); line.setAttributeNS(null, "x1", (((double) imageSize.width) / 2) + ""); line.setAttributeNS(null, "y1", (((double) imageSize.height) / 2) + ""); line.setAttributeNS(null, "x2", ((double) imageSize.width / 2) + ""); line.setAttributeNS(null, "y2", imageSize.height + ""); elementToAdd = line; // setting that the line uses the resource String id = resourceElement.getAttribute("id"); if (id == null) id = ""; line.setAttributeNS( null, "style", "stroke:none;fill:none;marker-start:url(#" + id + ");"); } root.appendChild(elementToAdd); // adding a rendering listener to the canvas GVTTreeRendererAdapter gVTTreeRendererAdapter = new GVTTreeRendererAdapter() { @Override public void gvtRenderingCompleted(GVTTreeRendererEvent evt) { Image bufferedImage = canvas.getOffScreen(); if (bufferedImage != null) { Graphics g = bufferedImage.getGraphics(); Color borderColor = MetalLookAndFeel.getSeparatorForeground(); g.setColor(borderColor); g.drawRect(0, 0, imageSize.width - 1, imageSize.height - 1); } setImage(fhandle, fresourceId, bufferedImage); // refreshing the panels that have been created when no // image was available for them Image image = null; for (ResourceRepresentation resourceRepresentation : new LinkedList<ResourceRepresentation>(resourceRepresentationList)) { if (resourceRepresentation != null) { resourceRepresentation.refreshRepresentation(); image = resourceRepresentation.getImage(); if (image != null) { resourceRepresentationList.remove(resourceRepresentation); } } } canvas.removeGVTTreeRendererListener(this); canvas.stopProcessing(); canvas.dispose(); } }; canvas.addGVTTreeRendererListener(gVTTreeRendererAdapter); // setting the document for the canvas canvas.setSVGDocument(doc); canvas.setBackground(Color.white); canvas.setBounds(1, 1, imageSize.width, imageSize.height); } } }
public void addPackageClass(PackageClass pClass) { Document doc; try { doc = getDocument(); } catch (Exception e) { e.printStackTrace(); return; } String name = pClass.getName(); // check if such class exists and remove duplicates Element rootEl = doc.getDocumentElement(); NodeList classEls = rootEl.getElementsByTagName(EL_CLASS); for (int i = 0; i < classEls.getLength(); i++) { Element nameEl = getElementByName((Element) classEls.item(i), EL_NAME); if (name.equals(nameEl.getTextContent())) { rootEl.removeChild(classEls.item(i)); } } Element classNode = doc.createElement(EL_CLASS); doc.getDocumentElement().appendChild(classNode); classNode.setAttribute(ATR_TYPE, PackageClass.ComponentType.SCHEME.getXmlName()); classNode.setAttribute(ATR_STATIC, "false"); Element className = doc.createElement(EL_NAME); className.setTextContent(name); classNode.appendChild(className); Element desrc = doc.createElement(EL_DESCRIPTION); desrc.setTextContent(pClass.getDescription()); classNode.appendChild(desrc); Element icon = doc.createElement(EL_ICON); icon.setTextContent(pClass.getIcon()); classNode.appendChild(icon); // graphics classNode.appendChild(generateGraphicsNode(doc, pClass.getGraphics())); // ports List<Port> ports = pClass.getPorts(); if (!ports.isEmpty()) { Element portsEl = doc.createElement(EL_PORTS); classNode.appendChild(portsEl); for (Port port : ports) { portsEl.appendChild(generatePortNode(doc, port)); } } // fields Collection<ClassField> fields = pClass.getFields(); if (!fields.isEmpty()) { Element fieldsEl = doc.createElement(EL_FIELDS); classNode.appendChild(fieldsEl); for (ClassField cf : fields) { fieldsEl.appendChild(generateFieldNode(doc, cf)); } } // write try { writeDocument(doc, new FileOutputStream(xmlFile)); } catch (FileNotFoundException e) { e.printStackTrace(); } }
/** * Creates an Element(or Node in XML in Document d) for single line * * @param x the line for which Element will be created * @param d Where the element will be added * @return the Element(for XML) created from line x */ private Element createWireEle(line x, Document d) { Element wire_el = d.createElement("wire"); wire_el.setAttribute("id", String.valueOf(x.getId())); Element strt_el = d.createElement("start"); Element strt_cmp_el = d.createElement("comp_id"); Text strt_cmp_txt = d.createTextNode(String.valueOf(x.getStartCmpId())); strt_cmp_el.appendChild(strt_cmp_txt); Element strt_mrk_el = d.createElement("mark_point"); Text strt_mrk_txt = d.createTextNode(String.valueOf(x.getStartMrkPt())); strt_mrk_el.appendChild(strt_mrk_txt); strt_el.appendChild(strt_cmp_el); strt_el.appendChild(strt_mrk_el); wire_el.appendChild(strt_el); Element inter_el = d.createElement("inter_point"); inter_el.setAttribute("no", String.valueOf(x.getInter_Line_segs().size())); for (Point p : x.getInter_Line_segs()) { Element pos_el = d.createElement("position"); Element x_el = d.createElement("x"); Text x_txt = d.createTextNode(String.valueOf(p.x)); x_el.appendChild(x_txt); pos_el.appendChild(x_el); Element y_el = d.createElement("y"); Text y_txt = d.createTextNode(String.valueOf(p.y)); y_el.appendChild(y_txt); pos_el.appendChild(y_el); inter_el.appendChild(pos_el); } wire_el.appendChild(inter_el); Element end_el = d.createElement("end"); Element end_cmp_el = d.createElement("comp_id"); Text end_cmp_txt = d.createTextNode(String.valueOf(x.getEndCmpId())); end_cmp_el.appendChild(end_cmp_txt); Element end_mrk_el = d.createElement("mark_point"); Text end_mrk_txt = d.createTextNode(String.valueOf(x.getEndMrkPt())); end_mrk_el.appendChild(end_mrk_txt); end_el.appendChild(end_cmp_el); end_el.appendChild(end_mrk_el); wire_el.appendChild(end_el); return wire_el; }