public code v1
This commit is contained in:
+153
@@ -0,0 +1,153 @@
|
||||
package flintstones.entity.sensitiveanalysismodel.ui;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.eclipse.swt.widgets.Composite;
|
||||
|
||||
import flintstones.entity.extensionenum.ExtensionEnum;
|
||||
import flintstones.entity.problemelement.entities.Alternative;
|
||||
import flintstones.entity.problemelement.entities.Criterion;
|
||||
import flintstones.entity.problemelement.entities.ProblemElement;
|
||||
import flintstones.entity.problemelement.entities.ProblemElementHelper;
|
||||
import flintstones.entity.sensitiveanalysismodel.SensitiveAnalysisModel;
|
||||
import flintstones.entity.sensitiveanalysismodel.SensitiveAnalysisModel.FieldsChanges;
|
||||
import flintstones.entity.sensitiveanalysismodel.ui.chart.RankingEvolutionChart;
|
||||
import flintstones.helper.html.table.HtmlTextTable;
|
||||
import flintstones.model.problemelement.service.IProblemElementService;
|
||||
|
||||
public abstract class SensitiveAnalysisModelUi {
|
||||
|
||||
public static final String EXTENSION_POINT = "flintstones.entity.sensitiveanalysismodel.ui.extension";
|
||||
|
||||
protected HtmlTextTable dmTable;
|
||||
protected HtmlTextTable rankingTable;
|
||||
protected HtmlTextTable changesTable;
|
||||
|
||||
protected RankingEvolutionChart chart;
|
||||
|
||||
protected SensitiveAnalysisModel model;
|
||||
|
||||
@Inject
|
||||
IProblemElementService problemService;
|
||||
|
||||
|
||||
/**
|
||||
* The Class Fields.
|
||||
*/
|
||||
public enum Fields implements ExtensionEnum {
|
||||
|
||||
/** The id. */
|
||||
uid,
|
||||
/** The model. */
|
||||
sensitivemodel,
|
||||
/** The implementation. */
|
||||
implementation
|
||||
}
|
||||
|
||||
public SensitiveAnalysisModel getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(SensitiveAnalysisModel model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public HtmlTextTable createDecisionMatrixTable(Composite decisionMatrixSection) {
|
||||
String[] rowHeader = getShortenedAlternativesNames();
|
||||
String[] colHeader = getShortenedCriteriaNames();
|
||||
String[][] valuesSTR = new String [rowHeader.length][colHeader.length];
|
||||
|
||||
Double[][] values = model.getDecisionMatrix();
|
||||
for(int i = 0; i < valuesSTR.length; i++) {
|
||||
for(int j = 0; j < valuesSTR[i].length; j++)
|
||||
valuesSTR[i][j] = "" + values[i][j];
|
||||
}
|
||||
|
||||
dmTable = new HtmlTextTable(decisionMatrixSection, valuesSTR, colHeader, rowHeader);
|
||||
return dmTable;
|
||||
}
|
||||
|
||||
public abstract HtmlTextTable createRankingTable(Composite rankingSection);
|
||||
|
||||
public abstract HtmlTextTable createChangesTable(Composite changesSection, FieldsChanges typeChange);
|
||||
|
||||
public void refreshDecisionMatrixTable() {
|
||||
Double[][] newValues = model.getDecisionMatrix();
|
||||
String[][] valuesSTR = new String[dmTable.getNumberOfRows()][dmTable.getNumberOfColumns()];
|
||||
for(int i = 0; i < valuesSTR.length; i++) {
|
||||
for(int j = 0; j < valuesSTR[i].length; j++) {
|
||||
valuesSTR[i][j] = "" + newValues[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
dmTable.refresh(valuesSTR);
|
||||
dmTable.render();
|
||||
}
|
||||
|
||||
public abstract void refreshRankingTable();
|
||||
|
||||
public abstract void refreshChangesTable(FieldsChanges typeChange);
|
||||
|
||||
public String[] getShortenedCriteriaNames() {
|
||||
Criterion[] allCriteria = ProblemElementHelper.asCriterions(problemService.getMainElements(Criterion.Type));
|
||||
String[] str = new String[allCriteria.length];
|
||||
|
||||
for(int i = 0; i < allCriteria.length; i++)
|
||||
str[i] = "C" + (i + 1);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public String[] getShortenedAlternativesNames() {
|
||||
Alternative[] allAlternatives = ProblemElementHelper.asAlternatives(problemService.getAll(Alternative.Type));
|
||||
String[] str = new String[allAlternatives.length];
|
||||
|
||||
for(int i = 0; i < allAlternatives.length; i++)
|
||||
str[i] = "A" + (i + 1);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public String[] getShortenedPairAlternativesNames() {
|
||||
Alternative[] allAlternatives = ProblemElementHelper.asAlternatives(problemService.getAll(Alternative.Type));
|
||||
|
||||
List<String> str = new LinkedList<>();
|
||||
for(int i = 0; i < allAlternatives.length - 1; i++) {
|
||||
for(int j = i + 1; j < allAlternatives.length; ++j) {
|
||||
str.add("A" + (i + 1) + "-" + "A" + (j + 1));
|
||||
}
|
||||
}
|
||||
|
||||
return str.toArray(new String[0]);
|
||||
}
|
||||
|
||||
public String[] getShortenedAllPairAlternativesNames() {
|
||||
Alternative[] allAlternatives = ProblemElementHelper.asAlternatives(problemService.getAll(Alternative.Type));
|
||||
|
||||
List<String> str = new LinkedList<>();
|
||||
for(int i = 0; i < allAlternatives.length - 1; i++) {
|
||||
for(int j = 0; j < allAlternatives.length; ++j) {
|
||||
if(i != j)
|
||||
str.add("A" + (i + 1) + "-" + "A" + (j + 1));
|
||||
}
|
||||
}
|
||||
|
||||
return str.toArray(new String[0]);
|
||||
}
|
||||
|
||||
public abstract RankingEvolutionChart createRankingEvolutionChart(Composite container, ProblemElement[] criteria, Criterion criterion, int width, int height, int style);
|
||||
|
||||
public String[] getCriteriaIds() {
|
||||
String[] ids = new String[problemService.getMainElements(Criterion.Type).length];
|
||||
|
||||
int cont = 0;
|
||||
for(ProblemElement pe: problemService.getMainElements(Criterion.Type)) {
|
||||
ids[cont] = pe.getName();
|
||||
cont++;
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
}
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
package flintstones.entity.sensitiveanalysismodel.ui.chart;
|
||||
|
||||
import java.awt.BasicStroke;
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.Stroke;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.swt.widgets.Composite;
|
||||
import org.jfree.chart.ChartFactory;
|
||||
import org.jfree.chart.JFreeChart;
|
||||
import org.jfree.chart.axis.NumberAxis;
|
||||
import org.jfree.chart.axis.NumberTickUnit;
|
||||
import org.jfree.chart.labels.XYToolTipGenerator;
|
||||
import org.jfree.chart.plot.PlotOrientation;
|
||||
import org.jfree.chart.plot.ValueMarker;
|
||||
import org.jfree.chart.plot.XYPlot;
|
||||
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
|
||||
import org.jfree.chart.swt.ChartComposite;
|
||||
import org.jfree.chart.ui.RectangleAnchor;
|
||||
import org.jfree.chart.ui.RectangleInsets;
|
||||
import org.jfree.data.xy.XYDataset;
|
||||
import org.jfree.data.xy.XYSeries;
|
||||
import org.jfree.data.xy.XYSeriesCollection;
|
||||
|
||||
import flintstones.entity.problemelement.entities.Criterion;
|
||||
import flintstones.entity.problemelement.entities.ProblemElement;
|
||||
import flintstones.entity.sensitiveanalysismodel.SensitiveAnalysisModel;
|
||||
|
||||
public abstract class RankingEvolutionChart {
|
||||
|
||||
protected SensitiveAnalysisModel model;
|
||||
|
||||
protected Criterion criterion;
|
||||
protected ProblemElement[] criteria;
|
||||
|
||||
protected JFreeChart chart;
|
||||
protected ChartComposite chartComposite;
|
||||
protected XYSeriesCollection dataset;
|
||||
protected ValueMarker currentWeightMarker;
|
||||
|
||||
public void refreshChart(Criterion criterion) {
|
||||
this.criterion = criterion;
|
||||
if (this.chart == null)
|
||||
this.chart = this.createChart(this.createDataset());
|
||||
else
|
||||
this.chart.getXYPlot()
|
||||
.setDataset(this.createDataset());
|
||||
|
||||
currentWeightMarker.setValue(model.getCriteriaWeights().get(criterion));
|
||||
|
||||
updateRange();
|
||||
}
|
||||
|
||||
private JFreeChart createChart(XYDataset dataset) {
|
||||
JFreeChart chart = ChartFactory.createXYLineChart("Ranking exchanges among alternatives", null, null, dataset,
|
||||
PlotOrientation.VERTICAL, true, true, false);
|
||||
|
||||
chart.setBackgroundPaint(Color.white);
|
||||
|
||||
XYPlot plot = chart.getXYPlot();
|
||||
plot.setBackgroundPaint(Color.white);
|
||||
plot.setDomainGridlinesVisible(true);
|
||||
plot.setRangeGridlinesVisible(true);
|
||||
|
||||
setDefaultRanges(plot);
|
||||
setRenderer(plot);
|
||||
setMarker(plot);
|
||||
|
||||
chart.getTitle().setFont(new Font("SansSerif", Font.BOLD, 12));
|
||||
|
||||
return chart;
|
||||
}
|
||||
|
||||
private void setDefaultRanges(XYPlot plot) {
|
||||
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
|
||||
rangeAxis.setRange(0, 1);
|
||||
rangeAxis.setTickUnit(new NumberTickUnit(0.1));
|
||||
|
||||
NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
|
||||
domainAxis.setRange(0.01, 1);
|
||||
domainAxis.setTickUnit(new NumberTickUnit(0.05));
|
||||
}
|
||||
|
||||
private void setRenderer(XYPlot plot) {
|
||||
@SuppressWarnings("serial")
|
||||
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer() {
|
||||
Stroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 7.0f,
|
||||
new float[] { 7.0f }, 0.0f);
|
||||
|
||||
@Override
|
||||
public Stroke getItemStroke(int row, int column) {
|
||||
return dashed;
|
||||
}
|
||||
};
|
||||
renderer.setDefaultShapesVisible(true);
|
||||
renderer.setDefaultShapesFilled(true);
|
||||
renderer.setDefaultStroke(new BasicStroke(2));
|
||||
renderer.setDefaultLegendTextFont(new Font("sans-serif", Font.BOLD, 8));
|
||||
renderer.setDefaultToolTipGenerator(createToolTipGenerator());
|
||||
plot.setRenderer(renderer);
|
||||
}
|
||||
|
||||
private XYToolTipGenerator createToolTipGenerator() {
|
||||
XYToolTipGenerator xyToolTipGenerator = new XYToolTipGenerator() {
|
||||
public String generateToolTip(XYDataset dataset, int series, int item) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
double x = dataset.getX(series, item).doubleValue();
|
||||
double y = dataset.getY(series, item).doubleValue();
|
||||
String coordX = Double.toString(Math.round(x * 10000d) / 10000d);
|
||||
String coordY = Double.toString(Math.round(y * 10000d) / 10000d);
|
||||
sb.append("(" + coordX + ", " + coordY + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
return sb.toString();
|
||||
}
|
||||
};
|
||||
|
||||
return xyToolTipGenerator;
|
||||
}
|
||||
|
||||
private void setMarker(XYPlot plot) {
|
||||
currentWeightMarker = new ValueMarker(0);
|
||||
currentWeightMarker.setLabelFont(new java.awt.Font("SansSerif", Font.BOLD, 9)); //$NON-NLS-1$
|
||||
currentWeightMarker.setLabel("Current weight");
|
||||
currentWeightMarker.setPaint(Color.red);
|
||||
currentWeightMarker.setStroke(new BasicStroke(2));
|
||||
currentWeightMarker.setLabelOffset(new RectangleInsets(10, 10, 10, 50));
|
||||
currentWeightMarker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
|
||||
currentWeightMarker.setLabelBackgroundColor(new Color(255, 255, 255));
|
||||
plot.addDomainMarker(currentWeightMarker);
|
||||
}
|
||||
|
||||
private void updateRange() {
|
||||
XYPlot plot = chart.getXYPlot();
|
||||
|
||||
List<Double> preferences = new LinkedList<>();
|
||||
for(int i = 0; i < dataset.getSeries().size(); ++i) {
|
||||
XYSeries serie = dataset.getSeries(i);
|
||||
preferences.add(serie.getMaxY());
|
||||
}
|
||||
|
||||
Collections.sort(preferences);
|
||||
|
||||
double max = 1;
|
||||
double tickUnit = 0.1;
|
||||
if(preferences.get(preferences.size() - 1) > 1) {
|
||||
max = preferences.get(preferences.size() - 1) + 1;
|
||||
tickUnit = max / 10d;
|
||||
}
|
||||
|
||||
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
|
||||
rangeAxis.setRange(0, 1);
|
||||
rangeAxis.setTickUnit(new NumberTickUnit(tickUnit));
|
||||
}
|
||||
|
||||
public abstract XYDataset createDataset();
|
||||
|
||||
public void initialize(SensitiveAnalysisModel model, ProblemElement[] criteria, Criterion criterion,
|
||||
Composite container, int width, int height, int style) {
|
||||
|
||||
this.model = model;
|
||||
this.criteria = criteria;
|
||||
this.criterion = criterion;
|
||||
|
||||
refreshChart(this.criterion);
|
||||
|
||||
this.chartComposite = new ChartComposite(container, style, this.chart, true);
|
||||
this.chartComposite.setSize(width, height);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user