📄 svgframe.java
字号:
final Node froot=root; final Node fdefs=defs; //the runnable that contains the code that will be executed to append the "defs" node to the root node Runnable runnable=new Runnable(){ public void run() { if(froot.getFirstChild()!=null){ froot.insertBefore(fdefs, froot.getFirstChild()); }else{ froot.appendChild(fdefs); } } }; enqueue(runnable); } } } return defs; } /** * returns the map associating an id to a resource node * @param doc the document * @param resourceTagNames the list of the tag names of the resources that should appear in the returned list * @return the map associating an id to a resource node */ public Hashtable<String, Element> getResourcesFromDefs(Document doc, LinkedList<String> resourceTagNames){ Hashtable<String, Element> idResources=new Hashtable<String, Element>(); //getting the list of the resources that can be found in the defs elements if(doc!=null && resourceTagNames!=null && resourceTagNames.size()>0){ //the list of the available defs nodes that can be found in the document final NodeList defsNodes=doc.getElementsByTagName("defs"); if(defsNodes!=null && defsNodes.getLength()>0){ Element defs=null, el=null; Node cur=null; String id=""; for(int i=0; i<defsNodes.getLength(); i++) { defs=(Element)defsNodes.item(i); if(defs!=null) { //for each child of the "defs" element, adds its id to the map for(cur=defs.getFirstChild(); cur!=null; cur=cur.getNextSibling()) { if(cur instanceof Element && resourceTagNames.contains(cur.getNodeName())){ el=(Element)cur; id=el.getAttribute("id"); if(id!=null && ! id.equals("")){ idResources.put(id, el); } } } } } } } return idResources; } /** * @return the list of the ids of the shape nodes that are contained in the given svg document */ public LinkedList<String> getShapeNodesIds(){ LinkedList<String> idNodes=new LinkedList<String>(); Document doc=getScrollPane().getSVGCanvas().getDocument(); if(doc!=null && doc.getDocumentElement()!=null){ Node cur=null; String id=""; Element el=null; //for each children of the root element (but the "defs" element), adds its id to the map for(NodeIterator it=new NodeIterator(doc.getDocumentElement()); it.hasNext();){ cur=it.next(); if(cur instanceof Element) { el=(Element)cur; if(SVGToolkit.isElementAShape(el)) { id=el.getAttribute("id"); if(id!=null && ! id.equals("")){ idNodes.add(id); } } } } } return idNodes; } /** * checks if the given node is using a resource and registers it on the canvas if this is such a case * @param node a node */ public void registerUsedResource(Node node){ if(node!=null && node instanceof Element){ //the map of the used resources Map usedResourcesMap=getUsedResources(); //getting the style attribute String style=((Element)node).getAttribute("style"), id=""; if(style!=null && ! style.equals("")){ LinkedList nodesList=null; //for each resource id, checks if it is contained in the style attribute for(Iterator it=usedResourcesMap.keySet().iterator(); it.hasNext();){ try{ id=(String)it.next(); nodesList=(LinkedList)usedResourcesMap.get(id); }catch (Exception ex){id=null; nodesList=null;} //adds the node in the used resource map if(id!=null && ! id.equals("") && style.indexOf("#".concat(id))!=-1 && nodesList!=null && ! nodesList.contains(node)){ addNodeUsingResource(id, node); } } } //registers all the used resources that could be found in the children nodes of this node for(Node cur=node.getFirstChild(); cur!=null; cur=cur.getNextSibling()){ if(cur instanceof Element){ registerUsedResource(cur); } } } } /** * checks if the given node is using a resource and unregisters it in the canvas if this is such a case * @param node a node */ public void unregisterAllUsedResource(Node node){ if(node!=null && node instanceof Element){ //the map of the used resources Map usedResourcesMap=getUsedResources(); //getting the style attribute String style=((Element)node).getAttribute("style"), id=""; if(style!=null && ! style.equals("")){ LinkedList nodesList=null; //for each resource id, checks if it is contained in the style attribute for(Iterator it=usedResourcesMap.keySet().iterator(); it.hasNext();){ try{ id=(String)it.next(); nodesList=(LinkedList)usedResourcesMap.get(id); }catch (Exception ex){id=null; nodesList=null;} //removes the node from the used resource map if(id!=null && ! id.equals("") && style.indexOf("#".concat(id))!=-1 && nodesList!=null && nodesList.contains(node)){ removeNodeUsingResource(id, node); } } } //unregisters all the used resources that could be found in the children nodes of this node for(Node cur=node.getFirstChild(); cur!=null; cur=cur.getNextSibling()){ if(cur instanceof Element){ unregisterAllUsedResource(cur); } } } } /** * scales the given rectangle * @param rectangle the rectangle to scale * @param toBaseScale true to scale it to 100% * false to scale it at the current canvas scale * @return the scaled rectangle */ public Rectangle2D.Double getScaledRectangle(Rectangle2D.Double rectangle, boolean toBaseScale){ Rectangle2D.Double rect=new Rectangle2D.Double(rectangle.getX(), rectangle.getY(), rectangle.getWidth(), rectangle.getHeight()); if(toBaseScale){ //applying the inverse of the transforms AffineTransform af=new AffineTransform(); try{ af.preConcatenate(scrollpane.getSVGCanvas().getRenderingTransform().createInverse()); }catch (Exception ex){} try{ af.preConcatenate(scrollpane.getSVGCanvas().getViewingTransform().createInverse()); }catch (Exception ex){} if(af!=null){ Rectangle2D rect2=af.createTransformedShape(rect).getBounds2D(); rect=new Rectangle2D.Double(rect2.getX(), rect2.getY(), rect2.getWidth(), rect2.getHeight()); } }else{ //applying the transforms AffineTransform af=new AffineTransform(); try{ af.preConcatenate(scrollpane.getSVGCanvas().getViewingTransform()); }catch (Exception ex){} try{ af.preConcatenate(scrollpane.getSVGCanvas().getRenderingTransform()); }catch (Exception ex){} if(af!=null){ Rectangle2D rect2=af.createTransformedShape(rect).getBounds2D(); rect=new Rectangle2D.Double(rect2.getX(), rect2.getY(), rect2.getWidth(), rect2.getHeight()); } } return rect; } /** * scales the given point * @param point the point to scale * @param toBaseScale true to scale it to 100% * false to scale it at the current canvas scale * @return the scaled point */ public Point2D.Double getScaledPoint(Point2D.Double point, boolean toBaseScale){ Point2D.Double point2D=new Point2D.Double(point.getX(), point.getY()); if(point!=null){ if(toBaseScale){ //applying the inverse of the transforms AffineTransform af=new AffineTransform(); try{ af.preConcatenate(scrollpane.getSVGCanvas().getRenderingTransform().createInverse()); }catch (Exception ex){} try{ af.preConcatenate(scrollpane.getSVGCanvas().getViewingTransform().createInverse()); }catch (Exception ex){} if(af!=null){ Point2D pt=af.transform(point, null); point2D=new Point2D.Double(pt.getX(), pt.getY()); } }else{ //applying the inverse of the transforms AffineTransform af=new AffineTransform(); try{ af.preConcatenate(scrollpane.getSVGCanvas().getViewingTransform()); }catch (Exception ex){} try{ af.preConcatenate(scrollpane.getSVGCanvas().getRenderingTransform()); }catch (Exception ex){} if(af!=null){ Point2D pt=af.transform(point, null); point2D=new Point2D.Double(pt.getX(), pt.getY()); } } } return point2D; } /** * picks the color at the given point on a canvas * @param point a point * @return the color corresponding to the given point */ public Color pickColor(Point point){ Color color=new Color(255, 255, 255); if(point!=null){ //getting the offscreen image of the canvas BufferedImage image=getScrollPane().getSVGCanvas().getOffscreen(); int pos=image.getRGB(point.x, point.y); ColorModel model=image.getColorModel(); if(pos!=0){ color=new Color(model.getRed(pos), model.getGreen(pos), model.getBlue(pos)); } } return color; } /** *the class of the state bar * * @author Jordi SUC * */ public class SVGStateBar extends JPanel{ /** * the labels for displayed information */ private JLabel fileName=new JLabel(), zoom=new JLabel() , lx=new JLabel(), ly=new JLabel(), lw=new JLabel(), lh=new JLabel(), infos=new JLabel(); /** * the default font */ private Font myFont=new Font("myFont", Font.ROMAN_BASELINE,10); private String statebarx="", statebary="", statebarw="", statebarh=""; /** * the constructor of the class */ public SVGStateBar(){ setPreferredSize(new Dimension(framePanel.getWidth(), 20)); //gets the labels from the resources ResourceBundle bundle=SVGEditor.getBundle(); if(bundle!=null){ statebarx=bundle.getString("labelx"); statebary=bundle.getString("labely"); statebarw=bundle.getString("labelw"); statebarh=bundle.getString("labelh"); } //Border borderr=new SoftBevelBorder(BevelBorder.RAISED); setBorder(new EmptyBorder(2, 2, 1, 1)); Border border=BorderFactory.createEtchedBorder(); fileName.setBorder(border); zoom.setBorder(border); infos.setBorder(border); JPanel pxy=new JPanel(); pxy.setLayout(new GridLayout(1,2,5,0)); pxy.setBorder(border); pxy.add(lx); pxy.add(ly); JPanel pwh=new JPanel(); pwh.setLayout(new GridLayout(1,2,5,0)); pwh.setBorder(border); pwh.add(lw); pwh.add(lh); fileName.setHorizontalAlignment(SwingConstants.CENTER); zoom.setHorizontalAlignment(SwingConstants.CENTER); lx.setHorizontalAlignment(SwingConstants.RIGHT); ly.setHorizontalAlignment(SwingConstants.LEFT); lw.setHorizontalAlignment(SwingConstants.RIGHT); lh.setHorizontalAlignment(SwingConstants.LEFT); infos.setHorizontalAlignment(SwingConstants.CENTER); setLayout(new GridLayout(1,5,2,2)); if(editor.isMultiWindow()){ fileName.setFont(myFont); zoom.setFont(myFont); lx.setFont(myFont); ly.setFont(myFont); lw.setFont(myFont); lh.setFont(myFont); infos.setFont(myFont); } add(fileName); add(zoom); add(pxy); add(pwh); add(infos); setSVGZoom("100 %"); } /** * sets the name of the SVG picture * @param nm the name of the SVG picture */ public void setSVGName(String nm){ fileName.setText(nm); } /** * sets a string representation of the zoom scale of the SVG picture * @param zm a string representation of the zoom scale of the SVG picture */ public void setSVGZoom(String zm){ zoom.setText(zm); } /** * sets a string representation of the x position of the mouse on the canvas * @param sx a string representation of the x position of the mouse on the canvas */ public void setSVGX(String sx){ if(sx!=null && !sx.equals("")){ lx.setText(statebarx+sx); }else{ lx.setText(""); } } /** * sets a string representation of the y position of the mouse on the canvas * @param sy a string representation of the y position of the mouse on the canvas */ public void setSVGY(String sy){ if(sy!=null && !sy.equals("")){ ly.setText(statebary+sy); }else{ ly.setText(""); } } /** * sets a string representation of the width of a shape * @param sw a string representation of the width of a shape */ public void setSVGW(String sw){ if(sw!=null && !sw.equals("")){ lw.setText(statebarw+sw); }else{ lw.setText(""); } } /** * sets a string representation of the height of a shape * @param sh a string representation of the height of a shape */ public void setSVGH(String sh){ if(sh!=null && !sh.equals("")){ lh.setText(statebarh+sh); }else{ lh.setText(""); } } /** * sets the string representing information to be displayed * @param in a string representing information to be displayed */ public void setSVGInfos(String in){ infos.setText(in); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -