Package org.openquark.cal.compiler

Examples of org.openquark.cal.compiler.PreludeTypeConstants


        Object newValue = null;

        Class<? extends ValueNode> handlerClass = valueNodeBuilderHelper.getValueNodeClass(destTypeExpr);
       
        PreludeTypeConstants typeConstants = valueNodeBuilderHelper.getPreludeTypeConstants();

        if (handlerClass.equals(ListOfCharValueNode.class) || destTypeExpr.sameType(typeConstants.getStringType())) {

            if (sourceVN instanceof ListValueNode && sourceVN.getTypeExpr().sameType(typeConstants.getCharListType())) {
                // Converting a List to a List of Char: we convert each of the list values

                ListValueNode listValueNode = (ListValueNode)sourceVN;

                StringBuilder sb = new StringBuilder();
                int nElements = listValueNode.getNElements();
                for (int i = 0; i < nElements; i++) {
                    sb.append(listValueNode.getValueAt(i).getCALValue());
                }

                newValue = sb.toString();

            } else {
                newValue = sourceVN.getTextValue();
            }

        } else if (destTypeExpr.sameType(typeConstants.getCharType())) {
           
            // Just take the first character of the text value (if any)
            String textValue = sourceVN.getTextValue();
            if (textValue.length() != 0) {
                newValue = Character.valueOf(textValue.charAt(0));
            }
        }

        if (sourceVN instanceof LiteralValueNode && !sourceVN.getTypeExpr().sameType(typeConstants.getStringType())) {

            LiteralValueNode sourceLiteralVN = (LiteralValueNode) sourceVN;

            if (sourceTypeExpr.sameType(typeConstants.getDoubleType()) &&
                destTypeExpr.sameType(typeConstants.getIntType())) {

                newValue = Integer.valueOf((int) Math.round(sourceLiteralVN.getDoubleValue().doubleValue()));

            } else if (sourceTypeExpr.sameType(typeConstants.getIntType()) &&
                       destTypeExpr.sameType(typeConstants.getDoubleType())) {

                newValue = Double.valueOf(sourceLiteralVN.getIntegerValue().intValue());
            }

        } else if (sourceVN instanceof RelativeDateTimeValueNode) {

            RelativeDateTimeValueNode sourceDateTimeVN = (RelativeDateTimeValueNode) sourceVN;

            if (handlerClass.equals(RelativeDateValueNode.class) || handlerClass.equals(RelativeTimeValueNode.class)) {

                // TEMP: May need to copy.
                newValue = sourceDateTimeVN.getDateTimeValue();
            }

        } else if (sourceVN instanceof ListOfCharValueNode || sourceVN.getTypeExpr().sameType(typeConstants.getStringType())) {

            String stringVal = sourceVN instanceof ListOfCharValueNode ?
                                ((ListOfCharValueNode)sourceVN).getStringValue() :
                                ((LiteralValueNode)sourceVN).getStringValue();

            if (handlerClass.equals(RelativeDateValueNode.class)) {

                DateFormat dateFormat = RelativeTemporalValueNode.getDateFormat(DateFormat.FULL, -1);

                try {

                    newValue = dateFormat.parse(stringVal);

                } catch (ParseException pe) {

                    // Okay, can't parse.  Don't transform.
                }

            } else if (handlerClass.equals(RelativeTimeValueNode.class)) {

                DateFormat timeFormat = RelativeTemporalValueNode.getDateFormat(-1, DateFormat.MEDIUM);

                try {

                    newValue = timeFormat.parse(stringVal);

                } catch (ParseException pe) {

                    // Okay, can't parse.  Don't transform.
                }

            } else if (handlerClass.equals(RelativeDateTimeValueNode.class)) {

                DateFormat dateTimeFormat = RelativeTemporalValueNode.getDateFormat(DateFormat.FULL, DateFormat.MEDIUM);

                try {

                    newValue = dateTimeFormat.parse(stringVal);

                } catch (ParseException pe) {

                    // Okay, can't parse.  Don't transform.
                }

            } else if (handlerClass.equals(LiteralValueNode.class)) {
             
                if (destTypeExpr.sameType(typeConstants.getDoubleType())) {

                    try {

                        newValue = Double.valueOf(stringVal);

                    } catch (NumberFormatException e) {

                        // Okay, can't parse.  Don't transform.
                    }

                } else if (destTypeExpr.sameType(typeConstants.getIntType())) {

                    try {

                        Double unRoundedVal = Double.valueOf(stringVal);
                        newValue = Integer.valueOf(unRoundedVal.intValue());

                    } catch (NumberFormatException e) {

                        // Okay, can't parse.  Don't transform.
                    }

                } else if (destTypeExpr.sameType(typeConstants.getCharType())) {

                    // Only transform when there's only 1 character in stringVal.
                    if (stringVal.length() == 1) {

                        newValue = Character.valueOf(stringVal.charAt(0));
                    }

                } else if (destTypeExpr.sameType(typeConstants.getBooleanType())) {

                    // Let's be forgiving, and allow case insensitivity.
                    if (stringVal.equalsIgnoreCase("True")) {

                        newValue = Boolean.TRUE;
View Full Code Here


            currentPreludeTypeInfo = workingModuleTypeInfo.getDependeeModuleTypeInfo(CAL_Prelude.MODULE_NAME);
        }
       
        if (currentPreludeTypeInfo != preludeTypeInfo) {   // also handles if info == null.
            this.preludeTypeInfo = currentPreludeTypeInfo;
            this.preludeTypeConstants = new PreludeTypeConstants(preludeTypeInfo);
        }
       
        return preludeTypeConstants;
    }
View Full Code Here

            ModuleTypeInfo preludeModuleTypeInfo = workspaceManager.getModuleTypeInfo(CAL_Prelude.MODULE_NAME);
            if (preludeModuleTypeInfo == null) {
                throw new IllegalStateException("Must have a successfully compiled Cal.Core.Prelude module before getPreludeTypeConstants can be called.");
            }
           
            preludeTypeConstants = new PreludeTypeConstants(preludeModuleTypeInfo);          
        }
       
        return preludeTypeConstants;
    }   
View Full Code Here

    /**
     * @param typeExpr the type expression to check
     * @return whether values of the type could be edited directly by editing the text in a text field.
     */
    public boolean isTextEditable(TypeExpr typeExpr) {
        PreludeTypeConstants typeConstants = getPreludeTypeConstants();
       
        return typeExpr.sameType(typeConstants.getCharType()) ||
            typeExpr.sameType(typeConstants.getByteType()) ||
            typeExpr.sameType(typeConstants.getShortType()) ||
            typeExpr.sameType(typeConstants.getIntType()) ||
            typeExpr.sameType(typeConstants.getIntegerType()) ||
            typeExpr.sameType(typeConstants.getDecimalType()) ||
            typeExpr.sameType(typeConstants.getLongType()) ||
            typeExpr.sameType(typeConstants.getDoubleType()) ||
            typeExpr.sameType(typeConstants.getStringType()) ||
            typeExpr.sameType(typeConstants.getCharListType());
    }
View Full Code Here

       
        // Remove the old key listener.
        ivjValueField.removeKeyListener(valueFieldKeyListener);
        valueFieldKeyListener = null;
       
        PreludeTypeConstants typeConstants = valueEditorManager.getValueNodeBuilderHelper().getPreludeTypeConstants();
       
        // Add a special key listener for certain types.
        if (typeExpr.sameType(typeConstants.getCharType())) {
           
            valueFieldKeyListener = new CharTypeKeyListener();
            ivjValueField.addKeyListener(valueFieldKeyListener);
       
        } else if (typeExpr.rootTypeConsApp() != null) {
View Full Code Here

       
        if (getValueNode() == null) {
            getLaunchEditorButton().setEnabled(false);

        } else if (isEditable()) {
            PreludeTypeConstants typeConstants = valueEditorManager.getValueNodeBuilderHelper().getPreludeTypeConstants();
            // TODO: Can this be removed or also used for numbers ?
            getLaunchEditorButton().setEnabled(!getValueNode().getTypeExpr().sameType(typeConstants.getCharType()));

        } else {
            getLaunchEditorButton().setEnabled(true);
        }
    }
View Full Code Here

        if (!oldChild.getTypeExpr().sameType(newChild.getTypeExpr())) {
   
            Map<ValueNode, TypeExpr> valueNodeToUnconstrainedTypeMap = getValueNodeToUnconstrainedTypeMap();
            Map<ValueNode, ValueNode> commitValueMap = valueEditorManager.getValueNodeCommitHelper().getCommitValues(oldChild, newChild, valueNodeToUnconstrainedTypeMap);
           
            PreludeTypeConstants typeConstants = valueEditorManager.getPreludeTypeConstants();
            TypeExpr charType = typeConstants.getCharType();
            ValueNode newValueNodeFromMap = commitValueMap.get(oldValueNode);

            // HACK: if it's a ListOfCharValueNode, convert it to a ListValueNode.
            //   What we need is a way to guarantee the type of value node that is returned by getCommitValues().
            //   This is not possible with the current form of transmuteValueNode() though.
            if (newValueNodeFromMap instanceof ListOfCharValueNode) {
                ListOfCharValueNode charListValueNode = (ListOfCharValueNode)newValueNodeFromMap;
                char[] charListValueArray = charListValueNode.getStringValue().toCharArray();

                ArrayList<ValueNode> newListValue = new ArrayList<ValueNode>(charListValueArray.length);
                for (final char charListValue : charListValueArray) {
                    newListValue.add(new LiteralValueNode(Character.valueOf(charListValue), charType));
                }
               
                newValueNode = new ListValueNode(newListValue, typeConstants.getCharListType(), new LiteralValueNode(new Character('a'), charType));
                replaceValueNode(newValueNode, true);
                return;

            } else {
                newValueNode = (ListValueNode)newValueNodeFromMap;
View Full Code Here

            currentText = "";
        }

        TypeExpr typeExpr = valueEntryPanel.getValueNode().getTypeExpr();
       
        PreludeTypeConstants typeConstants = valueEntryPanel.valueEditorManager.getPreludeTypeConstants();
      
        if (typeExpr.sameType(typeConstants.getDoubleType()) ||
            typeExpr.sameType(typeConstants.getFloatType())) {
               
            // Remove trailing -
            len = currentText.length();
            if (len > 0) {
                char lastChar = currentText.charAt(len - 1);
                if (lastChar == '-') {
                    currentText = currentText.substring(0, len - 1);
                    len--;
                    changed = true;
                }
            }

            // Check the unique case where the user types in a '.' first
            // Then we want to show '0.' instead
            if (currentText.indexOf(".") == 0) {
                currentText = "0" + currentText;
                len++;
                changed = true;
            }

            // Remove trailing 'e' or 'E'
            if (len > 0) {
                char lastChar = currentText.charAt(len - 1);
                if (lastChar == 'e' || lastChar == 'E') {
                    currentText = currentText.substring(0, len - 1);
                    len--;
                    changed = true;
                }
            }

            // If this is empty, set the text to zero
            if (len == 0) {

                currentText = "0.0";
                len = 3;
                changed = true;

            } else {

                // If the value is a perfect whole number, then want the ending ".0".
                int indexOfPeriod = currentText.indexOf(".");

                if (indexOfPeriod == -1) {
                   
                    // Check for an 'e' within the value, or if the value doesn't end with a digit (eg. "Infinity").
                    int indexOfE = Math.max(currentText.indexOf('e'), currentText.indexOf('E'));
                    boolean endsWithDigit = Character.isDigit(currentText.charAt(len - 1));

                    if (indexOfE < 0 && endsWithDigit) {

                        currentText = currentText + ".0";
                        len += 2;
                        changed = true;
                    }

                } else if (indexOfPeriod == (currentText.length() - 1)) {

                    currentText = currentText + "0";
                    len++;
                    changed = true;
                }
            }

        } else if (typeExpr.sameType(typeConstants.getDecimalType())) {
               
            // Remove trailing -
            len = currentText.length();
            if (len > 0) {
                char lastChar = currentText.charAt(len - 1);
                if (lastChar == '-') {
                    currentText = currentText.substring(0, len - 1);
                    len--;
                    changed = true;
                }
            }

            // Check the unique case where the user types in a '.' first
            // Then we want to show '0.' instead
            if (currentText.indexOf(".") == 0) {
                currentText = "0" + currentText;
                len++;
                changed = true;
            }

            // Remove trailing 'e' or 'E'
            if (len > 0) {
                char lastChar = currentText.charAt(len - 1);
                if (lastChar == 'e' || lastChar == 'E') {
                    currentText = currentText.substring(0, len - 1);
                    len--;
                    changed = true;
                }
            }

            // If this is empty, set the text to zero
            if (len == 0) {

                currentText = "0";
                len = 1;
                changed = true;

            }
           
        } else if (typeExpr.sameType(typeConstants.getIntType()) ||
                   typeExpr.sameType(typeConstants.getIntegerType()) ||
                   typeExpr.sameType(typeConstants.getByteType()) ||
                   typeExpr.sameType(typeConstants.getShortType()) ||
                   typeExpr.sameType(typeConstants.getLongType())) {

            // Remove trailing -
            len = currentText.length();
            if (len > 0) {
                char lastChar = currentText.charAt(len - 1);
                if (lastChar == '-') {
                    currentText = currentText.substring(0, len - 1);
                    len--;
                    changed = true;
                }
            }

            // If this is empty, set the text to zero
            if (len == 0) {
                currentText = "0";
                len = 1;
                changed = true;
            }

        } else if (typeExpr.sameType(typeConstants.getCharType())) {

            len = currentText.length();
            if (len != 1) {
                // Default value is a space.
                currentText = " ";
                changed = true;
            }

        } else if (typeExpr.isNonParametricType(CAL_RelativeTime.TypeConstructors.RelativeDate)) {

            RelativeDateValueNode dateValueNode = (RelativeDateValueNode) valueEntryPanel.getValueNode();

            // Check.  If it's parseable, then it's valid (of course!).
            // If not parseable, then give a default value of the date in ValueNode.
            DateFormat dateFormat = RelativeTemporalValueNode.getDateFormat(DateFormat.FULL, -1);
           
            try {
                dateFormat.parse(currentText);
            } catch (ParseException pe) {
                currentText = dateFormat.format(dateValueNode.getDateValue());
                changed = true;

            }

        } else if (typeExpr.isNonParametricType(CAL_RelativeTime.TypeConstructors.RelativeTime)) {

            RelativeTimeValueNode timeValueNode = (RelativeTimeValueNode) valueEntryPanel.getValueNode();

            // Check.  If it's parseable, then it's valid (of course!).
            // If not parseable, then give a default value of the time in ValueNode.
            DateFormat timeFormat = RelativeTemporalValueNode.getDateFormat(-1, DateFormat.MEDIUM);
           
            try {
                timeFormat.parse(currentText);
            } catch (ParseException pe) {
                currentText = timeFormat.format(timeValueNode.getTimeValue());
                changed = true;

            }

        } else if (typeExpr.isNonParametricType(CAL_RelativeTime.TypeConstructors.RelativeDateTime)) {

            RelativeDateTimeValueNode dateTimeValueNode = (RelativeDateTimeValueNode) valueEntryPanel.getValueNode();

            // Check.  If it's parseable, then it's valid (of course!).
            // If not parseable, then give a default value of the date time in ValueNode.
            DateFormat dateTimeFormat = RelativeTemporalValueNode.getDateFormat(DateFormat.FULL, DateFormat.MEDIUM);
           
            try {
                dateTimeFormat.parse(currentText);
            } catch (ParseException pe) {
                currentText = dateTimeFormat.format(dateTimeValueNode.getDateTimeValue());
                changed = true;
            }

        } else if (typeExpr.isNonParametricType(CAL_Time.TypeConstructors.Time)) {

            JTimeValueNode valueNode = (JTimeValueNode) valueEntryPanel.getValueNode();

            // Check.  If it's parseable, then it's valid (of course!).
            // If not parseable, then give a default value of the date time in ValueNode.
            DateFormat fmt=DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.LONG);
            fmt.setTimeZone(TimeZone.getDefault());

           
            try {
                fmt.parse(currentText);
            } catch (ParseException pe) {
                currentText = fmt.format(valueNode.getJavaDate());
                changed = true;
            }

        } else if (typeExpr.sameType(typeConstants.getCharListType())) {

            // Strings are valid.

        } else if (typeExpr.sameType(typeConstants.getStringType())) {

            // Strings are valid.

        } else {
View Full Code Here

        boolean valid = false;

        TypeExpr typeExpr = valueEntryPanel.getValueNode().getTypeExpr();
       
        PreludeTypeConstants typeConstants = valueEntryPanel.valueEditorManager.getPreludeTypeConstants();
                     
        if (typeExpr.sameType(typeConstants.getDoubleType()) ||
            typeExpr.sameType(typeConstants.getFloatType()) ||
            typeExpr.sameType(typeConstants.getDecimalType())) {
           
            //todoBI Float and Double should have different validation to take into account their ranges
                           
            valid = isGoodDouble(text);

        } else if (typeExpr.sameType(typeConstants.getIntType()) ||
                   typeExpr.sameType(typeConstants.getByteType()) ||
                   typeExpr.sameType(typeConstants.getShortType()) ||
                   typeExpr.sameType(typeConstants.getLongType())) {
          
            //todoBI the integral types should have different validation to take into account their ranges
          
            valid = isGoodInteger(text);

        } else if (typeExpr.sameType(typeConstants.getIntegerType())) {

            valid = isGoodBigInteger(text);
   
        } else if (typeExpr.sameType(typeConstants.getCharType())) {

            valid = isGoodChar(text);

        } else if (typeExpr.sameType(typeConstants.getCharListType())) {

            // Strings are always good!
            valid = true;

        } else if (typeExpr.sameType(typeConstants.getStringType())) {

            // Strings are always good!
            valid = true;

        } else if (typeExpr.isNonParametricType(CAL_RelativeTime.TypeConstructors.RelativeDate) ||
View Full Code Here

            }
        }

        // Now, do the update to the valueNode.
        TypeExpr typeExpr = valueEntryPanel.getValueNode().getTypeExpr();
        PreludeTypeConstants typeConstants = valueEntryPanel.valueEditorManager.getPreludeTypeConstants();
       
        if (typeExpr.sameType(typeConstants.getCharType())) {

            // The first char should be the char we want as the value.
            Character charVal = new Character(newVal.charAt(0));
            valueEntryPanel.replaceValueNode(new LiteralValueNode(charVal, typeExpr.copyTypeExpr()), true);
           
        } else if (typeExpr.sameType(typeConstants.getByteType())) {

            Double unRoundedVal = new Double(newVal);
            Byte byteVal = new Byte(unRoundedVal.byteValue());
            valueEntryPanel.replaceValueNode(new LiteralValueNode(byteVal, typeExpr.copyTypeExpr()), true);
     
        } else if (typeExpr.sameType(typeConstants.getShortType())) {

            Double unRoundedVal = new Double(newVal);
            Short shortVal = new Short(unRoundedVal.shortValue());
            valueEntryPanel.replaceValueNode(new LiteralValueNode(shortVal, typeExpr.copyTypeExpr()), true);
             
        } else if (typeExpr.sameType(typeConstants.getIntType())) {

            Double unRoundedVal = new Double(newVal);
            Integer integerVal = Integer.valueOf(unRoundedVal.intValue());
            valueEntryPanel.replaceValueNode(new LiteralValueNode(integerVal, typeExpr.copyTypeExpr()), true);

        } else if (typeExpr.sameType(typeConstants.getIntegerType())) {
           
            BigDecimal unRoundedVal = new BigDecimal(newVal);
            BigInteger bigIntegerVal;
           
            // Math.round uses a rounding strategy that BigDecimal does not provide, so we have to do
            // a little bit of fiddling to round in an equivalent fashion.
            if(unRoundedVal.signum() >= 0) {
                bigIntegerVal = unRoundedVal.setScale(0, BigDecimal.ROUND_HALF_UP).toBigInteger();
            } else {
                bigIntegerVal = unRoundedVal.setScale(0, BigDecimal.ROUND_HALF_DOWN).toBigInteger();
            }
           
            valueEntryPanel.replaceValueNode(new LiteralValueNode(bigIntegerVal, typeExpr.copyTypeExpr()), true);
       
        } else if (typeExpr.sameType(typeConstants.getDecimalType())) {

            BigDecimal decimalVal = new BigDecimal(newVal);
            valueEntryPanel.replaceValueNode(new LiteralValueNode(decimalVal, typeExpr.copyTypeExpr()), true);

        } else if (typeExpr.sameType(typeConstants.getLongType())) {

            Double unRoundedVal = new Double(newVal);
            Long longVal = new Long(unRoundedVal.longValue());
            valueEntryPanel.replaceValueNode(new LiteralValueNode(longVal, typeExpr.copyTypeExpr()), true);

        } else if (typeExpr.sameType(typeConstants.getFloatType())) {

            Float floatVal = new Float(newVal);
            valueEntryPanel.replaceValueNode(new LiteralValueNode(floatVal, typeExpr.copyTypeExpr()), true);

        } else if (typeExpr.sameType(typeConstants.getDoubleType())) {

            Double doubleVal = new Double(newVal);
            valueEntryPanel.replaceValueNode(new LiteralValueNode(doubleVal, typeExpr.copyTypeExpr()), true);

        } else if (typeExpr.sameType(typeConstants.getStringType())) {
           
            newVal = newVal.replace(ListOfCharValueNode.CHAR_RETURN_REPLACE, '\n');
            valueEntryPanel.replaceValueNode(new LiteralValueNode(newVal, typeExpr.copyTypeExpr()), true);
           
        } else if (typeExpr.sameType(typeConstants.getCharListType())) {

            // First, must replace the return replacement chars with return.
            newVal = newVal.replace(ListOfCharValueNode.CHAR_RETURN_REPLACE, '\n');
            valueEntryPanel.replaceValueNode(new ListOfCharValueNode(newVal, typeExpr.copyTypeExpr()), true);
       
View Full Code Here

TOP

Related Classes of org.openquark.cal.compiler.PreludeTypeConstants

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.