Class: ValueElement

Inherits:
Element show all
Defined in:
lib/fxmlloader/value_elts.rb

Instance Attribute Summary collapse

Attributes inherited from Element

#current, #eventHandlerAttributes, #instancePropertyAttributes, #lineNumber, #loadListener, #parent, #parentLoader, #staticPropertyAttributes, #staticPropertyElements, #value, #valueAdapter, #xmlStreamReader

Instance Method Summary collapse

Methods inherited from Element

#add, #addEventHandler, #applyProperty, #callz, #getProperties, #getValueAdapter, #initialize, #isBidirectionalBindingExpression, #isBindingExpression, #isCollection, #isTyped, #populateArrayFromString, #populateListFromString, #processEventHandlerAttributes, #processInstancePropertyAttributes, #processPropertyAttribute, #processValue3, #resolvePrefixedValue, #set, #staticLoad, #updateValue, #warnDeprecatedEscapeSequence

Constructor Details

This class inherits a constructor from Element

Instance Attribute Details

#fx_idObject

Returns the value of attribute fx_id.



57
58
59
# File 'lib/fxmlloader/value_elts.rb', line 57

def fx_id
  @fx_id
end

Instance Method Details

#getListValue(parent, listPropertyName, value) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fxmlloader/value_elts.rb', line 106

def getListValue( parent,  listPropertyName,  value)
  # If possible, coerce the value to the list item type
  if (parent.isTyped())
    listType = parent.getValueAdapter().getGenericType(listPropertyName);

    if (listType != nil)
      itemType = RubyWrapperBeanAdapter.getGenericListItemType(listType);

      if (itemType.is_a? ParameterizedType)
        itemType = (itemType).getRawType();
      end

      value = RubyWrapperBeanAdapter.coerce(value,itemType);
    end
  end

  return value;
end

#processAttribute(prefix, localName, value) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/fxmlloader/value_elts.rb', line 203

def processAttribute( prefix,  localName,  value)
  if (prefix != nil				&& prefix == (FXL::FX_NAMESPACE_PREFIX))
    if (localName == (FXL::FX_ID_ATTRIBUTE))
      # Verify that ID is a valid identifier
      if (value == (FXL::NULL_KEYWORD))
        raise LoadException.new("Invalid identifier.");
      end
#
#        value.length.times do |i|
#          # TODO: FIX
#          if (!Java.java.lang.Character.java_send :isJavaIdentifierPart, [Java::char], value[i].to_java(:char))
#            raise LoadException.new("Invalid identifier.");
#          end
#        end
      @fx_id = value;

    elsif (localName == (FXL::FX_CONTROLLER_ATTRIBUTE))
      if (parentLoader.current.parent != nil)
        raise LoadException.new(FXL::FX_NAMESPACE_PREFIX + ":" + FXL::FX_CONTROLLER_ATTRIBUTE		+ " can only be applied to root element.");
      end

      if (parentLoader.controller != nil)
        raise LoadException.new("Controller value already specified.");
      end

      if (!staticLoad)
        type = nil
        begin
          type = value.constantize_by
        rescue ClassNotFoundException => exception
          raise LoadException.new(exception);
        end

        begin
          if (parentLoader.controllerFactory == nil)
            # TODO: does this work?
            parentLoader.controller = type.new
          else
            parentLoder.controller = (parentLoader.controllerFactory.call(type));
          end
        rescue InstantiationException => exception
          raise LoadException.new(exception);
        rescue IllegalAccessException => exception
          raise LoadException.new(exception);
        end
      end
    else
      raise LoadException.new("Invalid attribute.");
    end
  else
    super(prefix, localName, value);
  end
end

#processCharactersObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/fxmlloader/value_elts.rb', line 178

def processCharacters()
  type = value.java_class
  defaultProperty = type.getAnnotation(DefaultProperty.java_class);

  # If the default property is a read-only list, add the value to it;
  # otherwise, set the value as the default property
  if (defaultProperty != nil)
    text = xmlStreamReader.getText();
    #TODO: FIX
    text = extraneousWhitespacePattern.matcher(text).replaceAll(" ");

    defaultPropertyName = defaultProperty.value();
    valueAdapter = getValueAdapter();

    if (valueAdapter.read_only?(defaultPropertyName) && List.class.isAssignableFrom(valueAdapter.getType(defaultPropertyName)))
      list = valueAdapter.get(defaultPropertyName);
      list.add(getListValue(self, defaultPropertyName, text));
    else
      valueAdapter.put(defaultPropertyName, text.strip);
    end
  else
    throw LoadException.new(type.getName() + " does not have a default property.");
  end
end

#processEndElementObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fxmlloader/value_elts.rb', line 71

def processEndElement()
  super

  # Build the value, if necessary
  if (value.is_a? Builder)
    updateValue(value.build());
    processValue();
  else
    processInstancePropertyAttributes();
  end
  processEventHandlerAttributes();
  # Process static property attributes
  if (staticPropertyAttributes.length > 0)
    for attribute in staticPropertyAttributes
      processPropertyAttribute(attribute);
    end
  end
  # Process static property elements
  if (staticPropertyElements.length > 0)
    for  element in staticPropertyElements
      RubyWrapperBeanAdapter.put(value, element.sourceType, element.name, element.value);
    end
  end

  rnest -1
  rputs value, ((rfx_id(value) && rfx_id_set?(value)) || rno_show?(value) ? ""  : "end")
  if (parent != nil)
    if (parent.isCollection())
      parent.add(value);
    else
      parent.set value
    end
  end
end

#processStartElementObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fxmlloader/value_elts.rb', line 59

def processStartElement
  super

  updateValue(constructValue());

  if (value.is_a? Builder)
    processInstancePropertyAttributes();
  else
    processValue();
  end
end

#processValueObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/fxmlloader/value_elts.rb', line 125

def processValue()
  # If this is the root element, update the value
  if (parent == nil)
    root = value;

    # checking version of fx namespace - throw exception if not supported
    fxNSURI = xmlStreamReader.getNamespaceContext().getNamespaceURI("fx");
    if (fxNSURI != nil)
      fxVersion = fxNSURI[(fxNSURI.rindex("/") + 1)..-1];
      if (parentLoader.compareJFXVersions(FxmlLoader::FX_NAMESPACE_VERSION, fxVersion) < 0)
        raise LoadException.new("Loading FXML document of version "	+ fxVersion + " by JavaFX runtime supporting version " + FxmlLoader::FX_NAMESPACE_VERSION);
      end
    end

    # checking the version JavaFX API - print warning if not supported
    defaultNSURI = xmlStreamReader.getNamespaceContext().getNamespaceURI("");
    if (defaultNSURI != nil)
      nsVersion = defaultNSURI[(defaultNSURI.rindex("/") + 1)..-1]
      jfx_version = if defined? FXL::JAVAFX_VERSION
        FXL::JAVAFX_VERSION
      else
        "http://javafx.com/javafx/2.2"
      end
      if (parentLoader.compareJFXVersions(jfx_version, nsVersion) < 0)
        Logging.getJavaFXLogger().warning("Loading FXML document with JavaFX API of version " + nsVersion + " by JavaFX runtime of version " + FXL::JAVAFX_VERSION);
      end
    end
  end

  # Add the value to the namespace
  if (@fx_id != nil)
    parentLoader.namespace[@fx_id] =  value

    # If the value defines an ID property, set it
    idProperty = value.java_class.annotation(IDProperty.java_class);

    if (idProperty != nil)
      properties = getProperties();
      # set fx:id property value to Node.id only if Node.id was not
      # already set when processing start element attributes
      if (properties[idProperty.value] == nil)
        properties[idProperty.value()]= @fx_id;
      end
    end
    rputs value, "__local_fx_id_setter.call(#{@fx_id.inspect}, self)"
    rfx_id value, @fx_id
    # Set the controller field value
    if (parentLoader.controller != nil)
      field = parentLoader.controller.instance_variable_set("@" + @fx_id, value)
    end
  end
end