How to workaround SWTResourceManager.getImage

samhu samhuatgmailnotwork. at gmail.com
Sat Oct 5 23:35:01 PDT 2013


Greetings!

I am trying to compile below dummyeclipse project ,but first
encountered the subjected issue.I knew to form a Image object one
can use:
[code]
Image image=new Image(display,imgfilename);//e.g. smile.jpg
[/code]
But I don't know how to load from a resource file(*.res) to form
a image as used in DWT java:
[code]
(new Label).setImage(SWTResourceManager.getImage("PROP_PS",
GIFFILE));
[/code]


dummyeclipse.d
[code]
//: \@SWT dummyeclipse.res -d
module dummyeclipse;

import org.eclipse.swt.all;


public class DummyEclipse : Composite {

	private ToolItem runToolItem;
	private ToolItem debugToolItem;
	private ToolItem newToolItem;
	private MenuItem openMenuItem;

	private Color[3] gradientColors;
	private int[2] percentArray = [50, 100];
	
	static const char[] GIFFILE = "GIFFILE";

	public this(Composite parent, int style) {
		super(parent, style);
		
		Display display = getDisplay();
		//SWTResourceManager.registerResourceUser(this);
		
		// not like java, stores image file in jar file,
		// we load image from compiled resource linked with this app
		gradientColors[0] = display.getSystemColor(SWT.COLOR_DARK_BLUE);
		gradientColors[1] = display.getSystemColor(SWT.COLOR_BLUE);
		gradientColors[2] = null;
		
		this.getShell().setImage(new image(diplay,"SAMPLE.gif"));;
		initGUI();
	}

	public void initGUI() {

		preInitGUI();
		this.setSize(new Point(489, 353));
		GridLayout thisLayout = new GridLayout(1, true);
		with(thisLayout){
			marginWidth = 0;
			marginHeight = 0;
			numColumns = 1;
			makeColumnsEqualWidth = true;
			horizontalSpacing = 0;
			verticalSpacing = 0;
		}
		this.setLayout(thisLayout);	

		createMenuBar();

		// Toolbar
		{
			Label label1 = new Label(this, SWT.HORIZONTAL|SWT.SEPARATOR);
			GridData label1LData = new GridData();
			label1LData.horizontalAlignment = GridData.FILL;
			label1LData.grabExcessHorizontalSpace = true;
			label1.setText("label1");
			label1.setLayoutData(label1LData);
			label1.setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));

			Composite composite_toolBar = new Composite(this, SWT.NONE);
			composite_toolBar.setLayout (new GridLayout ());
			composite_toolBar.setLayoutData (new GridData
(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL |
GridData.VERTICAL_ALIGN_FILL));
			createToolBar(composite_toolBar);
		}
		Composite composite_Main = new Composite(this, SWT.NONE);
		composite_Main.setLayout(new FillLayout());
		GridLayout composite8Layout = new GridLayout();
		composite8Layout.marginWidth = 0;
		composite8Layout.marginHeight = 0;
		composite8Layout.numColumns = 3;
		composite8Layout.verticalSpacing = 0;
		composite8Layout.horizontalSpacing = 1;
		GridData composite8LData = new GridData();
		composite8LData.grabExcessHorizontalSpace = true;
		composite8LData.grabExcessVerticalSpace = true;
		composite8LData.verticalAlignment = GridData.FILL;
		composite8LData.horizontalAlignment = GridData.FILL;
		composite_Main.setLayout(composite8Layout);
		composite_Main.setLayoutData(composite8LData);
		composite_Main.layout();
		
		SashForm sashForm_Main = new SashForm(composite_Main, SWT.NONE);
		GridData sashForm2LData = new GridData();
		sashForm2LData.horizontalAlignment = GridData.FILL;
		sashForm2LData.grabExcessHorizontalSpace = true;
		sashForm2LData.verticalAlignment = GridData.FILL;
		sashForm2LData.grabExcessVerticalSpace = true;
		sashForm_Main.setOrientation(SWT.VERTICAL);
		sashForm_Main.setLayoutData(sashForm2LData);

		SashForm sashForm_top = new SashForm(sashForm_Main, SWT.NONE);
		createTopLeftFolder(sashForm_top);
		createTopRigthFolder(sashForm_top);
		createBottomCFolder(sashForm_Main);
		
		this.layout();
		postInitGUI();
	}	

	/**
	* Auto-generated method to display this
	* %SUPERCLASS_NAME% inside a new Shell.
	*/
	public static void showGUI() {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		shell.setText("Dummy Eclipse");
		//Util.trace(shell.getText());
		DummyEclipse inst = new DummyEclipse(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x,
size.y);
			int MENU_HEIGHT = 22;
			if (shell.getMenuBar() !is null)
				shellBounds.height -= MENU_HEIGHT;
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	private CTabFolder createBottomCFolder(Composite parent){
		CTabFolder cTabFolder_bottom = new CTabFolder(parent,
SWT.BOTTOM | SWT.BORDER | SWT.FLAT);

		CTabItem cTabItem10 = new CTabItem(cTabFolder_bottom, SWT.NONE);
		cTabItem10.setText("  Properties  ");
		
		with(new CTabItem(cTabFolder_bottom, SWT.NONE))
		{
			setText("  Search  ");
			setControl(new Text(cTabFolder_bottom,
SWT.MULTI|SWT.V_SCROLL|SWT.H_SCROLL));
		}

		Composite composite12 = new Composite(cTabFolder_bottom,
SWT.NONE);
		cTabItem10.setControl(composite12);
		GridLayout composite12Layout = new GridLayout();
		composite12Layout.marginWidth = 0;
		composite12Layout.marginHeight = 0;
		composite12Layout.makeColumnsEqualWidth = true;
		composite12Layout.horizontalSpacing = 0;
		composite12Layout.verticalSpacing = 0;
		composite12.setLayout(composite12Layout);
		composite12.layout();

		Composite composite14 = new Composite(composite12, SWT.NONE);
		GridLayout composite14Layout = new GridLayout();
		composite14Layout.marginWidth = 0;
		composite14Layout.marginHeight = 0;
		composite14Layout.numColumns = 2;
		composite14Layout.horizontalSpacing = 0;
		composite14Layout.verticalSpacing = 0;
		GridData composite14LData = new GridData();
		composite14LData.grabExcessHorizontalSpace = true;
		composite14LData.horizontalAlignment = GridData.FILL;
		composite14LData.verticalAlignment = GridData.FILL;
		composite14.setLayout(composite14Layout);
		composite14.layout();
		composite14.setLayoutData(composite14LData);

		Table table1 = new Table(composite12, SWT.CHECK);
		GridData table1LData = new GridData();
		table1LData.grabExcessVerticalSpace = true;
		table1LData.grabExcessHorizontalSpace = true;
		table1LData.horizontalAlignment = GridData.FILL;
		table1LData.verticalAlignment = GridData.FILL;
		table1.setHeaderVisible(true);
		table1.setLinesVisible(true);
		table1.setLayoutData(table1LData);

		with(new TableColumn(table1, SWT.NONE)) {
			setText("Property");
			setWidth(100);
		}
		with(new TableColumn(table1, SWT.NONE)) {
			setText("Value");
			setWidth(100);
		}
		with(new TableItem(table1, SWT.NONE)) {
			setText("tableItem1");
			setChecked(true);
		}
		with(new TableItem(table1, SWT.NONE)) {
			setText("tableItem2");
			setGrayed(true);
		}
		CLabel cLabel7 = new CLabel(composite14, SWT.NONE);
		GridData cLabel7LData = new GridData();
		cLabel7LData.grabExcessHorizontalSpace = true;
		cLabel7LData.horizontalAlignment = GridData.FILL;
		cLabel7LData.verticalAlignment = GridData.FILL;
		cLabel7.setText("Properties");
		//cLabel7.setImage(SWTResourceManager.getImage("PROP_PS",
GIFFILE));
		cLabel7.setForeground(getDisplay().getSystemColor(SWT.COLOR_WHITE));
		cLabel7.setLayoutData(cLabel7LData);
		cLabel7.setBackground(gradientColors, percentArray);

		ToolBar toolBar = new ToolBar(composite14, SWT.FLAT);
		GridData toolBar4LData = new GridData();
		toolBar.setLayoutData(toolBar4LData);

		with(new ToolItem(toolBar, SWT.NONE)) {
			//setImage(SWTResourceManager.getImage("VIEW_MENU", GIFFILE));
		}

		with(new ToolItem(toolBar, SWT.NONE)) {
			//setImage(SWTResourceManager.getImage("CLOSE_VIEW", GIFFILE));
		}

		with(cTabFolder_bottom){
			setTabHeight(24);
			setSelection(0);
			setSimple(false);
			setSelectionForeground(getDisplay().getSystemColor(SWT.COLOR_WHITE));
		}
		cTabFolder_bottom.setSelectionBackground(gradientColors,
percentArray, true);
					
		return cTabFolder_bottom;
	}
	
	/**
	 * Listens to widgetSelected() events on SWT.DROP_DOWN type
CoolItems
	 * and opens/closes a menu when appropriate.
	 */
	public void onCoolItemSelection(SelectionEvent event){
		Menu menu = null;
		/**
		 * A selection event will be fired when the cool item
		 * is selected by its gripper or if the drop down arrow
		 * (or 'chevron') is selected. Examine the event detail
		 * to determine where the widget was selected.
		 */
		if (event.detail == SWT.ARROW) {
			/* If the popup menu is already up (i.e. user pressed arrow
twice),
			 * then dispose it.
			 */
			if (menu !is null) {
				menu.dispose();
				menu = null;
				return;
			}
			
			/* Get the cool item and convert its bounds to display
coordinates. */
			ToolBar toolBar = cast(ToolBar) event.widget;
			Rectangle itemBounds = toolBar.getBounds ();
			Point pt = toolBar.toDisplay(new Point (itemBounds.x,
itemBounds.y));
			itemBounds.x = pt.x;
			itemBounds.y = pt.y;
			
			/* Get the toolbar from the cool item. */
			ToolItem[] tools = toolBar.getItems ();
			int toolCount = tools.length;
							
			/* Convert the bounds of each tool item to display coordinates,
			 * and determine which ones are past the bounds of the cool
item.
			 */
			int i = 0;
			while (i < toolCount) {
				Rectangle toolBounds = tools[i].getBounds ();
				pt = toolBar.toDisplay(new Point(toolBounds.x, toolBounds.y));
				toolBounds.x = pt.x;
				toolBounds.y = pt.y;
		  		Rectangle intersection = itemBounds.intersection
(toolBounds);
		  		if (!intersection.equals (toolBounds)) break;
		  		i++;
			}
			
			/* Create a pop-up menu with items for each of the hidden
buttons. */
			menu = new Menu (toolBar);
			for (int j = i; j < toolCount; j++) {
				ToolItem tool = tools[j];
				Image image = tool.getImage();
				if (image is null) {
					new MenuItem (menu, SWT.SEPARATOR);
				} else {
					if ((tool.getStyle() & SWT.DROP_DOWN) != 0) {
						MenuItem menuItem = new MenuItem (menu, SWT.CASCADE);
						menuItem.setImage(image);
						Menu m = new Menu(menu);
						menuItem.setMenu(m);
//						for (int k = 0; k < 9; ++k) {
//							char[] text = (("DropDownData_") ~ k);
//							if (text.length != 0) {
//								MenuItem mi = new MenuItem(m, SWT.NONE);
//								mi.setText(text);
//								/* Application code to perform the action for the
submenu item would go here. */
//							} else {
//								new MenuItem(m, SWT.SEPARATOR);
//							}
//						}
					} else {
						MenuItem menuItem = new MenuItem (menu, SWT.NONE);
						menuItem.setImage(image);
					}
					/* Application code to perform the action for the menu item
would go here. */

				}
			}
			
			/* Display the pop-up menu at the lower left corner of the
arrow button.
			 * Dispose the menu when the user is done with it.
			 */
			pt = toolBar.toDisplay(new Point(event.x, event.y));
			menu.setLocation (pt.x, pt.y);
			menu.setVisible (true);
			Display display = toolBar.getDisplay ();
			while (menu !is null && !menu.isDisposed() && menu.isVisible
()) {
				if (!display.readAndDispatch ()) display.sleep ();
			}
			if (menu !is null) {
				menu.dispose ();
				menu = null;
			}
		}
	}
	
	private void createToolBar(Composite parent){
		ToolBar toolBar = new ToolBar(parent, SWT.FLAT);
		
		newToolItem = new ToolItem(toolBar, SWT.DROP_DOWN);
		//newToolItem.setImage(SWTResourceManager.getImage("NEW_WIZ",
GIFFILE));
		
		// java like anonymous class, remember to introduce
		// the parent class, here is "DummyEclipse" into
		class NewToolItemSelectionListener : SelectionAdapter {
			private DummyEclipse cData;
			public this(DummyEclipse cData){this.cData = cData;}
			public void widgetSelected(SelectionEvent evt) {
				cData.newToolItemWidgetSelected(evt);
			}
		};
		newToolItem.addSelectionListener(new
NewToolItemSelectionListener(this));
		
		ToolItem toolItem6 = new ToolItem(toolBar, SWT.NONE);
		//toolItem6.setImage(SWTResourceManager.getImage("SAVEAS_EDIT",
GIFFILE));

		ToolItem toolItem5 = new ToolItem(toolBar, SWT.NONE);
		//toolItem5.setImage(SWTResourceManager.getImage("SAVE_EDIT",
GIFFILE));

		ToolItem toolItem8 = new ToolItem(toolBar, SWT.NONE);
		//toolItem8.setImage(SWTResourceManager.getImage("PRINT_EDIT",
GIFFILE));

		new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem toolItem13 = new ToolItem(toolBar, SWT.NONE);
		//toolItem13.setImage(SWTResourceManager.getImage("BUILD_EXEC",
GIFFILE));
		
		
		debugToolItem = new ToolItem(toolBar, SWT.DROP_DOWN);
		debugToolItem.handleSelection(this,
&debugToolItemWidgetSelected);
		//debugToolItem.setImage(SWTResourceManager.getImage("DEBUG_EXEC",
GIFFILE));
		
		runToolItem = new ToolItem(toolBar, SWT.DROP_DOWN);
		//runToolItem.setImage(SWTResourceManager.getImage("RUN_EXEC",
GIFFILE));
		runToolItem.handleSelection(this,
&runToolItemWidgetSelected);			
	}

	private CTabFolder createTopLeftFolder(Composite parent) {
			
		CTabFolder cTabFolder_topleft = new CTabFolder(parent,
SWT.BOTTOM | SWT.BORDER);
		
		CTabItem cTabItem4 = new CTabItem(cTabFolder_topleft, SWT.NONE);
		cTabItem4.setText("  Package Explorer  ");

		CTabItem cTabItem5 = new CTabItem(cTabFolder_topleft, SWT.NONE);
		cTabItem5.setText("  Hierarchy  ");

		Composite composite10 = new Composite(cTabFolder_topleft,
SWT.NONE);
		cTabItem5.setControl(composite10);
		GridLayout composite10Layout = new GridLayout();
		composite10Layout.marginWidth = 0;
		composite10Layout.marginHeight = 0;
		composite10Layout.makeColumnsEqualWidth = true;
		composite10Layout.horizontalSpacing = 0;
		composite10Layout.verticalSpacing = 0;
		composite10.setLayout(composite10Layout);
		composite10.layout();

		Composite composite7 = new Composite(cTabFolder_topleft,
SWT.NONE);
		cTabItem4.setControl(composite7);
		GridLayout composite7Layout = new GridLayout();
		composite7Layout.marginWidth = 0;
		composite7Layout.marginHeight = 0;
		composite7Layout.makeColumnsEqualWidth = true;
		composite7Layout.horizontalSpacing = 0;
		composite7Layout.verticalSpacing = 0;
		composite7.setLayout(composite7Layout);
		composite7.layout();

		CLabel cLabel5 = new CLabel(composite10, SWT.NONE);
		GridData cLabel5LData = new GridData();
		cLabel5LData.horizontalAlignment = GridData.FILL;
		cLabel5LData.verticalAlignment = GridData.FILL;
		cLabel5LData.grabExcessHorizontalSpace = true;
		cLabel5.setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		cLabel5.setText("Hierarchy Test");
		//cLabel5.setImage(SWTResourceManager.getImage("SAMPLE",
GIFFILE));
		cLabel5.setLayoutData(cLabel5LData);

		Tree tree4 = new Tree(composite10, SWT.NONE);
		GridData tree4LData = new GridData();
		tree4LData.grabExcessHorizontalSpace = true;
		tree4LData.grabExcessVerticalSpace = true;
		tree4LData.horizontalAlignment = GridData.FILL;
		tree4LData.verticalAlignment = GridData.FILL;
		tree4.setLayoutData(tree4LData);

		TreeItem treeItem10 = new TreeItem(tree4, SWT.NONE);
		treeItem10.setText("Composite");
		treeItem10.setExpanded(true);

		TreeItem treeItem12 = new TreeItem(treeItem10, SWT.NONE);
		treeItem12.setText("Canvas");

		TreeItem treeItem11 = new TreeItem(treeItem10, SWT.NONE);
		treeItem11.setText("SashForm");

		Composite composite9 = new Composite(composite7, SWT.NONE);
		GridLayout composite9Layout = new GridLayout();
		composite9Layout.marginWidth = 0;
		composite9Layout.marginHeight = 0;
		composite9Layout.numColumns = 2;
		composite9Layout.horizontalSpacing = 0;
		composite9Layout.verticalSpacing = 0;
		GridData composite9LData = new GridData();
		composite9LData.heightHint = 24;
		composite9LData.grabExcessHorizontalSpace = true;
		composite9LData.horizontalAlignment = GridData.FILL;
		composite9.setLayout(composite9Layout);
		composite9.layout();
		composite9.setLayoutData(composite9LData);

		Tree tree3 = new Tree(composite7, SWT.NONE);
		GridData tree3LData = new GridData();
		tree3LData.grabExcessHorizontalSpace = true;
		tree3LData.grabExcessVerticalSpace = true;
		tree3LData.horizontalAlignment = GridData.FILL;
		tree3LData.verticalAlignment = GridData.FILL;
		tree3.setLayoutData(tree3LData);

		TreeItem treeItem7 = new TreeItem(tree3, SWT.NONE);
		treeItem7.setText("testPackage");
		//treeItem7.setImage(SWTResourceManager.getImage("PACKAGE",
GIFFILE));
		treeItem7.setExpanded(true);

		TreeItem treeItem8 = new TreeItem(treeItem7, SWT.NONE);
		treeItem8.setText("Class1");
		//treeItem8.setImage(SWTResourceManager.getImage("JCU_OBJ",
GIFFILE));

		TreeItem treeItem9 = new TreeItem(treeItem7, SWT.NONE);
		treeItem9.setText("Class2");
		//treeItem9.setImage(SWTResourceManager.getImage("JCU_OBJ",
GIFFILE));

		CLabel cLabel4 = new CLabel(composite9, SWT.NONE);
		GridData cLabel4LData = new GridData();
		cLabel4LData.verticalAlignment = GridData.FILL;
		cLabel4LData.horizontalAlignment = GridData.FILL;
		cLabel4LData.grabExcessHorizontalSpace = true;
		cLabel4.setText("Package Explorer");
		//cLabel4.setImage(SWTResourceManager.getImage("PACKAGE",
GIFFILE));
		cLabel4.setLayoutData(cLabel4LData);

		ToolBar toolBar5 = new ToolBar(composite9, SWT.FLAT);
		GridData toolBar5LData = new GridData();
		toolBar5.setLayoutData(toolBar5LData);

		ToolItem toolItem3 = new ToolItem(toolBar5, SWT.NONE);
		//toolItem3.setImage(SWTResourceManager.getImage("VIEW_MENU",
GIFFILE));

		ToolItem toolItem4 = new ToolItem(toolBar5, SWT.NONE);
		//toolItem4.setImage(SWTResourceManager.getImage("close_view",
GIFFILE));

		cTabFolder_topleft.setSelection(0);
		
		return cTabFolder_topleft;
	}
	
	private CTabFolder createTopRigthFolder(Composite parent){
			
		CTabFolder cTabFolder_topright = new CTabFolder(parent, SWT.TOP
| SWT.CLOSE | SWT.H_SCROLL | SWT.BORDER);

		CTabItem cTabItem6 = new CTabItem(cTabFolder_topright,
SWT.NONE);
		cTabItem6.setText("  FakeJava.java  ");
		//cTabItem6.setImage(SWTResourceManager.getImage("JCU_OBJ",
GIFFILE));

		CTabItem cTabItem7 = new CTabItem(cTabFolder_topright,
SWT.NONE);
		cTabItem7.setText("  FakeJava.form  ");
		//cTabItem7.setImage(SWTResourceManager.getImage("FORM",
GIFFILE));

		ScrolledComposite scrolledComposite1 = new ScrolledComposite(
			cTabFolder_topright,
			SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		cTabItem7.setControl(scrolledComposite1);

		Text text2 = new Text(cTabFolder_topright, SWT.MULTI
			| SWT.H_SCROLL
			| SWT.V_SCROLL);
		cTabItem6.setControl(text2);
		text2.setText("package fake;\n\npublic class FakeJava
{\n\n\tpublic FakeJava() {\n\t}\n\n}");

		with(cTabFolder_topright){
			setTabHeight(24);
			setSelection(0);
			setSimple(false);
			setSelectionForeground(getDisplay().getSystemColor(SWT.COLOR_WHITE));
		}
		cTabFolder_topright.setSelectionBackground(gradientColors,
percentArray, true);
		
		return cTabFolder_topright;
	}
	
	private void createMenuBar(){
		Menu menubar = new Menu(getShell(), SWT.BAR);
		getShell().setMenuBar(menubar);

		// file menu
		{
			MenuItem fileMenuItem = new MenuItem(menubar, SWT.CASCADE);
			Menu menu = new Menu(fileMenuItem);
			fileMenuItem.setText("&File");
			fileMenuItem.setMenu(menu);

			// new ...
			MenuItem newMenuItem = new MenuItem(menu, SWT.CASCADE);
			Menu items = new Menu(newMenuItem);
			newMenuItem.setMenu(items);
			newMenuItem.setText("&New");
			with(new MenuItem(items, SWT.PUSH)){
				setText("Project");
			};
			new MenuItem(items, SWT.SEPARATOR);
			with(new MenuItem(items, SWT.PUSH)){
				setText("Package");
			};
		
			new MenuItem(menu, SWT.SEPARATOR);

			// open
			openMenuItem = new MenuItem(menu, SWT.PUSH);
			with(openMenuItem){
				setText("&Open Dir");
				// handle anonymous delegate
				handleEvent(this, SWT.Selection, delegate(Event e){
					DummyEclipse d = cast(DummyEclipse)e.cData;
					auto DirectoryDialog dlg = new DirectoryDialog(d.getShell());
					dlg.setMessage("Find the dir ");
					char[] fullpath = dlg.open();
					if(fullpath !is null){
						MessageBox.showMsg(fullpath, ("You selected"), d.getShell()
);
					}
				});
			}

			// open multiple
			with(openMenuItem = new MenuItem(menu, SWT.PUSH)){
				setText("&Open Multiple");
				handleEvent(this, SWT.Selection, &onOpenMultiple);
			}
			
			// close
			with(new MenuItem(menu, SWT.PUSH)){
				setText("&Close");
				handleSelection(this, &onMenuSelected);
			}
			// save
			with(new MenuItem(menu, SWT.PUSH)){
				setText("&Save");
				handleSelection(this, &onMenuSelected);
				//setImage(SWTResourceManager.getImage("SAVE", GIFFILE));
			}
			// exit
			with(new MenuItem(menu, SWT.PUSH)){
				setText("&Exit");
				handleEvent(this, SWT.Selection, &onExit);
			}		
		}
		
		
		// edit menu
		{
			MenuItem editMenuItem = new MenuItem(menubar, SWT.CASCADE);
			Menu menu = new Menu(editMenuItem);
			editMenuItem.setText("&Edit");
			editMenuItem.setMenu(menu);
			with(new MenuItem(menu, SWT.PUSH)){setText("Undo");}
			with(new MenuItem(menu, SWT.PUSH)){setText("Redo");}
			new MenuItem(menu, SWT.SEPARATOR);
			with(new MenuItem(menu, SWT.PUSH)){setText("Cut");}
			with(new MenuItem(menu, SWT.PUSH)){setText("Paste");}
			with(new MenuItem(menu, SWT.PUSH)){setText("Copy");}
		}
		
		// create help menu
		{
			MenuItem helpMenuItem = new MenuItem(menubar, SWT.CASCADE);
			Menu menu = new Menu(helpMenuItem);
			helpMenuItem.setText("&Help");
			helpMenuItem.setMenu(menu);	
			MenuItem item = new MenuItem(menu, SWT.PUSH);
			item.setText("&About");
			// call Widget.handleEvent(int eventType, delegate()) to
			// handle a event
			item.handleEvent(this, SWT.Selection, &onHelpAbout);
		}

	}

	/**
	 * Add your pre-init code in here
	 */
	public void preInitGUI() {
	}

	/**
	 * Add your post-init code in here
	 */
	public void postInitGUI() {
	}

	/** Auto-generated event handler method */
	public void newToolItemWidgetSelected(SelectionEvent evt) {
		if (evt.detail == SWT.ARROW) {
			Menu menu = new Menu(getShell(), SWT.POP_UP);
			MenuItem mi = new MenuItem(menu, SWT.PUSH);
			mi.setText("New...");
			Rectangle b = newToolItem.getBounds();
			Point pt = new Point(b.x, b.y + b.height);
			pt = newToolItem.getParent().toDisplay(pt);
			menu.setLocation(pt.x, pt.y);
			menu.setVisible(true);
			mi.handleSelection(this, &onMenuSelected);
		}
	}

	/** Auto-generated event handler method */
	public void runToolItemWidgetSelected(SelectionEvent evt) {
		if (evt.detail == SWT.ARROW) {
			Menu menu = new Menu(getShell(), SWT.POP_UP);
			MenuItem mi = new MenuItem(menu, SWT.PUSH);
			mi.setText("Run...");
			Rectangle b = runToolItem.getBounds();
			Point pt = new Point(b.x, b.y + b.height);
			pt = runToolItem.getParent().toDisplay(pt);
			menu.setLocation(pt.x, pt.y);
			menu.setVisible(true);
		}
	}

	/** Auto-generated event handler method */
	public void debugToolItemWidgetSelected(SelectionEvent evt) {
		if (evt.detail == SWT.ARROW) {
			Menu menu = new Menu(getShell(), SWT.POP_UP);
			MenuItem mi = new MenuItem(menu, SWT.PUSH);
			mi.setText("Debug...");
			Rectangle b = debugToolItem.getBounds();
			Point pt = new Point(b.x, b.y + b.height);
			pt = debugToolItem.getParent().toDisplay(pt);
			menu.setLocation(pt.x, pt.y);
			menu.setVisible(true);
		}
	}
	
	private void onExit(Event e){
		MessageBox msg = new MessageBox(getShell(), SWT.OK |
SWT.CANCEL|SWT.ICON_QUESTION);
		msg.setText("Question");
		msg.setMessage("Are you sure you want to quit ?");
		if(msg.open() == SWT.OK){
			getShell().dispose();
		}
	}
	
	private void onHelpAbout(Event e){
		char[] str = (" === Dummy Eclipse Sample ===\r\n\r\n");
		str ~= "         Used to test SWT\r\n";
		str ~= "        Author : Shawn Liu\r\n\r\n\r\n";
		str ~= " http://www.dsource.org/projects/SWT     \r\n";
		
		Util.trace(str);

		MessageBox.showMsg(str, ("About DummyEclipse"), getShell());
	}
	
	void onOpenMultiple(Event e){
		auto FileDialog dlg = new FileDialog(getShell(),
SWT.MULTI|SWT.OPEN);
		dlg.setText("Multiple Selection Test");
		char[][] a;
		a ~= "*.txt;*.d";
		a ~= "*.*";
		dlg.setFilterExtensions(a);
		char[] fullpath = dlg.open();
		if(fullpath !is null){
			char[] str = "";
			str ~= fullpath ~"\r\n";
			char[][] names = dlg.getFileNames();
			if(names.length > 1){
				foreach(char[] s; names)
					str ~= s~"\r\n";
			}
			MessageBox.showMsg(str, ("You selected"), getShell());
		}
	}
	
	private void onMenuSelected (SelectionEvent e) {		
		MenuItem w = cast(MenuItem)e.widget;
		assert(w);
		Widget i = e.item;
		if(i !is null)
			Util.trace("e.item is ", i);
			
		char[] text = w.getText() ;
		
		// proccess event by item text
		switch( text ){
			case "Save":		
				auto FileDialog dlg = new FileDialog(getShell());
				char[][] a;
				a ~= "*.txt;*.d";
				a ~= "*.*";
				dlg.setFilterExtensions(a);
				char[] fullpath = dlg.open();
				if(fullpath !is null){
					MessageBox.showMsg(fullpath, ("You selected"), getShell());
				}
				break;
			case "CLOSE":
				break;
			case "ABOUT":
				break;
			default:
				break;
		}
	}


}

/** Auto-generated main method */
void main() {
	try{
		DummyEclipse.showGUI();
	}catch(Object o){
		MessageBox.showMsg(o.toString());
	}
}

[/code]
Help would be much appreciated.

Regards,
Sam


More information about the Digitalmars-d-dwt mailing list