📄 svggroupungroup.java
字号:
/* * Created on 28 avr. 2004 * ============================================= GNU LESSER GENERAL PUBLIC LICENSE Version 2.1 =============================================GLIPS Graffiti Editor, a SVG EditorCopyright (C) 2003 Jordi SUC, Philippe Gil, SARL ITRISThis library is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General PublicLicense as published by the Free Software Foundation; eitherversion 2.1 of the License, or (at your option) any later version.This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNULesser General Public License for more details.You should have received a copy of the GNU Lesser General PublicLicense along with this library; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USAContact : jordi.suc@itris.fr; philippe.gil@itris.fr ============================================= */package fr.itris.glips.svgeditor.domactions;import fr.itris.glips.svgeditor.*;import fr.itris.glips.svgeditor.canvas.*;import fr.itris.glips.svgeditor.menutool.*;import fr.itris.glips.svgeditor.selection.*;import fr.itris.glips.svgeditor.undoredo.*;import javax.swing.*;import java.awt.event.*;import java.util.*;import org.w3c.dom.*;import org.w3c.dom.svg.*;import fr.itris.glips.svgeditor.resources.*;/** * @author Jordi SUC * the class allowing to group or ungroup nodes in the svg document */public class SVGGroupUnGroup extends SVGModuleAdapter{ /** * the ids of the module */ final private String idgroupungroup="GroupUnGroup", idgroupug="Group", idgungroup="UnGroup"; /** * the labels */ private String labelgroupug="", labelgungroup=""; /** * the undo/redo labels */ private String undoredogroupug="", undoredogungroup=""; /** * the menu items that will be contained in the menu */ private JMenuItem group, ungroup; /** * the action listeners */ private ActionListener groupListener=null, ungroupListener=null; /** * the editor */ private SVGEditor editor=null; /** * the nodes that are currently selected */ private LinkedList<Element> selectednodes=new LinkedList<Element>(); /** * the constructor of the class * @param editor the editor */ public SVGGroupUnGroup(SVGEditor editor) { this.editor=editor; //gets the labels from the resources ResourceBundle bundle=SVGEditor.getBundle(); if(bundle!=null){ try{ labelgroupug=bundle.getString("labelgroupug"); labelgungroup=bundle.getString("labelgungroup"); undoredogroupug=bundle.getString("undoredogroupug"); undoredogungroup=bundle.getString("undoredogungroup"); }catch (Exception ex){} } //getting the icons ImageIcon groupIcon=SVGResource.getIcon("Group", false), dgroupIcon=SVGResource.getIcon("Group", true), ungroupIcon=SVGResource.getIcon("Ungroup", false), dungroupIcon=SVGResource.getIcon("Ungroup", true); //creates the menu items, sets the keyboard shortcuts group=new JMenuItem(labelgroupug, groupIcon); group.setDisabledIcon(dgroupIcon); group.setAccelerator(KeyStroke.getKeyStroke("ctrl G")); group.setEnabled(false); ungroup=new JMenuItem(labelgungroup, ungroupIcon); ungroup.setDisabledIcon(dungroupIcon); ungroup.setAccelerator(KeyStroke.getKeyStroke("ctrl U")); ungroup.setEnabled(false); //a listener that listens to the changes of the SVGFrames final ActionListener svgframeListener=new ActionListener(){ /** * a listener on the selection changes */ private ActionListener selectionListener=null; /** * the current selection module */ private SVGSelection selection=null; public void actionPerformed(ActionEvent e) { //clears the list of the selected items selectednodes.clear(); //disables the menuitems group.setEnabled(false); ungroup.setEnabled(false); final SVGFrame frame=getSVGEditor().getFrameManager().getCurrentFrame(); //if a selection listener is already registered on a selection module, it is removed if(selection!=null && selectionListener!=null){ selection.removeSelectionListener(selectionListener); } //gets the current selection module if(frame!=null){ selection=getSVGEditor().getSVGSelection(); } if(frame!=null && selection!=null){ manageSelection(); //the listener of the selection changes selectionListener=new ActionListener(){ public void actionPerformed(ActionEvent evt) { manageSelection(); } }; //adds the selection listener if(selectionListener!=null){ selection.addSelectionListener(selectionListener); } } } /** * updates the selected items and the state of the menu items */ protected void manageSelection(){ //disables the menuitems group.setEnabled(false); ungroup.setEnabled(false); LinkedList<Element> list=null; //gets the currently selected nodes list if(selection!=null){ list=selection.getCurrentSelection(getSVGEditor().getFrameManager().getCurrentFrame()); } selectednodes.clear(); //refresh the selected nodes list if(list!=null){ selectednodes.addAll(list); } if(selectednodes.size()>0){ group.setEnabled(true); //checks if all the selected nodes are g elements to ungroup them all LinkedList<Element> snodes=new LinkedList<Element>(selectednodes); Node current=null; boolean canBeEnabled=true; for(Iterator it=snodes.iterator(); it.hasNext();){ try{ current=(Node)it.next(); }catch (Exception ex){current=null;} if(current!=null){ canBeEnabled=canBeEnabled && current.getNodeName().equals("g"); } } if(canBeEnabled){ ungroup.setEnabled(true); } } } }; //adds the SVGFrame change listener editor.getFrameManager().addSVGFrameChangedListener(svgframeListener); //creating and adding the group menu item listener groupListener=new ActionListener(){ public void actionPerformed(ActionEvent e) { if(getSVGEditor().getSVGSelection()!=null && ! getSVGEditor().getSVGSelection().isActing()){ getSVGEditor().cancelActions(true); if(selectednodes.size()>0){ group(selectednodes); //sets that the svg has been modified getSVGEditor().getFrameManager().getCurrentFrame().setModified(true); } } } }; group.addActionListener(groupListener); //creating and adding the ungroup menu item listener ungroupListener=new ActionListener(){ public void actionPerformed(ActionEvent e) { if(getSVGEditor().getSVGSelection()!=null && ! getSVGEditor().getSVGSelection().isActing()){ getSVGEditor().cancelActions(true); if(selectednodes.size()>0){ ungroup(selectednodes); //sets that the svg has been modified getSVGEditor().getFrameManager().getCurrentFrame().setModified(true); } } } }; ungroup.addActionListener(ungroupListener); } /** * @return the editor */ public SVGEditor getSVGEditor(){ return editor; } @Override public Hashtable<String, JMenuItem> getMenuItems() { Hashtable<String, JMenuItem> menuItems=new Hashtable<String, JMenuItem>(); menuItems.put(idgroupug, group); menuItems.put(idgungroup, ungroup); return menuItems; } @Override public Collection<SVGPopupItem> getPopupItems() { LinkedList<SVGPopupItem> popupItems=new LinkedList<SVGPopupItem>(); //creating the group popup item SVGPopupItem item=new SVGPopupItem(getSVGEditor(), idgroupug, labelgroupug, "Group"){ @Override public JMenuItem getPopupItem(LinkedList nodes) { if(nodes!=null && nodes.size()>0){ menuItem.setEnabled(true); //adds the action listeners menuItem.addActionListener(groupListener); }else{ menuItem.setEnabled(false); } return super.getPopupItem(nodes); } }; popupItems.add(item); //creating the ungroup popup item item=new SVGPopupItem(getSVGEditor(), idgungroup, labelgungroup, "Ungroup"){ @Override public JMenuItem getPopupItem(LinkedList nodes) { if(nodes!=null && nodes.size()>0){ //checks if all the selected nodes are g elements to ungroup them all Node current=null; boolean canBeEnabled=true; for(Iterator it=nodes.iterator(); it.hasNext();){ try{current=(Node)it.next();}catch (Exception ex){current=null;} if(current!=null){ canBeEnabled=canBeEnabled && current.getNodeName().equals("g"); } } if(canBeEnabled){ menuItem.setEnabled(true); //adds the action listeners menuItem.addActionListener(ungroupListener); }else{ menuItem.setEnabled(false); } }else{ menuItem.setEnabled(false); } return super.getPopupItem(nodes); } }; popupItems.add(item); return popupItems; } /** * groups the nodes in the given list * @param list the nodes to be grouped */ protected void group(LinkedList<Element> list){ final SVGFrame frame=editor.getFrameManager().getCurrentFrame(); if(list!=null && list.size()>0 && frame!=null){ final Document doc=frame.getScrollPane().getSVGCanvas().getDocument(); //the selected nodes final LinkedList<Element> snodes=new LinkedList<Element>(list); //getting the parent node Node p=null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -