lundi 19 juin 2017

how to save selected check box in RCP application?

Hi I am beginner of RCP framework.I have used one Editor in eclipse RCP3 application name like "Food editor".In this one of Editor I have made one SWT table with SWT.Check Table item. I have facing problem regarding check box selection.

Current scenario of this editor is, when Food editor open then I select check box then I open new Editor name like Employee_Editor.Before Employee_Editor open,I closed Food editor with selected check box.Then again I open Food editor then previous one "Selected" check box display as "Unselected" why this happen?

See this image for firstly open editor part.

I got problem regarding check box selected check box dispaly as unselected.

I tryied to get soltion of this problem I serach on internet I found soltion using database "selected check box" save as datatype boolean. But I do not want to do this because when many data comes many time SQL query fire.

So ,I tried via defining one method "saveSelectedCheckBox" in createPartControl, which is save selected checkbox table item into "selectedTableItemList". but in also I got problem when I close food editor then again I open it again I got problem with "selectedTableItemList" with null value. I have share this code given below.

public class FoodDetailsEditor extends EditorPart {

public static final String ID = "rcp_demo.Editor.FoodDetailsEditor"; //$NON-NLS-1$Food_Details
public static final String BID = "Food_Details";
 private static final String STORE_SELECTION = "rcp_demo.Editor.FoodDetailsEditor";
private Table table;
 ArrayList<Integer>  selectedTableItemList= null;

@Override
public void createPartControl(Composite parent) {

    ScrolledComposite scrolledComposite_2 = new ScrolledComposite(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    scrolledComposite_2.setExpandHorizontal(true);
    scrolledComposite_2.setExpandVertical(true);

    Composite composite = new Composite(scrolledComposite_2, SWT.NONE);
    composite.setLayout(new GridLayout(1, false));

    table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION|SWT.CHECK);
    GridData gd_table = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
    gd_table.heightHint = 156;
    gd_table.widthHint = 565;
    table.setLayoutData(gd_table);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    TableColumn tblclmnCheckbox = new TableColumn(table, SWT.NONE);
    tblclmnCheckbox.setWidth(100);
    tblclmnCheckbox.setText("Checkbox");

    TableColumn tblclmnTiming = new TableColumn(table, SWT.NONE);
    tblclmnTiming.setWidth(100);
    tblclmnTiming.setText("Timing");

    TableColumn tblclmnMonday = new TableColumn(table, SWT.NONE);
    tblclmnMonday.setWidth(100);
    tblclmnMonday.setText("Monday");

    TableColumn tblclmnTuesday = new TableColumn(table, SWT.NONE);
    tblclmnTuesday.setWidth(100);
    tblclmnTuesday.setText("Tuesday");

    TableColumn tblclmnWednesday = new TableColumn(table, SWT.NONE);
    tblclmnWednesday.setWidth(100);
    tblclmnWednesday.setText("Wednesday");

    TableColumn tblclmnThursday = new TableColumn(table, SWT.NONE);
    tblclmnThursday.setWidth(100);
    tblclmnThursday.setText("Thursday");

    TableColumn tblclmnFriday = new TableColumn(table, SWT.NONE);
    tblclmnFriday.setWidth(100);
    tblclmnFriday.setText("Friday");

    TableColumn tblclmnSaturday = new TableColumn(table, SWT.NONE);
    tblclmnSaturday.setWidth(100);
    tblclmnSaturday.setText("Saturday");
    scrolledComposite_2.setContent(composite);
    scrolledComposite_2.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    TableItem item1 = new TableItem(table, SWT.NONE);
    item1.setText(new String[]{"","10:00 to 10:30","Food1","Food2","Food3","Food4","Food5"});
    TableItem item2 = new TableItem(table, SWT.NONE);
    item2.setText(new String[]{"","11:00 to 11:30","Food1","Food2","Food3","Food4","Food5"});
    TableItem item3 = new TableItem(table, SWT.NONE);
    item3.setText(new String[]{"","12:00 to 12:30","Food1","Food2","Food3","Food4","Food5"});
    TableItem item4 = new TableItem(table, SWT.NONE);
    item4.setText(new String[]{"","13:00 to 13:30","Food1","Food2","Food3","Food4","Food5"});


    table.addSelectionListener(new SelectionAdapter()
    {               
        @Override
        public void widgetSelected(SelectionEvent event)
        {
            if( event.detail == SWT.CHECK ) 
                {
                  if( table.indexOf( ( TableItem )event.item ) == table.getSelectionIndex() ) 
                   {

                    TableItem ti = ( TableItem )event.item;
                    ti.setChecked( !ti.getChecked() );                      
                    System.out.println("event.index:-"+event.item);                     

                    }
                } 
              saveSelectedCheckBox(table);
        }

        private void saveSelectedCheckBox(Table table) {
            // TODO Auto-generated method stub
             TableItem[] items =  table.getItems();
          selectedTableItemList=new ArrayList<Integer>();//Creating arraylist 

                for (int ro = 0; ro < table.getItemCount();ro++)
                {      if(items[ro].getChecked()== true)
                        {
                        System.out.println("items[ro]:=="+items[ro]);
                        selectedTableItemList.add(ro);
                        }

                    } 
                for(int row = 0; row < selectedTableItemList.size();row++)
                {
                     System.out.println("selectedTableItemList:--"+selectedTableItemList);
                }
        }
    });
}

@Override
public void setFocus() {
    // Set the focus
}

@Override
public void doSave(IProgressMonitor monitor) {
    // Do the Save operation    
}

@Override
public void doSaveAs() {
    // Do the Save As operation     
}

@Override
public void init(IEditorSite site, IEditorInput input)
        throws PartInitException {
    System.out.println("init called");
     System.out.println("selectedTableItemList:--"+selectedTableItemList);
    // Initialize the editor part
     if (!(input instanceof FoodDetailsEditorInput)) {
          throw new PartInitException("Invalid Input: Must be " + FoodDetailsEditorInput.class.getName());
      }
      setSite(site);
      setInput(input);
}

@Override
public boolean isDirty() 
{   
    return false;
}

@Override
public boolean isSaveAsAllowed() {
    System.out.println("isSaveAsAllowed called");
    return true;
}
}

I want when Food editor open then I select check box then I open new Editor name like Employee_Editor.Then again I open Food editor "Selected" check box as display as a "Selected". How to do this possible? please help me.

My system configuration:

Windows 64Bit OS.
Eclipse Kepler 32 bit.
jdk 1.8 




Aucun commentaire:

Enregistrer un commentaire