📄 functiondialog.java
字号:
} //------------------------------------------------------------------ private void init( Window owner, Color colour, String expression ) { // Set icons if ( owner != null ) setIconImages( owner.getIconImages( ) ); //---- Edit panel GridBagLayout gridBag = new GridBagLayout( ); GridBagConstraints gbc = new GridBagConstraints( ); JPanel controlPanel = new JPanel( gridBag ); GuiUtilities.setPaddedLineBorder( controlPanel ); int gridY = 0; // Label: colour JLabel colourLabel = new FLabel( COLOUR_STR ); gbc.gridx = 0; gbc.gridy = gridY; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.LINE_END; gbc.fill = GridBagConstraints.NONE; gbc.insets = AppConstants.COMPONENT_INSETS; gridBag.setConstraints( colourLabel, gbc ); controlPanel.add( colourLabel ); // Button: colour JPanel colourPanel = new JPanel( gridBag ); colourButton = new JButton( new ColourSampleIcon( COLOUR_BUTTON_ICON_WIDTH, COLOUR_BUTTON_ICON_HEIGHT ) ); colourButton.setMargin( COLOUR_BUTTON_MARGINS ); colourButton.setForeground( colour ); colourButton.setActionCommand( Command.CHOOSE_COLOUR ); colourButton.addActionListener( this ); colourLabel.setDisplayedMnemonic( KeyEvent.VK_L ); colourLabel.setLabelFor( colourButton ); gbc.gridx = 0; gbc.gridy = gridY; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.LINE_START; gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets( 0, 0, 0, 0 ); gridBag.setConstraints( colourButton, gbc ); colourPanel.add( colourButton ); // Buttons: function colour int numFunctions = 0; FunctionDocument document = App.getInstance( ).getDocument( ); if ( document != null ) numFunctions = document.getNumFunctions( ); if ( numFunctions > 0 ) { JPanel functionColourPanel = new JPanel( new GridLayout( 1, 0, 2, 0 ) ); functionColourButtons = new ColourButton[numFunctions]; for ( int i = 0; i < functionColourButtons.length; ++i ) { Function function = document.getFunction( i ); functionColourButtons[i] = new ColourButton( function.getColour( ) ); functionColourButtons[i].setToolTipText( function.getExpression( ).toString( ) ); functionColourButtons[i].setActionCommand( Command.SET_TO_FUNCTION_COLOUR + i ); functionColourButtons[i].addActionListener( this ); functionColourPanel.add( functionColourButtons[i] ); } gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.LINE_START; gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets( 0, 6, 0, 0 ); gridBag.setConstraints( functionColourPanel, gbc ); colourPanel.add( functionColourPanel ); } gbc.gridx = 1; gbc.gridy = gridY++; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.LINE_START; gbc.fill = GridBagConstraints.NONE; gbc.insets = AppConstants.COMPONENT_INSETS; gridBag.setConstraints( colourPanel, gbc ); controlPanel.add( colourPanel ); // Label: expression JLabel expressionLabel = new FLabel( EXPRESSION_STR ); gbc.gridx = 0; gbc.gridy = gridY; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.LINE_END; gbc.fill = GridBagConstraints.NONE; gbc.insets = AppConstants.COMPONENT_INSETS; gridBag.setConstraints( expressionLabel, gbc ); controlPanel.add( expressionLabel ); // Field: expression expressionField = new ExpressionField( EXPRESSION_FIELD_LENGTH, expression ); expressionLabel.setDisplayedMnemonic( KeyEvent.VK_E ); expressionLabel.setLabelFor( expressionField ); gbc.gridx = 1; gbc.gridy = gridY++; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.LINE_START; gbc.fill = GridBagConstraints.NONE; gbc.insets = AppConstants.COMPONENT_INSETS; gridBag.setConstraints( expressionField, gbc ); controlPanel.add( expressionField ); //---- Button panel: copy, paste, clear JPanel editButtonPanel = new JPanel( new GridLayout( 1, 0, 8, 0 ) ); // Button: copy JButton copyButton = new FButton( COPY_STR ); copyButton.setMnemonic( KeyEvent.VK_C ); copyButton.setToolTipText( COPY_TOOL_TIP_STR ); copyButton.setActionCommand( Command.COPY ); copyButton.addActionListener( this ); if ( !AppConfig.getInstance( ).hasPermissionAccessClipboard( ) ) copyButton.setEnabled( false ); editButtonPanel.add( copyButton ); // Button: paste pasteButton = new FButton( PASTE_STR ); pasteButton.setMnemonic( KeyEvent.VK_P ); pasteButton.setToolTipText( PASTE_TOOL_TIP_STR ); pasteButton.setActionCommand( Command.PASTE ); pasteButton.addActionListener( this ); pasteButton.setEnabled( false ); flavorsChanged( null ); editButtonPanel.add( pasteButton ); // Button: clear JButton clearButton = new FButton( CLEAR_STR ); clearButton.setMnemonic( KeyEvent.VK_E ); clearButton.setToolTipText( CLEAR_TOOL_TIP_STR ); clearButton.setActionCommand( Command.CLEAR ); clearButton.addActionListener( this ); editButtonPanel.add( clearButton ); //---- Button panel: OK, cancel JPanel okCancelButtonPanel = new JPanel( new GridLayout( 1, 0, 8, 0 ) ); // Button: OK JButton okButton = new FButton( AppConstants.OK_STR ); okButton.setActionCommand( Command.ACCEPT ); okButton.addActionListener( this ); okCancelButtonPanel.add( okButton ); // Button: cancel JButton cancelButton = new FButton( AppConstants.CANCEL_STR ); cancelButton.setActionCommand( Command.CLOSE ); cancelButton.addActionListener( this ); okCancelButtonPanel.add( cancelButton ); //---- Button panel JPanel buttonPanel = new JPanel( gridBag ); buttonPanel.setBorder( BorderFactory.createEmptyBorder( 2, 24, 3, 24 ) ); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.5; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.CENTER; gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets( 0, 0, 0, 12 ); gridBag.setConstraints( editButtonPanel, gbc ); buttonPanel.add( editButtonPanel ); gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.5; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.CENTER; gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets( 0, 12, 0, 0 ); gridBag.setConstraints( okCancelButtonPanel, gbc ); buttonPanel.add( okCancelButtonPanel ); //---- Main panel JPanel mainPanel = new JPanel( gridBag ); mainPanel.setBorder( BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) ); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets( 0, 0, 0, 0 ); gridBag.setConstraints( controlPanel, gbc ); mainPanel.add( controlPanel ); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets( 4, 0, 0, 0 ); gridBag.setConstraints( buttonPanel, gbc ); mainPanel.add( buttonPanel ); //---- Window // Set content pane setContentPane( mainPanel ); // Set orientation of components according to locale App.applyOrientationByLocale( this ); // Add key commands to action map KeyAction.create( mainPanel, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, KEY_COMMANDS, this ); // Add clipboard flavor listener try { getToolkit( ).getSystemClipboard( ).addFlavorListener( this ); } catch ( Exception e ) { // ignore } // Dispose of window explicitly setDefaultCloseOperation( DO_NOTHING_ON_CLOSE ); // Handle window closing addWindowListener( new WindowEventHandler( ) ); // Prevent dialog from being resized setResizable( false ); // Resize dialog to its preferred size pack( ); // Set location of dialog box if ( location == null ) location = GuiUtilities.getComponentLocation( this, owner ); setLocation( location ); // Set default button getRootPane( ).setDefaultButton( okButton ); // Set focus expressionField.requestFocusInWindow( ); // Show dialog setVisible( true ); } //------------------------------------------------------------------ private void validateValues( ) throws AppException { try { expressionField.getExpression( ); } catch ( Expression.Exception e ) { expressionField.requestFocusInWindow( ); expressionField.setCaretPosition( e.getPos( ) ); throw e; } } //------------------------------------------------------------------ private void doChooseColour( ) { Color colour = JColorChooser.showDialog( this, COLOUR_TITLE_STR, colourButton.getForeground( ) ); if ( colour != null ) colourButton.setForeground( colour ); } //------------------------------------------------------------------ private void doSetToFunctionColour( String str ) { colourButton.setForeground( functionColourButtons[Integer.parseInt( str )].getForeground( ) ); } //------------------------------------------------------------------ private void doCopy( ) { try { try { StringSelection selection = new StringSelection( expressionField.getText( ) ); getToolkit( ).getSystemClipboard( ).setContents( selection, selection ); } catch ( IllegalStateException e ) { throw new AppException( ErrorId.CLIPBOARD_UNAVAILABLE, e ); } } catch ( AppException e ) { JOptionPane.showMessageDialog( this, e, App.SHORT_NAME + " : " + CLIPBOARD_ERROR_STR, JOptionPane.ERROR_MESSAGE ); } } //------------------------------------------------------------------ private void doPaste( ) { try { try { expressionField.setText( (String)getToolkit( ).getSystemClipboard( ).getContents( this ). getTransferData( DataFlavor.stringFlavor ) ); } catch ( IllegalStateException e ) { throw new AppException( ErrorId.CLIPBOARD_UNAVAILABLE, e ); } catch ( UnsupportedFlavorException e ) { throw new AppException( ErrorId.NO_TEXT_ON_CLIPBOARD ); } catch ( IOException e ) { throw new AppException( ErrorId.FAILED_TO_GET_CLIPBOARD_DATA, e ); } } catch ( AppException e ) { JOptionPane.showMessageDialog( this, e, App.SHORT_NAME + " : " + CLIPBOARD_ERROR_STR, JOptionPane.ERROR_MESSAGE ); } } //------------------------------------------------------------------ private void doClear( ) { expressionField.setText( null ); } //------------------------------------------------------------------ private void doAccept( ) { try { validateValues( ); accepted = true; doClose( ); } catch ( AppException e ) { JOptionPane.showMessageDialog( this, e, App.SHORT_NAME + " : " + SYNTAX_ERROR_STR, JOptionPane.ERROR_MESSAGE ); } } //------------------------------------------------------------------ private void doClose( ) { try { getToolkit( ).getSystemClipboard( ).removeFlavorListener( this ); } catch ( Exception e ) { // ignore } location = getLocation( ); setVisible( false ); dispose( ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class variables//////////////////////////////////////////////////////////////////////// private static Point location;////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// private ExpressionField expressionField; private JButton colourButton; private ColourButton[] functionColourButtons; private JButton pasteButton; private boolean accepted;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -