Search in sources :

Example 1 with StoreFilter

use of com.extjs.gxt.ui.client.store.StoreFilter in project jahia by Jahia.

the class CodeEditorTabItem method init.

@Override
public void init(final NodeHolder engine, final AsyncTabItem tab, String locale) {
    gwtJahiaNode = engine.getNode();
    tab.setLayout(new BorderLayout());
    tab.setScrollMode(Style.Scroll.AUTO);
    final HorizontalPanel horizontalPanel = new HorizontalPanel();
    horizontalPanel.setSpacing(10);
    horizontalPanel.setVerticalAlign(Style.VerticalAlignment.MIDDLE);
    tab.add(horizontalPanel, new BorderLayoutData(Style.LayoutRegion.NORTH, 40));
    final HorizontalPanel actions = new HorizontalPanel();
    actions.setVerticalAlign(Style.VerticalAlignment.MIDDLE);
    horizontalPanel.add(actions);
    if (!tab.isProcessed()) {
        // Add list of properties
        GWTJahiaNodeProperty typeName = engine.getProperties().get("nodeTypeName");
        if (typeName == null) {
            typeName = engine.getPresetProperties().get("nodeTypeName");
        }
        if (engine.getProperties().containsKey(codePropertyName)) {
            codeProperty = engine.getProperties().get(codePropertyName);
        } else {
            codeProperty = new GWTJahiaNodeProperty(codePropertyName, "", GWTJahiaNodePropertyType.STRING);
        }
        Button indentButton = new Button(Messages.get("label.indentAll"));
        indentButton.addStyleName("button-indent");
        indentButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent buttonEvent) {
                if (codeField != null) {
                    codeField.indent();
                }
            }
        });
        if (stubType != null) {
            // stub type is defined -> create combos for code snippets
            final Button addAllButton = new Button(Messages.get("label.addAll"));
            addAllButton.addStyleName("button-addAll");
            final Button addButton = new Button(Messages.get("label.add"));
            addButton.addStyleName("button-add");
            snippetType = new ComboBox<GWTJahiaValueDisplayBean>();
            snippetType.setTypeAhead(true);
            snippetType.getListView().setStyleAttribute(FONT_SIZE, FONT_SIZE_VALUE);
            snippetType.setTriggerAction(ComboBox.TriggerAction.ALL);
            snippetType.setForceSelection(true);
            snippetType.setWidth(200);
            snippetType.removeAllListeners();
            snippetType.setStore(new ListStore<GWTJahiaValueDisplayBean>());
            snippetType.setAllowBlank(false);
            snippetType.setDisplayField("display");
            snippetType.addSelectionChangedListener(new SelectionChangedListener<GWTJahiaValueDisplayBean>() {

                @Override
                public void selectionChanged(SelectionChangedEvent<GWTJahiaValueDisplayBean> se) {
                    // snippet type has changed -> populate code snippets
                    mirrorTemplates.clear();
                    mirrorTemplates.getStore().removeAll();
                    mirrorTemplates.getStore().add(snippets.get(se.getSelectedItem().getValue()));
                    if (mirrorTemplates.getStore().getModels().size() > 0) {
                        addAllButton.enable();
                        mirrorTemplates.setEmptyText(Messages.get("label.stub.choose.codeTemplate"));
                    } else {
                        addAllButton.disable();
                    }
                    addButton.disable();
                }
            });
            // code templates combo
            mirrorTemplates = new ComboBox<GWTJahiaValueDisplayBean>() {

                public void doQuery(String q, boolean forceAll) {
                    final String query = q;
                    StoreFilter<GWTJahiaValueDisplayBean> filter = new StoreFilter<GWTJahiaValueDisplayBean>() {

                        @Override
                        public boolean select(Store<GWTJahiaValueDisplayBean> store, GWTJahiaValueDisplayBean parent, GWTJahiaValueDisplayBean item, String property) {
                            return item.getDisplay().contains(query);
                        }
                    };
                    if (q == null) {
                        q = "";
                    }
                    FieldEvent fe = new FieldEvent(this);
                    fe.setValue(q);
                    if (!fireEvent(Events.BeforeQuery, fe)) {
                        return;
                    }
                    if (q.length() >= 1) {
                        if (!q.equals(lastQuery)) {
                            lastQuery = q;
                            store.clearFilters();
                            if (store.getFilters() != null) {
                                store.getFilters().clear();
                            }
                            store.addFilter(filter);
                            store.applyFilters(getDisplayField());
                            expand();
                        }
                    } else {
                        lastQuery = "";
                        store.clearFilters();
                        if (store.getFilters() != null) {
                            store.getFilters().clear();
                        }
                        expand();
                    }
                }
            };
            mirrorTemplates.setTypeAhead(true);
            mirrorTemplates.getListView().setStyleAttribute(FONT_SIZE, FONT_SIZE_VALUE);
            mirrorTemplates.setTriggerAction(ComboBox.TriggerAction.ALL);
            mirrorTemplates.setForceSelection(true);
            mirrorTemplates.setWidth(300);
            mirrorTemplates.removeAllListeners();
            mirrorTemplates.setStore(new ListStore<GWTJahiaValueDisplayBean>());
            mirrorTemplates.getStore().sort("display", Style.SortDir.ASC);
            mirrorTemplates.setAllowBlank(true);
            mirrorTemplates.setDisplayField("display");
            String path = engine.isExistingNode() ? engine.getNode().getPath() : engine.getTargetNode().getPath();
            String nodeType = typeName != null ? typeName.getValues().get(0).getString() : null;
            // get code editor data from the server
            JahiaContentManagementService.App.getInstance().initializeCodeEditor(path, !engine.isExistingNode(), nodeType, stubType, new BaseAsyncCallback<RpcMap>() {

                public void onSuccess(RpcMap result) {
                    if (!result.isEmpty() && result.get("snippets") != null) {
                        // we have got snippets -> populate snippet type combo
                        snippets = (Map<String, List<GWTJahiaValueDisplayBean>>) result.get("snippets");
                        for (String type : snippets.keySet()) {
                            snippetType.getStore().add(new GWTJahiaValueDisplayBean(type, Messages.get("label.snippetType." + type, type)));
                        }
                        snippetType.setValue(snippetType.getStore().getAt(0));
                        addButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

                            @Override
                            public void componentSelected(ButtonEvent buttonEvent) {
                                if (mirrorTemplates.getValue() != null) {
                                    codeField.insertProperty(mirrorTemplates.getValue().getValue());
                                }
                            }
                        });
                        Label label = new Label(Messages.get("label.snippetType", "Snippet Type"));
                        label.setStyleAttribute(FONT_SIZE, FONT_SIZE_VALUE);
                        actions.add(label);
                        actions.add(snippetType);
                        label = new Label(Messages.get("label.codeMirrorTemplates", "Code Template"));
                        label.setStyleAttribute(FONT_SIZE, FONT_SIZE_VALUE);
                        actions.add(label);
                        actions.add(mirrorTemplates);
                        addButton.disable();
                        mirrorTemplates.addSelectionChangedListener(new SelectionChangedListener<GWTJahiaValueDisplayBean>() {

                            @Override
                            public void selectionChanged(SelectionChangedEvent<GWTJahiaValueDisplayBean> se) {
                                addButton.enable();
                            }
                        });
                        actions.add(addButton);
                        // create add all snippets addButton
                        addAllButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

                            @Override
                            public void componentSelected(ButtonEvent ce) {
                                StringBuilder s = new StringBuilder();
                                for (GWTJahiaValueDisplayBean value : mirrorTemplates.getStore().getModels()) {
                                    s.append(value.getValue()).append("\n");
                                }
                                codeField.insertProperty(s.toString());
                            }
                        });
                        actions.add(addAllButton);
                    }
                    if (!engine.getProperties().containsKey(codePropertyName)) {
                        Map<String, String> stubs = (Map<String, String>) result.get("stubs");
                        if (stubs.size() == 1) {
                            codeProperty = new GWTJahiaNodeProperty(codePropertyName, stubs.values().iterator().next(), GWTJahiaNodePropertyType.STRING);
                            initEditor(tab);
                        } else if (stubs.size() > 1) {
                            actions.hide();
                            final LayoutContainer w = new LayoutContainer(new CenterLayout());
                            final ComboBox<GWTJahiaValueDisplayBean> stubsCombo = new ComboBox<GWTJahiaValueDisplayBean>();
                            stubsCombo.setWidth(300);
                            stubsCombo.setTypeAhead(true);
                            stubsCombo.getListView().setStyleAttribute(FONT_SIZE, FONT_SIZE_VALUE);
                            stubsCombo.setTriggerAction(ComboBox.TriggerAction.ALL);
                            stubsCombo.setForceSelection(true);
                            stubsCombo.setStore(new ListStore<GWTJahiaValueDisplayBean>());
                            stubsCombo.setDisplayField("display");
                            stubsCombo.setEmptyText(Messages.get("label.stub.select"));
                            for (String stub : stubs.keySet()) {
                                String display;
                                String viewName;
                                if (stub.indexOf('/') != -1) {
                                    viewName = stub.substring(stub.indexOf("."), stub.lastIndexOf("."));
                                    display = Messages.get("label.stub" + viewName);
                                } else {
                                    display = Messages.get("label.stub.default");
                                    viewName = "";
                                }
                                GWTJahiaValueDisplayBean value = new GWTJahiaValueDisplayBean(stubs.get(stub), display);
                                value.set("viewName", viewName);
                                stubsCombo.getStore().add(value);
                            }
                            stubsCombo.addSelectionChangedListener(new SelectionChangedListener<GWTJahiaValueDisplayBean>() {

                                @Override
                                public void selectionChanged(SelectionChangedEvent<GWTJahiaValueDisplayBean> se) {
                                    w.removeFromParent();
                                    initEditor(tab);
                                    Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {

                                        public void execute() {
                                            codeField.insertProperty(stubsCombo.getValue().getValue());
                                        }
                                    });
                                    if (engine instanceof CreateContentEngine) {
                                        final CreateContentEngine createContentEngine = (CreateContentEngine) engine;
                                        createContentEngine.setTargetName(createContentEngine.getTargetName() + stubsCombo.getValue().get("viewName"));
                                    }
                                    actions.show();
                                    codeField.show();
                                }
                            });
                            w.add(stubsCombo);
                            tab.add(w, new BorderLayoutData(Style.LayoutRegion.CENTER));
                            tab.layout();
                        } else {
                            initEditor(tab);
                        }
                    } else {
                        initEditor(tab);
                    }
                }
            });
        } else {
            if (availableCodeMirrorModes != null && availableCodeMirrorModes.size() > 0) {
                actions.add(new Label(Messages.get("label.selectSyntaxHighlighting", "Select syntax highlighting") + ": "));
                actions.add(getModeCombo());
            }
            initEditor(tab);
        }
        if (isEditable(gwtJahiaNode)) {
            actions.add(indentButton);
        }
        actions.show();
        tab.setProcessed(true);
        readOnly = engine.getNode() != null && engine.getNode().isLocked();
        if (codeField != null) {
            codeField.setReadOnly(readOnly);
        }
    }
}
Also used : Label(com.extjs.gxt.ui.client.widget.Label) RpcMap(com.extjs.gxt.ui.client.data.RpcMap) BorderLayout(com.extjs.gxt.ui.client.widget.layout.BorderLayout) GWTJahiaNodeProperty(org.jahia.ajax.gwt.client.data.definition.GWTJahiaNodeProperty) Button(com.extjs.gxt.ui.client.widget.button.Button) HorizontalPanel(com.extjs.gxt.ui.client.widget.HorizontalPanel) CenterLayout(com.extjs.gxt.ui.client.widget.layout.CenterLayout) BorderLayoutData(com.extjs.gxt.ui.client.widget.layout.BorderLayoutData) ComboBox(com.extjs.gxt.ui.client.widget.form.ComboBox) LayoutContainer(com.extjs.gxt.ui.client.widget.LayoutContainer) StoreFilter(com.extjs.gxt.ui.client.store.StoreFilter) GWTJahiaValueDisplayBean(org.jahia.ajax.gwt.client.data.GWTJahiaValueDisplayBean) Map(java.util.Map) RpcMap(com.extjs.gxt.ui.client.data.RpcMap)

Aggregations

RpcMap (com.extjs.gxt.ui.client.data.RpcMap)1 StoreFilter (com.extjs.gxt.ui.client.store.StoreFilter)1 HorizontalPanel (com.extjs.gxt.ui.client.widget.HorizontalPanel)1 Label (com.extjs.gxt.ui.client.widget.Label)1 LayoutContainer (com.extjs.gxt.ui.client.widget.LayoutContainer)1 Button (com.extjs.gxt.ui.client.widget.button.Button)1 ComboBox (com.extjs.gxt.ui.client.widget.form.ComboBox)1 BorderLayout (com.extjs.gxt.ui.client.widget.layout.BorderLayout)1 BorderLayoutData (com.extjs.gxt.ui.client.widget.layout.BorderLayoutData)1 CenterLayout (com.extjs.gxt.ui.client.widget.layout.CenterLayout)1 Map (java.util.Map)1 GWTJahiaValueDisplayBean (org.jahia.ajax.gwt.client.data.GWTJahiaValueDisplayBean)1 GWTJahiaNodeProperty (org.jahia.ajax.gwt.client.data.definition.GWTJahiaNodeProperty)1