[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.rcp] Re: Show view in Action.run( )

Hi Francis,

I'll post my code as follows. Regarding test case, do you have any references for RCP testing? Thanks. Please help.

-------My Action class-----
public class MyAction extends Action
                             implements ActionFactory.IWorkbenchAction {
	private final IWorkbenchWindow window;
	private final static String ID = "actions.myaction";
	private ViewTest vp;
	
	public MyAction(IWorkbenchWindow window) {
		this.window = window;
		setId(ID);
		setActionDefinitionId(ID);
		setText("&Action 2...");
		setToolTipText(".... ");
		setImageDescriptor(AbstractUIPlugin
                      .imageDescriptorFromPlugin(
				"plugin.id", "someImg.gif"));
				
	}
	
	//@Override
	public void dispose() {	}
	
	public void run()  {
		final IWorkbenchPage pg = window.getActivePage();
		ViewTest view = new ViewTest();
		try {
			ViewTest.show.put(Constants.SLIDECARD, true);
			Display.getDefault().syncExec(new Runnable() {

				public void run() {
				   try {
				      vp =  (ViewTest)pg.showView(
					 ViewTest.ID,
                                         Constants.SLIDECARD,
                                         IWorkbenchPage.VIEW_ACTIVATE);
				   } catch (PartInitException e) {
					 e.printStackTrace();
				   }
				}
				
			});
									
			if (vp == null) {
				vp = (ViewTest)pg.showView(
                                    ViewTest.ID,
                                    Constants.SLIDECARD,
                                    IWorkbenchPage.VIEW_ACTIVATE);
				vp.refresh();
			} else {
				System.out.println("gameView != null");
			}
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e1) {
				//
			}
			
			
			
			// put a message box here for the above view
                        // to show content
                        MessageBox msgBox = new
                            MessageBox(window.getShell(),
				       SWT.ICON_QUESTION | SWT.YES |
                                       SWT.NO |SWT.CANCEL);
			msgBox.setMessage(" close the view?");
			msgBox.setText("...");
			int response = msgBox.open();
			switch(response){
			  case SWT.YES:
				  pg.hideView(vp);
				break;
			  case SWT.NO:
				System.out.println("No...");
				break;
			  case SWT.CANCEL:
				System.out.println("...");
				break;
			}

pg.ViewTest.show.put(Constants.SLIDECARD,
false);
ViewTest.show.put(Constants.CARDDATA, true);
hideView(vp);

ViewTest vt = (ViewTest)pg.showView(ViewTest.ID,
Constants.CARDDATA,
IWorkbenchPage.VIEW_ACTIVATE);
vt.setCardData();

MessageBox msgBox = new
MessageBox(window.getShell(),
SWT.ICON_QUESTION | SWT.YES |
SWT.NO |SWT.CANCEL);
msgBox.setMessage("continue?");
msgBox.setText("...");
int response = msgBox.open();
switch(response){
case SWT.YES:
pg.hideView(vt);
break;
case SWT.NO:
System.out.println("No...");
break;
case SWT.CANCEL:
System.out.println("...");
break;
break;
}

ViewTest.show.put(Constants.CARDDATA, false);
ViewTest.show.put(Constants.SHOWNUMBERKEY,
true);

ViewTest pv = (ViewTest)
pg.showView(ViewTest.ID,


Constants.SHOWNUMBERKEY,

IWorkbenchPage.VIEW_ACTIVATE);

msgBox = new MessageBox(window.getShell(),
SWT.ICON_INFORMATION | SWT.OK);
msgBox.setMessage("Got it?");
msgBox.setText("...");
response = msgBox.open();

try {
PlatformUI
.getWorkbench()
.getProgressService()
.busyCursorWhile( new
IRunnableWithProgress() {
public void run(IProgressMonitor
monitor) throws
InvocationTargetException,
InterruptedException {
monitor.beginTask("Processing
request...", 100);
...
monitor.done();
}
}
);

ViewTest.show.put(Constants.APPROVESTATUS, true);
ViewTest.show.put(Constants.SHOWNUMBERKEY, false);
pg.hideView(pv);
ViewTest statView = (ViewTest)pg.showView(ViewTest.ID,


Constants.APPROVESTATUS,

                        IWorkbenchPage.VIEW_ACTIVATE);
  } catch (Exception e) {	}
  } catch (PartInitException pe) {}
 }// end of run method
} // end of MyAction class



-------My View Class-------
public class ViewTest extends ViewPart {
	public static final String ID = "views.test";
	private Composite pane;
	private Canvas canvas;
	private Image img;
	private Text nameText;
	private Text numText;
	private Text dateText;
	private Text pText;
	private Label statusLabel;
	private Composite parentComp;
	
	static Map<String, Boolean> show =
                       new HashMap<String, Boolean>();
	static {
		show.put(Constants.SLIDECARD, false);
		show.put(Constants.CARDDATA, false);
		show.put(Constants.SHOWNUMBERKEY, false);
		show.put(Constants.APPROVESTATUS, false);
	}

	public ViewTest() {
		
	}

	@Override
	public void createPartControl(final Composite parent) {
		parentComp = parent;
		Display disp = parent.getDisplay();
		Shell shl = parent.getShell();
		
		Boolean slidecard = show.get(Constants.SLIDECARD);
		if (slidecard) {
			drawPage1(parent);
			return;
		}
		
		boolean cardData = show.get(Constants.CARDDATA);
		if (cardData) {
			drawCardData(parent);
			return;
		}
		
		boolean numberKeys = show.get(Constants.SHOWNUMBERKEY);
		if (numberKeys) {
			drawNumberKeys(parent);
			return;
		}
		
		boolean status = show.get(Constants.APPROVESTATUS);
		if (status) {
			displayStatus(parent);
			return;
		}
	}
	
	private void drawPage1(Composite parent) {
	
		RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
	        parent.setLayout(rowLayout);
				
		//  tested either Label or Image, won't show
                /*Label label1 = new Label(parent,SWT.NONE);
		label1.setText("service...");
		label1.setBounds(0,0,100,20);*/
		InputStream is = getClass()
                                  .getResourceAsStream(
                                    "/images/slidingCard.GIF");
			
		img = new Image(parent.getDisplay(), is);
		Label imgLabel = new Label(parent, SWT.CENTER);
		imgLabel.setImage(img);
		imgLabel.setLocation(200, 50);
		
		Label slideLabel = new Label(parent, SWT.CENTER);
		slideLabel.setText("Customer Slides the card");
	}
	
	private void drawCardData(Composite parent) {
		System.out.println("ViewTest.drawCardData()...[1]...");
		Composite cardComp = new Composite(parent, SWT.NONE);
		cardComp.setLayout(new FormLayout());
		
		Group cardData = new Group(cardComp, SWT.NONE);
		cardData.setText("Card Data");
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		cardData.setLayout(gridLayout);
		
		FormData formData = new FormData();
		formData.left = new FormAttachment(20, 0);
		formData.right = new FormAttachment(80, 0);
		formData.top = new FormAttachment(0, 10);
		formData.bottom = new FormAttachment(100, -10);
		cardData.setLayoutData(formData);
		
		// 1. customer name
		Label nameLabel = new Label(cardData, SWT.NONE);
		nameLabel.setText("Customer Name:");
		nameText = new Text(cardData, SWT.BORDER | SWT.LEFT);
		nameText.setBackground(new Color(parent.getDisplay(),
                                              255, 255, 255));
		nameText.setEditable(false);

		GridData data = new GridData();
		//data.grabExcessHorizontalSpace = true;
		data.horizontalSpan = 1;
		data.widthHint = 140;
		nameText.setLayoutData(data);
		
		// 2. card number
		Label numLabel = new Label(cardData, SWT.NONE);
		numLabel.setText("Card Number:");
		numText = new Text(cardData, SWT.BORDER | SWT.LEFT);
		numText.setBackground(new Color(parent.getDisplay(),
                          255, 255, 255));
		numText.setEditable(false);

		GridData numData = new GridData();
		numData.horizontalSpan = 1;
		numData.widthHint = 140;
		numText.setLayoutData(numData);
		
		// 3. expiration date
		Label dateLabel = new Label(cardData, SWT.NONE);
		dateLabel.setText("Expiration Date:");
		dateText = new Text(cardData, SWT.BORDER | SWT.LEFT);
		dateText.setBackground(new Color(parent.getDisplay(),
                      255, 255, 255));
		dateText.setEditable(false);

		GridData dateData = new GridData();
		dateData.horizontalSpan = 1;
		dateData.widthHint = 140;
		dateText.setLayoutData(dateData);
		
		// 4. ...
		Label pLabel = new Label(cardData, SWT.NONE);
		pLabel.setText("...:");
		pText = new Text(cardData, SWT.BORDER | SWT.LEFT);
		pText.setBackground(new Color(parent.getDisplay(),
                   255, 255, 255));
		pText.setEditable(false);

		GridData pData = new GridData();
		pData.horizontalSpan = 1;
		pData.widthHint = 140;
		pText.setLayoutData(pData);
		
	}
	
	private void drawNumberKeys(final Composite parent) {
		Composite numKeysComp = new Composite(parent, SWT.NONE);
		numKeysComp.setLayout(new FormLayout());
		
		canvas = new Canvas(numKeysComp,
                                    SWT.COLOR_WIDGET_BACKGROUND );
		canvas.setSize(500, 400);
		canvas.setLocation(100, 100);
		canvas.setBackground(new Color(parent.getDisplay(), 153,
                                        204, 255));
		
		FormData formData = new FormData();
		formData.left = new FormAttachment(20, 0);
		formData.right = new FormAttachment(80, 0);
		formData.top = new FormAttachment(0, 10);
		formData.bottom = new FormAttachment(75, 0);
		canvas.setLayoutData(formData);
		
		InputStream is = getClass().getResourceAsStream(
                                   "/images/newpp2.gif");
		final Image keysImg = new Image(parent.getDisplay(),
                                               is);
		
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent event) {
				event.gc.drawText("Customer enters
                                                  number...", 20, 20);
				event.gc.drawImage(keysImg, 20, 50);
			}
		});
		
		// 4. p
		Group pGrp = new Group(numKeysComp, SWT.NONE);
		pGrp.setText("p Data");
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		pGrp.setLayout(gridLayout);
		
		FormData pGrpFrmDat = new FormData();
		pGrpFrmDat.left = new FormAttachment(20, 0);
		pGrpFrmDat.right = new FormAttachment(80, 0);
		pGrpFrmDat.top = new FormAttachment(canvas, 10);
		pGrpFrmDat.bottom = new FormAttachment(100, -10);
		pGrp.setLayoutData(pGrpFrmDat);
		
		Label pLabel = new Label(pGrp, SWT.NONE);
		pLabel.setText("p:");
		pText = new Text(pGrp, SWT.BORDER | SWT.LEFT);
		pText.setBackground(new Color(parent.getDisplay(),
                       255, 255, 255));
		pText.setEditable(false);
		
		GridData pData = new GridData();
		pData.horizontalSpan = 1;
		pData.widthHint = 140;
		pText.setLayoutData(pData);
	}
	
	private void displayStatus(Composite parent) {
		Composite statusComp = new Composite(parent, SWT.NONE);		
		statusComp.setLayout(new FormLayout());
		
		Group statGrp = new Group(statusComp, SWT.NONE);
		statGrp.setText("Approve Status");
		
		statGrp.setLayout(new FillLayout(SWT.CENTER));
		
		FormData statDat = new FormData();
		statDat.left = new FormAttachment(20, 0);
		statDat.right = new FormAttachment(80, 0);
		statDat.top = new FormAttachment(30, 0);
		statDat.bottom = new FormAttachment(60, 0);
		statGrp.setLayoutData(statDat);
		
		statusLabel = new Label(statGrp, SWT.NONE);
		statusLabel.setForeground(new Color(parent.getDisplay(),
                                                  0, 102, 0));
		statusLabel.setFont(new Font(parent.getDisplay(),
                     "Verdana", 14,  SWT.BOLD |	SWT.ITALIC));
		statusLabel.setText("");
	}
}  // end of View class



Francis Upton wrote:
Can you make this a small test case and post your code so we can see what's going on?

Ben Smith wrote:
Hi Alles,

I am new and wondering whether I could get help here. I have an Action. I want that by Clicking it would cause several instances of a View to show. However, using IWorkbenchPage.showView(...) did not show the view content (e.g., a label or an image). The program seemed to skip it and continue to run the code after the showView() statement (e.g., System.out.println(...) or Thread.sleep(...)). If I inserted a MessageBox after the showView() statement. The IWorkbenchPage.showView() then displayed the view. Why do I need a MessageBox to have the view shown? Can I show the view without the MessageBox? Any help please?

Ben