Skip to content

Commit

Permalink
Fixes #8 - Add an analysis preview for each source
Browse files Browse the repository at this point in the history
  • Loading branch information
ben12 committed Nov 23, 2014
1 parent be03c7b commit beddb0d
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public class InputRequirementSource
/**
* List of covered document sources by this document source.
*/
@NotNullElement
@NotNullElement(message = "{unknown.cover.source}")
private final List<InputRequirementSource> covers = new ArrayList<>();

/**
Expand Down Expand Up @@ -325,9 +325,10 @@ public String toString()
builder.append(name);
builder.append(" (");
builder.append(sourcePath);
builder.append("):\n");
builder.append("):\n\n");
for (Requirement req : requirements)
{
builder.append("--------------------------------------------------------------------\n");
builder.append(req.toString());
builder.append('\n');
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/ben12/reta/model/Requirement.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,11 @@ public String toString()
builder.append("\n");
}
}
builder.append("\tContent: ");
builder.append(content);
if (!content.isEmpty())
{
builder.append("\tContent:");
builder.append(content);
}

return builder.toString();
}
Expand Down
41 changes: 32 additions & 9 deletions src/main/java/com/ben12/reta/util/RETAAnalysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,11 @@ public void parse(DoubleProperty progress) throws IOException
}

private void parse(InputRequirementSource requirementSource) throws IOException
{
parse(requirementSource, null, Integer.MAX_VALUE);
}

public void parse(InputRequirementSource requirementSource, StringBuilder sourceText, int limit) throws IOException
{
logger.info("Start parsing " + requirementSource.getName());

Expand All @@ -509,11 +514,11 @@ private void parse(InputRequirementSource requirementSource) throws IOException
patternEnd = Pattern.compile(requirementSource.getReqEnd(), Pattern.MULTILINE);
}

parseMultiRequirementByFile(requirementSource, patternStart, patternEnd, patternRef);
parseMultiRequirementByFile(requirementSource, patternStart, patternEnd, patternRef, sourceText, limit);
}
else
{
parseReferencesInFiles(requirementSource, patternRef);
parseReferencesInFiles(requirementSource, patternRef, sourceText, limit);
}

logger.info("End parsing " + requirementSource.getName());
Expand All @@ -524,8 +529,8 @@ private void parse(InputRequirementSource requirementSource) throws IOException
* @param newPatternRef
* @throws IOException
*/
private void parseReferencesInFiles(InputRequirementSource requirementSource, Pattern patternRef)
throws IOException
private void parseReferencesInFiles(InputRequirementSource requirementSource, Pattern patternRef,
StringBuilder sourceText, int limit) throws IOException
{
final ConcatReader reader = getReader(requirementSource);
Path root = Paths.get(requirementSource.getSourcePath());
Expand All @@ -537,11 +542,20 @@ private void parseReferencesInFiles(InputRequirementSource requirementSource, Pa
final CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE);
StringBuilder builder = new StringBuilder(3 * BUFFER_SIZE);
int r = reader.read(buffer);
while (r >= 0)
while (r >= 0 && (limit == Integer.MAX_VALUE || limit > 0))
{
buffer.flip();
builder.append(buffer, 0, r);
builder.append(buffer, 0, Math.min(limit, r));
if (sourceText != null)
{
buffer.rewind();
sourceText.append(buffer, 0, Math.min(limit, r));
}
buffer.clear();
if (limit != Integer.MAX_VALUE)
{
limit -= r;
}

Path prevPath = reader.getCurrentPath();
r = reader.read(buffer);
Expand Down Expand Up @@ -576,19 +590,28 @@ private void parseReferencesInFiles(InputRequirementSource requirementSource, Pa
* @throws IOException
*/
private void parseMultiRequirementByFile(InputRequirementSource requirementSource, Pattern patternStart,
Pattern patternEnd, Pattern patternRef) throws IOException
Pattern patternEnd, Pattern patternRef, StringBuilder sourceText, int limit) throws IOException
{
boolean requirementStarted = false;
Requirement requirement = null;
final ConcatReader reader = getReader(requirementSource);
final CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE);
StringBuilder builder = new StringBuilder(3 * BUFFER_SIZE);
int r = reader.read(buffer);
while (r >= 0)
while (r >= 0 && (limit == Integer.MAX_VALUE || limit > 0))
{
buffer.flip();
builder.append(buffer, 0, r);
builder.append(buffer, 0, Math.min(limit, r));
if (sourceText != null)
{
buffer.rewind();
sourceText.append(buffer, 0, Math.min(limit, r));
}
buffer.clear();
if (limit != Integer.MAX_VALUE)
{
limit -= r;
}

Path path = reader.getCurrentPath();
r = reader.read(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ private void rebuild()
}

panes = new ArrayList<>(sourceConfigurations.getPanes());
if (!panes.isEmpty())
{
sourceConfigurations.setExpandedPane(panes.get(0));
}

bufferingManager.revert();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ben12/reta/view/MainConfigurationUI.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<?import com.ben12.reta.view.control.*?>
<?import com.ben12.reta.view.validation.*?>

<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="com.ben12.reta.view.MainConfigurationController" prefWidth="600"
<VBox fx:id="root" xmlns:fx="http://javafx.com/fxml" fx:controller="com.ben12.reta.view.MainConfigurationController" prefWidth="600"
prefHeight="700">
<fx:define>
<Image fx:id="errorImg" url="@/com/ben12/reta/resources/images/error.png" />
Expand All @@ -23,7 +23,7 @@
</Menu>
</menus>
</MenuBar>
<GridPane fx:id="root" VBox.vgrow="ALWAYS" hgap="2" vgap="2">
<GridPane VBox.vgrow="ALWAYS" hgap="2" vgap="2">
<padding>
<Insets bottom="2" left="2" right="2" top="2" />
</padding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.ben12.reta.view;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
Expand All @@ -31,6 +32,8 @@
import java.util.Map.Entry;
import java.util.ResourceBundle;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javafx.beans.binding.Bindings;
Expand All @@ -44,6 +47,9 @@
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
Expand All @@ -54,6 +60,9 @@
import javafx.scene.image.ImageView;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Callback;
import javafx.util.StringConverter;
import javafx.util.converter.IntegerStringConverter;
Expand All @@ -68,6 +77,7 @@
import com.ben12.reta.view.buffering.PropertyBufferingValidation;
import com.ben12.reta.view.buffering.SimpleObjectPropertyBuffering;
import com.ben12.reta.view.control.MapTableView;
import com.ben12.reta.view.control.MessageDialog;
import com.ben12.reta.view.validation.ValidationUtils;
import com.google.common.base.Objects;
import com.google.common.base.Splitter;
Expand All @@ -82,6 +92,8 @@ public class SourceConfigurationController
/** Keeps a reference on buffered properties. */
private final List<Buffering<?>> bufferedProperties = new ArrayList<>();

private InputRequirementSource requirementSource = null;

private ObservableList<InputRequirementSource> sources = null;

private ObservableList<ObjectProperty<String>> sourcesName = null;
Expand Down Expand Up @@ -171,6 +183,12 @@ public class SourceConfigurationController
@FXML
private Button deleteReference;

@FXML
private TextField previewLimit;

@FXML
private Button preview;

private ObservableMapBuffering<String, Integer> referenceMap;

private FileChooser fileChooser = null;
Expand Down Expand Up @@ -199,11 +217,13 @@ public <T> void initializeLabeled(TextInputControl input, ImageView validity,
ValidationUtils.bindValidationLabel(input, validity, property);
}

public ObjectProperty<String> bind(BufferingManager newBufferingManager, InputRequirementSource requirementSource,
ObservableList<InputRequirementSource> newSources, ObservableList<ObjectProperty<String>> newSourcesName,
public ObjectProperty<String> bind(BufferingManager newBufferingManager,
InputRequirementSource newRequirementSource, ObservableList<InputRequirementSource> newSources,
ObservableList<ObjectProperty<String>> newSourcesName,
ObservableList<Callback<InputRequirementSource, Void>> callBacks,
Callback<InputRequirementSource, Void> mainCallBack) throws NoSuchMethodException
{
requirementSource = newRequirementSource;
bufferingManager = newBufferingManager;
sources = newSources;
sourcesName = newSourcesName;
Expand Down Expand Up @@ -349,6 +369,9 @@ else if (!validityProperty().get())
.or(Bindings.selectString(selectedAttribute, "key").isEqualTo(Requirement.ATTRIBUTE_TEXT)));
deleteReference.disableProperty().bind(referencesTable.getSelectionModel().selectedItemProperty().isNull());

preview.disableProperty().bind(
bufferingManager.bufferingProperty().or(Bindings.not(bufferingManager.validProperty())));

return nameProperty;
}

Expand Down Expand Up @@ -453,6 +476,56 @@ protected void addNewReference(ActionEvent event)
}
}

@FXML
protected void preview(ActionEvent event)
{
try
{
String limitStr = previewLimit.getText();

int limit;
try
{
limit = Integer.parseInt(limitStr);
}
catch (NumberFormatException e)
{
// limitStr empty or not a number
limit = Integer.MAX_VALUE;
}

StringBuilder sourceText = new StringBuilder(Math.min(limit, 2 * 1024));

RETAAnalysis.getInstance().parse(requirementSource, sourceText, limit);

Stage previewStage = new Stage(StageStyle.UTILITY);
previewStage.initOwner(titledPane.getScene().getWindow());
previewStage.initModality(Modality.APPLICATION_MODAL);

ResourceBundle labels = ResourceBundle.getBundle("com/ben12/reta/view/Labels");

FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("SourcePreviewUI.fxml"));
loader.setResources(labels);
Parent root = (Parent) loader.load();

SourcePreviewController previewController = loader.getController();
previewController.analysedTextProperty().setValue(sourceText.toString());
previewController.resultTextProperty().setValue(requirementSource.toString());

previewStage.setScene(new Scene(root));
previewStage.setTitle(labels.getString("preview.title"));
previewStage.sizeToScene();
previewStage.show();
}
catch (IOException e)
{
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "", e);
MessageDialog.showErrorMessage(titledPane.getScene().getWindow(), e.getMessage());
e.printStackTrace();
}
}

public void disconnect()
{
bufferingManager.removeAll(bufferedProperties);
Expand Down
26 changes: 11 additions & 15 deletions src/main/java/com/ben12/reta/view/SourceConfigurationUI.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
<?import javafx.geometry.*?>
<?import com.ben12.reta.view.control.*?>
<?import com.ben12.reta.view.validation.*?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.Label?>

<TitledPane xmlns:fx="http://javafx.com/fxml" fx:controller="com.ben12.reta.view.SourceConfigurationController"
fx:id="titledPane" prefWidth="400" prefHeight="500">
Expand All @@ -29,16 +27,6 @@
<ColumnConstraints minWidth="50" />
<ColumnConstraints minWidth="50" />
</columnConstraints>
<rowConstraints>
<RowConstraints vgrow="NEVER" />
<RowConstraints vgrow="NEVER" />
<RowConstraints vgrow="NEVER" />
<RowConstraints vgrow="NEVER" />
<RowConstraints vgrow="NEVER" />
<RowConstraints vgrow="NEVER" />
<RowConstraints vgrow="ALWAYS" />
<RowConstraints vgrow="ALWAYS" />
</rowConstraints>
<children>
<Label minHeight="25" GridPane.columnIndex="0" GridPane.rowIndex="0" text="%rename" />
<TextField fx:id="name" promptText="%rename.help" GridPane.columnIndex="1" GridPane.rowIndex="0"
Expand Down Expand Up @@ -190,11 +178,12 @@
<Separator HBox.hgrow="ALWAYS" />
</children>
</HBox>

<Button fx:id="deleteAttribute" GridPane.columnIndex="0" GridPane.rowIndex="1" text="%delete.att.index"
maxWidth="1000" onAction="#deleteAttribute" />

<MapTableView fx:id="attributesTable" GridPane.columnIndex="1" GridPane.rowIndex="1" editable="true">
<MapTableView fx:id="attributesTable" minHeight="50" GridPane.columnIndex="1" GridPane.rowIndex="1"
editable="true">
<columns>
<TableColumn fx:id="reqAttNameColumn" text="%attribute" />
<TableColumn fx:id="reqAttGroupColumn" text="%req.start.att.index" sortType="ASCENDING" />
Expand Down Expand Up @@ -238,7 +227,8 @@
<Button fx:id="deleteReference" GridPane.columnIndex="0" GridPane.rowIndex="1" text="%delete.att.index"
maxWidth="1000" onAction="#deleteReference" />

<MapTableView fx:id="referencesTable" GridPane.columnIndex="1" GridPane.rowIndex="1" editable="true">
<MapTableView fx:id="referencesTable" minHeight="50" GridPane.columnIndex="1" GridPane.rowIndex="1"
editable="true">
<columns>
<TableColumn fx:id="refAttNameColumn" text="%attribute" />
<TableColumn fx:id="refAttGroupColumn" text="%ref.att.index" sortType="ASCENDING" />
Expand All @@ -256,6 +246,12 @@
</HBox>
</children>
</GridPane>
<HBox alignment="CENTER_RIGHT" spacing="5" GridPane.columnIndex="0" GridPane.rowIndex="9" GridPane.columnSpan="4">
<children>
<TextField fx:id="previewLimit" HBox.hgrow="ALWAYS" promptText="%preview.limit.help" />
<Button fx:id="preview" text="%preview.button" minWidth="50" onAction="#preview" />
</children>
</HBox>
</children>
</GridPane>
</content>
Expand Down
Loading

0 comments on commit beddb0d

Please sign in to comment.