📄 svgsaveclose.java
字号:
/* * Created on 23 mars 2004 * ============================================= GNU LESSER GENERAL PUBLIC LICENSE Version 2.1 ============================================= GLIPS Graffiti Editor, a SVG Editor Copyright (C) 2003 Jordi SUC, Philippe Gil, SARL ITRIS This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 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 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact : jordi.suc@itris.fr; philippe.gil@itris.fr ============================================= */package fr.itris.glips.svgeditor.svgfile;import fr.itris.glips.svgeditor.*;import fr.itris.glips.svgeditor.canvas.*;import javax.swing.*;import java.util.*;import java.util.zip.*;import java.awt.event.*;import java.io.*;import org.w3c.dom.*;import java.net.*;import java.nio.*;import java.nio.channels.*;import fr.itris.glips.svgeditor.resources.*;import java.awt.*;/** * @author Jordi SUC, Maciej Wojtkiewicz * the class that creates the static menu item exit in the menu bar */public class SVGSaveClose extends SVGModuleAdapter{ /** * the ids of the module */ final private String idsaveclose="SaveClose", idsave="Save", idsaveas="SaveAs", idclose="Close", idcloseall="CloseAll"; /** * the labels */ private String labelsave="", labelsaveas="", labelsaveall="", labelclose="", labelcloseall=""; /** * labels */ private String savecloseconfirmtitle="", savecloseconfirmmessage="", savewarningmessage="", savewarningtitle="", closeallwarningtitle="", closeallwarningmessage="", savecloseerasetitle="", savecloseerasemessage=""; /** * the editor */ private SVGEditor editor; /** * the menu item that will be added to the menubar */ private JMenuItem save=null, saveAs=null, saveAll=null, close=null, closeAll=null; /** * the constructor of the class * @param editor the editor */ public SVGSaveClose(SVGEditor editor){ this.editor=editor; //gets the labels from the resources ResourceBundle bundle=SVGEditor.getBundle(); if(bundle!=null){ try{ labelsave=bundle.getString("labelsave"); labelsaveas=bundle.getString("labelsaveas"); labelsaveall=bundle.getString("labelsaveall"); labelclose=bundle.getString("labelclose"); labelcloseall=bundle.getString("labelcloseall"); savecloseconfirmtitle=bundle.getString("savecloseconfirmtitle"); savecloseconfirmmessage=bundle.getString("savecloseconfirmmessage"); savewarningmessage=bundle.getString("savewarningmessage"); savewarningtitle=bundle.getString("savewarningtitle"); closeallwarningtitle=bundle.getString("closeallwarningtitle"); closeallwarningmessage=bundle.getString("closeallwarningmessage"); savecloseerasetitle=bundle.getString("erasefiletitle"); savecloseerasemessage=bundle.getString("erasefilemessage"); }catch (Exception ex){} } //a listener that listens to the changes of the SVGFrames final ActionListener svgframeListener=new ActionListener(){ public void actionPerformed(ActionEvent e) { final SVGFrame frame=getSVGEditor().getFrameManager().getCurrentFrame(); if(frame!=null){ //enables the menuitems save.setEnabled(true); saveAs.setEnabled(true); saveAll.setEnabled(true); close.setEnabled(true); closeAll.setEnabled(true); }else{ //disables the menuitems save.setEnabled(false); saveAs.setEnabled(false); saveAll.setEnabled(false); close.setEnabled(false); closeAll.setEnabled(false); } } }; //adds the SVGFrame change listener editor.getFrameManager().addSVGFrameChangedListener(svgframeListener); //getting the icons ImageIcon saveIcon=SVGResource.getIcon("Save", false), dsaveIcon=SVGResource.getIcon("Save", true), saveAsIcon=SVGResource.getIcon("SaveAs", false), dsaveAsIcon=SVGResource.getIcon("SaveAs", true), closeIcon=SVGResource.getIcon("Close", false), dcloseIcon=SVGResource.getIcon("Close", true), closeAllIcon=SVGResource.getIcon("CloseAll", false), dcloseAllIcon=SVGResource.getIcon("CloseAll", true); //handling the menu items save=new JMenuItem(labelsave, saveIcon); save.setDisabledIcon(dsaveIcon); save.setAccelerator(KeyStroke.getKeyStroke("ctrl S")); save.setEnabled(false); saveAs=new JMenuItem(labelsaveas, saveAsIcon); saveAs.setDisabledIcon(dsaveAsIcon); saveAs.setAccelerator(KeyStroke.getKeyStroke("alt ctrl S")); saveAs.setEnabled(false); saveAll=new JMenuItem(labelsaveall); saveAll.setEnabled(false); close=new JMenuItem(labelclose, closeIcon); close.setDisabledIcon(dcloseIcon); close.setAccelerator(KeyStroke.getKeyStroke("ctrl Q")); close.setEnabled(false); closeAll=new JMenuItem(labelcloseall, closeAllIcon); closeAll.setEnabled(false); closeAll.setDisabledIcon(dcloseAllIcon); //adds a listener to the menu item save.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ SVGFrame frame=getSVGEditor().getFrameManager().getCurrentFrame(); if(frame!=null){ saveAction(frame, false, false); } } }); //adds a listener to the menu item saveAs.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ SVGFrame frame=getSVGEditor().getFrameManager().getCurrentFrame(); if(frame!=null){ saveAction(frame, true, false); } } }); close.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ SVGFrame frame=getSVGEditor().getFrameManager().getCurrentFrame(); if(frame!=null){ closeAction(frame); } } } ); final String fcloseallwarningtitle=closeallwarningtitle; final String fcloseallwarningmessage=closeallwarningmessage; closeAll.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { boolean displayDialog=false; Collection<SVGFrame> frames=new LinkedList<SVGFrame>(getSVGEditor().getFrameManager().getFrames()); if(frames!=null && frames.size()>0){ SVGFrame frame=null; for(Iterator it=frames.iterator(); it.hasNext();){ try{ frame=(SVGFrame)it.next(); }catch (Exception ex){frame=null;} if(frame!=null && frame.isModified()){ displayDialog=true; break; } } } int returnVal=-1; if(displayDialog){ //asks the user if the frames should be deleted returnVal=JOptionPane.showConfirmDialog( SVGEditor.getParent(), fcloseallwarningmessage, fcloseallwarningtitle, JOptionPane.YES_NO_OPTION); } //closes all the frames if(returnVal!=JOptionPane.NO_OPTION) { SVGFrame frame=null; for(Iterator it=frames.iterator(); it.hasNext();){ try{ frame=(SVGFrame)it.next(); }catch (Exception ex){frame=null;} if(frame!=null){ final SVGFrame fframe=frame; SwingUtilities.invokeLater(new Runnable() { public void run() { close(fframe); } }); } } } } }); } /** * @return the editor */ public SVGEditor getSVGEditor(){ return editor; } @Override public Hashtable<String, JMenuItem> getMenuItems() { Hashtable<String, JMenuItem> menuItems=new Hashtable<String, JMenuItem>(); menuItems.put(idsave,save); menuItems.put(idsaveas,saveAs); menuItems.put(idclose,close); menuItems.put(idcloseall,closeAll); return menuItems; } /** * closes the frame of the given name */ public void closeAllAction(){ final HashSet<SVGFrame> frames=new HashSet<SVGFrame>(getSVGEditor().getFrameManager().getFrames()); SwingUtilities.invokeLater(new Runnable() { public void run() { for(SVGFrame frame : frames) { closeActionMethod(frame); } } }); } /** * closes the frame of the given name * @param frame a svg frame */ public void closeAction(final SVGFrame frame){ SwingUtilities.invokeLater(new Runnable() { public void run() { closeActionMethod(frame); } }); } /** * the method that closes the given frame * @param frame a frame */ protected void closeActionMethod(SVGFrame frame) { if(frame.isModified()){ //if the file has been modified, a dialog to confirm the close action is shown int returnVal=JOptionPane.showConfirmDialog( SVGEditor.getParent(), savecloseconfirmmessage, savecloseconfirmtitle, JOptionPane.YES_NO_CANCEL_OPTION); if(returnVal==JOptionPane.YES_OPTION){ saveAction(frame, false, true); }else if(returnVal!=JOptionPane.CANCEL_OPTION){ //closes the frame close(frame); } }else { //closes the frame close(frame); } } /** * saves the content of the given frame and closes it if necessary * @param frame a svg frame * @param isSaveAs whether it is a save as action or not * @param isCloseAction */ public void saveAction(final SVGFrame frame, final boolean isSaveAs, final boolean isCloseAction){ SwingUtilities.invokeLater(new Runnable() { public void run() { boolean allowCloseAction=true; if(frame.isModified() || isSaveAs){ File file=null; try{file=new File(new URI(frame.getName()));}catch (Exception ex){file=null;} if(file!=null && ! file.exists()){ file=null; } //if the file has never been saved or a save as action is being done if(file==null || isSaveAs){ //opens a file chooser JFileChooser fileChooser=new JFileChooser(); if(getSVGEditor().getResource().getCurrentDirectory()!=null){ fileChooser.setCurrentDirectory(getSVGEditor().getResource().getCurrentDirectory()); } fileChooser.setFileFilter(new SVGFileFilter()); fileChooser.setMultiSelectionEnabled(false); File selectedFile=null; int rVal=-1; int returnVal=-1; do{ //showing the file chooser returnVal=fileChooser.showSaveDialog(SVGEditor.getParent()); if(returnVal==JFileChooser.APPROVE_OPTION){ getSVGEditor().getResource().setCurrentDirectory(fileChooser.getCurrentDirectory()); selectedFile=fileChooser.getSelectedFile(); //normalizes the selected file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -