📄 svgtext.java
字号:
/* * Created on 25 mai 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.shape;import fr.itris.glips.svgeditor.*;import fr.itris.glips.svgeditor.canvas.*;import fr.itris.glips.svgeditor.selection.*;import fr.itris.glips.svgeditor.undoredo.*;import org.w3c.dom.svg.*;import org.w3c.dom.*;import javax.swing.*;import javax.swing.border.*;import java.awt.event.*;import java.awt.geom.*;import java.awt.*;import java.text.*;import java.util.*;import fr.itris.glips.svgeditor.resources.*;/** * @author Jordi SUC * the class that allows to add, select,modify the properties, delete, transform a text on the canvas */public class SVGText extends SVGShape{ /** * the reference of an object of this class */ private final SVGText svgText=this; /** * the action listener used to draw the text */ private TextActionListener textAction=null; /** * the constructor of the class * @param editor the editor */ public SVGText(SVGEditor editor) { super(editor); ids.put("id","text"); ids.put("idmenuitem","Text"); //gets the labels from the resources ResourceBundle bundle=SVGEditor.getBundle(); if(bundle!=null){ try{ labels.put("label", bundle.getString("shapetextlabel")); labels.put("undoredocreate", bundle.getString("shapetextundoredocreate")); labels.put("undoredotranslate", bundle.getString("shapeundoredotranslate")); labels.put("undoredoresize", bundle.getString("shapetextundoredoresize")); labels.put("undoredorotate", bundle.getString("shapetextundoredorotate")); labels.put("helpcreate", bundle.getString("shapetexthelpcreate")); labels.put("initdialogtitle", bundle.getString("shapetextinitdialogtitle")); labels.put("initdialoghelp", bundle.getString("shapetextinitdialoghelp")); labels.put("initok", bundle.getString("labelok")); labels.put("initcancel", bundle.getString("labelcancel")); }catch (Exception ex){} } //the icons icon=SVGResource.getIcon((String)ids.get("idmenuitem"), false); disabledIcon=SVGResource.getIcon((String)ids.get("idmenuitem"), true); //the menu item menuitem=new JMenuItem((String)labels.get("label"), icon); menuitem.setDisabledIcon(disabledIcon); menuitem.setEnabled(false); textAction=new TextActionListener(); //the toggle button toolItem=new JToggleButton(disabledIcon); toolItem.setEnabled(false); toolItem.setToolTipText((String)labels.get("label")); //adds a listener to the menu item and the toggle button menuitem.addActionListener(textAction); toolItem.addActionListener(textAction); //a listener that listens to the changes of the SVGFrames final ActionListener svgframeListener=new ActionListener(){ public void actionPerformed(ActionEvent e) { if(getSVGEditor().getFrameManager().getFrameNumber()>0){ menuitem.setEnabled(true); //menuitem.setIcon(icon); toolItem.setEnabled(true); toolItem.setIcon(icon); }else{ menuitem.setEnabled(false); //menuitem.setIcon(disabledIcon); toolItem.setEnabled(false); toolItem.setIcon(disabledIcon); } textAction.reset(); } }; //adds the SVGFrame change listener editor.getFrameManager().addSVGFrameChangedListener(svgframeListener); } /** * @return a map associating a menu item id to its menu item object */ public Hashtable getMenuItems(){ Hashtable menuItems=new Hashtable(); menuItems.put((String)ids.get("idmenuitem"), menuitem); return menuItems; } /** * @return a map associating a tool item id to its tool item object */ public Hashtable getToolItems(){ Hashtable toolItems=new Hashtable(); toolItems.put((String)ids.get("idmenuitem"), toolItem); return toolItems; } /** * draws a text * @param frame the current SVGFrame * @param point the clicked point * @param value the value of the text to be drawn */ protected void drawText(SVGFrame frame, Point2D.Double point, String value){ if(frame!=null && point!=null){ Document doc=frame.getScrollPane().getSVGCanvas().getDocument(); if(getSVGEditor().getSVGSelection()!=null && doc!=null){ final Element parent=getSVGEditor().getSVGSelection().getCurrentParentElement(frame); if(parent!=null){ DecimalFormatSymbols symbols=new DecimalFormatSymbols(); symbols.setDecimalSeparator('.'); DecimalFormat format=new DecimalFormat("######.#",symbols); if(value!=null && !value.equals("")){ // creates the text element final Element text = doc.createElementNS(doc.getDocumentElement().getNamespaceURI(),"text"); Text textValue=doc.createTextNode(value); text.appendChild(textValue); text.setAttributeNS(null,"x", format.format(point.x)); text.setAttributeNS(null,"y", format.format(point.y)); String colorString=getSVGEditor().getColorChooser().getColorString(getSVGEditor().getSVGColorManager().getCurrentColor()); text.setAttributeNS(null, "style", "font-size:12pt;fill:".concat(colorString.concat(";"))); //attaches the element to the svg root element parent.appendChild(text); //sets that the svg has been modified frame.setModified(true); //creates final variables final Document fdoc=doc; final Node ftext=text; final SVGFrame fframe=frame; //create the undo/redo action and insert it into the undo/redo stack if(getSVGEditor().getUndoRedo()!=null){ SVGUndoRedoAction action=new SVGUndoRedoAction((String)labels.get("undoredocreate")){ public void undo(){ parent.removeChild(text); } public void redo(){ parent.appendChild(text); } }; SVGSelection selection=getSVGEditor().getSVGSelection(); if(selection!=null){ selection.deselectAll(frame, false, true); selection.addUndoRedoAction(frame, action); selection.handleNodeSelection(frame, text); selection.addUndoRedoAction(frame, new SVGUndoRedoAction((String)labels.get("undoredocreate")){}); selection.refreshSelection(frame); }else{ SVGUndoRedoActionList actionlist=new SVGUndoRedoActionList((String)labels.get("undoredocreate")); actionlist.add(action); getSVGEditor().getUndoRedo().addActionList(frame, actionlist); } } } } } } } /** * @author Jordi SUC * * A class allowing to enter the initial value of a text */ protected class TextDialog extends JDialog{ /** * the text field in which the text will be entered */ private JTextField textField=new JTextField(15); protected final JDialog dialog=this; /** * the constructor of the class */ protected TextDialog(JFrame parent){ super(parent, (String)labels.get("initdialogtitle"), true); //the content pane the dialog Container pane=getContentPane(); pane.setLayout(new BorderLayout()); //creates the widgets used to enter the text value JPanel panel=new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); JLabel label=new JLabel((String)labels.get("initdialoghelp")+" : "); panel.add(label); panel.add(textField); panel.setBorder(new EmptyBorder(10,10,5,10)); //creates the ok and cancel buttons JPanel buttons=new JPanel(); final JButton ok=new JButton((String)labels.get("initok")), cancel=new JButton((String)labels.get("initcancel")); buttons.setLayout(new FlowLayout(FlowLayout.CENTER,5,5)); buttons.add(ok); buttons.add(cancel); buttons.setBorder(new EmptyBorder(0,10,5,10)); pane.add(panel, BorderLayout.CENTER); pane.add(buttons, BorderLayout.SOUTH); //creating the listeners final ActionListener buttonsListener=new ActionListener(){ public void actionPerformed(ActionEvent evt) { if( ! evt.getSource().equals(ok)){ textField.setText(""); } dialog.setVisible(false); ok.removeActionListener(this); cancel.removeActionListener(this); } }; //adds the listeners ok.addActionListener(buttonsListener); cancel.addActionListener(buttonsListener); final AWTEventListener keyListener=new AWTEventListener(){ public void eventDispatched(AWTEvent e) { if(e instanceof KeyEvent && dialog.isVisible()){ KeyEvent kev=(KeyEvent)e;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -