Search in sources :

Example 11 with BeanAdapter

use of org.apache.pivot.beans.BeanAdapter in project pivot by apache.

the class Component method setSkin.

/**
 * Sets the skin, replacing any previous skin.
 *
 * @param skin The new skin.
 */
@SuppressWarnings("unchecked")
protected void setSkin(Skin skin) {
    Utils.checkNull(skin, "skin");
    if (this.skin != null) {
        throw new IllegalStateException("Skin is already installed.");
    }
    this.skin = skin;
    styles = new BeanAdapter(skin);
    skin.install(this);
    // Apply any defined type styles
    LinkedList<Class<?>> styleTypes = new LinkedList<>();
    Class<?> type = getClass();
    while (type != Object.class) {
        styleTypes.insert(type, 0);
        type = type.getSuperclass();
    }
    for (Class<?> styleType : styleTypes) {
        Map<String, ?> stylesMap = typedStyles.get((Class<? extends Component>) styleType);
        if (stylesMap != null) {
            setStyles(stylesMap);
        }
    }
    invalidate();
    repaint();
}
Also used : BeanAdapter(org.apache.pivot.beans.BeanAdapter) LinkedList(org.apache.pivot.collections.LinkedList)

Example 12 with BeanAdapter

use of org.apache.pivot.beans.BeanAdapter in project pivot by apache.

the class DataBinding method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    form = (Form) namespace.get("form");
    loadJavaButton = (PushButton) namespace.get("loadJavaButton");
    loadJSONButton = (PushButton) namespace.get("loadJSONButton");
    clearButton = (PushButton) namespace.get("clearButton");
    sourceLabel = (Label) namespace.get("sourceLabel");
    loadJavaButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            form.load(new BeanAdapter(CONTACT));
            sourceLabel.setText("Java");
        }
    });
    loadJSONButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            JSONSerializer serializer = new JSONSerializer();
            try (InputStream inputStream = getClass().getResourceAsStream("contact.json")) {
                form.load(serializer.readObject(inputStream));
                sourceLabel.setText("JSON");
            } catch (Exception exception) {
                System.err.println(exception);
            }
            button.setEnabled(true);
        }
    });
    clearButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            form.clear();
            sourceLabel.setText("");
        }
    });
}
Also used : ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) InputStream(java.io.InputStream) BeanAdapter(org.apache.pivot.beans.BeanAdapter) JSONSerializer(org.apache.pivot.json.JSONSerializer)

Example 13 with BeanAdapter

use of org.apache.pivot.beans.BeanAdapter in project pivot by apache.

the class ComponentPropertyInspectorSkin method sourceChanged.

@Override
public void sourceChanged(ComponentInspector componentInspector, Component previousSource) {
    Component source = componentInspector.getSource();
    clearControls();
    Form.SectionSequence sections = form.getSections();
    sections.remove(0, sections.getLength());
    if (previousSource != null) {
        beanMonitor.getPropertyChangeListeners().remove(propertyChangeListener);
    }
    if (source == null) {
        beanMonitor = null;
    } else {
        beanMonitor = new BeanMonitor(source);
        beanMonitor.getPropertyChangeListeners().add(propertyChangeListener);
        Class<?> sourceType = source.getClass();
        HashMap<Class<?>, List<String>> declaringClassPartitions = new HashMap<>(classComparator);
        // Partition the properties by their declaring class
        BeanAdapter beanAdapter = new BeanAdapter(source);
        for (String propertyName : beanAdapter) {
            if (beanMonitor.isNotifying(propertyName) && !beanAdapter.isReadOnly(propertyName)) {
                Method method = BeanAdapter.getGetterMethod(sourceType, propertyName);
                Class<?> declaringClass = method.getDeclaringClass();
                List<String> propertyNames = declaringClassPartitions.get(declaringClass);
                if (propertyNames == null) {
                    propertyNames = new ArrayList<>(nameComparator);
                    declaringClassPartitions.put(declaringClass, propertyNames);
                }
                propertyNames.add(propertyName);
            }
        }
        // Add the controls
        for (Class<?> declaringClass : declaringClassPartitions) {
            Form.Section section = new Form.Section();
            section.setHeading(declaringClass.getSimpleName());
            sections.add(section);
            List<String> propertyNames = declaringClassPartitions.get(declaringClass);
            for (String propertyName : propertyNames) {
                Class<?> type = beanAdapter.getType(propertyName);
                addControl(beanAdapter, propertyName, type, section);
            }
        }
    }
}
Also used : BeanMonitor(org.apache.pivot.beans.BeanMonitor) Form(org.apache.pivot.wtk.Form) HashMap(org.apache.pivot.collections.HashMap) Method(java.lang.reflect.Method) List(org.apache.pivot.collections.List) ArrayList(org.apache.pivot.collections.ArrayList) BeanAdapter(org.apache.pivot.beans.BeanAdapter) Component(org.apache.pivot.wtk.Component)

Example 14 with BeanAdapter

use of org.apache.pivot.beans.BeanAdapter in project pivot by apache.

the class StockTrackerWindow method refreshDetail.

private void refreshDetail() {
    StockQuote stockQuote = null;
    int firstSelectedIndex = stocksTableView.getFirstSelectedIndex();
    if (firstSelectedIndex != -1) {
        int lastSelectedIndex = stocksTableView.getLastSelectedIndex();
        if (firstSelectedIndex == lastSelectedIndex) {
            @SuppressWarnings("unchecked") List<StockQuote> tableData = (List<StockQuote>) stocksTableView.getTableData();
            stockQuote = tableData.get(firstSelectedIndex);
        } else {
            stockQuote = new StockQuote();
        }
    } else {
        stockQuote = new StockQuote();
    }
    detailPane.load(new BeanAdapter(stockQuote));
}
Also used : ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) BeanAdapter(org.apache.pivot.beans.BeanAdapter)

Aggregations

BeanAdapter (org.apache.pivot.beans.BeanAdapter)14 Dictionary (org.apache.pivot.collections.Dictionary)6 Map (org.apache.pivot.collections.Map)4 Sequence (org.apache.pivot.collections.Sequence)4 ArrayList (org.apache.pivot.collections.ArrayList)3 List (org.apache.pivot.collections.List)3 SerializationException (org.apache.pivot.serialization.SerializationException)3 ParameterizedType (java.lang.reflect.ParameterizedType)2 HashMap (org.apache.pivot.collections.HashMap)2 Component (org.apache.pivot.wtk.Component)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 BeanMonitor (org.apache.pivot.beans.BeanMonitor)1 DefaultProperty (org.apache.pivot.beans.DefaultProperty)1 LinkedList (org.apache.pivot.collections.LinkedList)1 MapAdapter (org.apache.pivot.collections.adapter.MapAdapter)1