static void initLookAndFeel() {
    MetalLookAndFeel mlf = new MetalLookAndFeel();
    mlf.setCurrentTheme(new SipCommunicatorColorTheme());

    try {
      UIManager.setLookAndFeel(mlf);
    } catch (UnsupportedLookAndFeelException ex) {
      console.error("Failed to set custom look and feel", ex);
    }
  }
Ejemplo n.º 2
0
 public static void main(String[] args) {
   Console.run(new Button2(), 200, 75);
 }
/**
 * The class is used to request user SubscriptionAuthorisations. When an incoming subscription is
 * received the user must approve or refuse it for notifications to be sent.
 *
 * <p>Company: Network Research Team, Louis Pasteur University
 *
 * @author Emil Ivov
 * @version 1.0
 */
public class SubscriptionAuthorizationDialog extends JDialog implements ActionListener {
  private static final Console console = Console.getConsole(SubscriptionAuthorizationDialog.class);
  private JPanel contentPane = new JPanel();
  private Border border1;
  private JPanel controlsPane = new JPanel();
  private BorderLayout borderLayout1 = new BorderLayout();
  private GridLayout gridLayout1 = new GridLayout();
  private JComboBox responsesComboBox = new JComboBox();
  private JPanel buttonsPane = new JPanel();
  private JButton okButton = new JButton();
  private FlowLayout flowLayout1 = new FlowLayout();
  private JEditorPane messagePane = new JEditorPane();

  private String messageString1 =
      "The following user has requested to subscribe for your presence status.";
  private String namePrefix = "Name: <b>";
  private String nameSuffix = "</b>";
  private String addressPrefix = "Address: <b>";
  private String addressSuffix = "</b>";
  private String messagePrefix = "Message: ";
  private String messageSuffix = "";
  private String messageString2 = "What would you like to do?";

  private String name = null;
  private String address = null;
  JScrollPane messageScrollPane = new JScrollPane();

  public SubscriptionAuthorizationDialog(JFrame owner) throws HeadlessException {
    super(owner, true);
    try {
      jbInit();
      initComponents();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void initComponents() {
    setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

    messagePane.setBackground(contentPane.getBackground());
    messagePane.setEditorKit(new HTMLEditorKit());
    messagePane.setForeground(contentPane.getForeground());

    setSize(450, 360);

    // center the window
    int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - getWidth()) / 2;
    int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - getHeight()) / 2;
    setLocation(x, y);

    okButton.addActionListener(this);
  }

  private void jbInit() throws Exception {
    border1 = BorderFactory.createEmptyBorder(20, 20, 20, 20);
    contentPane.setBorder(border1);
    contentPane.setLayout(borderLayout1);
    controlsPane.setLayout(gridLayout1);
    gridLayout1.setColumns(1);
    gridLayout1.setHgap(10);
    gridLayout1.setRows(0);
    gridLayout1.setVgap(10);
    okButton.setVerifyInputWhenFocusTarget(true);
    okButton.setMnemonic('O');
    okButton.setText("OK");
    buttonsPane.setLayout(flowLayout1);
    flowLayout1.setAlignment(FlowLayout.CENTER);
    messagePane.setEditable(false);
    messagePane.setText("");
    borderLayout1.setHgap(10);
    borderLayout1.setVgap(10);
    this.setTitle("Subscription Authorization");
    this.getContentPane().add(contentPane, BorderLayout.CENTER);
    contentPane.add(controlsPane, BorderLayout.SOUTH);
    controlsPane.add(responsesComboBox, null);
    controlsPane.add(buttonsPane, null);
    buttonsPane.add(okButton, null);
    contentPane.add(messageScrollPane, BorderLayout.CENTER);
    messageScrollPane.getViewport().add(messagePane, null);
  }

  private void setSubscriberDetails(
      String subscriberName, String subscriberAddress, String message) {
    this.name = subscriberName;
    this.address = subscriberAddress;

    Color color = messagePane.getForeground();

    messagePane.setText(
        "<FONT COLOR=\"#"
            + Integer.toHexString(color.getRGB()).substring(2)
            + "\">"
            + messageString1
            + "<p>"
            + namePrefix
            + name
            + nameSuffix
            + "<br>"
            + addressPrefix
            + address
            + addressSuffix
            + "<p>"
            + ((message != null && message.trim().length() > 0)
                ? messagePrefix + message + messageSuffix + "<p>"
                : "")
            + messageString2
            + "</FONT>");
  }

  public static String obtainAuthorisationResponse(
      JFrame owner, SubscriptionRequestUIModel request) {
    SubscriptionAuthorizationDialog sad = new SubscriptionAuthorizationDialog(owner);
    sad.setSubscriberDetails(
        request.getRequestingPartyDisplayName(),
        request.getRequestingPartyAddress(),
        request.getReasonPhrase());

    String[] responses = request.getAcceptedResponses();
    Vector resVector = new Vector();

    for (int i = 0; i < responses.length; i++) resVector.add(responses[i]);

    sad.responsesComboBox.setModel(new DefaultComboBoxModel(resVector));
    sad.responsesComboBox.setSelectedIndex(0);

    sad.show();
    return sad.responsesComboBox.getSelectedItem().toString();
  }

  public static void main(String[] args) {
    initLookAndFeel();
    String response =
        obtainAuthorisationResponse(
            null,
            new SubscriptionRequestUIModel() {
              public String[] getAcceptedResponses() {
                String[] arr = new String[3];
                arr[0] = "response 1";
                arr[1] = "ouaba daba";
                arr[2] = "ouaba dabaasdfaf";

                return arr;
              }

              public String getRequestingPartyDisplayName() {
                return "Emil Ivov";
              }

              public String getRequestingPartyAddress() {
                return "*****@*****.**";
              }

              public String getReasonPhrase() {
                return "Hello, could I please add you to my contact list?";
              }
            });

    System.out.println("Returned response = " + response);
  }

  static void initLookAndFeel() {
    MetalLookAndFeel mlf = new MetalLookAndFeel();
    mlf.setCurrentTheme(new SipCommunicatorColorTheme());

    try {
      UIManager.setLookAndFeel(mlf);
    } catch (UnsupportedLookAndFeelException ex) {
      console.error("Failed to set custom look and feel", ex);
    }
  }

  public void actionPerformed(ActionEvent evt) {
    dispose();
  }
}