📄 svgtext.java
字号:
if(kev.getID()==KeyEvent.KEY_PRESSED){ if(kev.getKeyCode()==KeyEvent.VK_ENTER){ ok.doClick(); }else if(kev.getKeyCode()==KeyEvent.VK_CANCEL){ cancel.doClick(); } } } } }; Toolkit.getDefaultToolkit().addAWTEventListener(keyListener, AWTEvent.KEY_EVENT_MASK); //dealing with the dialog close button dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); pack(); //sets the location of the dialog box int x=(int)(getSVGEditor().getParent().getLocationOnScreen().getX()+getSVGEditor().getParent().getWidth()/2-getSize().getWidth()/2), y=(int)(getSVGEditor().getParent().getLocationOnScreen().getY()+getSVGEditor().getParent().getHeight()/2-getSize().getHeight()/2); setLocation(x,y); } /** * @return the value of the text entered */ protected String getText(){ return textField.getText(); } } /** * used to remove the listener added to draw a rectangle when the user clicks on the menu item */ public void cancelActions(){ if(textAction!=null){ toolItem.removeActionListener(textAction); toolItem.setSelected(false); toolItem.addActionListener(textAction); textAction.cancelActions(); } } /** * * @author Jordi SUC * the class allowing to know the position of the future text */ protected class TextActionListener implements ActionListener{ /** * the hashtable associating a frame to its mouse adapter */ private final Hashtable mouseAdapterFrames=new Hashtable(); /** * an instance of this class */ private final TextActionListener action=this; /** * the cursor used when creating a rectangle */ private Cursor createCursor; private boolean isActive=false; /** * the source component */ private Object source=null; /** * the constructor of the class */ protected TextActionListener(){ createCursor=getSVGEditor().getCursors().getCursor("text"); } /** * resets the listener */ protected void reset(){ if(isActive){ Collection frames=getSVGEditor().getFrameManager().getFrames(); Iterator it; SVGFrame frm=null; LinkedList toBeRemoved=new LinkedList(); Object mouseListener=null; //removes all the motion adapters from the frames for(it=mouseAdapterFrames.keySet().iterator(); it.hasNext();){ try{frm=(SVGFrame)it.next();}catch (Exception ex){frm=null;} if(frm!=null && ! frames.contains(frm)){ try{ mouseListener=mouseAdapterFrames.get(frm); frm.getScrollPane().getSVGCanvas().removeMouseListener((MouseAdapter)mouseListener); frm.getScrollPane().getSVGCanvas().removeMouseMotionListener((MouseMotionListener)mouseListener); }catch (Exception ex){} toBeRemoved.add(frm); } } //removes the frames that have been closed for(it=toBeRemoved.iterator(); it.hasNext();){ try{mouseAdapterFrames.remove(it.next());}catch (Exception ex){} } TextMouseListener tml=null; //adds the new motion adapters for(it=frames.iterator(); it.hasNext();){ try{frm=(SVGFrame)it.next();}catch (Exception ex){frm=null;} if(frm!=null && ! mouseAdapterFrames.containsKey(frm)){ tml=new TextMouseListener(frm); try{ frm.getScrollPane().getSVGCanvas().addMouseListener(tml); frm.getScrollPane().getSVGCanvas().addMouseMotionListener(tml); frm.getScrollPane().getSVGCanvas().setSVGCursor(createCursor); }catch (Exception ex){} mouseAdapterFrames.put(frm, tml); } } } } /** * used to remove the listener added to draw a rectangle when the user clicks on the menu item */ protected void cancelActions(){ if(isActive){ //removes the listeners Iterator it; SVGFrame frm=null; LinkedList toBeRemoved=new LinkedList(); Object mouseListener=null; //removes all the motion adapters from the frames for(it=mouseAdapterFrames.keySet().iterator(); it.hasNext();){ try{frm=(SVGFrame)it.next();}catch (Exception ex){frm=null;} if(frm!=null){ //resets the information displayed frm.getStateBar().setSVGW(""); frm.getStateBar().setSVGH(""); frm.getScrollPane().getSVGCanvas().setSVGCursor(frm.getSVGEditor().getCursors().getCursor("default")); try{ mouseListener=mouseAdapterFrames.get(frm); frm.getScrollPane().getSVGCanvas().removeMouseListener((MouseAdapter)mouseListener); frm.getScrollPane().getSVGCanvas().removeMouseMotionListener((MouseMotionListener)mouseListener); }catch (Exception ex){} toBeRemoved.add(frm); } } //removes the frames that have been closed for(it=toBeRemoved.iterator(); it.hasNext();){ try{mouseAdapterFrames.remove(it.next());}catch (Exception ex){} } isActive=false; } } /** * the action to be done * @param e the event */ public void actionPerformed(ActionEvent e){ if((e.getSource() instanceof JMenuItem && ! toolItem.isSelected()) || (e.getSource() instanceof JToggleButton)){ getSVGEditor().cancelActions(false); if(getSVGEditor().getFrameManager().getCurrentFrame()!=null){ toolItem.removeActionListener(textAction); toolItem.setSelected(true); toolItem.addActionListener(textAction); //the listener is active isActive=true; source=e.getSource(); Collection frames=getSVGEditor().getFrameManager().getFrames(); Iterator it; SVGFrame frm=null; TextMouseListener tml=null; //adds the new motion adapters for(it=frames.iterator(); it.hasNext();){ try{frm=(SVGFrame)it.next();}catch (Exception ex){frm=null;} if(frm!=null){ tml=new TextMouseListener(frm); try{ frm.getScrollPane().getSVGCanvas().addMouseListener(tml); frm.getScrollPane().getSVGCanvas().addMouseMotionListener(tml); frm.getScrollPane().getSVGCanvas().setSVGCursor(createCursor); }catch (Exception ex){} mouseAdapterFrames.put(frm, tml); } } } } } protected class TextMouseListener extends MouseAdapter implements MouseMotionListener{ /** * the points of the area corresponding to the future rectangle */ private Point2D.Double point=null; private SVGFrame frame; /** * the constructor of the class * @param frame */ public TextMouseListener(SVGFrame frame){ this.frame=frame; } /** * @param evt the event */ public void mouseDragged(MouseEvent evt) { } /** * @param evt the event */ public void mouseMoved(MouseEvent evt) { } /** * @param evt the event */ public void mouseReleased(MouseEvent evt){ if(evt.getPoint()!=null){ final Point2D.Double point=frame.getAlignedWithRulersPoint(evt.getPoint()); //gets the text value by displaying a dialog box TextDialog dialog=null; if(getSVGEditor().getParent() instanceof JFrame){ dialog=new TextDialog((JFrame)getSVGEditor().getParent()); }else{ dialog=new TextDialog(new JFrame("")); } dialog.setVisible(true); final String value=dialog.getText(); // attaches the element to the svg root element Runnable runnable=new Runnable(){ public void run() { svgText.drawText(frame, point, value); } }; frame.enqueue(runnable); } getSVGEditor().cancelActions(true); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -