blob: 1f38dc442c7c906f9154059cbdc351b272f9ffae [file] [log] [blame]
/**
* Copyright (c) 2019 CEA LIST.
*
* All rights reserved. This program and the accompanying materials
* are the property of the CEA.
* Any use is subject to specific agreement with the CEA.
* Contributors:
*
* Gabriel Pedroza (CEA LIST) - API extensions and modifications
*
*
*/
package org.eclipse.papyrus.pdp4eng.designer.datastrategies.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;
@SuppressWarnings("serial")
public class KAnonymityOutcomesDialog extends JFrame
{
public KAnonymityOutcomesDialog(
EList<Element> tableList,
ArrayList<Property> QuasiIdentifiers, int kAnonymityTarget, int kAnonymityValue, int[] lowerUpperBounds){
if (tableList!=null && QuasiIdentifiers!= null &&
tableList.size()>0 && QuasiIdentifiers.size()>0 ) {
// Avoid repeated elements in the list of Quasi Identifiers
EList<String> uniqueQIList = new BasicEList<String>();
for (Iterator<Property> QuasiIdentifiersIt = QuasiIdentifiers.iterator(); QuasiIdentifiersIt.hasNext(); ) {
Property property = QuasiIdentifiersIt.next();
if (property!=null && !uniqueQIList.contains(property.getName())) {
uniqueQIList.add(property.getName());
}
}
if (uniqueQIList.size()>0) {
int noColumns = uniqueQIList.size()+1;
int noRows = tableList.size();
System.out.println("No. Columns: "+noColumns);
System.out.println("No. Rows: "+noRows);
//String[] outcomesList = new String[noRows];
String[] headerRow = new String[noColumns];
Object[][] kAnonymizedTable = new Object[noRows][noColumns];
for (int i=0; i<noRows; i++) {
Element currentElmt = tableList.get(i);
EList<Stereotype> selectedStereotypeList = currentElmt.getAppliedStereotypes();
if (selectedStereotypeList!=null && selectedStereotypeList.size()>0) {
Stereotype selectedStereotype = selectedStereotypeList.get(0);
if (currentElmt!=null && currentElmt instanceof org.eclipse.uml2.uml.Classifier) {
Classifier currentElmtClass = (Classifier) currentElmt;
kAnonymizedTable[i][0]= currentElmtClass.getName();
for (int j=0; j<noColumns-1; j++) {
String QIattribute = uniqueQIList.get(j);
if (QIattribute!=null) {
kAnonymizedTable[i][j+1] = currentElmtClass.getValue(selectedStereotype, QIattribute).toString();
}
}
}
}
}
headerRow[0]= "ElementName";
for (int j=0; j<noColumns-1; j++) {
headerRow[j+1] = (String) uniqueQIList.get(j);
}
// add the table to the frame
JTable table = new JTable(kAnonymizedTable, headerRow){
private static final long serialVersionUID = 1L;
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (isRowSelected(row) && isColumnSelected(column)) {
((JComponent) c).setBorder(new LineBorder(Color.GREEN));
}
return c;
}
};
if ((kAnonymityTarget>kAnonymityValue)) {
table.setDefaultRenderer(Object.class, new CellRenderer(lowerUpperBounds[0],lowerUpperBounds[1]));
}
this.add(new JScrollPane(table));
this.pack();
// auxiliary elements of the frame
this.setTitle("K-Anonymity Outcomes");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JTextArea kanonymityText;
if (kAnonymityTarget>kAnonymityValue) {
kanonymityText = new JTextArea("K-ANONYMITY OUTCOMES\n\n"+
"K-Anonymity target: "+kAnonymityTarget+
"\nK-Anonymity achieved: "+kAnonymityValue+
"\n\nProperty satisfied? "+ (kAnonymityTarget<=kAnonymityValue)+
"\n\n A concerned data set is highlighted!");
kanonymityText.setForeground(Color.MAGENTA);
} else {
kanonymityText = new JTextArea("K-ANONYMITY OUTCOMES\n\n"+
"K-Anonymity target: "+kAnonymityTarget+
"\nK-Anonymity achieved: "+kAnonymityValue+
"\n\nProperty satisfied? "+ (kAnonymityTarget<=kAnonymityValue));
kanonymityText.setForeground(Color.BLUE);
}
kanonymityText.setEditable(false);
this.add(kanonymityText, BorderLayout.BEFORE_LINE_BEGINS);
this.pack();
JButton myButton = new JButton(" CLOSE ");
myButton.addActionListener(eAL -> {
this.dispose();
pack();
});
this.add(myButton, BorderLayout.SOUTH);
this.pack();
this.setVisible(true);
}
}
}
class CellRenderer extends DefaultTableCellRenderer {
public int lower;
public int upper;
CellRenderer(int lowerBound,int upperBound){
lower=lowerBound;
upper=upperBound;
}
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
if (row >=lower && row <upper) {
setBackground(Color.LIGHT_GRAY);
setForeground(Color.MAGENTA);
} else {
setBackground(Color.WHITE);
setForeground(Color.BLACK);
}
return super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
}
}
public static void main(String[] args) {
Object[] options1 = { "Try This Number", "Choose A Random Number", "Quit" };
JPanel panel = new JPanel();
panel.add(new JLabel("Enter number between 0 and 1000"));
JTextField textField = new JTextField(10);
panel.add(textField);
int result = JOptionPane.showOptionDialog(null, panel, "Enter a Number",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null,
options1, null);
if (result == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, textField.getText());
}
}
}