📄 svgnewopen.java
字号:
/** * gets the module's name * @return the module's name */ public String getName(){ return idnewopen; } /** * The class of the action that will be executed when clicking on the "New" menu item */ protected class NewActionListener implements ActionListener{ /** * the dialog box displayed when clicking on the "New" menu item */ private NewDialog dialog; /** * the constructor of the class */ protected NewActionListener(){ if(getSVGEditor().getParent() instanceof JFrame){ dialog=new NewDialog((JFrame)getSVGEditor().getParent()); }else{ dialog=new NewDialog(new JFrame("")); } /*final AWTEventListener keyListener=new AWTEventListener(){ public void eventDispatched(AWTEvent e) { if(e instanceof KeyEvent && dialog.isVisible()){ KeyEvent kev=(KeyEvent)e; 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);*/ } /** * the method called when receiving an event * @param e the event */ public void actionPerformed(ActionEvent e){ //sets the location of the dialog box int x=(int)(getSVGEditor().getParent().getLocationOnScreen().getX()+getSVGEditor().getParent().getWidth()/2-dialog.getSize().getWidth()/2), y=(int)(getSVGEditor().getParent().getLocationOnScreen().getY()+getSVGEditor().getParent().getHeight()/2-dialog.getSize().getHeight()/2); dialog.setLocation(x,y); int res=dialog.showDialog(); if(res==NewDialog.OK){ String width=dialog.getWidthString(), height=dialog.getHeightString(); newDoc(width, height); } } /** * the class of the dialog that will be displayed when cliking on the New menu item */ protected class NewDialog extends JDialog{ /** * the OK constant */ public static final int OK=0; /** * the CANCEL constant */ public static final int CANCEL=1; /** * the width and height */ private String width="400", height="400"; /** * the unit */ private String unit="px"; /** * whether to preserve the ratio aspect or not */ private boolean preserveRatio=false; /** * the ratio */ private double ratio=1; /** * the returned type */ private int returnedRes=CANCEL; /** * the constructor of the class * @param parent the parent frame */ protected NewDialog(JFrame parent){ super(parent, labelnew, true); //creating the combo box used to change the units final JComboBox unitsCombo=getSVGEditor().getSVGToolkit().getUnitsComboBoxChooser(); //the labels for the textfields JLabel widthLabel=new JLabel(newopenlabelwidth.concat(" :")); JLabel heightLabel=new JLabel(newopenlabelheight.concat(" :")); widthLabel.setHorizontalAlignment(SwingConstants.RIGHT); heightLabel.setHorizontalAlignment(SwingConstants.RIGHT); //the textfields that will be used to enter the size values SpinnerNumberModel widthModel=new SpinnerNumberModel(400, 0, Double.MAX_VALUE, 1); SpinnerNumberModel heightModel=new SpinnerNumberModel(400, 0, Double.MAX_VALUE, 1); final JSpinner widthSpinner=new JSpinner(widthModel), heightSpinner=new JSpinner(heightModel); ((JSpinner.DefaultEditor)widthSpinner.getEditor()).getTextField().setColumns(5); ((JSpinner.DefaultEditor)heightSpinner.getEditor()).getTextField().setColumns(5); //the preserve ratio toggle button final JToggleButton preserveRatioButton=new JToggleButton("p"); preserveRatioButton.setSelected(false); //creating the listener to the preserve ratio button preserveRatioButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { preserveRatio=preserveRatioButton.isSelected(); if(preserveRatio){ ratio=((Double)widthSpinner.getValue()).doubleValue()/((Double)heightSpinner.getValue()).doubleValue(); } } }); //the buttons used to cancel or validate the values entered final JButton okButton=new JButton(labelok), cancelButton=new JButton(labelcancel); //adding the listener to the combo unitsCombo.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { if(unitsCombo.getSelectedItem()!=null){ //storing the old units values String oldUnit=unit; double oldWidthValue=SVGToolkit.getPixelledNumber(width+oldUnit); double oldHeightValue=SVGToolkit.getPixelledNumber(height+oldUnit); unit=unitsCombo.getSelectedItem().toString(); //computing the new values double newWidthValue=SVGToolkit.convertFromPixelToUnit(oldWidthValue, unit); double newHeightValue=SVGToolkit.convertFromPixelToUnit(oldHeightValue, unit); //setting the new values width=newWidthValue+""; height=newHeightValue+""; widthSpinner.setValue(newWidthValue); heightSpinner.setValue(newHeightValue); } } }); //adding the listener to the spinners ChangeListener changeListener=new ChangeListener(){ public void stateChanged(ChangeEvent evt) { if(evt.getSource().equals(widthSpinner)){ width=widthSpinner.getValue()+""; if(preserveRatio){ heightSpinner.removeChangeListener(this); double h=((Double)widthSpinner.getValue()).doubleValue()/ratio; height=h+""; heightSpinner.setValue(h); heightSpinner.addChangeListener(this); } }else{ height=heightSpinner.getValue()+""; if(preserveRatio){ widthSpinner.removeChangeListener(this); double w=((Double)heightSpinner.getValue()).doubleValue()*ratio; width=w+""; widthSpinner.setValue(w); widthSpinner.addChangeListener(this); } } } }; widthSpinner.addChangeListener(changeListener); heightSpinner.addChangeListener(changeListener); //adding the listener to the buttons ActionListener buttonsListener=new ActionListener(){ public void actionPerformed(ActionEvent evt) { if(evt.getSource().equals(okButton)){ returnedRes=OK; }else if(evt.getSource().equals(cancelButton)){ returnedRes=CANCEL; } setVisible(false); } }; okButton.addActionListener(buttonsListener); cancelButton.addActionListener(buttonsListener); //building the dialog JPanel contentPanel=new JPanel(); GridBagLayout gridBag=new GridBagLayout(); contentPanel.setLayout(gridBag); GridBagConstraints c=new GridBagConstraints(); c.fill=GridBagConstraints.HORIZONTAL; c.insets=new Insets(2, 2, 2, 2); c.anchor=GridBagConstraints.EAST; c.gridwidth=1; gridBag.setConstraints(widthLabel, c); contentPanel.add(widthLabel); c.anchor=GridBagConstraints.WEST; c.gridwidth=1; gridBag.setConstraints(widthSpinner, c); contentPanel.add(widthSpinner); JLabel wpxLabel=new JLabel(" px"); c.anchor=GridBagConstraints.WEST; c.gridwidth=GridBagConstraints.REMAINDER; c.fill=GridBagConstraints.NONE; gridBag.setConstraints(wpxLabel, c); contentPanel.add(wpxLabel); /*c.anchor=GridBagConstraints.CENTER; c.gridwidth=GridBagConstraints.REMAINDER; c.gridheight=2; gridBag.setConstraints(unitsCombo, c); contentPanel.add(unitsCombo);*/ /*c.anchor=GridBagConstraints.CENTER; c.gridwidth=1; c.gridheight=2; gridBag.setConstraints(unitsCombo, c); contentPanel.add(unitsCombo); c.anchor=GridBagConstraints.CENTER; c.gridwidth=GridBagConstraints.REMAINDER; c.gridheight=2; gridBag.setConstraints(preserveRatioButton, c); contentPanel.add(preserveRatioButton);*/ c.anchor=GridBagConstraints.EAST; c.fill=GridBagConstraints.HORIZONTAL; c.gridwidth=1; gridBag.setConstraints(heightLabel, c); contentPanel.add(heightLabel); c.anchor=GridBagConstraints.WEST; c.gridwidth=1; gridBag.setConstraints(heightSpinner, c); contentPanel.add(heightSpinner); JLabel hpxLabel=new JLabel(" px"); c.anchor=GridBagConstraints.WEST; c.gridwidth=GridBagConstraints.REMAINDER; c.fill=GridBagConstraints.NONE; gridBag.setConstraints(hpxLabel, c); contentPanel.add(hpxLabel); //the buttons panel JPanel buttonsPanel=new JPanel(); buttonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); buttonsPanel.add(okButton); buttonsPanel.add(cancelButton); getContentPane().setLayout(new BorderLayout(0, 4)); getContentPane().add(contentPanel, BorderLayout.CENTER); getContentPane().add(buttonsPanel, BorderLayout.SOUTH); pack(); } /** * @return the width string */ public String getWidthString() { return width+unit; } /** * @return the width string */ public String getHeightString() { return height+unit; } /** * showing the new dialog * @return whether the user clicked on the OK or CANCEL button */ public int showDialog(){ setVisible(true); return returnedRes; } } } /** * cancels all the actions that could be running */ public void cancelActions(){ } /** * layout some elements in the module */ public void initialize(){ }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -