Example usage for com.google.common.base Service state

List of usage examples for com.google.common.base Service state

Introduction

In this page you can find the example usage for com.google.common.base Service state.

Prototype

State state();

Source Link

Document

Returns the lifecycle state of the service.

Usage

From source file:com.notifier.desktop.Application.java

protected <T extends Service & Named> Future<State> startService(final T service, final String category) {
    logger.info("Starting [{}] {}", service.getName(), category == null ? "" : category);
    final Future<Service.State> future = service.start();
    ListenableFuture<State> listenableFuture = Futures.makeListenable(future);
    listenableFuture.addListener(new Runnable() {
        @Override/*w w  w  . j a va2s  . co  m*/
        public void run() {
            try {
                future.get();
            } catch (InterruptedException e) {
                return;
            } catch (ExecutionException e) {
                logger.error(
                        "Error starting [" + service.getName() + "]" + (category == null ? "" : " " + category),
                        e.getCause());
                notifyStartupError(e.getCause());
            }
        }
    }, executorService);
    return future;
}

From source file:com.notifier.desktop.view.PreferencesDialog.java

public void open() {
    try {/*ww w  .  ja va  2  s  .  co  m*/
        preferences.read();

        Shell parent = getParent();
        dialogShell = new Shell(parent, SWT.DIALOG_TRIM);
        dialogShell.setBackgroundMode(SWT.INHERIT_DEFAULT);

        FormLayout shellLayout = new FormLayout();
        shellLayout.marginBottom = 10;
        dialogShell.setLayout(shellLayout);
        dialogShell.setText(Application.NAME + " Preferences");
        dialogShell.addDisposeListener(new DisposeListener() {
            @Override
            public void widgetDisposed(DisposeEvent e) {
                try {
                    preferences.write();
                } catch (IOException ex) {
                    logger.error("Error saving preferences", ex);
                    Dialogs.showError(swtManager, "Error saving preferences",
                            "An error ocurred while saving preferences. Please, try again.", false);
                } finally {
                    swtManager.setShowingPreferencesDialog(false);
                    msnBroadcaster.setListener(null);
                }
            }
        });

        // General Group

        generalGroup = createPGroup();
        GridLayout generalGroupLayout = new GridLayout();
        generalGroupLayout.makeColumnsEqualWidth = true;
        generalGroup.setLayout(generalGroupLayout);
        FormData generalGroupLData = new FormData();
        generalGroupLData.top = new FormAttachment(0, 7);
        generalGroupLData.left = new FormAttachment(0, 10);
        generalGroupLData.right = new FormAttachment(100, -10);
        generalGroup.setLayoutData(generalGroupLData);
        generalGroup.setText("General Options");
        generalGroup.addExpandListener(new GroupExpandListener(ApplicationPreferences.Group.GENERAL));

        startAtLoginCheckbox = new Button(generalGroup, SWT.CHECK | SWT.LEFT);
        GridData startAtLoginCheckboxLData = new GridData();
        startAtLoginCheckboxLData.horizontalIndent = 5;
        startAtLoginCheckbox.setLayoutData(startAtLoginCheckboxLData);
        startAtLoginCheckbox.setText("Start at login");
        startAtLoginCheckbox.setToolTipText("Start Android Notifier Desktop when you login");
        startAtLoginCheckbox.setSelection(preferences.isStartAtLogin());
        startAtLoginCheckbox.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                if (OperatingSystems.CURRENT_FAMILY == Family.MAC) {
                    Dialogs.showError(swtManager, "Start at Login Support",
                            "Please, use your system Startup Manager to start me at login.", false);
                    startAtLoginCheckbox.setSelection(false);
                } else {
                    final boolean enabled = startAtLoginCheckbox.getSelection();
                    executorService.execute(new Runnable() {
                        @Override
                        public void run() {
                            if (application.adjustStartAtLogin(enabled, false)) {
                                preferences.setStartAtLogin(enabled);
                            } else {
                                preferences.setStartAtLogin(!enabled);
                                swtManager.update(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (!startAtLoginCheckbox.isDisposed()) {
                                            startAtLoginCheckbox.setSelection(!enabled);
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });

        privateModeCheckbox = new Button(generalGroup, SWT.CHECK | SWT.LEFT);
        GridData privateModeCheckboxLData = new GridData();
        privateModeCheckboxLData.horizontalIndent = 5;
        privateModeCheckbox.setLayoutData(privateModeCheckboxLData);
        privateModeCheckbox.setText("Private mode");
        privateModeCheckbox.setToolTipText(
                "Notifications will not show contact details, for example, names and phone numbers");
        privateModeCheckbox.setSelection(preferences.isPrivateMode());
        privateModeCheckbox.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                boolean enabled = privateModeCheckbox.getSelection();
                notificationManager.setPrivateMode(enabled);
                preferences.setPrivateMode(enabled);
            }
        });
        // group.setExpanded() must be called after initialization of the group contents
        // so nested widgets are correctly hidden if group is not expanded
        generalGroup.setExpanded(preferences.isGroupExpanded(ApplicationPreferences.Group.GENERAL));

        // Notification Reception Group

        notificationReceptionMethodsGroup = createPGroup();
        GridLayout notificationMethodsGroupLayout = new GridLayout();
        notificationMethodsGroupLayout.numColumns = 2;
        notificationReceptionMethodsGroup.setLayout(notificationMethodsGroupLayout);
        FormData notificationMethodsGroupLData = new FormData();
        notificationMethodsGroupLData.top = new FormAttachment(generalGroup, 2);
        notificationMethodsGroupLData.left = new FormAttachment(0, 10);
        notificationMethodsGroupLData.right = new FormAttachment(100, -10);
        notificationReceptionMethodsGroup.setLayoutData(notificationMethodsGroupLData);
        notificationReceptionMethodsGroup.setText("Notification Reception Methods");
        notificationReceptionMethodsGroup
                .addExpandListener(new GroupExpandListener(ApplicationPreferences.Group.RECEPTION));

        wifiCheckbox = new Button(notificationReceptionMethodsGroup, SWT.CHECK | SWT.LEFT);
        GridData wifiCheckboxLData = new GridData();
        wifiCheckboxLData.horizontalIndent = 5;
        wifiCheckboxLData.horizontalSpan = 2;
        wifiCheckbox.setLayoutData(wifiCheckboxLData);

        // This description is for information and help configuring the android app
        // This app will bind to all available network interfaces to maximize chances of success
        String hostDescription;
        String localHostName = networkManager.getLocalHostName();
        String localHostAddress = networkManager.getLocalHostAddress().getHostAddress();
        if (localHostAddress == null) {
            hostDescription = "";
        } else {
            hostDescription = "(";
            hostDescription += Strings.isNullOrEmpty(localHostName) ? "" : localHostName + " / ";
            hostDescription += localHostAddress;
            hostDescription += ")";
        }
        wifiCheckbox.setText("Wifi " + hostDescription);
        wifiCheckbox.setSelection(preferences.isReceptionWithWifi());
        wifiCheckbox.setToolTipText(
                "Use this address to setup your device to send notifications on local network or setup your router to forward connections incoming over cell network. Make sure port 10600 is open in firewall, see Setup wiki page for more information");
        wifiCheckbox.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                final boolean enabled = wifiCheckbox.getSelection();
                if (!enabled && preferences.isReceptionWithUpnp()) {
                    application.adjustUpnpManager(false);
                    preferences.setReceptionWithUpnp(false);
                    swtManager.update(new Runnable() {
                        @Override
                        public void run() {
                            if (!internetCheckbox.isDisposed()) {
                                internetCheckbox.setSelection(false);
                            }
                        }
                    });
                }
                Future<Service.State> future = application.adjustWifiTransport(enabled);
                new ServiceAdjustListener(future, wifiCheckbox, new PreferenceSetter() {
                    @Override
                    public void onSuccess() {
                        preferences.setReceptionWithWifi(enabled);
                    }

                    @Override
                    public void onError() {
                        preferences.setReceptionWithWifi(false);
                    }
                }).start();
            }
        });

        internetCheckbox = new Button(notificationReceptionMethodsGroup, SWT.CHECK | SWT.LEFT);
        GridData internetCheckboxLData = new GridData();
        internetCheckboxLData.horizontalIndent = 5;
        internetCheckboxLData.horizontalSpan = 2;
        internetCheckbox.setLayoutData(internetCheckboxLData);
        internetCheckbox.setText("Internet (UPnP)");
        internetCheckbox.setSelection(preferences.isReceptionWithUpnp());
        internetCheckbox.setToolTipText("Make " + Application.NAME
                + " configure port forwarding automatically in your router to get notifications over cell network, see Setup wiki page for more information");
        internetCheckbox.setEnabled(preferences.isReceptionWithWifi());
        internetCheckbox.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                final boolean enabled = internetCheckbox.getSelection();
                Future<Service.State> future = application.adjustUpnpManager(enabled);
                new ServiceAdjustListener(future, internetCheckbox, new PreferenceSetter() {
                    @Override
                    public void onSuccess() {
                        preferences.setReceptionWithUpnp(enabled);
                    }

                    @Override
                    public void onError() {
                        preferences.setReceptionWithUpnp(false);
                    }
                }).start();
            }
        });
        wifiCheckbox.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                internetCheckbox.setEnabled(wifiCheckbox.getSelection());
            }
        });

        bluetoothCheckbox = new Button(notificationReceptionMethodsGroup, SWT.CHECK | SWT.LEFT);
        GridData bluetoothCheckboxLData = new GridData();
        bluetoothCheckboxLData.horizontalIndent = 5;
        bluetoothCheckboxLData.horizontalSpan = 2;
        bluetoothCheckbox.setLayoutData(bluetoothCheckboxLData);
        bluetoothCheckbox.setText("Bluetooth");
        bluetoothCheckbox.setSelection(preferences.isReceptionWithBluetooth());
        bluetoothCheckbox.setToolTipText("See how-to on the project wiki to setup bluetooth connections");
        bluetoothCheckbox.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                final boolean enabled = bluetoothCheckbox.getSelection();
                Future<Service.State> future = application.adjustBluetoothTransport(enabled);
                new ServiceAdjustListener(future, bluetoothCheckbox, new PreferenceSetter() {
                    @Override
                    public void onSuccess() {
                        preferences.setReceptionWithBluetooth(enabled);
                    }

                    @Override
                    public void onError() {
                        preferences.setReceptionWithBluetooth(false);
                    }
                }).start();
            }
        });

        usbCheckbox = new Button(notificationReceptionMethodsGroup, SWT.CHECK | SWT.LEFT);
        GridData usbCheckboxLData = new GridData();
        usbCheckboxLData.horizontalIndent = 5;
        usbCheckbox.setLayoutData(usbCheckboxLData);
        usbCheckbox.setText("USB");
        usbCheckbox.setSelection(preferences.isReceptionWithUsb());
        usbCheckbox.setToolTipText("Receive notifications over USB");
        usbCheckbox.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                final boolean enabled = usbCheckbox.getSelection();
                if (enabled && preferences.getAndroidSdkHome().isEmpty()) {
                    selectAndroidSdkHome();
                    if (preferences.getAndroidSdkHome().isEmpty()) {
                        usbCheckbox.setSelection(false);
                        return;
                    }
                }
                Future<Service.State> future = application.adjustUsbTransport(enabled);
                new ServiceAdjustListener(future, usbCheckbox, new PreferenceSetter() {
                    @Override
                    public void onSuccess() {
                        preferences.setReceptionWithUsb(enabled);
                    }

                    @Override
                    public void onError() {
                        preferences.setReceptionWithUsb(false);
                    }
                }).start();
            }
        });

        final Button sdkHomeButton = new Button(notificationReceptionMethodsGroup, SWT.PUSH | SWT.CENTER);
        GridData sdkHomeButtonLData = new GridData();
        sdkHomeButton.setLayoutData(sdkHomeButtonLData);
        sdkHomeButton.setText("Select Android SDK directory...");
        sdkHomeButton.setEnabled(preferences.isReceptionWithUsb());
        sdkHomeButton.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                selectAndroidSdkHome();
            }
        });
        usbCheckbox.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                sdkHomeButton.setEnabled(usbCheckbox.getSelection());
            }
        });

        encryptCommunicationCheckbox = new Button(notificationReceptionMethodsGroup, SWT.CHECK | SWT.LEFT);
        GridData encryptCommunicationCheckboxLData = new GridData();
        encryptCommunicationCheckboxLData.horizontalIndent = 5;
        encryptCommunicationCheckboxLData.horizontalSpan = 2;
        encryptCommunicationCheckbox.setLayoutData(encryptCommunicationCheckboxLData);
        encryptCommunicationCheckbox.setText("Decrypt notifications");
        encryptCommunicationCheckbox.setSelection(preferences.isEncryptCommunication());
        encryptCommunicationCheckbox.setToolTipText("\"Encrypt notifications\" must be enabled in android app");
        encryptCommunicationCheckbox.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                boolean enabled = encryptCommunicationCheckbox.getSelection();
                byte[] key = new byte[0];
                notificationParser.setEncryption(enabled, key);
                preferences.setEncryptCommunication(enabled);
                preferences.setCommunicationPassword(key);
                communicationPasswordText.setMessage(PASSWORD_NOT_SET_MESSAGE);
            }
        });

        Label communicationPasswordLabel = new Label(notificationReceptionMethodsGroup, SWT.NONE);
        GridData communicationPasswordLabelLData = new GridData();
        communicationPasswordLabelLData.horizontalIndent = 5;
        communicationPasswordLabel.setLayoutData(communicationPasswordLabelLData);
        communicationPasswordLabel.setText("Passphrase:");

        communicationPasswordText = new Text(notificationReceptionMethodsGroup,
                SWT.BORDER | SWT.SINGLE | SWT.PASSWORD);
        GridData communicationPasswordTextLData = new GridData();
        communicationPasswordTextLData.grabExcessHorizontalSpace = true;
        communicationPasswordTextLData.horizontalAlignment = GridData.FILL;
        communicationPasswordText.setLayoutData(communicationPasswordTextLData);
        if (preferences.getCommunicationPassword().length > 0) {
            communicationPasswordText.setMessage(PASSWORD_SET_MESSAGE);
        } else {
            communicationPasswordText.setMessage(PASSWORD_NOT_SET_MESSAGE);
        }
        if (OperatingSystems.CURRENT_FAMILY == OperatingSystems.Family.LINUX) {
            communicationPasswordText.setEditable(preferences.isEncryptCommunication());
        } else {
            communicationPasswordText.setEnabled(preferences.isEncryptCommunication());
        }
        communicationPasswordText.setToolTipText("Type the security passphrase set in your device");
        communicationPasswordText.addKeyListener(new KeyListener() {
            @Override
            public void keyPressed(KeyEvent e) {
                // Do nothing
            }

            @Override
            public void keyReleased(KeyEvent e) {
                communicationPasswordChanged = true;
                String password = communicationPasswordText.getText();
                setCommunicationPassword(password);
            }
        });
        communicationPasswordText.addListener(SWT.FocusIn, new Listener() {
            @Override
            public void handleEvent(Event event) {
                communicationPasswordChanged = false;
            }
        });
        communicationPasswordText.addListener(SWT.FocusOut, new Listener() {
            @Override
            public void handleEvent(Event event) {
                if (communicationPasswordChanged) {
                    String password = communicationPasswordText.getText();
                    setCommunicationPassword(password);
                    if (communicationPasswordText.getText().length() == 0) {
                        communicationPasswordText.setMessage(PASSWORD_NOT_SET_MESSAGE);
                    } else {
                        communicationPasswordText.setMessage(PASSWORD_SET_MESSAGE);
                    }
                }
                communicationPasswordText.setText("");
            }
        });
        encryptCommunicationCheckbox.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                boolean enabled = encryptCommunicationCheckbox.getSelection();
                if (OperatingSystems.CURRENT_FAMILY == OperatingSystems.Family.LINUX) {
                    communicationPasswordText.setEditable(enabled);
                } else {
                    communicationPasswordText.setEnabled(enabled);
                }
            }
        });
        notificationReceptionMethodsGroup
                .setExpanded(preferences.isGroupExpanded(ApplicationPreferences.Group.RECEPTION));

        // Notification Display Group

        notificationDisplayMethodsGroup = createPGroup();
        GridLayout notificationDisplayMethodsGroupLayout = new GridLayout();
        notificationDisplayMethodsGroupLayout.numColumns = 2;
        notificationDisplayMethodsGroup.setLayout(notificationDisplayMethodsGroupLayout);
        FormData notificationDisplayMethodsGroupLData = new FormData();
        notificationDisplayMethodsGroupLData.top = new FormAttachment(notificationReceptionMethodsGroup, 2);
        notificationDisplayMethodsGroupLData.left = new FormAttachment(0, 10);
        notificationDisplayMethodsGroupLData.right = new FormAttachment(100, -10);
        notificationDisplayMethodsGroup.setLayoutData(notificationDisplayMethodsGroupLData);
        notificationDisplayMethodsGroup.setText("Notification Display Methods");
        notificationDisplayMethodsGroup
                .addExpandListener(new GroupExpandListener(ApplicationPreferences.Group.DISPLAY));

        systemDefaultCheckbox = new Button(notificationDisplayMethodsGroup, SWT.CHECK | SWT.LEFT);
        GridData systemDefaultCheckboxLData = new GridData();
        systemDefaultCheckboxLData.horizontalIndent = 5;
        systemDefaultCheckboxLData.horizontalSpan = 2;
        systemDefaultCheckbox.setLayoutData(systemDefaultCheckboxLData);
        systemDefaultCheckbox.setText("System default");
        systemDefaultCheckbox.setToolTipText("Use system default mechanism to show notifications");
        systemDefaultCheckbox.setSelection(preferences.isDisplayWithSystemDefault());
        systemDefaultCheckbox.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                final boolean enabled = systemDefaultCheckbox.getSelection();
                Future<Service.State> future = application.adjustSystemDefaultBroadcaster(enabled);
                new ServiceAdjustListener(future, systemDefaultCheckbox, new PreferenceSetter() {
                    @Override
                    public void onSuccess() {
                        preferences.setDisplayWithSystemDefault(enabled);
                    }

                    @Override
                    public void onError() {
                        preferences.setDisplayWithSystemDefault(false);
                    }
                }).start();
            }
        });

        growlCheckbox = new Button(notificationDisplayMethodsGroup, SWT.CHECK | SWT.LEFT);
        GridData growlCheckboxLData = new GridData();
        growlCheckboxLData.horizontalIndent = 5;
        growlCheckboxLData.horizontalSpan = 2;
        growlCheckbox.setLayoutData(growlCheckboxLData);
        growlCheckbox.setText("Growl Notification Transport Protocol");
        growlCheckbox.setSelection(preferences.isDisplayWithGrowl());
        growlCheckbox.setToolTipText(
                "Use GNTP to show notifications, network notifications must be enabled in Growl, see how-to on the project wiki to setup");
        growlCheckbox.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                final boolean enabled = growlCheckbox.getSelection();
                Future<Service.State> future = application.adjustGrowlBroadcaster(enabled);
                new ServiceAdjustListener(future, growlCheckbox, new PreferenceSetter() {
                    @Override
                    public void onSuccess() {
                        preferences.setDisplayWithGrowl(enabled);
                    }

                    @Override
                    public void onError() {
                        preferences.setDisplayWithGrowl(false);
                    }
                }).start();
            }
        });

        if (OperatingSystems.CURRENT_FAMILY == Family.LINUX) {
            libnotifyCheckbox = new Button(notificationDisplayMethodsGroup, SWT.CHECK | SWT.LEFT);
            GridData libnotifyCheckboxLData = new GridData();
            libnotifyCheckboxLData.horizontalIndent = 5;
            libnotifyCheckboxLData.horizontalSpan = 2;
            libnotifyCheckbox.setLayoutData(libnotifyCheckboxLData);
            libnotifyCheckbox.setText("Libnotify");
            libnotifyCheckbox.setToolTipText("Use notify-send command to show notifications");
            libnotifyCheckbox.setSelection(preferences.isDisplayWithLibnotify());
            libnotifyCheckbox.addListener(SWT.Selection, new Listener() {
                @Override
                public void handleEvent(Event event) {
                    final boolean enabled = libnotifyCheckbox.getSelection();
                    Future<Service.State> future = application.adjustLibnotifyBroadcaster(enabled);
                    new ServiceAdjustListener(future, libnotifyCheckbox, new PreferenceSetter() {
                        @Override
                        public void onSuccess() {
                            preferences.setDisplayWithLibnotify(enabled);
                        }

                        @Override
                        public void onError() {
                            preferences.setDisplayWithLibnotify(false);
                        }
                    }).start();
                }
            });
        }

        msnCheckbox = new Button(notificationDisplayMethodsGroup, SWT.CHECK | SWT.LEFT);
        GridData msnCheckboxLData = new GridData();
        msnCheckboxLData.horizontalIndent = 5;
        msnCheckbox.setLayoutData(msnCheckboxLData);
        switch (msnBroadcaster.state()) {
        case NEW:
        case FAILED:
        case TERMINATED:
            msnCheckbox.setText(MSN_OFFLINE_LABEL);
            break;
        case STARTING:
            msnCheckbox.setText(MSN_LOGGING_IN_LABEL);
            break;
        case RUNNING:
            msnCheckbox.setText(MSN_ONLINE_LABEL);
            break;
        case STOPPING:
            msnCheckbox.setText(MSN_LOGGING_OUT_LABEL);
            break;
        default:
            throw new IllegalStateException("Unknown msn broadcaster state: " + msnBroadcaster.state());
        }
        msnCheckbox.setSelection(preferences.isDisplayWithMsn());
        msnCheckbox.setToolTipText("Send notifications over Windows Live instant messaging");
        msnCheckbox.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                final boolean enabled = msnCheckbox.getSelection();
                if (Strings.isNullOrEmpty(preferences.getMsnUsername())) {
                    showMsnDetailDialog();
                } else {
                    Future<Service.State> future = application.adjustMsnBroadcaster(enabled);
                    new ServiceAdjustListener(future, msnCheckbox, new PreferenceSetter() {
                        @Override
                        public void onSuccess() {
                            preferences.setDisplayWithMsn(enabled);
                        }

                        @Override
                        public void onError() {
                            preferences.setDisplayWithMsn(false);
                        }
                    }).start();
                }
            }
        });
        msnBroadcaster.setListener(new InstantMessagingNotificationBroadcaster.Listener() {
            @Override
            public void loggingIn() {
                swtManager.update(new Runnable() {
                    @Override
                    public void run() {
                        if (!msnCheckbox.isDisposed()) {
                            msnCheckbox.setText(MSN_LOGGING_IN_LABEL);
                        }
                    }
                });
            }

            @Override
            public void loggedIn() {
                swtManager.update(new Runnable() {
                    @Override
                    public void run() {
                        if (!msnCheckbox.isDisposed()) {
                            msnCheckbox.setText(MSN_ONLINE_LABEL);
                        }
                    }
                });
            }

            @Override
            public void loggingOut() {
                swtManager.update(new Runnable() {
                    @Override
                    public void run() {
                        if (!msnCheckbox.isDisposed()) {
                            msnCheckbox.setText(MSN_LOGGING_OUT_LABEL);
                        }
                    }
                });
            }

            @Override
            public void loggedOut() {
                swtManager.update(new Runnable() {
                    @Override
                    public void run() {
                        if (!msnCheckbox.isDisposed()) {
                            msnCheckbox.setText(MSN_OFFLINE_LABEL);
                        }
                    }
                });
            }
        });

        msnDetailButton = new Button(notificationDisplayMethodsGroup, SWT.PUSH | SWT.CENTER);
        GridData msnDetailButtonLData = new GridData();
        msnDetailButton.setLayoutData(msnDetailButtonLData);
        msnDetailButton.setText("Details...");
        msnDetailButton.setEnabled(msnCheckbox.getSelection());
        msnDetailButton.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                showMsnDetailDialog();
            }
        });
        msnCheckbox.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                msnDetailButton.setEnabled(msnCheckbox.getSelection());
            }
        });

        notificationDisplayMethodsGroup
                .setExpanded(preferences.isGroupExpanded(ApplicationPreferences.Group.DISPLAY));

        // Notification Actions Groups
        notificationActionsGroup = createPGroup();
        FillLayout notificationActionsGroupLayout = new FillLayout();
        notificationActionsGroupLayout.marginHeight = 5;
        notificationActionsGroup.setLayout(notificationActionsGroupLayout);
        FormData notificationActionsGroupLData = new FormData();
        notificationActionsGroupLData.top = new FormAttachment(notificationDisplayMethodsGroup, 2);
        notificationActionsGroupLData.left = new FormAttachment(0, 10);
        notificationActionsGroupLData.right = new FormAttachment(100, -10);
        notificationActionsGroup.setLayoutData(notificationActionsGroupLData);
        notificationActionsGroup.setText("Notification Actions");
        notificationActionsGroup
                .addExpandListener(new GroupExpandListener(ApplicationPreferences.Group.ACTION));

        notificationTypesTabFolder = new TabFolder(notificationActionsGroup, SWT.NONE);
        for (Notification.Type type : Notification.Type.values()) {
            createNotificationTypeTabItem(notificationTypesTabFolder, type);
        }
        notificationTypesTabFolder.pack();
        notificationActionsGroup.setExpanded(preferences.isGroupExpanded(ApplicationPreferences.Group.ACTION));

        // Devices Group

        devicesGroup = createPGroup();
        GridLayout devicesGroupLayout = new GridLayout();
        devicesGroupLayout.numColumns = 2;
        devicesGroup.setLayout(devicesGroupLayout);
        FormData devicesGroupLData = new FormData();
        devicesGroupLData.top = new FormAttachment(notificationActionsGroup, 2);
        devicesGroupLData.left = new FormAttachment(0, 10);
        devicesGroupLData.right = new FormAttachment(100, -10);
        devicesGroup.setLayoutData(devicesGroupLData);
        devicesGroup.setText("Devices");
        devicesGroup.addExpandListener(new GroupExpandListener(ApplicationPreferences.Group.PAIRING));

        anyDeviceRadioButton = new Button(devicesGroup, SWT.RADIO | SWT.LEFT);
        GridData anyDeviceComboBoxLData = new GridData();
        anyDeviceComboBoxLData.horizontalIndent = 5;
        anyDeviceComboBoxLData.horizontalSpan = 2;
        anyDeviceRadioButton.setLayoutData(anyDeviceComboBoxLData);
        anyDeviceRadioButton.setText("Receive notifications from any device");
        anyDeviceRadioButton.setSelection(preferences.isReceptionFromAnyDevice());
        anyDeviceRadioButton.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                deviceManager.setReceptionFromAnyDevice(anyDeviceRadioButton.getSelection());
                preferences.setReceptionFromAnyDevice(anyDeviceRadioButton.getSelection());
                devicesList.setEnabled(false);
                addDeviceButton.setEnabled(false);
                removeDeviceButton.setEnabled(false);
            }
        });

        onlyDeviceRadioButton = new Button(devicesGroup, SWT.RADIO | SWT.LEFT);
        GridData onlyDeviceRadioButtonLData = new GridData();
        onlyDeviceRadioButtonLData.horizontalIndent = 5;
        onlyDeviceRadioButtonLData.horizontalSpan = 2;
        onlyDeviceRadioButton.setLayoutData(onlyDeviceRadioButtonLData);
        onlyDeviceRadioButton.setText("Receive notifications only from these devices");
        onlyDeviceRadioButton.setSelection(!preferences.isReceptionFromAnyDevice());
        onlyDeviceRadioButton.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                deviceManager.setReceptionFromAnyDevice(!onlyDeviceRadioButton.getSelection());
                preferences.setReceptionFromAnyDevice(!onlyDeviceRadioButton.getSelection());
                devicesList.setEnabled(true);
                addDeviceButton.setEnabled(true);
                removeDeviceButton.setEnabled(true);
            }
        });

        GridData devicesListLData = new GridData();
        devicesListLData.heightHint = 63;
        devicesListLData.verticalSpan = 2;
        devicesListLData.grabExcessHorizontalSpace = true;
        devicesListLData.horizontalAlignment = GridData.FILL;
        devicesList = new List(devicesGroup, SWT.SINGLE | SWT.BORDER);
        devicesList.setLayoutData(devicesListLData);
        devicesList.setEnabled(!preferences.isReceptionFromAnyDevice());
        pairedDevices.putAll(preferences.getAllowedDevices());
        for (String name : pairedDevices.values()) {
            devicesList.add(name);
        }

        addDeviceButton = new Button(devicesGroup, SWT.PUSH | SWT.CENTER);
        GridData addDeviceButtonLData = new GridData();
        addDeviceButtonLData.widthHint = 76;
        addDeviceButtonLData.heightHint = 29;
        addDeviceButton.setLayoutData(addDeviceButtonLData);
        addDeviceButton.setText("Add");
        addDeviceButton.setEnabled(!preferences.isReceptionFromAnyDevice());
        addDeviceButton.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                final PairingDialog dialog = new PairingDialog(swtManager);
                dialog.open(new Listener() {
                    @Override
                    public void handleEvent(Event cancelEvent) {
                        deviceManager.cancelWaitForPairing();
                        dialog.close();
                    }
                });
                deviceManager.waitForPairing(new DeviceManager.PairingListener() {
                    @Override
                    public void onPairingSuccessful(final String deviceId) {
                        if (!preferences.getAllowedDevicesIds().contains(deviceId)) {
                            deviceManager.cancelWaitForPairing();
                            swtManager.update(new Runnable() {
                                @Override
                                public void run() {
                                    dialog.close();
                                    final DeviceEditorDialog deviceDialog = new DeviceEditorDialog(swtManager,
                                            deviceId, "My Android");
                                    deviceDialog.open(new DeviceEditorDialog.SubmitListener() {
                                        @Override
                                        public boolean onDeviceName(String name) {
                                            if (preferences.getAllowedDevicesNames().contains(name)) {
                                                return false;
                                            }
                                            preferences.addAllowedDeviceId(deviceId, name);
                                            pairedDevices.put(deviceId, name);
                                            devicesList.add(name);
                                            deviceManager.pairDevice(deviceId, name);
                                            deviceDialog.close();
                                            return true;
                                        }
                                    });
                                }
                            });
                        }
                    }
                });
            }
        });

        removeDeviceButton = new Button(devicesGroup, SWT.PUSH | SWT.CENTER);
        GridData removeDeviceButtonLData = new GridData();
        removeDeviceButtonLData.widthHint = 76;
        removeDeviceButtonLData.heightHint = 29;
        removeDeviceButton.setLayoutData(removeDeviceButtonLData);
        removeDeviceButton.setText("Remove");
        removeDeviceButton.setEnabled(!preferences.isReceptionFromAnyDevice());
        removeDeviceButton.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                String[] items = devicesList.getSelection();
                if (items.length > 0) {
                    String deviceName = items[0];
                    String deviceId = pairedDevices.inverse().get(deviceName);
                    preferences.removeAllowedDeviceId(deviceId, deviceName);
                    devicesList.remove(deviceName);
                    deviceManager.unpairDevice(deviceId);
                }
            }
        });
        devicesGroup.setExpanded(preferences.isGroupExpanded(ApplicationPreferences.Group.PAIRING));

        // OK/Cancel Buttons

        okButton = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
        FormData okButtonLData = new FormData();
        okButtonLData.width = 70;
        okButtonLData.height = 28;
        okButtonLData.top = new FormAttachment(devicesGroup, 6);
        okButtonLData.right = new FormAttachment(100, -10);
        okButton.setLayoutData(okButtonLData);
        okButton.setText("Save");
        okButton.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                dialogShell.close();
            }
        });

        dialogShell.layout();
        dialogShell.pack();

        Dialogs.centerDialog(dialogShell);
        dialogShell.open();
        Dialogs.bringToForeground(dialogShell);
        swtManager.setShowingPreferencesDialog(true);
    } catch (Exception e) {
        logger.error("Error showing preferences dialog", e);
    }
}

From source file:com.notifier.desktop.view.PreferencesDialog.java

protected void showMsnDetailDialog() {
    final InstantMessagingDetailDialog dialog = new InstantMessagingDetailDialog(swtManager,
            preferences.getMsnUsername(), preferences.getMsnPassword(), preferences.getMsnTarget());
    dialog.open(new InstantMessagingDetailDialog.SubmitListener() {
        private boolean testing;

        @Override/*from  w  w w .  ja v  a  2s.co m*/
        public void onTest(final String username, final String password, final String target) {
            testing = true;
            final Future<Service.State> stopFuture = msnBroadcaster.stop();
            Futures.makeListenable(stopFuture).addListener(new Runnable() {
                public void run() {
                    configureMsnBroadcaster(username, password, target);
                    final Future<Service.State> startFuture = msnBroadcaster.start();
                    Futures.makeListenable(startFuture).addListener(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                startFuture.get();
                                swtManager.update(new Runnable() {
                                    @Override
                                    public void run() {
                                        testing = false;
                                        dialog.testSuccessful();
                                    }
                                });
                            } catch (InterruptedException e) {
                                return;
                            } catch (ExecutionException e) {
                                testing = false;
                                swtManager.update(new Runnable() {
                                    @Override
                                    public void run() {
                                        dialog.testFailed();
                                    }
                                });
                            }
                        }
                    }, executorService);
                }
            }, executorService);
        }

        @Override
        public void onSubmit(String username, String password, String target) {
            preferences.setMsnUsername(username);
            preferences.setMsnPassword(password);
            preferences.setMsnTarget(target);
            if (configureMsnBroadcaster(username, password, target)) {
                Future<Service.State> future = msnBroadcaster.stop();
                Futures.makeListenable(future).addListener(new Runnable() {
                    public void run() {
                        application.adjustMsnBroadcaster(true);
                    }
                }, executorService);
            }
        }

        @Override
        public void onCancel() {
            if (Strings.isNullOrEmpty(preferences.getMsnUsername())) {
                msnCheckbox.setSelection(false);
                msnDetailButton.setEnabled(false);
            }
            if (testing) {
                msnBroadcaster.stop();
            }
        }

        public boolean configureMsnBroadcaster(String username, String password, String target) {
            boolean changed = false;

            String oldUsername = msnBroadcaster.getUsername();
            if (!Strings.nullToEmpty(oldUsername).equals(username)) {
                changed = true;
            }

            String oldPassword = msnBroadcaster.getPassword();
            if (!Strings.nullToEmpty(oldPassword).equals(password)) {
                changed = true;
            }

            String oldTarget = msnBroadcaster.getTargetUsername();
            if (!Strings.nullToEmpty(oldTarget).equals(target)) {
                changed = true;
            }

            msnBroadcaster.setUsername(username);
            msnBroadcaster.setPassword(password);
            msnBroadcaster.setTargetUsername(target);

            return changed;
        }
    });
}