Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

How to disable checkbox in a GUI in Java SWT?

Hi,
I am working on creating a GUI using JAVA SWT package. In this GUI I have three checkboxes to filter the results on a table.

However, it takes some time to populate the whole data on the table and I do not want the users to use the checkboxes before the the data generation is completed.

Therefore, I want to disable these checkboxes while the process is going on. I know how to dosable icons but I don't how to disable checkboxes.

Can you please help  me with it?


This is how I call this class in the main method in GUI.java class

public static void main(String[] args) throws SQLException, ClassNotFoundException {

SOME CODE IN HERE
 
csFilteringCheckBoxes = new CSFilteringCheckBoxes(compRESULTS, SWT.FLAT);

SOME CODE IN HERE

}


This is the  class that creates the checkboxes and does the filtering:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;

public class CSFilteringCheckBoxes extends Composite {
    
    private static Shell shell;
    private static Table table;
    private static String[] titles;
    private static Group groupResults;
    private static Display display;
    
    private Button error = new Button(this, SWT.CHECK);
    private Button failed = new Button(this, SWT.CHECK);
    private Button passed = new Button(this, SWT.CHECK);
    private Label caption = new Label(this, SWT.HORIZONTAL | SWT.CENTER);
    
    private static List<CheckDetail> checkDetails = null;
    private static List<CheckDetail> checkDetailsTable = null;

    boolean fileCreated = false;
    boolean unChecked = false;
    
    
    public CSFilteringCheckBoxes(Composite parent, int style) {
        super(parent, style);
        Font font = new Font(parent.getDisplay(),"Arial",9, SWT.NORMAL);
        final Color lightgray;
        lightgray = new Color (parent.getDisplay(), 140, 140, 140);
        
        GridLayout gl = new GridLayout(); gl.verticalSpacing = 4;
        setLayout(gl);
        error.setText("Error");
        failed.setText("Failed");
        passed.setText("Passed");
        caption.setText("Filtering");
        caption.setFont(font);
      
        setAllChecked();
        
        
        caption.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // center the caption
        setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        error.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        failed.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        passed.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        caption.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        
        
        failed.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                if (failed.getSelection()) {
                    unChecked = false;
                    //show failed checks
                  //DEBUG:: System.out.println("Failed checked");
                    try {
                        showFilteredResults("Failed");
                        List<FilterCheckStatus> filterCheckStatus_new = getFilterChecks();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    try {
                        unChecked = true;
                        showFilteredResults("Failed");
                        List<FilterCheckStatus> filterCheckStatus_new = getFilterChecks();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                  
                }
            }
        });
   
    
        
        passed.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                if (getFilterChecks().get(0).getPassedStatus()) {
                    unChecked = false;
                    //show failed checks
                  //DEBUG:: System.out.println("Passed checked");
                    try {
                        showFilteredResults("Passed");
                        List<FilterCheckStatus> filterCheckStatus_new = getFilterChecks();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    try {
                        unChecked = true;
                        showFilteredResults("Passed");
                        List<FilterCheckStatus> filterCheckStatus_new = getFilterChecks();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                }
            }
        });
        
               
        error.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
               
                if (error.getSelection()) {
                    unChecked = false;
                    //show failed checks
                  //DEBUG:: System.out.println("Error checked");
                    try {
                        showFilteredResults("Error");
                        List<FilterCheckStatus> filterCheckStatus_new = getFilterChecks();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    try {
                        unChecked = true;
                        showFilteredResults("Error");
                        List<FilterCheckStatus> filterCheckStatus_new = getFilterChecks();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                }
            }
            });              
     } 
    
    public void showFilteredResults(String status) throws IOException, ClassNotFoundException{

        List<CheckDetail> checkDetails = null;
        List<CheckDetail> checkDetailsTable = null;
        String content = "";
        
        String tempDir = null;
        String OS = System.getProperty("os.name").toLowerCase();
        
        if (OS.indexOf("win") >= 0){
            tempDir = "C:/Temp/mtcheck";
        } else{
            tempDir = "/tmp/mtcheck";
        }
        
        
        File f = new File(tempDir + "/" + "mtcheckDetails.txt");
        if (!f.exists()){
            checkDetails = CheckRunner.getList();
            
            FileOutputStream fout= new FileOutputStream (tempDir + "/" + "mtcheckDetails.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fout);
            oos.writeObject(checkDetails);
            fout.close();
            
            FileOutputStream foutTable= new FileOutputStream (tempDir + "/" + "mtcheckDetailsTable.txt");
            ObjectOutputStream oosTable = new ObjectOutputStream(foutTable);
            oosTable.writeObject(checkDetails);
            foutTable.close();
        }
        
        FileInputStream fin= new FileInputStream (tempDir + "/" + "mtcheckDetails.txt");
        ObjectInputStream ois = new ObjectInputStream(fin);
        checkDetails= (List<CheckDetail>)ois.readObject();
        fin.close();

        
        FileInputStream finTable= new FileInputStream (tempDir + "/" + "mtcheckDetailsTable.txt");
        ObjectInputStream oisTable = new ObjectInputStream(finTable);
        checkDetailsTable= (List<CheckDetail>)oisTable.readObject();
        finTable.close();
        
        boolean edited = false;
        boolean newRun = false;
        
        if (!unChecked){  //means checked
            for(Iterator<CheckDetail> i = checkDetails.iterator();i.hasNext();) {
            CheckDetail checkDetail = i.next();
          //DEBUG:: System.out.println("Detail Status:" + checkDetail.getStatus());
            
                if (!status.equalsIgnoreCase("ERROR")){
                      if (!checkDetail.getStatus().equalsIgnoreCase(status)){
                          i.remove();
                          edited = true;
                          newRun = false;
                      } else{
                          edited = true;
                          newRun = false;
                      }
                } else {
                    if (!checkDetail.getStatus().contains("n/a")){  //This is a special case for errors. It will change after I update the CLI error message format.  
                        i.remove();
                        edited= true;
                        newRun = false;
                    } else{
                        edited= true;
                        newRun = false;
                    }
                }
            }                      
            checkDetailsTable.addAll(checkDetails);
        }
            
        if(unChecked){
            if (checkDetailsTable != null){
                for(Iterator<CheckDetail> i = checkDetailsTable.iterator();i.hasNext();) {
                    CheckDetail checkDetailTable = i.next();
                  //DEBUG:: System.out.println("Detail Status:" + checkDetailTable.getStatus());                  
                    
                    if (!status.equalsIgnoreCase("ERROR")){
                        if (checkDetailTable.getStatus().equalsIgnoreCase(status)){
                            //checkDetails.remove(i);
                            i.remove();
                            edited= true;
                            newRun = true;
                        } else{
                            edited= true;
                            newRun = true;
                        }
                    } else {
                        if (checkDetailTable.getStatus().contains("n/a")){
                            i.remove();
                            edited= true;
                            newRun = true;
                        } else{
                            edited= true;
                            newRun = true;
                        }
                    }
                }
            }
            checkDetails = checkDetailsTable;
        }
                  
        FileOutputStream foutTable= new FileOutputStream (tempDir + "/" + "mtcheckDetailsTable.txt");
        ObjectOutputStream oosTable = new ObjectOutputStream(foutTable);
        oosTable.writeObject(checkDetailsTable);
        foutTable.close();
        
        //get the list of failed  checks
        if (edited){
            boolean evenRow = true;
            Gui.populateResultsTable(checkDetails, shell, table, titles, newRun, evenRow, groupResults);  
        }
    }
    
    
    public static void setTable(Table table, String[] titles) {
        CSFilteringCheckBoxes.table = table;
        CSFilteringCheckBoxes.titles = titles;
    }  
    
    
    public static void setShell(Shell shell, Display display) {
        CSFilteringCheckBoxes.shell = shell; 
        CSFilteringCheckBoxes.display = display;
    }
    
    public static void setGroupResults(Group groupResults) {
    	CSFilteringCheckBoxes.groupResults = groupResults; 
    }
    
    public void setAllChecked(){
        error.setSelection(true);
        //DEBUG:: System.out.println("Error Status after: " + error.getSelection());
        failed.setSelection(true);
        passed.setSelection(true);       
    }
       
    public List<FilterCheckStatus> getFilterChecks(){
        FilterCheckStatus filterCheckStatus = new FilterCheckStatus();
        List<FilterCheckStatus> filterCheckStatuses = new ArrayList<FilterCheckStatus>();
        
        boolean errorState = error.getSelection();
        filterCheckStatus.setErrorStatus(errorState);
        boolean failedState = failed.getSelection();
        filterCheckStatus.setFailedStatus(failedState);
        boolean passedState = passed.getSelection();
        filterCheckStatus.setPassedStatus(passedState);
        
        filterCheckStatuses.add(filterCheckStatus);
        return filterCheckStatuses;
    }
    
    public static void setTableDataInitialData() throws IOException{
        checkDetails = CheckRunner.getList();
        
        String tempDir = null;
        String OS = System.getProperty("os.name").toLowerCase();
        
        if (OS.indexOf("win") >= 0){
            tempDir = "C:/Temp/mtcheck";
        } else{
            tempDir = "/tmp/mtcheck";
        }
        
        FileOutputStream foutTable= new FileOutputStream (tempDir + "/" + "mtcheckDetailsTable.txt");
        ObjectOutputStream oosTable = new ObjectOutputStream(foutTable);
        oosTable.writeObject(checkDetails);
        foutTable.close();        
    }
    
    public static List<CheckDetail> getTableData() throws IOException, ClassNotFoundException{
              
        String tempDir = null;
        String OS = System.getProperty("os.name").toLowerCase();
        
        if (OS.indexOf("win") >= 0){
            tempDir = "C:/Temp/mtcheck";
        } else{
            tempDir = "/tmp/mtcheck";
        }
        
        FileInputStream finTable= new FileInputStream (tempDir + "/" + "mtcheckDetailsTable.txt");
        ObjectInputStream oisTable = new ObjectInputStream(finTable);
        checkDetailsTable= (List<CheckDetail>)oisTable.readObject();
        finTable.close();
        
        return checkDetailsTable;
    }
            
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial