📄 svgvisualresourcegradientcolorchooserchildwidget.java
字号:
content.add(arrowsPanel); //the buttons panel c.gridwidth=1; c.anchor=GridBagConstraints.EAST; gridBag.setConstraints(buttonPanel, c); content.add(buttonPanel); //the gradient panel c.gridwidth=GridBagConstraints.REMAINDER; c.anchor=GridBagConstraints.WEST; gridBag.setConstraints(gradientPanel, c); content.add(gradientPanel); //the properties panel c.gridwidth=GridBagConstraints.REMAINDER; c.anchor=GridBagConstraints.WEST; gridBag.setConstraints(propPanel, c); content.add(propPanel); //setting the component of the widget component=content; disposer=new Runnable(){ public void run() { final SVGFrame frame=svgEditor.getFrameManager().getCurrentFrame(); //sorts the child nodes according to the "offset" attribute value if(frame!=null && fresourceObject!=null && arrowsListener.isModified()){ //sorts the gradient items list Collections.sort(gradientItems, comparator); //removes all the children of the resource node Node cur=null; for(cur=fresourceObject.getResourceNode().getFirstChild(); cur!=null; cur=cur.getNextSibling()){ fresourceObject.getResourceNode().removeChild(cur); } //re-adds all the children in the correct order SVGGradientChooserItem item=null; SVGVisualResourceObjectChild resChild=null; for(Iterator it=gradientItems.iterator(); it.hasNext();){ try{ item=(SVGGradientChooserItem)it.next(); resChild=item.getResourceChildObject(); cur=resChild.getChildElement(); }catch (Exception ex){cur=null;} if(cur!=null){ fresourceObject.getResourceNode().appendChild(cur); } } } gradientItems.clear(); //removes the listener to the mouse events on the arrows panel arrowsPanel.removeMouseListener(arrowsListener); arrowsPanel.removeMouseMotionListener(arrowsListener); //removes the listeners addBt.removeActionListener(addBtListener); deleteBt.removeActionListener(deleteBtListener); colorBt.removeActionListener(colorBtListener); slider.removeMouseListener(sliderListener); slider.removeChangeListener(sliderChangeListener); arrowsListener.dispose(); } }; } } /** * the interface used to listens to the mouse events on an arrows panel * * @author Jordi SUC */ protected interface SVGGradientChooserArrowsListener extends MouseListener, MouseMotionListener{ /** * @return the currently selected item */ public SVGGradientChooserItem getCurrentItem(); /** * the method used to refresh the display of the arrows and the gradient panel */ public void refresh(); /** * creates and adds a gradient item to the list * @param resChild a resource child */ public void createGradientItem(SVGVisualResourceObjectChild resChild); /** * removes a gradient item from the list */ public void removeCurrentGradientItem(); /** * @return the boolean telling if the gradient items have been modified or not */ public boolean isModified(); /** * disposes the object */ public void dispose(); } /** * the items thatwill be displayed to build a gradient * * @author Jordi SUC * */ protected class SVGGradientChooserItem{ /** * the colors used */ private final Color ITEM_COLOR=Color.BLUE, ITEM_FILL_COLOR=new Color(0, 0, 255, 50), ITEM_SELECTED_COLOR=Color.RED, ITEM_SELECTED_FILL_COLOR=new Color(255, 0, 0, 50), BORDER_HIGHLIGHT_COLOR=MetalLookAndFeel.getSeparatorForeground(), BORDER_SHADOW_COLOR=MetalLookAndFeel.getSeparatorBackground(); /** * the resource child */ private SVGVisualResourceObjectChild resChild; /** * the boolean telling if the item is selected */ private boolean isSelected=false; /** * the width of a gradient item */ private int itemWidth=12; /** * the position of the item along the y-axis */ private int xPosition=0; /** * the position of the item along the y-axis */ private int yPosition=0; /** * the color of the item */ private Color color=null; /** * the opacity for the color of the item, a double in the [0..1] interval */ private double opacity=1; /** * the resource object for the position of the color */ private SVGVisualResourceObjectAttribute posResAttribute=null; /** * the resource object for the style of the item */ private SVGVisualResourceObjectAttribute styleResAttribute=null; /** * the size of the arrows panel */ private Dimension arrowsPanelSize=null; /** * the size of the gradient panel */ private Dimension gradientPanelSize=null; /** * the path to be displayed */ private GeneralPath path=null, colorRect=null; /** * the constructor of the class * @param resChild a resource child * @param arrowsPanelSize the size of of the arrows panel * @param gradientPanelSize the size of of the gradient panel */ protected SVGGradientChooserItem(SVGVisualResourceObjectChild resChild, Dimension arrowsPanelSize, Dimension gradientPanelSize){ this.resChild=resChild; if(arrowsPanelSize!=null){ this.yPosition=(int)arrowsPanelSize.height; } this.arrowsPanelSize=arrowsPanelSize; this.gradientPanelSize=gradientPanelSize; //sets the position of the item along the x-axis xPosition=itemWidth/2; if(resChild!=null){ //getting the list of the attributes of the resource child LinkedList attributes=resChild.getAttributes(); if(attributes!=null){ SVGVisualResourceObjectAttribute att=null; //for each attribute, checks if it has one of the following names and then stores it for(int i=0;i<attributes.size();i++){ try{ att=(SVGVisualResourceObjectAttribute)attributes.get(i); }catch (Exception ex){att=null;} if(att!=null){ if(att.getModel().getName().equals("offset")){ posResAttribute=att; }else if(att.getModel().getName().equals("style")){ styleResAttribute=att; } } } } } //getting the values of the item xPosition=computeXPosition(); color=computeColor(); opacity=computeOpacity(); path=new GeneralPath(); path.moveTo(0, 0); path.lineTo(-itemWidth/2, -itemWidth/2); path.lineTo(itemWidth/2, -itemWidth/2); path.lineTo(0, 0); colorRect=new GeneralPath(); colorRect.moveTo(-itemWidth/2, -7); colorRect.lineTo(-itemWidth/2, -17); colorRect.lineTo(itemWidth/2, -17); colorRect.lineTo(itemWidth/2, -7); colorRect.closePath(); } /** * @return the resource child object */ protected SVGVisualResourceObjectChild getResourceChildObject(){ return resChild; } /** * @return the offset of the color computed from the attribute value */ protected int computeXPosition(){ int pos=0; if(posResAttribute!=null && arrowsPanelSize!=null){ String val=posResAttribute.getValue(); //normalizing the string if(val==null){ val=""; } val=val.replaceAll("\\s+", ""); if(val.indexOf("%")!=-1) { val=val.replaceAll("%", ""); int pos2=(int)Double.parseDouble(val); pos=(pos2*(arrowsPanelSize.width-itemWidth))/100+itemWidth/2; }else{ double pos2=Double.parseDouble(val); pos=(int)(pos2*(arrowsPanelSize.width-itemWidth))+itemWidth/2; } } return pos; } /** * @return the offset of the color */ protected int getXPosition(){ return xPosition; } /** * @return the offset of the color in the percentage format */ protected int getXPositionPercent(){ int xPos=0; if(arrowsPanelSize!=null){ double percent=100*(xPosition-itemWidth/2)/(arrowsPanelSize.width-itemWidth); xPos=(int)percent; } return xPos; } /** * sets the offset of the color * @param xPosition the x position */ protected synchronized void setXPosition(int xPosition){ if(xPosition<itemWidth/2){ xPosition=itemWidth/2; } if(xPosition>(arrowsPanelSize.width-itemWidth/2)){ xPosition=arrowsPanelSize.width-itemWidth/2; } this.xPosition=xPosition; } /** * sets the value of the offset in the attribute */ protected void applyXPosition(){ if(posResAttribute!=null && arrowsPanelSize!=null){ double percent=100*(xPosition-itemWidth/2)/(arrowsPanelSize.width-itemWidth); posResAttribute.setValue(format.format((int)percent)+"%"); } } /** * @return the color */ protected Color getColor(){ return color; } /** * @return the opacity a double in the [0..1] interval */ protected double getOpacity(){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -