Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » How to get an Object from a ComboViewer?
How to get an Object from a ComboViewer? [message #953456] Mon, 22 October 2012 05:04 Go to next message
Matthias Bollen is currently offline Matthias Bollen
Messages: 1
Registered: October 2012
Location: Gie├čen, Germany
Junior Member
Hello everyone,

currently I try to make a program with JTAPI and XTAPI implementation.
Now I have a problem with the ComboViewer. As you can see on the picture I can see and select the Tapi-line in the comboViewer.

But how can I get or select the object from the comboViewer?


protected void createContents() {

   ...  // the other Shell Components like Buttons and Labels

        combo = new Combo(shell, SWT.NONE);
        combo.setBounds(2, 10, 181, 23);

        comboViewer = new ComboViewer(combo);
        comboViewer.setContentProvider(new ArrayContentProvider());
        comboViewer.setLabelProvider(new LabelProvider() {
                public String getText(Object element) {
	                  return ((Terminal) element).getName();
                }
        });

        comboViewer.addSelectionChangedListener(new ISelectionChangedListener() {
	        public void selectionChanged(SelectionChangedEvent event) {
		          IStructuredSelection selection = (IStructuredSelection) event.getSelection();
		          Terminal selectedObject = (Terminal) selection.getFirstElement();
	        }
        });

        // Call the Method initJTapi
        initJTapi();
}





private void initJTapi() {
	try {
		JtapiPeer peer = JtapiPeerFactory.getJtapiPeer("net.xtapi.XJtapiPeer");
			
	        // Provider
		myprovider = peer.getProvider("net.xtapi.serviceProvider.MSTAPI");

		Terminal[] t = myprovider.getTerminals();
                
                // put all (terminal) objects you can find in a list
		for (int loop = 0; loop < t.length; loop++) {
			liste.add(loop, t[loop]);
		}
		comboViewer.setInput(liste);
	} catch (Exception excp) {
		MessageDialog.openInformation(shell, "Warnung",	"Can not get a provider.");
	}

	ISelection selection = comboViewer.getSelection();
	
	if (!selection.isEmpty()) {
		IStructuredSelection structuredSelection = (IStructuredSelection) selection;      
                // cast the object to an terminal object
		m_terminal = (Terminal) structuredSelection.getFirstElement();
	}
}
  • Attachment: program.png
    (Size: 7.88KB, Downloaded 71 times)
Re: How to get an Object from a ComboViewer? [message #954715 is a reply to message #953456] Tue, 23 October 2012 03:37 Go to previous message
Cedric Moonen is currently offline Cedric Moonen
Messages: 271
Registered: August 2009
Senior Member
Quote:
But how can I get or select the object from the comboViewer?


Not sure I fully understand your question since you already have a partial answer in your code.

To get the selected object from the comboViewer:

ISelection selection = comboViewer.getSelection();
if (!selection.isEmpty()) {
   IStructuredSelection structuredSelection = (IStructuredSelection) selection;      
   // cast the object to an terminal object
   m_terminal = (Terminal) structuredSelection.getFirstElement();
}


(that's taken from your piece of code)

To set the selection of the comboViewer:

ISelection selection = new StructuredSelection(m_terminal);
comboViewer.setSelection(selection);



One additional remark: if you set an ArrayContentProvider as content provider of your ComboViewer, you can pass an array of elements as input (it doesn't need to be a List). So, this code:
Terminal[] t = myprovider.getTerminals();
                
// put all (terminal) objects you can find in a list
for (int loop = 0; loop < t.length; loop++) {
   liste.add(loop, t[loop]);
}
comboViewer.setInput(liste);


can be simplified to:
comboViewer.setInput(myprovider.getTerminals());
Previous Topic:[Databinding] Caret position with undo/redo
Next Topic:Context Help for TitleAreaDialog
Goto Forum:
  


Current Time: Mon Sep 01 09:59:40 EDT 2014

Powered by FUDForum. Page generated in 0.02021 seconds