📄 svgnewopen.java
字号:
/* * Created on 23 mars 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.svgfile;import fr.itris.glips.svgeditor.*;import fr.itris.glips.svgeditor.canvas.*;import fr.itris.glips.svgeditor.resources.*;import java.io.*;import javax.swing.*;import javax.swing.event.*;import java.awt.event.*;import java.awt.*;import java.util.*;import java.net.*;/** * @author Jordi SUC * the class that creates the static menu item open in the menu bar */public class SVGNewOpen extends SVGModuleAdapter{ /** * the ids of the module */ final private String idnewopen="NewOpen", idnew="New", idopen="Open", idopenrecent="OpenRecent"; /** * the labels */ private String labelnew="", labelopen="", labelopenrecent="", labeluntitled=""; /** * labels */ private String labelok="", labelcancel="", newopenlabelwidth="", newopenlabelheight="", newwarningmessage="", newwarningtitle="", openwarningmessage="", openwarningtitle="", messageformaterror=""; /** * the menu items */ private JMenuItem newit, open; /** * the openRecent menu */ private JMenu openRecent; /** * the editor */ private SVGEditor editor; /** * the constructor of the class * @param editor the editor */ public SVGNewOpen(SVGEditor editor){ this.editor=editor; //gets the labels from the resources ResourceBundle bundle=SVGEditor.getBundle(); if(bundle!=null){ try{ labelnew=bundle.getString("labelnewopennew"); labelopen=bundle.getString("labelnewopenopen"); labelopenrecent=bundle.getString("labelopenrecent"); labeluntitled=bundle.getString("labeluntitled"); labelok=bundle.getString("labelok"); labelcancel=bundle.getString("labelcancel"); newopenlabelwidth=bundle.getString("labelwidth"); newopenlabelheight=bundle.getString("labelheight"); newwarningmessage=bundle.getString("newwarningmessage"); newwarningtitle=bundle.getString("newwarningtitle"); openwarningmessage=bundle.getString("openwarningmessage"); openwarningtitle=bundle.getString("openwarningtitle"); messageformaterror=bundle.getString("messageformaterror"); }catch (Exception ex){} } //getting the icons ImageIcon newIcon=SVGResource.getIcon("New", false), dnewIcon=SVGResource.getIcon("New", true), openIcon=SVGResource.getIcon("Open", false), dopenIcon=SVGResource.getIcon("Open", true); newit=new JMenuItem(labelnew, newIcon); newit.setDisabledIcon(dnewIcon); newit.setAccelerator(KeyStroke.getKeyStroke("ctrl N")); open=new JMenuItem(labelopen, openIcon); open.setDisabledIcon(dopenIcon); open.setAccelerator(KeyStroke.getKeyStroke("ctrl O")); //adds a listener to the menu item newit.addActionListener(new NewActionListener()); //adds a listener to the menu item open.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ JFileChooser fileChooser=new JFileChooser(); if(getSVGEditor().getResource().getCurrentDirectory()!=null){ fileChooser.setCurrentDirectory(getSVGEditor().getResource().getCurrentDirectory()); } SVGFileFilter fileFilter=new SVGFileFilter(); fileChooser.setFileFilter(fileFilter); fileChooser.setMultiSelectionEnabled(true); int returnVal=fileChooser.showOpenDialog(SVGEditor.getParent()); if(returnVal == JFileChooser.APPROVE_OPTION) { getSVGEditor().getResource().setCurrentDirectory(fileChooser.getCurrentDirectory()); //opens all the selected files File[] files=fileChooser.getSelectedFiles(); for(int i=0;i<files.length;i++){ if(files[i]!=null && fileFilter.acceptFile(files[i])){ open(files[i]); }else if(files[i]!=null){ JOptionPane.showMessageDialog(getSVGEditor().getParent(), messageformaterror); } } } } }); //the openRecent menu openRecent=new JMenu(labelopenrecent); buildOpenRecentMenu(); } /** * @return the editor */ public SVGEditor getSVGEditor(){ return editor; } /** * @return a map associating a menu item id to its menu item object */ public Hashtable getMenuItems(){ Hashtable menuItems=new Hashtable(); menuItems.put(idnew,newit); menuItems.put(idopen,open); menuItems.put(idopenrecent,openRecent); return menuItems; } /** * @return a map associating a tool item id to its tool item object */ public Hashtable getToolItems(){ return null; } /** * Returns the collection of the popup items * @return the collection of the popup items */ public Collection getPopupItems(){ return null; } /** * builds the "open recent" menu */ public void buildOpenRecentMenu(){ openRecent.setEnabled(false); //removes the action listeners of the previous menu items JMenuItem item=null; ActionListener[] tal=null; int i, j; for(i=0;i<openRecent.getItemCount();i++){ try{ item=openRecent.getItem(i); }catch (Exception ex){item=null;} if(item!=null){ tal=item.getActionListeners(); for(j=0;j<tal.length;j++){ item.removeActionListener(tal[j]); } } } openRecent.removeAll(); //gets the paths and adds the menu items to the menu Collection filePaths=getSVGEditor().getResource().getRecentFiles(); String path=null, shortPath=null; for(Iterator it=filePaths.iterator(); it.hasNext();){ try{path=(String)it.next();}catch (Exception ex){path=null;} if(path!=null && ! path.equals("")){ //computing the string that only contains the name of the file try{ shortPath=new File(new URI(path).toString()).getName(); }catch (Exception ex){shortPath=null;ex.printStackTrace();} if(shortPath==null || (shortPath!=null && shortPath.equals(""))){ shortPath=path; } item=new JMenuItem(shortPath); item.setToolTipText(path); final String fpath=path; item.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { SwingUtilities.invokeLater(new Runnable() { public void run() { try{ open(new File(new URI(fpath))); }catch (Exception ex){ex.printStackTrace();} } }); } }); openRecent.add(item); if(! openRecent.isEnabled()){ openRecent.setEnabled(true); } } } } /** * the method called to open a svg file */ public void openAction(){ SwingUtilities.invokeLater(new Runnable() { public void run() { //launches a file chooser JFileChooser fileChooser=new JFileChooser(); fileChooser.setFileFilter(new SVGFileFilter()); fileChooser.setMultiSelectionEnabled(false); int returnVal = fileChooser.showOpenDialog(getSVGEditor().getParent()); if(returnVal == JFileChooser.APPROVE_OPTION) { open(fileChooser.getSelectedFile()); } } }); } /** * opens a new SVG picture * @param file the file to be opened */ public void open(File file){ SVGFrame frame=null; boolean hasSucceeded=false; if(file!=null && file.exists()){ //checking if an svg file having the same name does not already exist String path=file.toURI().toString(); frame=getSVGEditor().getFrameManager().getFrame(path); if(frame!=null){ getSVGEditor().getFrameManager().setCurrentFrame(path); }else{ frame=getSVGEditor().getFrameManager().createFrame(file.toURI().toString()); //sets the uri of the svg file frame.getScrollPane().getSVGCanvas().setURI(file.toURI().toString()); //adding the file name to the list of the recent files getSVGEditor().getResource().addRecentFile(file.toURI().toString()); buildOpenRecentMenu(); } hasSucceeded=true; } //if the file could not be opened, a dialog is displayed to show that an error occured if(! hasSucceeded){ JOptionPane.showMessageDialog(getSVGEditor().getParent(), openwarningmessage, openwarningtitle, JOptionPane.ERROR_MESSAGE); if(frame!=null){ getSVGEditor().getFrameManager().removeFrame(frame.getName()); } //removing the file name from the list of the recent files getSVGEditor().getResource().removeRecentFile(file.toURI().toString()); buildOpenRecentMenu(); } } /** * creates a new document * @param width the width of the new document * @param height the height of the new document */ public void newDoc(String width, String height){ SVGFrame frame=getSVGEditor().getFrameManager().createFrame(labeluntitled); if(frame!=null){ frame.getScrollPane().getSVGCanvas().newDocument(width, height); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -