Search in sources :

Example 1 with TreePanelSelectionModel

use of com.extjs.gxt.ui.client.widget.treepanel.TreePanelSelectionModel in project kapua by eclipse.

the class DeviceConfigComponents method initConfigPanel.

@SuppressWarnings("unchecked")
private void initConfigPanel() {
    m_configPanel = new ContentPanel();
    m_configPanel.setBorders(false);
    m_configPanel.setBodyBorder(false);
    m_configPanel.setHeaderVisible(false);
    m_configPanel.setStyleAttribute("background-color", "white");
    m_configPanel.setScrollMode(Scroll.AUTO);
    BorderLayout borderLayout = new BorderLayout();
    m_configPanel.setLayout(borderLayout);
    // center
    m_centerData = new BorderLayoutData(LayoutRegion.CENTER);
    m_centerData.setMargins(new Margins(0));
    // west
    BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 200);
    westData.setSplit(true);
    westData.setCollapsible(true);
    westData.setMargins(new Margins(0, 5, 0, 0));
    // loader and store
    RpcProxy<List<GwtConfigComponent>> proxy = new RpcProxy<List<GwtConfigComponent>>() {

        @Override
        protected void load(Object loadConfig, AsyncCallback<List<GwtConfigComponent>> callback) {
            if (m_selectedDevice != null && m_dirty && m_initialized) {
                if (m_selectedDevice.isOnline()) {
                    m_tree.mask(MSGS.loading());
                    gwtDeviceManagementService.findDeviceConfigurations(m_selectedDevice, callback);
                } else {
                    List<GwtConfigComponent> comps = new ArrayList<GwtConfigComponent>();
                    GwtConfigComponent comp = new GwtConfigComponent();
                    comp.setId(MSGS.deviceNoDeviceSelected());
                    comp.setName(MSGS.deviceNoComponents());
                    comp.setDescription(MSGS.deviceNoConfigSupported());
                    comps.add(comp);
                    callback.onSuccess(comps);
                }
            } else {
                List<GwtConfigComponent> comps = new ArrayList<GwtConfigComponent>();
                GwtConfigComponent comp = new GwtConfigComponent();
                comp.setId(MSGS.deviceNoDeviceSelected());
                comp.setName(MSGS.deviceNoDeviceSelected());
                comp.setDescription(MSGS.deviceNoDeviceSelected());
                comps.add(comp);
                callback.onSuccess(comps);
            }
            m_dirty = false;
        }
    };
    m_loader = new BaseTreeLoader<GwtConfigComponent>(proxy);
    m_loader.addLoadListener(new DataLoadListener());
    m_treeStore = new TreeStore<ModelData>(m_loader);
    m_tree = new TreePanel<ModelData>(m_treeStore);
    m_tree.setWidth(200);
    m_tree.setDisplayProperty("componentName");
    m_tree.setBorders(true);
    m_tree.setAutoSelect(true);
    m_tree.setStyleAttribute("background-color", "white");
    m_configPanel.add(m_tree, westData);
    // 
    // Selection Listener for the component
    // make sure the form is not dirty before switching.
    m_tree.getSelectionModel().addListener(Events.BeforeSelect, new Listener<BaseEvent>() {

        @SuppressWarnings("rawtypes")
        @Override
        public void handleEvent(BaseEvent be) {
            final BaseEvent theEvent = be;
            SelectionEvent<ModelData> se = (SelectionEvent<ModelData>) be;
            final GwtConfigComponent componentToSwitchTo = (GwtConfigComponent) se.getModel();
            if (m_devConfPanel != null && m_devConfPanel.isDirty()) {
                // cancel the event first
                theEvent.setCancelled(true);
                // need to reselect the current entry
                // as the BeforeSelect event cleared it
                // we need to do this without raising events
                TreePanelSelectionModel selectionModel = (TreePanelSelectionModel) m_tree.getSelectionModel();
                selectionModel.setFiresEvents(false);
                selectionModel.select(false, m_devConfPanel.getConfiguration());
                selectionModel.setFiresEvents(true);
                // ask for confirmation before switching
                MessageBox.confirm(MSGS.confirm(), MSGS.deviceConfigDirty(), new Listener<MessageBoxEvent>() {

                    public void handleEvent(MessageBoxEvent ce) {
                        // if confirmed, delete
                        Dialog dialog = ce.getDialog();
                        if (dialog.yesText.equals(ce.getButtonClicked().getText())) {
                            m_devConfPanel.removeFromParent();
                            m_devConfPanel = null;
                            m_tree.getSelectionModel().select(false, componentToSwitchTo);
                        }
                    }
                });
            } else {
                refreshConfigPanel(componentToSwitchTo);
                // this is needed to select the item in the Tree
                // Temporarly disable the firing of the selection events
                // to avoid an infinite loop as BeforeSelect would be invoked again.
                TreePanelSelectionModel selectionModel = (TreePanelSelectionModel) m_tree.getSelectionModel();
                selectionModel.setFiresEvents(false);
                selectionModel.select(false, componentToSwitchTo);
                // renable firing of the events
                selectionModel.setFiresEvents(true);
            }
        }
    });
    m_tree.setIconProvider(new ModelIconProvider<ModelData>() {

        public AbstractImagePrototype getIcon(ModelData model) {
            if (model instanceof GwtConfigComponent) {
                String icon = ((GwtConfigComponent) model).getComponentIcon();
                if ("CloudService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.cloud16());
                } else if ("ClockService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.clock16());
                } else if ("DataService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.databaseConnect());
                } else if ("DiagnosticsService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.diagnostics());
                } else if ("MqttDataTransport".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.mqtt());
                } else if ("PositionService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.gps16());
                } else if ("SslManagerService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.lock16());
                } else if ("WatchdogService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.dog16());
                } else if ("CommandPasswordService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.terminal());
                } else if ("VpnService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.vpn());
                } else if ("ProvisioningService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.provisioning16());
                } else if ("DenaliService".equals(icon)) {
                    // WebConsole: DenaliService
                    return AbstractImagePrototype.create(Resources.INSTANCE.monitorDenali());
                } else if ("BluetoothService".equals(icon)) {
                    // Bluetooth: BluetoothService
                    return AbstractImagePrototype.create(Resources.INSTANCE.bluetooth());
                } else if (icon != null && icon.toLowerCase().startsWith("img://")) {
                    // Replace the base fake protocol with the console base URL
                    // that was not available on the server side.
                    icon = icon.replaceFirst("img://", GWT.getHostPageBaseURL());
                    return new ScaledAbstractImagePrototype(IconHelper.createPath(icon, 16, 16));
                } else {
                    return AbstractImagePrototype.create(Resources.INSTANCE.plugin16());
                }
            }
            return null;
        }
    });
}
Also used : ScaledAbstractImagePrototype(org.eclipse.kapua.app.console.client.util.ScaledAbstractImagePrototype) AbstractImagePrototype(com.google.gwt.user.client.ui.AbstractImagePrototype) ModelData(com.extjs.gxt.ui.client.data.ModelData) Listener(com.extjs.gxt.ui.client.event.Listener) KapuaLoadListener(org.eclipse.kapua.app.console.client.util.KapuaLoadListener) SelectionListener(com.extjs.gxt.ui.client.event.SelectionListener) AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) ArrayList(java.util.ArrayList) RpcProxy(com.extjs.gxt.ui.client.data.RpcProxy) MessageBoxEvent(com.extjs.gxt.ui.client.event.MessageBoxEvent) BorderLayout(com.extjs.gxt.ui.client.widget.layout.BorderLayout) Dialog(com.extjs.gxt.ui.client.widget.Dialog) SelectionEvent(com.extjs.gxt.ui.client.event.SelectionEvent) Margins(com.extjs.gxt.ui.client.util.Margins) List(java.util.List) ArrayList(java.util.ArrayList) GwtConfigComponent(org.eclipse.kapua.app.console.shared.model.GwtConfigComponent) ScaledAbstractImagePrototype(org.eclipse.kapua.app.console.client.util.ScaledAbstractImagePrototype) BorderLayoutData(com.extjs.gxt.ui.client.widget.layout.BorderLayoutData) BaseEvent(com.extjs.gxt.ui.client.event.BaseEvent) TreePanelSelectionModel(com.extjs.gxt.ui.client.widget.treepanel.TreePanelSelectionModel) ContentPanel(com.extjs.gxt.ui.client.widget.ContentPanel)

Aggregations

ModelData (com.extjs.gxt.ui.client.data.ModelData)1 RpcProxy (com.extjs.gxt.ui.client.data.RpcProxy)1 BaseEvent (com.extjs.gxt.ui.client.event.BaseEvent)1 Listener (com.extjs.gxt.ui.client.event.Listener)1 MessageBoxEvent (com.extjs.gxt.ui.client.event.MessageBoxEvent)1 SelectionEvent (com.extjs.gxt.ui.client.event.SelectionEvent)1 SelectionListener (com.extjs.gxt.ui.client.event.SelectionListener)1 Margins (com.extjs.gxt.ui.client.util.Margins)1 ContentPanel (com.extjs.gxt.ui.client.widget.ContentPanel)1 Dialog (com.extjs.gxt.ui.client.widget.Dialog)1 BorderLayout (com.extjs.gxt.ui.client.widget.layout.BorderLayout)1 BorderLayoutData (com.extjs.gxt.ui.client.widget.layout.BorderLayoutData)1 TreePanelSelectionModel (com.extjs.gxt.ui.client.widget.treepanel.TreePanelSelectionModel)1 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)1 AbstractImagePrototype (com.google.gwt.user.client.ui.AbstractImagePrototype)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 KapuaLoadListener (org.eclipse.kapua.app.console.client.util.KapuaLoadListener)1 ScaledAbstractImagePrototype (org.eclipse.kapua.app.console.client.util.ScaledAbstractImagePrototype)1 GwtConfigComponent (org.eclipse.kapua.app.console.shared.model.GwtConfigComponent)1