📄 popupdialog.java
字号:
shell.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent event) { handleDispose(); } }); } /** * The <code>PopupDialog</code> implementation of this <code>Window</code> * method creates and lays out the top level composite for the dialog. It * then calls the <code>createTitleMenuArea</code>, * <code>createDialogArea</code>, and <code>createInfoTextArea</code> * methods to create an optional title and menu area on the top, a dialog * area in the center, and an optional info text area at the bottom. * Overriding <code>createDialogArea</code> and (optionally) * <code>createTitleMenuArea</code> and <code>createTitleMenuArea</code> * are recommended rather than overriding this method. * * @param parent * the composite used to parent the contents. * * @return the control representing the contents. */ protected Control createContents(Composite parent) { Composite composite = new Composite(parent, SWT.NONE); GridLayout layout = new GridLayout(1, false); layout.marginHeight = POPUP_MARGINHEIGHT; layout.marginWidth = POPUP_MARGINWIDTH; layout.verticalSpacing = POPUP_VERTICALSPACING; layout.horizontalSpacing = POPUP_HORIZONTALSPACING; composite.setLayout(layout); GridData gd = new GridData(GridData.FILL_BOTH); composite.setLayoutData(gd); // Title area if (hasTitleArea()) { createTitleMenuArea(composite); titleSeparator = createHorizontalSeparator(composite); } // Content dialogArea = createDialogArea(composite); // Create a grid data layout data if one was not provided. // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=118025 if (dialogArea.getLayoutData() == null) { dialogArea.setLayoutData(new GridData(GridData.FILL_BOTH)); } // Info field if (hasInfoArea()) { infoSeparator = createHorizontalSeparator(composite); createInfoTextArea(composite); } applyColors(composite); applyFonts(composite); return composite; } /** * Creates and returns the contents of the dialog (the area below the title * area and above the info text area. * <p> * The <code>PopupDialog</code> implementation of this framework method * creates and returns a new <code>Composite</code> with standard margins * and spacing. * <p> * The returned control's layout data must be an instance of * <code>GridData</code>. This method must not modify the parent's * layout. * <p> * Subclasses must override this method but may call <code>super</code> as * in the following example: * * <pre> * Composite composite = (Composite) super.createDialogArea(parent); * //add controls to composite as necessary * return composite; * </pre> * * @param parent * the parent composite to contain the dialog area * @return the dialog area control */ protected Control createDialogArea(Composite parent) { Composite composite = new Composite(parent, SWT.NONE); GridLayout layout = new GridLayout(); layout.marginHeight = POPUP_MARGINHEIGHT; layout.marginWidth = POPUP_MARGINWIDTH; layout.verticalSpacing = POPUP_VERTICALSPACING; layout.horizontalSpacing = POPUP_HORIZONTALSPACING; composite.setLayout(layout); GridData gd = new GridData(GridData.FILL_BOTH); composite.setLayoutData(gd); return composite; } /** * Returns the control that should get initial focus. Subclasses may * override this method. * * @return the Control that should receive focus when the popup opens. */ protected Control getFocusControl() { return dialogArea; } /** * Sets the tab order for the popup. Clients should override to introduce * specific tab ordering. * * @param composite * the composite in which all content, including the title area * and info area, was created. This composite's parent is the * shell. */ protected void setTabOrder(Composite composite) { // default is to do nothing } /** * Returns a boolean indicating whether the popup should have a title area * at the top of the dialog. Subclasses may override. Default behavior is to * have a title area if there is to be a menu or title text. * * @return <code>true</code> if a title area should be created, * <code>false</code> if it should not. */ protected boolean hasTitleArea() { return titleText != null || showDialogMenu; } /** * Returns a boolean indicating whether the popup should have an info area * at the bottom of the dialog. Subclasses may override. Default behavior is * to have an info area if info text was provided at the time of creation. * * @return <code>true</code> if a title area should be created, * <code>false</code> if it should not. */ protected boolean hasInfoArea() { return infoText != null; } /** * Creates the title and menu area. Subclasses typically need not override * this method, but instead should use the constructor parameters * <code>showDialogMenu</code> and <code>showPersistAction</code> to * indicate whether a menu should be shown, and * <code>createTitleControl</code> to to customize the presentation of the * title. * * <p> * If this method is overridden, the returned control's layout data must be * an instance of <code>GridData</code>. This method must not modify the * parent's layout. * * @param parent * The parent composite. * @return The Control representing the title and menu area. */ protected Control createTitleMenuArea(Composite parent) { Composite titleAreaComposite = new Composite(parent, SWT.NONE); GridLayout layout = new GridLayout(2, false); layout.marginHeight = POPUP_MARGINHEIGHT; layout.marginWidth = POPUP_MARGINWIDTH; layout.verticalSpacing = POPUP_VERTICALSPACING; layout.horizontalSpacing = POPUP_HORIZONTALSPACING; titleAreaComposite.setLayout(layout); titleAreaComposite .setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); createTitleControl(titleAreaComposite); if (showDialogMenu) { createDialogMenu(titleAreaComposite); } return titleAreaComposite; } /** * Creates the control to be used to represent the dialog's title text. * Subclasses may override if a different control is desired for * representing the title text, or if something different than the title * should be displayed in location where the title text typically is shown. * * <p> * If this method is overridden, the returned control's layout data must be * an instance of <code>GridData</code>. This method must not modify the * parent's layout. * * @param parent * The parent composite. * @return The Control representing the title area. */ protected Control createTitleControl(Composite parent) { titleLabel = new Label(parent, SWT.NONE); GridData gd = new GridData(GridData.FILL_HORIZONTAL); if (!showDialogMenu) { gd.horizontalSpan = 2; } titleLabel.setLayoutData(gd); Font font = titleLabel.getFont(); FontData[] fontDatas = font.getFontData(); for (int i = 0; i < fontDatas.length; i++) { fontDatas[i].setStyle(SWT.BOLD); } titleFont = new Font(titleLabel.getDisplay(), fontDatas); titleLabel.setFont(titleFont); if (titleText != null) { titleLabel.setText(titleText); } return titleLabel; } /** * Creates the optional info text area. This method is only called if the * <code>hasInfoArea()</code> method returns true. Subclasses typically * need not override this method, but may do so. * * <p> * If this method is overridden, the returned control's layout data must be * an instance of <code>GridData</code>. This method must not modify the * parent's layout. * * * @param parent * The parent composite. * @return The control representing the info text area. * * @see PopupDialog#hasInfoArea() * @see PopupDialog#createTitleControl(Composite) */ protected Control createInfoTextArea(Composite parent) { // Status label infoLabel = new Label(parent, SWT.RIGHT); infoLabel.setText(infoText); Font font = infoLabel.getFont(); FontData[] fontDatas = font.getFontData(); for (int i = 0; i < fontDatas.length; i++) { fontDatas[i].setHeight(fontDatas[i].getHeight() * 9 / 10); } infoFont = new Font(infoLabel.getDisplay(), fontDatas); infoLabel.setFont(infoFont); GridData gd = new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING); infoLabel.setLayoutData(gd); infoLabel.setForeground(parent.getDisplay().getSystemColor( SWT.COLOR_WIDGET_DARK_SHADOW)); return infoLabel; } /** * Create a horizontal separator for the given parent. * * @param parent * The parent composite. * @return The Control representing the horizontal separator. */ private Control createHorizontalSeparator(Composite parent) { Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.LINE_DOT); separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); return separator; } /** * Create the dialog's menu for the move and resize actions. * * @param parent * The parent composite. */ private void createDialogMenu(Composite parent) { toolBar = new ToolBar(parent, SWT.FLAT); ToolItem viewMenuButton = new ToolItem(toolBar, SWT.PUSH, 0); toolBar.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END)); menuImage = ImageDescriptor.createFromFile(PopupDialog.class, "images/popup_menu.gif").createImage();//$NON-NLS-1$ disabledMenuImage = ImageDescriptor.createFromFile(PopupDialog.class, "images/popup_menu_disabled.gif").createImage();//$NON-NLS-1$ viewMenuButton.setImage(menuImage); viewMenuButton.setDisabledImage(disabledMenuImage); viewMenuButton.setToolTipText(JFaceResources .getString("PopupDialog.menuTooltip")); //$NON-NLS-1$ viewMenuButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { showDialogMenu(); } }); viewMenuButton.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { menuImage.dispose(); menuImage = null; disabledMenuImage.dispose(); disabledMenuImage = null; } }); } /** * Fill the dialog's menu. Subclasses may extend or override. * * @param dialogMenu * The dialog's menu. */ protected void fillDialogMenu(IMenuManager dialogMenu) { dialogMenu.add(new GroupMarker("SystemMenuStart")); //$NON-NLS-1$ dialogMenu.add(new MoveAction()); dialogMenu.add(new ResizeAction()); if (showPersistAction) { dialogMenu.add(new PersistBoundsAction()); } dialogMenu.add(new Separator("SystemMenuEnd")); //$NON-NLS-1$ } /** * Perform the requested tracker action (resize or move). * * @param style * The track style (resize or move). */ private void performTrackerAction(int style) { Shell shell = getShell(); if (shell == null || shell.isDisposed()) { return; } Tracker tracker = new Tracker(shell.getDisplay(), style); tracker.setStippled(true); Rectangle[] r = new Rectangle[] { shell.getBounds() }; tracker.setRectangles(r); // Ignore any deactivate events caused by opening the tracker. // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=120656 boolean oldListenToDeactivate = listenToDeactivate; listenToDeactivate = false; if (tracker.open()) { if (shell != null && !shell.isDisposed()) { shell.setBounds(tracker.getRectangles()[0]); } } listenToDeactivate = oldListenToDeactivate; } /** * Show the dialog's menu. This message has no effect if the receiver was * not configured to show a menu. Clients may call this method in order to * trigger the menu via keystrokes or other gestures. Subclasses typically * do not override method. */ protected void showDialogMenu() { if (!showDialogMenu) { return; } if (menuManager == null) { menuManager = new MenuManager(); fillDialogMenu(menuManager); } // Setting this flag works around a problem that remains on X only, // whereby activating the menu deactivates our shell. listenToDeactivate = !"gtk".equals(SWT.getPlatform()); //$NON-NLS-1$ Menu menu = menuManager.createContextMenu(getShell()); Rectangle bounds = toolBar.getBounds(); Point topLeft = new Point(bounds.x, bounds.y + bounds.height); topLeft = getShell().toDisplay(topLeft); menu.setLocation(topLeft.x, topLeft.y); menu.setVisible(true); } /** * Set the text to be shown in the popup's info area. This message has no * effect if there was no info text supplied when the dialog first opened. * Subclasses may override this method. * * @param text * the text to be shown when the info area is displayed. * */ protected void setInfoText(String text) { infoText = text; if (infoLabel != null) { infoLabel.setText(text); } } /** * Set the text to be shown in the popup's title area. This message has no * effect if there was no title label specified when the dialog was * originally opened. Subclasses may override this method. * * @param text * the text to be shown when the title area is displayed. * */ protected void setTitleText(String text) { titleText = text; if (titleLabel != null) { titleLabel.setText(text); } } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -