Java tutorial
/******************************************************************************* * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH . * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * s IT Solutions AT Spardat GmbH - initial API and implementation *******************************************************************************/ package at.spardat.xma.guidesign.presentation; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Locale; import org.eclipse.core.runtime.Platform; import org.eclipse.emf.common.util.EList; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.action.Separator; import org.eclipse.jface.util.Assert; import org.eclipse.jface.viewers.ContentViewer; import org.eclipse.jface.viewers.IBaseLabelProvider; import org.eclipse.jface.viewers.ILabelProvider; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.nebula.widgets.tablecombo.TableCombo; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.SashForm; import org.eclipse.swt.custom.ScrolledComposite; import org.eclipse.swt.events.DisposeEvent; import org.eclipse.swt.events.DisposeListener; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.TabFolder; import org.eclipse.swt.widgets.TabItem; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import at.spardat.xma.guidesign.DataLabel; import at.spardat.xma.guidesign.DatePicker; import at.spardat.xma.guidesign.EmbeddedPage; import at.spardat.xma.guidesign.HiddenWidget; import at.spardat.xma.guidesign.IComponentCalculateable; import at.spardat.xma.guidesign.IDialogPageCalculateable; import at.spardat.xma.guidesign.IDialogRoot; import at.spardat.xma.guidesign.IImageUrl; import at.spardat.xma.guidesign.NotebookPage; import at.spardat.xma.guidesign.PageComposite; import at.spardat.xma.guidesign.SimpleCombo; import at.spardat.xma.guidesign.XMAButton; import at.spardat.xma.guidesign.XMACombo; import at.spardat.xma.guidesign.XMAComponent; import at.spardat.xma.guidesign.XMAComposite; import at.spardat.xma.guidesign.XMAContainer; import at.spardat.xma.guidesign.XMADialogPage; import at.spardat.xma.guidesign.XMAGrid; import at.spardat.xma.guidesign.XMAGroup; import at.spardat.xma.guidesign.XMALabel; import at.spardat.xma.guidesign.XMAList; import at.spardat.xma.guidesign.XMAPage; import at.spardat.xma.guidesign.XMAPagingControl; import at.spardat.xma.guidesign.XMASashForm; import at.spardat.xma.guidesign.XMAScrolledComposite; import at.spardat.xma.guidesign.XMASeperator; import at.spardat.xma.guidesign.XMATabFolder; import at.spardat.xma.guidesign.XMATable; import at.spardat.xma.guidesign.XMATableColumn; import at.spardat.xma.guidesign.XMAText; import at.spardat.xma.guidesign.XMATree; import at.spardat.xma.guidesign.XMAWidget; import at.spardat.xma.guidesign.types.PagingControlCustomStyle; import at.spardat.xma.guidesign.types.PagingControlStyle; import at.spardat.xma.guidesign.util.MyHashlist; import at.spardat.xma.mdl.paging.PagingControlClient; import at.spardat.xma.mdl.table.TableLayoutManager; import at.spardat.xma.page.Scaler; import at.spardat.xma.page.StatusBar; /** * @author s1462 * * To change this generated comment edit the template variable "typecomment": * Window>Preferences>Java>Templates. * To enable and disable the creation of type comments go to * Window>Preferences>Java>Code Generation. * @modified */ public class UIPreviewer extends ContentViewer { PreviewerWidgetFactory widgetFactory; //for debugging private static boolean debug = false; { String value = Platform.getDebugOption("at.spardat.xma.guidesigner/debug/preview"); if (value != null && value.equalsIgnoreCase("true")) { UIPreviewer.debug = true; } } static void debug(String message) { if (UIPreviewer.debug) { System.out.println(message); } } // end debugging stuff /** * counts the suspend-/resumeRefresh counts */ private int suspRefreshCounter = 0; /** * holds the context menu for the controls */ private MenuManager contextMenu; private static int count = 0; /** * holds the color that marks the current selection in the outside viewers */ private final Color markColor; /** * holds the Component object, to which the actual input belongs */ private XMAComponent rootComponent; /** * represents the actual selected widget */ private XMAWidget actWidget = null; /** * represents the former selected widget */ private XMAWidget oldWidget = null; /** * holds the DialogPage object, to wich the actual input belongs */ private IDialogRoot rootDialog; /** * the parent composite of this previewer */ private Composite parent; /** * the scrolled composite, that contains the simulate xma dialog shell */ private ScrolledComposite scrollComp; /** * the shell composite contains the page composite of the rootDialog and a statusbar */ private Composite shellComp; /** * the hashtable holds the list of controls for setting formadata * in a second step */ private MyHashlist adjControls; /** * the editor (for notification of selections) */ private GuidesignEditor editor; private boolean oldInputIsInvalid = false; UIPreviewerColorsAndFont colorsAndFont; /** * Constructor for UIPreviewer. */ public UIPreviewer(Composite _parent, GuidesignEditor _editor) { parent = _parent; editor = _editor; FillLayout parentLayout = new FillLayout(); parent.setLayout(parentLayout); scrollComp = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); markColor = getControl().getDisplay().getSystemColor(SWT.COLOR_YELLOW); createContextMenu(); } /** * Set the Preview according the selection state of the Outline Viewer. * The widget tree is shown according information available for the scrollComp * Dialogpage and its descendents. * */ private void setPreview() { if (shellComp != null) { //dispose the old preview shellComp.dispose(); } //produce a standard view shellComp = new Composite(scrollComp, SWT.BORDER); /* Preview colors and fonts */ colorsAndFont = new UIPreviewerColorsAndFont(shellComp); shellComp.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { if (colorsAndFont != null) { colorsAndFont.disposeColorsAndFonts(); colorsAndFont = null; } } }); FormLayout groupLayout = new FormLayout(); shellComp.setLayout(groupLayout); if (rootDialog == null || rootDialog.getComposite() == null) { //no data available to show Label message = new Label(shellComp, SWT.NONE); message.setText("For this selection ore the current state no PreView available!"); FormData data = new FormData(); data.left = new FormAttachment(0, 0); data.top = new FormAttachment(0, 0); message.setLayoutData(data); //do some cleanings //at least neccessary for the case where the page composite is deleted oldWidget = actWidget; actWidget = null; } else { StatusBar statusBar = null; adjControls = new MyHashlist(); /* Create the statusbar */ if (rootDialog instanceof XMADialogPage && ((XMADialogPage) rootDialog).isYnStatusBar()) { statusBar = createStatusBar(shellComp); } /* Create the WidgetFactory */ XMAComponent xmaComponent = (rootDialog.getComposite() != null) ? rootDialog.getComposite().getComponent() : null; if (xmaComponent != null) { xmaComponent.loadGuiDesignerProperties(editor.getResourceFile()); } widgetFactory = new PreviewerWidgetFactory(xmaComponent); /* Create the Dialogpage composite */ Composite dialogPageComp = (Composite) createControl(rootDialog.getComposite(), shellComp); //set the fixed formdata of the dialogPageComp setDialogpageCompFormData(dialogPageComp, statusBar); //set the formdata of all other underlying widgets setFormData(); //compute the size of scrolled composites computeSize(); dialogPageComp.layout(true); } //now set the outest pagecomposite in the scrollable parent scrollComp.setContent(shellComp); //and set the size setShellCompSize(shellComp); parent.layout(true); super.hookControl(scrollComp); } /** * Compute the size of scrolled composites. * This has to be done after setting all layout data! * @since 2.3.0 * @author S1462 */ private void computeSize() { Collection contColl = adjControls.values(); if (!contColl.isEmpty()) { Iterator i = contColl.iterator(); while (i.hasNext()) { Control c = (Control) i.next(); if (c instanceof ScrolledComposite) { XMAScrolledComposite xmaScroll = (XMAScrolledComposite) c.getData(); xmaScroll.computeSize(c, adjControls); } } } } /** * Set the size of the simulated shell. */ private void setShellCompSize(Composite shellComp) { final Scaler scaler = Scaler.getInstance(shellComp); int style = SWT.NONE; int rootWidth; int rootHeight; //to ensure the right size set the coresponding styles if (rootDialog == null) { //no dialog or one of its child selected rootWidth = scaler.convertXToCurrent(400); rootHeight = scaler.convertYToCurrent(300); } else { //remove 18 pixel from the height because of the title bar rootWidth = scaler.convertXToCurrent(rootDialog.getQntWidth()); rootHeight = scaler.convertYToCurrent(rootDialog.getQntHeight() - 18); } if (rootHeight < 1 || rootWidth < 1) { shellComp.pack(); } else { Shell test = new Shell(parent.getShell().getDisplay(), style); test.setSize(rootWidth, rootHeight); Rectangle rect = test.getClientArea(); shellComp.setSize(rect.width, rect.height); test.dispose(); } } /** * set the fixed formdata of the dialogPageComp * @param dialogPComp * @param statusBar */ private void setDialogpageCompFormData(Composite dialogPComp, StatusBar statusBar) { FormData data = new FormData(); data.left = new FormAttachment(0, 0); data.right = new FormAttachment(100, 0); data.top = new FormAttachment(0, 0); if (statusBar != null) { data.bottom = new FormAttachment(statusBar, 0, SWT.DEFAULT); } else { data.bottom = new FormAttachment(100, 0); } dialogPComp.setLayoutData(data); } /** * Creates the status bar composite on the bottom of the parent composite * @param parentComp */ private StatusBar createStatusBar(Composite parentComp) { StatusBar statusBar = new StatusBar(parentComp, SWT.NONE); FormData data = new FormData(); data.left = new FormAttachment(0, 0); data.right = new FormAttachment(100, 0); data.bottom = new FormAttachment(100, 0); statusBar.setLayoutData(data); return statusBar; } /** * * @param xmaParent * @param swtParent * @return */ private Composite buildWidgetTree(XMAComposite xmaParent, Composite swtParent) { final Scaler scaler = Scaler.getInstance(swtParent); Composite comp; if (xmaParent instanceof XMAGroup) { //SWT Group to instanciate comp = widgetFactory.createGroup(swtParent, xmaParent); } else { //all other cases a normal composite to instanciate comp = widgetFactory.createComposite(swtParent, xmaParent); } FormLayout groupLayout = new FormLayout(); if (xmaParent != null) { //when the composite is already set groupLayout.marginHeight = scaler.convertYToCurrent(xmaParent.getQntMarginHeight()); groupLayout.marginWidth = scaler.convertXToCurrent(xmaParent.getQntMarginWidth()); comp.setLayout(groupLayout); if (xmaParent.getControls() != null) { Iterator iter = xmaParent.getControls().iterator(); XMAWidget widget = null; //first create the widgets ... for (int i = 0; iter.hasNext(); i++) { widget = (XMAWidget) iter.next(); createControl(widget, comp); } //second set the TabList of the composite xmaParent.setTabList(comp, adjControls); } } else { comp.setLayout(groupLayout); } return comp; } /** * create the formdata of the widgets in the adjacent control table. * */ private void setFormData() { Collection contColl = adjControls.values(); if (!contColl.isEmpty()) { Iterator i = contColl.iterator(); while (i.hasNext()) { Control c = (Control) i.next(); XMAWidget w = (XMAWidget) c.getData(); Assert.isNotNull(w); w.createLayoutInfo(c, adjControls); } } //debug(getControlsAsString(adjControls)); } /** * Build the SashForm and its child component. * @param xmaSashform * @param swtParent * @return */ private SashForm buildSashForm(XMASashForm xmaSashform, Composite swtParent) { SashForm sashform = new SashForm(swtParent, xmaSashform.getStyle()); if (xmaSashform.getLeftEl() != null) { createControl(xmaSashform.getLeftEl(), sashform); } if (xmaSashform.getRightEl() != null) { createControl(xmaSashform.getRightEl(), sashform); } return sashform; } /** * Build the SashForm and its child component. * @param xmaSashform * @param swtParent * @return */ private ScrolledComposite buildScrolledComposite(XMAScrolledComposite xmaScrollComp, Composite swtParent) { ScrolledComposite scrollComp = new ScrolledComposite(swtParent, xmaScrollComp.getStyle()); if (xmaScrollComp.getInnerComp() != null) { createControl(xmaScrollComp.getInnerComp(), scrollComp); } return scrollComp; } /** * Build the TabFolder and its pages component. * @param xmaSashform * @param swtParent * @return */ private TabFolder buildTabFolder(XMATabFolder xmaTabfolder, Composite swtParent) { TabFolder tabfolder = new TabFolder(swtParent, xmaTabfolder.getStyle()); Iterator i = xmaTabfolder.getNbPage().iterator(); while (i.hasNext()) { NotebookPage page = (NotebookPage) i.next(); TabItem item = new TabItem(tabfolder, page.getStyle()); page.setProps(item); //dispose image resources item.addDisposeListener(ImageDisposer.getInstance()); item.setControl(createControl(page.getComposite(), tabfolder)); } return tabfolder; } /** * Build the content of a XMAContainer. * If the container holds a ComponentUri it shows a magenta colored composite. * If the container holds a local embedded page ist shows the embedde page. * @param xmaContainer * @param swtParent * @return */ private Composite buildContainer(XMAContainer xmaContainer, Composite swtParent) { Composite container; // create the composite that represents the container container = new Composite(swtParent, xmaContainer.getStyle()); if (xmaContainer.getEmbeddedPage() != null) { // create layout info of the container FormLayout layout = new FormLayout(); layout.marginHeight = 0; layout.marginWidth = 0; container.setLayout(layout); // we have to show the embedded page inplace EmbeddedPage page = xmaContainer.getEmbeddedPage(); Composite embPageComp = (Composite) createControl(page.getComposite(), container); //finally embed the page in the layout FormData fdata = new FormData(); fdata.left = new FormAttachment(0, 0); fdata.top = new FormAttachment(0, 0); fdata.right = new FormAttachment(100, 0); fdata.bottom = new FormAttachment(100, 0); embPageComp.setLayoutData(fdata); } return container; } private TableCombo buildTableCombo(XMATable xmaTable, Composite parent) { TableCombo tableCombo = new TableCombo(parent, xmaTable.getStyle()); String[] columnNames = new String[xmaTable.getColumn().size()]; int[] columnBounds = new int[xmaTable.getColumn().size()]; if (xmaTable.getColumn() != null) { Iterator iter = xmaTable.getColumn().iterator(); for (int i = 0; iter.hasNext(); i++) { XMATableColumn xmaColumn = (XMATableColumn) iter.next(); columnNames[i] = xmaColumn.getNamInstance(); columnBounds[i] = 200; } tableCombo.defineColumns(columnNames, columnBounds); tableCombo.setDisplayColumnIndex(0); } tableCombo.setShowTableHeader(true); return tableCombo; } /** * Build the Table and its columns * @param xmaTable * @param swtParent where the table will be placed * @return */ private Table buildTable(XMATable xmaTable, Composite swtParent) { Table table = new Table(swtParent, xmaTable.getStyle()); TableLayoutManager mgr = null; if (xmaTable.getColumn() != null) { Iterator iter = xmaTable.getColumn().iterator(); for (int i = 0; iter.hasNext(); i++) { XMATableColumn xmaTC = (XMATableColumn) iter.next(); if (!xmaTC.isYnHiden()) { //hiden columns don't have to be previewed TableColumn tc = new TableColumn(table, xmaTC.getStyle()); xmaTC.setProps(tc); //dispose image resources tc.addDisposeListener(ImageDisposer.getInstance()); } } final Scaler scaler = Scaler.getInstance(swtParent); // use the table layout manager mgr = new TableLayoutManager(); mgr.manageTable(table, TableLayoutManager.NONE); // keep a ref to the manager to facilitate table layout calculation action xmaTable.setLayoutManager(mgr); if (xmaTable.getQntDefaultColumnWidth() > -1) mgr.setDefaultAbsolutWidth(scaler.convertXToCurrent(xmaTable.getQntDefaultColumnWidth())); if (xmaTable.getQntDefaultColumnMinWidth() > -1) mgr.setDefaultMinWidth(scaler.convertXToCurrent(xmaTable.getQntDefaultColumnMinWidth())); if (xmaTable.getQntDefaultColumnMaxWidth() > -1) mgr.setDefaultMaxWidth(scaler.convertXToCurrent(xmaTable.getQntDefaultColumnMaxWidth())); if (xmaTable.getQntColumnMinWidthLimit() > -1) mgr.setMinLimit(scaler.convertXToCurrent(xmaTable.getQntColumnMinWidthLimit())); if (xmaTable.getCntStrechColumn() > -1) mgr.setStrechColumn(xmaTable.getSWTColumIndex(xmaTable.getCntStrechColumn())); int i = 0; for (iter = xmaTable.getColumn().iterator(); iter.hasNext();) { XMATableColumn xmaTC = (XMATableColumn) iter.next(); if (!xmaTC.isYnHiden()) { // using table layout infos from the columns if (xmaTC.getQntPercent() > -1) mgr.setPercent(i, xmaTC.getQntPercent()); if (xmaTC.getQntMinWidth() > -1) mgr.setMinWidth(i, scaler.convertXToCurrent(xmaTC.getQntMinWidth())); if (xmaTC.getQntMaxWidth() > -1) mgr.setMaxWidth(i, scaler.convertXToCurrent(xmaTC.getQntMaxWidth())); if (xmaTC.isYnAutoPack()) { mgr.setAbsolutWidth(i, table.getColumn(i).getWidth()); } else if (xmaTC.getQntWidth() > -1) { mgr.setAbsolutWidth(i, scaler.convertXToCurrent(xmaTC.getQntWidth())); } i++; } } } // finally layout the table if (mgr != null) mgr.layout(); return table; } /** * * @param xmaComposite * @param swtParent * @return the presentation of a flex generated composite (only a colored rectancle without contetn) * @since 3.2.2.3 * @author s1462 */ private Control buildGeneratedComposite(XMAComposite xmaComposite, Composite swtParent) { Composite composite; // create the composite that represents the container composite = new Composite(swtParent, xmaComposite.getStyle()); // create layout info of the container FormLayout layout = new FormLayout(); layout.marginHeight = 0; layout.marginWidth = 0; composite.setLayout(layout); return composite; } /** * * @param xmaComposite * @param swtParent * @return the presentation of a flex generated composite (only a colored rectancle without contetn) * @since 3.2.2.3 * @author s1462 */ private Control buildGeneratedGroup(XMAGroup xmagroup, Composite swtParent) { Group group; // create the composite that represents the container group = new Group(swtParent, xmagroup.getStyle()); // create layout info of the container FormLayout layout = new FormLayout(); layout.marginHeight = 0; layout.marginWidth = 0; group.setLayout(layout); return group; } /** * * @param widget * @param parent * @return * @modified */ private Control createControl(XMAWidget widget, Composite parent) { // Control control = null; //this code could be replaced with a switch if (widget instanceof XMAText) { control = widgetFactory.createText(parent, widget); } if (widget instanceof XMALabel) { control = widgetFactory.createLabel(parent, widget); } if (widget instanceof DataLabel) { control = widgetFactory.createLabel(parent, widget); } if (widget instanceof XMAButton) { control = widgetFactory.createButton(parent, widget); } if (widget instanceof XMACombo) { control = widgetFactory.createCombo(parent, widget); } if (widget instanceof SimpleCombo) { control = widgetFactory.createCombo(parent, widget); } if (widget instanceof XMAList) { control = widgetFactory.createList(parent, widget); } if (widget instanceof XMATree) { control = widgetFactory.createTree(parent, widget); } if (widget instanceof XMASeperator) { control = widgetFactory.createLabel(parent, widget); } if (widget instanceof XMATable) { if (((XMATable) widget).isYnCombo()) control = buildTableCombo((XMATable) widget, parent); else control = buildTable((XMATable) widget, parent); } if (widget instanceof XMAGroup) { // if (((XMAGroup)widget).isYnSimpleLayout()) { // control = buildGeneratedGroup((XMAGroup) widget ,parent); // } else { control = buildWidgetTree((XMAGroup) widget, parent); // } } else if (widget instanceof XMAComposite) { // if (((XMAComposite)widget).isYnSimpleLayout()) { // control = buildGeneratedComposite((XMAComposite) widget ,parent); // } else { control = buildWidgetTree((XMAComposite) widget, parent); } // } if (widget instanceof XMASashForm) { control = buildSashForm((XMASashForm) widget, parent); } if (widget instanceof XMATabFolder) { control = buildTabFolder((XMATabFolder) widget, parent); } if (widget instanceof XMAContainer) { control = buildContainer((XMAContainer) widget, parent); } if (widget instanceof XMAScrolledComposite) { control = buildScrolledComposite((XMAScrolledComposite) widget, parent); } if (widget instanceof DatePicker) { control = new at.spardat.xma.guidesign.presentation.DatePicker(parent, widget.getStyle()); } if (widget instanceof XMAGrid) { control = new at.spardat.xma.guidesign.presentation.XMAGrid(parent, widget.getStyle()); } if (widget instanceof XMAPagingControl) { XMAPagingControl ctrl = ((XMAPagingControl) widget); int style = ctrl.getPagingControlStyle().getValue(); if (style == PagingControlStyle.CUSTOM.getValue()) { style = 0; for (PagingControlCustomStyle customStyle : (java.util.List<PagingControlCustomStyle>) PagingControlCustomStyle.VALUES) { if (ctrl.getCustomStyles().isStyleSet(customStyle)) { style |= customStyle.getValue(); } } } control = new PagingControlClient(parent, Locale.getDefault(), style, widget.getStyle()); } if (control != null) { //workaround to ensure that every widget has his item provider associated AdapterFactoryLabelProvider provider = (AdapterFactoryLabelProvider) getLabelProvider(); provider.getText(widget); //set properties to ensure the right appearance widget.setProps(control); if (parent != scrollComp) { //we don't need the outer scrolled component in the adj list adjControls.put(widget, control); } control.addMouseListener(new MouseAdapter() { public void mouseDown(MouseEvent e) { debug("Mouse down on: " + e.widget.getData().toString()); ISelection selection = new StructuredSelection(e.widget.getData()); UIPreviewer.this.fireSelectionChanged(new SelectionChangedEvent(UIPreviewer.this, selection)); editor.setContentOutlineSelection(selection); } }); if (widget instanceof IImageUrl) { //dispose image resources control.addDisposeListener(ImageDisposer.getInstance()); } setContextMenuFor(control); /* Colors and fonts*/ colorsAndFont.setColorsAndFont(control, widget); return control; } else { //there is an unsopported widget //throw new IllegalArgumentException(widget.getClass() + "not supported!"); return null; } } /** * @see org.eclipse.jface.viewers.Viewer#inputChanged() */ protected void inputChanged(Object input, Object oldinput) { debug("in input changed: " + input); if (oldInputIsInvalid || input != oldinput) { oldInputIsInvalid = false; //set the root component if (input instanceof Resource) { rootComponent = (XMAComponent) ((Resource) input).getContents().get(0); //somewhat tricky, but resource : component is 1:1 and resource doesn't knows //a dialogpage input = rootComponent; } if (input instanceof XMAComponent) rootComponent = (XMAComponent) input; if (input instanceof IComponentCalculateable) { rootComponent = ((IComponentCalculateable) input).getComponent(); } //if input is in the part-of hierarchie of the widget XMAWidget widget = getXMAWidget(input); input = (widget != null) ? widget : input; //set the DialogPage to be shown //at this time input the dialogpage can be calculated from the input IDialogRoot dialogPage = null; if (input instanceof IDialogPageCalculateable) { dialogPage = ((IDialogPageCalculateable) input).getDialogPage(); } //and refresh the previewer if neccessary if (rootDialog != dialogPage) { //set the new DialogPage rootDialog = dialogPage; setPreview(); //reset the actual widget to the root composite if (dialogPage != null && dialogPage.getComposite() != null) { actWidget = dialogPage.getComposite(); } else { actWidget = null; } oldWidget = null; } } } /** * If input is in the part-of hierarchie of the widget, get the next widget parent * @param input * @return a xmawidget if input parameter is in the part-of hierarchie of the widget */ private XMAWidget getXMAWidget(Object input) { //if selection is a tablecolumn if (input instanceof XMATableColumn) { return ((XMATableColumn) input).getTable(); } if (input instanceof NotebookPage) { return ((NotebookPage) input).getComposite(); } return null; } /** * get the actual selected SWT Widget in the Previewer * @param xmaWidget - the selected object * @return the actual selected SWT Widget in the Previewer or null if not known */ private Control getActWidget(XMAWidget xmaWidget) { // getActWidget must not take care of embedded pages in a dialog page, // because if a widget of an embedded page is selected the input ins changed // therefore it is ensured, that in the context of this method // MyHashlist.get returns only an arraylist with one element ArrayList controls = (ArrayList) adjControls.get(xmaWidget); if (controls != null) { return (Control) controls.iterator().next(); } return null; } /** * @see org.eclipse.jface.viewers.Viewer#getControl() */ public Control getControl() { return scrollComp; } /** * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection() */ public ISelection getSelection() { if (actWidget != null) return new StructuredSelection(actWidget); else return null; } /** * @see org.eclipse.jface.viewers.Viewer#refresh() */ public void refresh() { debug(++count + ". refresh call"); //perhaps the the actual root dialog was deleted if (!rootDialogExists()) { rootDialog = null; } //handle move's to other dialog roots if (actWidget != null) { setRootDialog(actWidget.getDialogPage()); } if (!isRefreshSuspended()) { setPreview(); } } /** * set the rootDialog member * @param _rootDialog */ private void setRootDialog(IDialogRoot _rootDialog) { if (_rootDialog != null) { rootDialog = _rootDialog; } } /** * * @return true if the cashed rootDialog is still in the page list * of the root component */ private boolean rootDialogExists() { if (rootDialog == null) { return false; } //we can only check this, if we check if the parent componente //of the rootdialog already exists Iterator pages = rootComponent.getPage().iterator(); while (pages.hasNext()) { if (pages.next() == rootDialog) return true; } return false; } /** * Copied from TreeViewer!!! * The tree viewer implementation of this <code>Viewer</code> framework * method ensures that the given label provider is an instance * of <code>ILabelProvider</code>. */ public void setLabelProvider(IBaseLabelProvider labelProvider) { Assert.isTrue(labelProvider instanceof ILabelProvider); super.setLabelProvider(labelProvider); } // private ITreeContentProvider getTreeContentProvider () { // Assert.isTrue(getContentProvider() instanceof ITreeContentProvider); // return (ITreeContentProvider) getContentProvider(); // } /** * @see org.eclipse.jface.viewers.Viewer#setSelection(ISelection, boolean) */ public void setSelection(ISelection selection, boolean reveal) { debug("setSelection call"); if (selection != null && !selection.isEmpty() && selection instanceof IStructuredSelection) { Iterator selectedElements = ((IStructuredSelection) selection).iterator(); if (selectedElements.hasNext()) { // Get the first selected element. // At this time only the first selection was handled Object selectedElement = selectedElements.next(); //if input is in the part-of hierarchie of the widget, get the next widget parent XMAWidget widget = getXMAWidget(selectedElement); selectedElement = (widget != null) ? widget : selectedElement; if (selectedElement instanceof XMAWidget && !(selectedElement instanceof HiddenWidget)) { widget = (XMAWidget) selectedElement; oldWidget = actWidget; actWidget = widget; if (reveal) { // unmark the old selected widget if (oldWidget != null && getActWidget(oldWidget) != null) { oldWidget.markWidget(getActWidget(oldWidget), null); } // mark the actual selected widget if (actWidget != null) { // mantis 18594 fix selectNBPageInHierarchy(actWidget); //work around the problems where the control was already null if (getActWidget(actWidget) != null) { actWidget.markWidget(getActWidget(actWidget), markColor); } } } } else if (actWidget != null) { // hidden widget are not in the previewer but the actWidget has to be deselected if (selectedElement instanceof HiddenWidget || selectedElement instanceof XMAPage || selectedElement instanceof XMAComponent) { Control ctrl = getActWidget(actWidget); if (ctrl != null) { // unmark the old selection actWidget.markWidget(ctrl, null); } //there is no actual widget at this time actWidget = null; } } } debug("UIPrev. Current Selection:" + selection.toString()); } } /* * fix mantis 18594 (election sychron. with nbpage content) thru recursiv method which selects all * relevant nb-pages in the hierarchy- */ private void selectNBPageInHierarchy(XMAWidget _actWidget) { if (_actWidget.isContainedInNotebook()) { //select the coresponding tab PageComposite pageComp = _actWidget.getPageComposite(_actWidget); NotebookPage actPage = (NotebookPage) pageComp.getPage(); XMATabFolder folder = actPage.getFolder(); EList pageList = folder.getNbPage(); Iterator iter = pageList.iterator(); int index = -1; for (int i = 0; iter.hasNext(); i++) { NotebookPage page = (NotebookPage) iter.next(); if (page.equals(actPage)) { index = i; break; } } TabFolder tabfolder = (TabFolder) getActWidget(folder); tabfolder.setSelection(index); selectNBPageInHierarchy(folder); } } protected void createContextMenu() { contextMenu = new MenuManager("#PopUp", "at.spardat.xma.guidesign.presentation.UIPreViewer.Popup"); contextMenu.add(new Separator("additions")); contextMenu.setRemoveAllWhenShown(true); contextMenu.addMenuListener(editor); editor.getSite().registerContextMenu(contextMenu, this); } protected void setContextMenuFor(Control control) { Menu menu = contextMenu.createContextMenu(control); control.setMenu(menu); } //use this for debugging private String getControlsAsString(MyHashlist controls) { StringBuffer result = new StringBuffer(1024); Collection contColl = controls.values(); if (!contColl.isEmpty()) { Iterator i = contColl.iterator(); int j = 0; while (i.hasNext()) { Control cont = (Control) i.next(); Assert.isNotNull(cont); if (cont.getData() instanceof XMAComposite) result.append("----------------- Composite ---------------\n"); result.append("control "); result.append(new Integer(j++).toString()); result.append(" "); result.append(cont.getData().toString()); result.append("\n"); FormData data = (FormData) cont.getLayoutData(); if (data != null) { result.append("fdata: "); result.append(data.toString()); result.append("\n"); if (data.top != null) { result.append("topAttach: "); result.append(data.top.toString()); result.append("\n"); } if (data.bottom != null) { result.append("bottomAttach: "); result.append(data.bottom.toString()); result.append("\n"); } if (data.left != null) { result.append("leftAttach: "); result.append(data.left.toString()); result.append("\n"); } if (data.right != null) { result.append("rightAttach: "); result.append(data.right.toString()); result.append("\n"); } } } } return result.toString(); } /** * stops refreshing the content of this previewer until * resumeRefresh() is called. * */ protected void suspendRefresh() { suspRefreshCounter += 1; } /** * restarts refreshing of the content of this previewer as soon as * all suspenders are resumed. After the last suspender is resumed * the preview ist refreshed. * */ protected void resumeRefresh() { if (isRefreshSuspended()) { suspRefreshCounter -= 1; } if (suspRefreshCounter == 0) { setPreview(); } } /** * * @return true if refreshing the previewer is suspended */ private boolean isRefreshSuspended() { if (suspRefreshCounter > 0) { return true; } return false; } /** * Set a flag that the old input is invalid. This forces that in the next * call of inputChanged the old input is replaced by the new input. */ public void invalidateOldInput() { oldInputIsInvalid = true; } }