@Override
 protected DetailComposite getComposite(Composite parent) {
   IdentitySpecifierDetailsComposite c =
       new IdentitySpecifierDetailsComposite(parent, parent.getStyle());
   if (getEObject() instanceof Column) {
     IdentitySpecifier is = ((Column) getEObject()).getIdentitySpecifier();
     if (is != null) c.setElement(is);
   }
   return c;
 }
Exemple #2
0
 /**
  * Returns a <code>java.awt.Frame</code> which is the embedded frame associated with the specified
  * composite.
  *
  * @param parent the parent <code>Composite</code> of the <code>java.awt.Frame</code>
  * @return a <code>java.awt.Frame</code> the embedded frame or <code>null</code>.
  * @exception IllegalArgumentException
  *     <ul>
  *       <li>ERROR_NULL_ARGUMENT - if the parent is null
  *     </ul>
  *
  * @since 3.2
  */
 public static Frame getFrame(Composite parent) {
   if (parent == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
   if ((parent.getStyle() & SWT.EMBEDDED) == 0) return null;
   return (Frame) parent.getData(EMBEDDED_FRAME_KEY);
 }
Exemple #3
0
  /**
   * Creates a new <code>java.awt.Frame</code>. This frame is the root for the AWT components that
   * will be embedded within the composite. In order for the embedding to succeed, the composite
   * must have been created with the SWT.EMBEDDED style.
   *
   * <p>IMPORTANT: As of JDK1.5, the embedded frame does not receive mouse events. When a
   * lightweight component is added as a child of the embedded frame, the cursor does not change. In
   * order to work around both these problems, it is strongly recommended that a heavyweight
   * component such as <code>java.awt.Panel</code> be added to the frame as the root of all
   * components.
   *
   * @param parent the parent <code>Composite</code> of the new <code>java.awt.Frame</code>
   * @return a <code>java.awt.Frame</code> to be the parent of the embedded AWT components
   * @exception IllegalArgumentException
   *     <ul>
   *       <li>ERROR_NULL_ARGUMENT - if the parent is null
   *       <li>ERROR_INVALID_ARGUMENT - if the parent Composite does not have the SWT.EMBEDDED style
   *     </ul>
   *
   * @since 3.0
   */
  public static Frame new_Frame(final Composite parent) {
    if (parent == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
    if ((parent.getStyle() & SWT.EMBEDDED) == 0) {
      SWT.error(SWT.ERROR_INVALID_ARGUMENT);
    }

    final int /*long*/ handle = parent.view.id;

    Class clazz = null;
    try {
      String className =
          embeddedFrameClass != null ? embeddedFrameClass : "apple.awt.CEmbeddedFrame";
      if (embeddedFrameClass == null) {
        clazz = Class.forName(className, true, ClassLoader.getSystemClassLoader());
      } else {
        clazz = Class.forName(className);
      }
    } catch (ClassNotFoundException cne) {
      SWT.error(SWT.ERROR_NOT_IMPLEMENTED, cne);
    } catch (Throwable e) {
      SWT.error(SWT.ERROR_UNSPECIFIED, e, " [Error while starting AWT]");
    }

    initializeSwing();
    Object value = null;
    Constructor constructor = null;
    try {
      constructor = clazz.getConstructor(new Class[] {long.class});
      value = constructor.newInstance(new Object[] {new Long(handle)});
    } catch (Throwable e) {
      SWT.error(SWT.ERROR_NOT_IMPLEMENTED, e);
    }
    final Frame frame = (Frame) value;
    frame.addNotify();

    parent.setData(EMBEDDED_FRAME_KEY, frame);

    /* Forward the iconify and deiconify events */
    final Listener shellListener =
        new Listener() {
          public void handleEvent(Event e) {
            switch (e.type) {
              case SWT.Deiconify:
                EventQueue.invokeLater(
                    new Runnable() {
                      public void run() {
                        frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_DEICONIFIED));
                      }
                    });
                break;
              case SWT.Iconify:
                EventQueue.invokeLater(
                    new Runnable() {
                      public void run() {
                        frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_ICONIFIED));
                      }
                    });
                break;
            }
          }
        };
    Shell shell = parent.getShell();
    shell.addListener(SWT.Deiconify, shellListener);
    shell.addListener(SWT.Iconify, shellListener);

    /*
     * Generate the appropriate events to activate and deactivate
     * the embedded frame. This is needed in order to make keyboard
     * focus work properly for lightweights.
     */
    Listener listener =
        new Listener() {
          public void handleEvent(Event e) {
            switch (e.type) {
              case SWT.Dispose:
                Shell shell = parent.getShell();
                shell.removeListener(SWT.Deiconify, shellListener);
                shell.removeListener(SWT.Iconify, shellListener);
                parent.setVisible(false);
                EventQueue.invokeLater(
                    new Runnable() {
                      public void run() {
                        try {
                          frame.dispose();
                        } catch (Throwable e) {
                        }
                      }
                    });
                break;
              case SWT.FocusIn:
                EventQueue.invokeLater(
                    new Runnable() {
                      public void run() {
                        if (frame.isActive()) return;
                        try {
                          Class clazz = frame.getClass();
                          Method method =
                              clazz.getMethod(
                                  "synthesizeWindowActivation", new Class[] {boolean.class});
                          if (method != null)
                            method.invoke(frame, new Object[] {new Boolean(true)});
                        } catch (Throwable e) {
                          e.printStackTrace();
                        }
                      }
                    });
                break;
              case SWT.Deactivate:
                EventQueue.invokeLater(
                    new Runnable() {
                      public void run() {
                        if (!frame.isActive()) return;
                        try {
                          Class clazz = frame.getClass();
                          Method method =
                              clazz.getMethod(
                                  "synthesizeWindowActivation", new Class[] {boolean.class});
                          if (method != null)
                            method.invoke(frame, new Object[] {new Boolean(false)});
                        } catch (Throwable e) {
                          e.printStackTrace();
                        }
                      }
                    });
                break;
            }
          }
        };

    parent.addListener(SWT.FocusIn, listener);
    parent.addListener(SWT.Deactivate, listener);
    parent.addListener(SWT.Dispose, listener);

    parent
        .getDisplay()
        .asyncExec(
            new Runnable() {
              public void run() {
                if (parent.isDisposed()) return;
                final Rectangle clientArea = parent.getClientArea();
                EventQueue.invokeLater(
                    new Runnable() {
                      public void run() {
                        frame.setSize(clientArea.width, clientArea.height);
                        frame.validate();

                        // Bug in Cocoa AWT? For some reason the frame isn't showing up on first
                        // draw.
                        // Toggling visibility seems to be the only thing that works.
                        frame.setVisible(false);
                        frame.setVisible(true);
                      }
                    });
              }
            });

    return frame;
  }
  /** @param parent */
  private void createImportExportButtons(final Composite parent) {
    final Composite composite = new Composite(parent, parent.getStyle());
    final GridLayout layout = new GridLayout();
    layout.numColumns = 4;
    composite.setLayout(layout);
    this.importButton = new Button(composite, SWT.PUSH);
    this.importButton.setText("Import");

    this.importButton.addSelectionListener(
        new SelectionListener() {

          @Override
          public void widgetSelected(final SelectionEvent event) {

            try {

              if (getAllTemplatesPreferenceKey().equals(P_ALL_TEMPLATES)) {
                // processXML("templates-config.xml",
                // TEMPLATE_PREFERENCE_NAME.ALL_TEMPLATES.toString(), TEMPLATES_FOLDER);
                importXML(
                    "templates-config.xml",
                    TEMPLATE_PREFERENCE_NAME.ALL_TEMPLATES.toString(),
                    TEMPLATES_FOLDER);
              } else if (getAllTemplatesPreferenceKey().equals(P_ALL_COMMON_TEMPLATES)) {
                // processXML("additional-templates-config.xml",
                // TEMPLATE_PREFERENCE_NAME.ALL_ADDITIONAL_TEMPLATES.toString(),TEMPLATES_FOLDER);
                importXML(
                    "common-templates-config.xml",
                    TEMPLATE_PREFERENCE_NAME.ALL_COMMON_TEMPLATES.toString(),
                    TEMPLATES_FOLDER);
              } else if (getAllTemplatesPreferenceKey().equals(P_DATABASE_ALL_TEMPLATES)) {
                // processXML("database-templates-config.xml",
                // TEMPLATE_PREFERENCE_NAME.ALL_DATABASE_TEMPLATES.name(),DB_TEMPLATES_FOLDER);
                importXML(
                    "database-templates-config.xml",
                    TEMPLATE_PREFERENCE_NAME.ALL_DATABASE_TEMPLATES.name(),
                    DB_TEMPLATES_FOLDER);
              } /* else if (getAllTemplatesPreferenceKey().equals(P_ALL_ADDITIONAL_DATABASE_TEMPLATES)) {
                //processXML("additional-database-templates-config.xml",TEMPLATE_PREFERENCE_NAME.ALL_ADDITIONAL_DATABASE_TEMPLATES.name(), DB_TEMPLATES_FOLDER);
                importXML("additional-database-templates-config.xml",
                		TEMPLATE_PREFERENCE_NAME.ALL_ADDITIONAL_DATABASE_TEMPLATES.name(), DB_TEMPLATES_FOLDER);
                } else if (getAllTemplatesPreferenceKey().equals(P_FILE_ALL_TEMPLATES)) {
                //processXML("file-templates-config.xml", TEMPLATE_PREFERENCE_NAME.ALL_FILE_TEMPLATES.name(), FILE_TEMPLATES_FOLDER);
                importXML("file-templates-config.xml", TEMPLATE_PREFERENCE_NAME.ALL_FILE_TEMPLATES.name(), FILE_TEMPLATES_FOLDER);
                }
                */
            } catch (final Exception ex) {
              try {
                throw new Exception(
                    "There was some error in Import templates : " + ex.getMessage());
              } catch (final Exception ex1) {
                ex1.printStackTrace();
              }
              ex.printStackTrace();
            } finally {
              MessageDialog.openInformation(
                  new Shell(), "Success", "Import was successfully completed .");
            }
          }

          @Override
          public void widgetDefaultSelected(final SelectionEvent arg0) {}
        });

    this.exportButton = new Button(composite, SWT.PUSH);
    this.exportButton.setText("Export");

    this.exportButton.addSelectionListener(
        new SelectionListener() {

          @Override
          public void widgetSelected(final SelectionEvent event) {
            try {
              if (getAllTemplatesPreferenceKey().equals(P_ALL_TEMPLATES)) {
                // processXML("templates-config.xml",
                // TEMPLATE_PREFERENCE_NAME.ALL_TEMPLATES.toString(), TEMPLATES_FOLDER);
                exportXML(
                    "templates-config.xml",
                    TEMPLATE_PREFERENCE_NAME.ALL_TEMPLATES.toString(),
                    TEMPLATES_FOLDER);
              } else if (getAllTemplatesPreferenceKey().equals(P_ALL_COMMON_TEMPLATES)) {
                // processXML("additional-templates-config.xml",
                // TEMPLATE_PREFERENCE_NAME.ALL_ADDITIONAL_TEMPLATES.toString(),TEMPLATES_FOLDER);
                exportXML(
                    "common-templates-config.xml",
                    TEMPLATE_PREFERENCE_NAME.ALL_COMMON_TEMPLATES.toString(),
                    TEMPLATES_FOLDER);
              } else if (getAllTemplatesPreferenceKey().equals(P_DATABASE_ALL_TEMPLATES)) {
                // processXML("database-templates-config.xml",
                // TEMPLATE_PREFERENCE_NAME.ALL_DATABASE_TEMPLATES.name(),DB_TEMPLATES_FOLDER);
                exportXML(
                    "database-templates-config.xml",
                    TEMPLATE_PREFERENCE_NAME.ALL_DATABASE_TEMPLATES.name(),
                    DB_TEMPLATES_FOLDER);
              } /*else if (getAllTemplatesPreferenceKey().equals(P_ALL_ADDITIONAL_DATABASE_TEMPLATES)) {
                //processXML("additional-database-templates-config.xml",TEMPLATE_PREFERENCE_NAME.ALL_ADDITIONAL_DATABASE_TEMPLATES.name(), DB_TEMPLATES_FOLDER);
                exportXML("additional-database-templates-config.xml",
                		TEMPLATE_PREFERENCE_NAME.ALL_ADDITIONAL_DATABASE_TEMPLATES.name(), DB_TEMPLATES_FOLDER);
                } else if (getAllTemplatesPreferenceKey().equals(P_FILE_ALL_TEMPLATES)) {
                //processXML("file-templates-config.xml", TEMPLATE_PREFERENCE_NAME.ALL_FILE_TEMPLATES.name(), FILE_TEMPLATES_FOLDER);
                exportXML("file-templates-config.xml", TEMPLATE_PREFERENCE_NAME.ALL_FILE_TEMPLATES.name(), FILE_TEMPLATES_FOLDER);
                }*/
            } catch (final Exception ex) {
              try {
                throw new Exception(
                    "There was some error in Export templates : " + ex.getMessage());
              } catch (final Exception ex1) {
                ex1.printStackTrace();
              }
              ex.printStackTrace();
            }
          }

          @Override
          public void widgetDefaultSelected(final SelectionEvent arg0) {}
        });
  }
    @Override
    protected void createContent(Composite parent) {
      fSashForm = new SashForm(parent, parent.getStyle());
      fSashForm.setOrientation(SWT.VERTICAL);

      // update presentation context
      AbstractDebugView view = getViewToEmulate();
      fContext = new PresentationContext(TCFModel.ID_EXPRESSION_HOVER);
      if (view != null) {
        // copy over properties
        IPresentationContext copy = ((TreeModelViewer) view.getViewer()).getPresentationContext();
        try {
          String[] properties = copy.getProperties();
          for (int i = 0; i < properties.length; i++) {
            String key = properties[i];
            fContext.setProperty(key, copy.getProperty(key));
          }
        } catch (NoSuchMethodError e) {
          // ignore
        }
      }

      fViewer =
          new TreeModelViewer(fSashForm, SWT.MULTI | SWT.VIRTUAL | SWT.FULL_SELECTION, fContext);
      fViewer.setAutoExpandLevel(fExpansionLevel);

      if (view != null) {
        // copy over filters
        StructuredViewer structuredViewer = (StructuredViewer) view.getViewer();
        if (structuredViewer != null) {
          ViewerFilter[] filters = structuredViewer.getFilters();
          for (int i = 0; i < filters.length; i++) {
            fViewer.addFilter(filters[i]);
          }
        }
      }
      fInputService = new ViewerInputService(fViewer, this);
      fTree = fViewer.getTree();

      if (fShowDetailPane) {
        fDetailPaneComposite = SWTFactory.createComposite(fSashForm, 1, 1, GridData.FILL_BOTH);
        Layout layout = fDetailPaneComposite.getLayout();
        if (layout instanceof GridLayout) {
          GridLayout gl = (GridLayout) layout;
          gl.marginHeight = 0;
          gl.marginWidth = 0;
        }

        fDetailPane = new DetailPaneProxy(new DetailPaneContainer());
        fDetailPane.display(null); // Bring up the default pane so the
        // user doesn't see an empty
        // composite

        fTree.addSelectionListener(
            new SelectionListener() {
              public void widgetSelected(SelectionEvent e) {
                fDetailPane.display((IStructuredSelection) fViewer.getSelection());
              }

              public void widgetDefaultSelected(SelectionEvent e) {}
            });
      }

      initSashWeights();

      // add update listener to auto-select and display details of root
      // expression
      fViewer.addViewerUpdateListener(
          new IViewerUpdateListener() {
            public void viewerUpdatesComplete() {
              fViewer
                  .getDisplay()
                  .timerExec(
                      100,
                      new Runnable() {
                        public void run() {
                          if (fViewer.getControl().isDisposed()) return;
                          TreeSelection selection = (TreeSelection) fViewer.getSelection();
                          if (selection.isEmpty())
                            selection = new TreeSelection(fViewer.getTopElementPath());
                          fViewer.setSelection(selection);
                          if (fDetailPane != null) fDetailPane.display(selection);
                        }
                      });
            }

            public void viewerUpdatesBegin() {}

            public void updateStarted(IViewerUpdate update) {}

            public void updateComplete(IViewerUpdate update) {}
          });

      setBackgroundColor(getShell().getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
    }
  private void drag(Event dragEvent) {
    DNDEvent event = new DNDEvent();
    event.widget = this;
    event.x = dragEvent.x;
    event.y = dragEvent.y;
    event.time = OS.GetMessageTime();
    event.doit = true;
    notifyListeners(DND.DragStart, event);
    if (!event.doit || transferAgents == null || transferAgents.length == 0) return;

    int[] pdwEffect = new int[1];
    int operations = opToOs(getStyle());
    Display display = control.getDisplay();
    String key = "org.eclipse.swt.internal.win32.runMessagesInIdle"; // $NON-NLS-1$
    Object oldValue = display.getData(key);
    display.setData(key, Boolean.TRUE);
    ImageList imagelist = null;
    Image image = event.image;
    hwndDrag = 0;
    topControl = null;
    if (image != null) {
      imagelist = new ImageList(SWT.NONE);
      imagelist.add(image);
      topControl = control.getShell();
      /*
       * Bug in Windows. The image is inverted if the shell is RIGHT_TO_LEFT.
       * The fix is to create a transparent window that covers the shell client
       * area and use it during the drag to prevent the image from being inverted.
       * On XP if the shell is RTL, the image is not displayed.
       */
      int offsetX = event.offsetX;
      hwndDrag = topControl.handle;
      if ((topControl.getStyle() & SWT.RIGHT_TO_LEFT) != 0) {
        offsetX = image.getBounds().width - offsetX;
        RECT rect = new RECT();
        OS.GetClientRect(topControl.handle, rect);
        hwndDrag =
            OS.CreateWindowEx(
                OS.WS_EX_TRANSPARENT | OS.WS_EX_NOINHERITLAYOUT,
                WindowClass,
                null,
                OS.WS_CHILD | OS.WS_CLIPSIBLINGS,
                0,
                0,
                rect.right - rect.left,
                rect.bottom - rect.top,
                topControl.handle,
                0,
                OS.GetModuleHandle(null),
                null);
        OS.ShowWindow(hwndDrag, OS.SW_SHOW);
      }
      OS.ImageList_BeginDrag(imagelist.getHandle(), 0, offsetX, event.offsetY);
      /*
       * Feature in Windows. When ImageList_DragEnter() is called,
       * it takes a snapshot of the screen  If a drag is started
       * when another window is in front, then the snapshot will
       * contain part of the other window, causing pixel corruption.
       * The fix is to force all paints to be delivered before
       * calling ImageList_DragEnter().
       */
      if (OS.IsWinCE) {
        OS.UpdateWindow(topControl.handle);
      } else {
        int flags = OS.RDW_UPDATENOW | OS.RDW_ALLCHILDREN;
        OS.RedrawWindow(topControl.handle, null, 0, flags);
      }
      POINT pt = new POINT();
      pt.x = dragEvent.x;
      pt.y = dragEvent.y;
      OS.MapWindowPoints(control.handle, 0, pt, 1);
      RECT rect = new RECT();
      OS.GetWindowRect(hwndDrag, rect);
      OS.ImageList_DragEnter(hwndDrag, pt.x - rect.left, pt.y - rect.top);
    }
    int result = COM.DRAGDROP_S_CANCEL;
    try {
      result =
          COM.DoDragDrop(iDataObject.getAddress(), iDropSource.getAddress(), operations, pdwEffect);
    } finally {
      // ensure that we don't leave transparent window around
      if (hwndDrag != 0) {
        OS.ImageList_DragLeave(hwndDrag);
        OS.ImageList_EndDrag();
        imagelist.dispose();
        if (hwndDrag != topControl.handle) OS.DestroyWindow(hwndDrag);
        hwndDrag = 0;
        topControl = null;
      }
      display.setData(key, oldValue);
    }
    int operation = osToOp(pdwEffect[0]);
    if (dataEffect == DND.DROP_MOVE) {
      operation =
          (operation == DND.DROP_NONE || operation == DND.DROP_COPY)
              ? DND.DROP_TARGET_MOVE
              : DND.DROP_MOVE;
    } else {
      if (dataEffect != DND.DROP_NONE) {
        operation = dataEffect;
      }
    }
    event = new DNDEvent();
    event.widget = this;
    event.time = OS.GetMessageTime();
    event.doit = (result == COM.DRAGDROP_S_DROP);
    event.detail = operation;
    notifyListeners(DND.DragEnd, event);
    dataEffect = DND.DROP_NONE;
  }
  /**
   * Creates a new <code>java.awt.Frame</code>. This frame is the root for the AWT components that
   * will be embedded within the composite. In order for the embedding to succeed, the composite
   * must have been created with the SWT.EMBEDDED style.
   *
   * <p>IMPORTANT: As of JDK1.5, the embedded frame does not receive mouse events. When a
   * lightweight component is added as a child of the embedded frame, the cursor does not change. In
   * order to work around both these problems, it is strongly recommended that a heavyweight
   * component such as <code>java.awt.Panel</code> be added to the frame as the root of all
   * components.
   *
   * @param parent the parent <code>Composite</code> of the new <code>java.awt.Frame</code>
   * @return a <code>java.awt.Frame</code> to be the parent of the embedded AWT components
   * @exception IllegalArgumentException
   *     <ul>
   *       <li>ERROR_NULL_ARGUMENT - if the parent is null
   *       <li>ERROR_INVALID_ARGUMENT - if the parent Composite does not have the SWT.EMBEDDED style
   *     </ul>
   *
   * @since 3.0
   */
  public static Frame new_Frame(final Composite parent) {
    if (parent == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
    if ((parent.getStyle() & SWT.EMBEDDED) == 0) {
      SWT.error(SWT.ERROR_INVALID_ARGUMENT);
    }
    int /*long*/ handle = parent.embeddedHandle;
    /*
     * Some JREs have implemented the embedded frame constructor to take an integer
     * and other JREs take a long.  To handle this binary incompatibility, use
     * reflection to create the embedded frame.
     */
    Class clazz = null;
    try {
      String className =
          embeddedFrameClass != null ? embeddedFrameClass : "sun.awt.X11.XEmbeddedFrame";
      clazz = Class.forName(className);
    } catch (Throwable e) {
      SWT.error(SWT.ERROR_NOT_IMPLEMENTED, e, " [need JDK 1.5 or greater]");
    }
    initializeSwing();
    Object value = null;
    Constructor constructor = null;
    try {
      constructor = clazz.getConstructor(new Class[] {int.class, boolean.class});
      value =
          constructor.newInstance(new Object[] {new Integer((int) /*64*/ handle), Boolean.TRUE});
    } catch (Throwable e1) {
      try {
        constructor = clazz.getConstructor(new Class[] {long.class, boolean.class});
        value = constructor.newInstance(new Object[] {new Long(handle), Boolean.TRUE});
      } catch (Throwable e2) {
        SWT.error(SWT.ERROR_NOT_IMPLEMENTED, e2);
      }
    }
    final Frame frame = (Frame) value;
    parent.setData(EMBEDDED_FRAME_KEY, frame);
    if (Device.DEBUG) {
      loadLibrary();
      setDebug(frame, true);
    }
    try {
      /* Call registerListeners() to make XEmbed focus traversal work */
      Method method = clazz.getMethod("registerListeners", null);
      if (method != null) method.invoke(value, null);
    } catch (Throwable e) {
    }
    final AWTEventListener awtListener =
        new AWTEventListener() {
          public void eventDispatched(AWTEvent event) {
            if (event.getID() == WindowEvent.WINDOW_OPENED) {
              final Window window = (Window) event.getSource();
              if (window.getParent() == frame) {
                parent
                    .getDisplay()
                    .asyncExec(
                        new Runnable() {
                          public void run() {
                            if (parent.isDisposed()) return;
                            Shell shell = parent.getShell();
                            loadLibrary();
                            int /*long*/ awtHandle = getAWTHandle(window);
                            if (awtHandle == 0) return;
                            int /*long*/ xWindow =
                                OS.gdk_x11_drawable_get_xid(
                                    OS.GTK_WIDGET_WINDOW(OS.gtk_widget_get_toplevel(shell.handle)));
                            OS.XSetTransientForHint(
                                OS.gdk_x11_display_get_xdisplay(OS.gdk_display_get_default()),
                                awtHandle,
                                xWindow);
                          }
                        });
              }
            }
          }
        };
    frame.getToolkit().addAWTEventListener(awtListener, AWTEvent.WINDOW_EVENT_MASK);
    final Listener shellListener =
        new Listener() {
          public void handleEvent(Event e) {
            switch (e.type) {
              case SWT.Deiconify:
                EventQueue.invokeLater(
                    new Runnable() {
                      public void run() {
                        frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_DEICONIFIED));
                      }
                    });
                break;
              case SWT.Iconify:
                EventQueue.invokeLater(
                    new Runnable() {
                      public void run() {
                        frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_ICONIFIED));
                      }
                    });
                break;
            }
          }
        };
    Shell shell = parent.getShell();
    shell.addListener(SWT.Deiconify, shellListener);
    shell.addListener(SWT.Iconify, shellListener);

    Listener listener =
        new Listener() {
          public void handleEvent(Event e) {
            switch (e.type) {
              case SWT.Dispose:
                Shell shell = parent.getShell();
                shell.removeListener(SWT.Deiconify, shellListener);
                shell.removeListener(SWT.Iconify, shellListener);
                parent.setVisible(false);
                EventQueue.invokeLater(
                    new Runnable() {
                      public void run() {
                        frame.getToolkit().removeAWTEventListener(awtListener);
                        frame.dispose();
                      }
                    });
                break;
              case SWT.Resize:
                if (Library.JAVA_VERSION >= Library.JAVA_VERSION(1, 6, 0)) {
                  final Rectangle clientArea = parent.getClientArea();
                  EventQueue.invokeLater(
                      new Runnable() {
                        public void run() {
                          frame.setSize(clientArea.width, clientArea.height);
                        }
                      });
                }
                break;
            }
          }
        };
    parent.addListener(SWT.Dispose, listener);
    parent.addListener(SWT.Resize, listener);

    parent
        .getDisplay()
        .asyncExec(
            new Runnable() {
              public void run() {
                if (parent.isDisposed()) return;
                final Rectangle clientArea = parent.getClientArea();
                EventQueue.invokeLater(
                    new Runnable() {
                      public void run() {
                        frame.setSize(clientArea.width, clientArea.height);
                        frame.validate();
                      }
                    });
              }
            });
    return frame;
  }