@Override
    public void acceptButtonActionPerformed() {
        String selectedNames[] = ds.getSelectedData();
        if(selectedNames.length == 3 || selectedNames.length == 4){
            DataFrame df = mainApplication.getActiveDataFrame();
            boolean[] selection = getValidComposition(df, selectedNames);
            int [] mapping = df.getMapingToData(selectedNames, selection);
            double[][] data = df.getNumericalData(selectedNames, mapping);
            double [][]cdata = CoDaStats.centerData(data);
            cdata = CoDaStats.transformRawCLR(cdata);

            Matrix Z = (new Matrix(cdata)).transpose();

            SingularValueDecomposition svd =
                new SingularValueDecomposition(Z);

            double[][] pcomp = new Matrix(CoDaStats.transformCLRRaw(svd.getV().getArray())).transpose().getArray();
            int dim = selectedNames.length;

            String groupedBy = ds.getSelectedGroup();

            CoDaPackMain.outputPanel.addOutput(
                        new OutputPlotHeader("Principal components", selectedNames));

            double cpExp[] = coda.BasicStats.cumulativeProportionExplained(svd.getSingularValues());

            int rank = svd.rank();
            String pcnames[] = new String[rank];
            String pcheaders[] = new String[dim+1];
            double ternarypcomp[][] = new double[rank][dim+1];

            pcheaders[dim] = "Cum.Prop.Exp.";
            for(int j=0;j<dim;j++){
                pcheaders[j] = selectedNames[j];
            }
            for(int i=0;i<rank;i++){
                pcnames[i] = "PC" + (i+1);
                ternarypcomp[i][dim] = cpExp[i];
                for(int j=0;j<dim;j++)
                    ternarypcomp[i][j] = pcomp[i][j];
            }

            CoDaPackMain.outputPanel.addOutput(
                    new OutputTableTwoEntries("Principal Components", pcheaders, pcnames, ternarypcomp));

            CoDaPlotWindow window = null;
            if(selectedNames.length == 3){

                PrincipalComponent2dBuilder builder =
                        new PrincipalComponent2dBuilder(
                            selectedNames, data, pcomp[0], pcomp[1]).mapping(mapping);
                
                if(groupedBy != null){
                    builder.groups(coda.Utils.reduceData(
                            df.getDefinedGroup(groupedBy),
                            selection), coda.Utils.getCategories(
                            df.getCategoricalData(groupedBy),selection));
                }

                window = new TernaryPlot2dWindow(
                        df, builder.build(), "Principal Component Plot");

                window.setVisible(true);
                setVisible(false);                
                                
            }
            if(dim == 4){
                PrincipalComponent3dBuilder builder =
                        new PrincipalComponent3dBuilder(
                            selectedNames, data, pcomp[0], pcomp[1], pcomp[2]).mapping(mapping);

               
                if(groupedBy != null){
                    builder.groups(coda.Utils.reduceData(
                            df.getDefinedGroup(groupedBy),
                            selection), coda.Utils.getCategories(
                            df.getCategoricalData(groupedBy),selection));
                }

                window = new TernaryPlot3dWindow(df, builder.build(),
                        "Principal Component Plot 3d");
                window.setVisible(true);
                setVisible(false);
            }
        }else{
            JOptionPane.showMessageDialog(this, "<html>Select <b>three</b> or <b>four</b> variables</html>");
        }
    }