Package com.subhajit.eclipse.swt

Source Code of com.subhajit.eclipse.swt.SwtDialog

package com.subhajit.eclipse.swt;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import swingintegration.example.AwtEnvironment;
import swingintegration.example.EmbeddedSwingComposite;

import com.subhajit.common.util.StrUtils;
import com.subhajit.gui.Dialog;
import com.subhajit.gui.GuiUtils;

/**
* This class demonstrates JFace's InputDialog class
*/
public class SwtDialog extends /* org.eclipse.jface.dialogs.Dialog */
ApplicationWindow implements AncestorListener {
  private JPanel panel;
  private int width, height;
  private String dialogCaption;
  private Display display;
  private Shell parentShell;

  private static Display sharedDisplay = null;

  private final static java.awt.Dimension screenSize = Toolkit
      .getDefaultToolkit().getScreenSize();

  /**
   * GetInput constructor
   */
  public SwtDialog(JPanel panel, String dialogCaption, int width, int height) {
    this(null, panel, dialogCaption, width, height);
  }

  public SwtDialog(Shell parentShell, JPanel panel, String dialogCaption,
      int width, int height) {
    super(parentShell);
    this.parentShell = parentShell;
    this.panel = panel;
    this.dialogCaption = dialogCaption;
    this.width = width;
    this.height = height;
    GuiUtils.setComponentWidth(panel, width);
    GuiUtils.setComponentHeight(panel, height);
  }

  /**
   * Runs the application
   */
  public void run() {
    // Don't return from open() until window closes
    setBlockOnOpen(parentShell != null);

    // Open the main window
    open();

    if (sharedDisplay == null) {
      sharedDisplay = display;
    }
  }

  protected Point getInitialSize() {
    return new Point(width, height);
  }

  protected void initializeBounds() {
    this.getShell().setBounds((screenSize.width - width) / 2,
        (screenSize.height - height) / 2, width, height);
  }

  /**
   * Configures the shell
   *
   * @param shell
   *            the shell
   */
  protected void configureShell(Shell shell) {
    super.configureShell(shell);

    // Set the title bar text
    shell.setText(dialogCaption);
  }

  /**
   * Creates the main window's contents
   *
   * @param parent
   *            the main window
   * @return Control
   */
  protected Control createContents(Composite parent) {
    display = parent.getDisplay();
    EmbeddedSwingComposite composite = new EmbeddedSwingComposite(parent,
        SWT.APPLICATION_MODAL | SWT.FILL) {
      protected JComponent createSwingComponent() {
        panel.setSize(width, height);
        panel.setPreferredSize(new Dimension(width, height));
        panel.setMaximumSize(new Dimension(width, height));
        panel.setMinimumSize(new Dimension(width, height));

        System.out.println(panel.getSize().width);

        panel.addAncestorListener(SwtDialog.this);
        return panel;
      }
    };
    composite.populate();
    return composite;
  }

  public void ancestorAdded(AncestorEvent event) {
  }

  public void ancestorRemoved(AncestorEvent event) {
    display.asyncExec(new Runnable() {
      public void run() {
        close();
      }
    });
  }

  public void ancestorMoved(AncestorEvent event) {
  }

  public static void showSwt(Throwable exc) {
    info(toHtmlStackTrace(exc), "Exception");
  }

  public static String toHtmlStackTrace(Throwable exc) {
    try {
      StringBuilder str = new StringBuilder();
      BufferedReader bufIn = null;
      try {
        bufIn = new BufferedReader(new StringReader(StrUtils
            .toString(exc)));
        while (true) {
          String line = bufIn.readLine();
          if (line == null) {
            break;
          }
          if (str.length() != 0) {
            str.append("<br>").append(line);
          } else {
            str.append("<b>").append(line).append("</b>");
          }
        }
      } finally {
        if (bufIn != null) {
          bufIn.close();
        }
      }
      return str.toString();
    } catch (IOException e) {
      return exc.getMessage();
    }
  }

  public static void show(Throwable exc) {
    showSwt(exc);
  }

  public static class PanelRunner implements Runnable {
    private final JPanel panel;
    private final String dialogCaption;
    private final int width;
    private final int height;

    public PanelRunner(JPanel panel, String dialogCaption, int width,
        int height) {
      super();
      this.panel = panel;
      this.dialogCaption = dialogCaption;
      this.width = width;
      this.height = height;
      if (Display.getCurrent() != null) {
        this.panel.putClientProperty("current.display", Display
            .getCurrent());
      }
    }

    public void run() {
      JDialog dialog = Dialog.newDialog(panel, width, height);
      if (width >= 0 && height >= 0) {
        dialog.setResizable(false);
      }
      dialog.setTitle(dialogCaption);
      dialog.setModal(true);
      dialog.setVisible(true);
    }
  }

  public static void newDialog(JPanel panel, String dialogCaption, int width,
      int height) {
    AwtEnvironment.getInstance(Display.getCurrent()).invokeAndBlockSwt(
        new PanelRunner(panel, dialogCaption, width, height));
  }

  public static String ask(String dialogCaption, String prompt, String value) {
    return ask(prompt, value);
  }

  public static String ask(String prompt, String value) {
    Display currentDisplay = Display.getCurrent();
    if (currentDisplay != null) {
      AskRunnable askRunnable = new AskRunnable(prompt, value);
      AwtEnvironment.getInstance(Display.getCurrent()).invokeAndBlockSwt(
          askRunnable);
      return askRunnable.ret;
    } else {
      return null;
    }
  }

  public static String askPassword(String dialogCaption, String prompt,
      String initialValue) {
    AskPasswordRunnable askRunnable = new AskPasswordRunnable(prompt,
        initialValue);
    AwtEnvironment.getInstance(Display.getCurrent()).invokeAndBlockSwt(
        askRunnable);
    return askRunnable.ret;
  }

  public static class AskRunnable implements Runnable {
    private final String prompt;
    private final String initialValue;
    private String ret;

    public AskRunnable(String prompt, String initialValue) {
      super();
      this.prompt = prompt;
      this.initialValue = initialValue;
    }

    public void run() {
      ret = Dialog.ask(prompt, initialValue);
    }
  }

  public static class AskPasswordRunnable implements Runnable {
    private final String prompt;
    private final String initialValue;
    private String ret;

    public AskPasswordRunnable(String prompt, String initialValue) {
      super();
      this.prompt = prompt;
      this.initialValue = initialValue;
    }

    public void run() {
      ret = Dialog.askPassword(prompt, initialValue);
    }
  }

  public static String askPassword(String prompt, String initialValue) {
    return askPassword("", prompt, initialValue);
  }

  public static class SelectRunnable implements Runnable {
    private final String prompt;
    private final String[] options;
    private final String initialValue;
    private String ret;

    public SelectRunnable(String prompt, String[] options,
        String initialValue) {
      super();
      this.prompt = prompt;
      this.options = options;
      this.initialValue = initialValue;
    }

    public void run() {
      ret = Dialog.select(prompt, options, initialValue);
    }
  }

  public static String select(String dialogCaption, String prompt,
      String[] options, String initialValue) {
    SelectRunnable runnable = new SelectRunnable(prompt, options,
        initialValue);
    AwtEnvironment.getInstance(Display.getCurrent()).invokeAndBlockSwt(
        runnable);
    return runnable.ret;
  }

  public static void info(String htmlMessage, String dialogCaption) {
    info(htmlMessage, new String[] { "Ok" }, dialogCaption);
  }

  // public static Object info(String htmlMessage, String[] buttonCaptions,
  // String dialogCaption) {
  // JPanel messagePanel = new ActionListenerPanel();
  // messagePanel.setVisible(true);
  // messagePanel.invalidate();
  // messagePanel.setLayout(new GridLayout(1, 1));
  //
  // JEditorPane editorPane = Dialog.setupHtmlComponent(htmlMessage);
  // messagePanel.add(editorPane);
  //
  // IButtonSpec[] buttonSpecs = new IButtonSpec[buttonCaptions.length];
  // for (int i = 0; i < buttonSpecs.length; i++) {
  // DefaultButtonSpec ok = new DefaultButtonSpec();
  // ok.setCaption(buttonCaptions[i]);
  // ok.setConfirm(true);
  // ok.setCancel(false);
  // ok.setApply(false);
  // buttonSpecs[i] = ok;
  // }
  //
  // JOptionPane op = new JOptionPane(messagePanel);
  // op.setOptions(buttonCaptions);
  // op.setVisible(true);
  // op.invalidate();
  // JDialog dlg = op.createDialog(null, dialogCaption);
  // int calcWidth = dlg.getWidth();
  // int calcHeight = dlg.getHeight();
  // dlg.dispose();
  //
  // InnerPanel innerPanel = new InnerPanel();
  // innerPanel.init(messagePanel, buttonSpecs);
  // GuiUtils.setComponentWidth(innerPanel, calcWidth);
  // GuiUtils.setComponentHeight(innerPanel, calcHeight);
  //
  // newDialog(innerPanel, dialogCaption, calcWidth, calcHeight);
  // return innerPanel.getValue();
  // }

  public static class InfoDialogRunnable implements Runnable {
    private final String htmlMessage;
    private final String[] buttonCaptions;
    private final String dialogCaption;
    private String ret;

    public InfoDialogRunnable(String htmlMessage, String[] buttonCaptions,
        String dialogCaption) {
      super();
      this.htmlMessage = htmlMessage;
      this.buttonCaptions = buttonCaptions;
      this.dialogCaption = dialogCaption;
    }

    public void run() {
      ret = (String) Dialog.info(htmlMessage, buttonCaptions,
          dialogCaption);
    }
  }

  // public static class ThrowableRunner implements Runnable {
  // private final Throwable exc;
  //
  // public ThrowableRunner(Throwable exc) {
  // super();
  // this.exc = exc;
  // }
  //
  // public void run() {
  // JDialog dialog = Dialog.newDialog(new ThrowableViewer(exc), Toolkit
  // .getDefaultToolkit().getScreenSize().width / 2, Toolkit
  // .getDefaultToolkit().getScreenSize().height / 2);
  // dialog.setModal(true);
  // dialog.setVisible(true);
  // }
  // }

  public static Object info(final String htmlMessage,
      final String[] buttonCaptions, final String dialogCaption) {
    Display currentDisplay = Display.getCurrent();
    if (currentDisplay != null) {
      InfoDialogRunnable runnable = new InfoDialogRunnable(htmlMessage,
          buttonCaptions, dialogCaption);
      AwtEnvironment.getInstance(Display.getCurrent()).invokeAndBlockSwt(
          runnable);
      return runnable.ret;
    } else {
      return null;
    }
  }

  /**
   * The application entry point
   *
   * @param args
   *            the command line arguments
   */
  public static void main(String[] args) {
    int status = 0;
    try {
      System.out.println(SwtDialog.ask("Name", "Please enter you name",
          ""));
      System.out.println(SwtDialog.select("Fruits",
          "Please select your favourite fruit...", new String[] {
              "Apple", "Orange", "Banana" }, "Apple"));
      System.out
          .println(SwtDialog
              .info(
                  "This is <b>bold</b> and this is <i>italic</i>. <br>This is an HTML panel.<hr>",
                  new String[] { "Red", "Blue", "Green" },
                  "Html"));
    } catch (Throwable exc) {
      status = 1;
      exc.printStackTrace();
    } finally {
      System.exit(status);
    }
  }
}
TOP

Related Classes of com.subhajit.eclipse.swt.SwtDialog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.