📄 functiondocument.java
字号:
updateView( ViewComponent.BUTTONS ); // Update tab text and title and menus in main window MainWindow mainWindow = App.getInstance( ).getMainWindow( ); if ( mainWindow != null ) { App.getInstance( ).updateTabText( this ); mainWindow.updateTitleAndMenus( ); } } //------------------------------------------------------------------ private FunctionView getView( ) { return App.getInstance( ).getView( this ); } //------------------------------------------------------------------ private FunctionView.FunctionList getFunctionList( ) { return getView( ).getFunctionList( ); } //------------------------------------------------------------------ private int getSelectedIndex( ) { return ( (getView( ) == null) ? -1 : getFunctionList( ).getSelectedIndex( ) ); } //------------------------------------------------------------------ private void setXInterval( PlotInterval interval ) { xInterval = new PlotInterval( interval ); updateView( ViewComponent.X_INTERVAL ); } //------------------------------------------------------------------ private void setYInterval( PlotInterval interval ) { yInterval = new PlotInterval( interval ); updateView( ViewComponent.Y_INTERVAL ); } //------------------------------------------------------------------ private Edit setXIntervalEdit( PlotInterval interval ) { Edit edit = null; if ( interval.isValid( ) ) { edit = new Edit.XInterval( xInterval, interval ); setXInterval( interval ); } else Toolkit.getDefaultToolkit( ).beep( ); return edit; } //------------------------------------------------------------------ private Edit setYIntervalEdit( PlotInterval interval ) { Edit edit = null; if ( interval.isValid( ) ) { edit = new Edit.YInterval( yInterval, interval ); setYInterval( interval ); } else Toolkit.getDefaultToolkit( ).beep( ); return edit; } //------------------------------------------------------------------ private Edit setIntervals( PlotInterval xInterval, PlotInterval yInterval, boolean end ) { Edit.Compound edit = null; if ( xStartInterval == null ) xStartInterval = new PlotInterval( this.xInterval ); if ( yStartInterval == null ) yStartInterval = new PlotInterval( this.yInterval ); if ( xInterval.isValid( ) && yInterval.isValid( ) ) { if ( end && (!xStartInterval.equals( xInterval ) || !yStartInterval.equals( yInterval )) ) { edit = new Edit.Compound( ); edit.addEdit( new Edit.XInterval( xStartInterval, xInterval ) ); edit.addEdit( new Edit.YInterval( yStartInterval, yInterval ) ); } setXInterval( xInterval ); setYInterval( yInterval ); } else Toolkit.getDefaultToolkit( ).beep( ); if ( end ) { xStartInterval = null; yStartInterval = null; } return edit; } //------------------------------------------------------------------ private boolean isXml( CharSequence text ) { return ( (text.length( ) >= XML_PREFIX.length( )) && text.subSequence( 0, XML_PREFIX.length( ) ).toString( ).equals( XML_PREFIX ) ); } //------------------------------------------------------------------ private boolean hasCommentNodes( Node node ) { NodeList childNodes = node.getChildNodes( ); for ( int i = 0; i < childNodes.getLength( ); ++i ) { node = childNodes.item( i ); if ( (node.getNodeType( ) == Node.COMMENT_NODE) || hasCommentNodes( node ) ) return true; } return false; } //------------------------------------------------------------------ private void parseXml( StringBuilder text ) throws AppException { // Convert file to DOM document Document document = XmlUtilities.createDOMDocument( text.toString( ) ); // Test root element Element element = document.getDocumentElement( ); String elementPath = ElementName.FUNCTION_LIST; if ( !element.getNodeName( ).equals( elementPath ) ) throw new FileException( ErrorId.INCORRECT_DOCUMENT_FORMAT, file ); // Attribute: version String paramName = elementPath + "." + AttrName.VERSION; String paramValue = XmlUtilities.getAttribute( element, AttrName.VERSION ); if ( paramValue == null ) throw new XmlParseException( ErrorId.NO_ATTRIBUTE, file, paramName ); try { int version = Integer.parseInt( paramValue ); if ( version < 0 ) throw new NumberFormatException( ); if ( version > MAX_SUPPORTED_VERSION ) { String[] substStrs = { paramValue }; throw new FileException( ErrorId.UNSUPPORTED_DOCUMENT_VERSION, file, substStrs ); } } catch ( NumberFormatException e ) { throw new XmlParseException( ErrorId.INVALID_ATTRIBUTE, file, paramName, paramValue ); } // Attribute: x interval paramName = elementPath + "." + AttrName.X_INTERVAL; paramValue = XmlUtilities.getAttribute( element, AttrName.X_INTERVAL ); if ( paramValue != null ) { try { xInterval = parseInterval( paramValue ); } catch ( AppException e ) { throw new XmlParseException( e.getId( ), file, paramName, paramValue ); } } // Attribute: y interval paramName = elementPath + "." + AttrName.Y_INTERVAL; paramValue = XmlUtilities.getAttribute( element, AttrName.Y_INTERVAL ); if ( paramValue != null ) { try { yInterval = parseInterval( paramValue ); } catch ( AppException e ) { throw new XmlParseException( e.getId( ), file, paramName, paramValue ); } } // Parse function elements int colourIndex = 0; try { boolean hasComment = false; NodeList childNodes = element.getChildNodes( ); for ( int i = 0; i < childNodes.getLength( ); ++i ) { Node node = childNodes.item( i ); if ( node.getNodeType( ) == Node.ELEMENT_NODE ) { String elementName = node.getNodeName( ); if ( elementName.equals( Comment.getElementName( ) ) ) { if ( hasComment ) throw new FileException( ErrorId.MULTIPLE_COMMENT_ELEMENTS, file ); comment = new Comment( (Element)node ); hasComment = true; continue; } if ( node.getNodeName( ).equals( ElementName.FUNCTION ) ) { if ( isFull( ) ) throw new FileException( ErrorId.TOO_MANY_FUNCTIONS, file ); Color defaultColour = AppConfig.getInstance( ).getFunctionColour( colourIndex++ ); functions.add( parseFunction( (Element)node, defaultColour ) ); continue; } } } } catch ( XmlParseException e ) { throw new XmlParseException( e, file ); } // Test for comments commented = hasCommentNodes( document ); } //------------------------------------------------------------------ private Function parseFunction( Element element, Color defaultColour ) throws XmlParseException { String elementPath = XmlUtilities.getElementPath( element ); // Attribute: expression String paramName = elementPath + "." + AttrName.EXPRESSION; String paramValue = XmlUtilities.getAttribute( element, AttrName.EXPRESSION ); if ( paramValue == null ) throw new XmlParseException( ErrorId.NO_ATTRIBUTE, paramName ); Expression expression = null; try { expression = new Expression( paramValue ); } catch ( Expression.Exception e ) { throw new XmlParseException( ErrorId.INVALID_ATTRIBUTE, paramName, paramValue ); } // Attribute: colour paramName = elementPath + "." + AttrName.COLOUR; paramValue = XmlUtilities.getAttribute( element, AttrName.COLOUR ); Color colour = defaultColour; if ( paramValue != null ) { try { colour = ColourUtilities.parseColour( paramValue ); } catch ( IllegalArgumentException e ) { throw new XmlParseException( ErrorId.INVALID_ATTRIBUTE, paramName, paramValue ); } catch ( IndexOutOfBoundsException e ) { throw new XmlParseException( ErrorId.ATTRIBUTE_OUT_OF_BOUNDS, paramName, paramValue ); } } return new Function( colour, expression ); } //------------------------------------------------------------------ private void parseText( StringBuilder text, List<String> errorStrs ) throws AppException { int commentIndent = 0; StringBuilder commentBuffer = new StringBuilder( ); int colourIndex = 0; int lineNum = 0; int fileIndex = 0; String line = null; TextState state = TextState.COMMENT_FIRST_LINE; while ( state != TextState.DONE ) { // Get next line from file while ( line == null ) { if ( fileIndex < text.length( ) ) { int startIndex = fileIndex; fileIndex = text.indexOf( "\n", fileIndex ); if ( fileIndex < 0 ) fileIndex = text.length( ); line = text.substring( startIndex, fileIndex ); ++fileIndex; ++lineNum; if ( line.length( ) == 0 ) line = null; } else { state = TextState.DONE; break; } } // Process line switch ( state ) { case COMMENT_FIRST_LINE: if ( line.charAt( 0 ) == COMMENT_PREFIX_CHAR ) { for ( int i = 1; i < line.length( ); ++i ) { if ( line.charAt( i ) != ' ' ) break; ++commentIndent; } commentBuffer.append( line.substring( commentIndent + 1 ) ); line = null; state = TextState.COMMENT; } else state = TextState.STATEMENT; break; case COMMENT: if ( line.charAt( 0 ) == COMMENT_PREFIX_CHAR ) { int endIndex = Math.min( commentIndent + 1, line.length( ) ); int index = 1; while ( index < endIndex ) { if ( line.charAt( index ) != ' ' ) break; ++index; } commentBuffer.append( '\n' ); commentBuffer.append( line.substring( index ) ); line = null; } else state = TextState.STATEMENT; break; case STATEMENT: { // Strip any comment int index = line.indexOf( COMMENT_PREFIX_CHAR ); if ( index >= 0 ) { commented = true; line = line.substring( 0, index ); } // Strip leading and trailing spaces and control chars line = line.trim( ); // Set next state according to type of statement if ( line.length( ) == 0 ) line = null; else state = (line.charAt( 0 ) == INTERVAL_PREFIX_CHAR) ? TextState.INTERVAL : TextState.FUNCTION; break; } case INTERVAL: try { if ( line.length( ) < 2 ) throw new AppException( ErrorId.INVALID_INTERVAL_IDENTIFIER ); switch ( Character.toLowerCase( line.charAt( 1 ) ) ) { case 'x': xInterval = parseInterval( line.substring( 2 ) ); break; case 'y': yInterval = parseInterval( l
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -