Search in sources :

Example 21 with JochreSession

use of com.joliciel.jochre.JochreSession in project jochre by urieli.

the class LoginController method onClick$btnLogin.

@Listen("onClick = #btnLogin")
public void onClick$btnLogin(Event event) {
    try {
        LOG.debug("onClick$btnLogin");
        Session session = Sessions.getCurrent();
        SecurityDao securityDao = SecurityDao.getInstance(jochreSession);
        User user = null;
        try {
            user = securityDao.findUser(txtUserName.getValue());
        } catch (EntityNotFoundException enfe) {
            LOG.debug("Unknown user: " + txtUserName.getValue());
            lblError.setVisible(true);
        }
        if (user != null) {
            boolean success = user.login(txtPassword.getValue());
            if (!success) {
                LOG.debug("Login failed");
                lblError.setVisible(true);
                Parameters parameters = securityDao.loadParameters();
                Date lastFailedLoginAttempt = parameters.getLastFailedLoginAttempt();
                Date now = new Date();
                long diff = now.getTime() - lastFailedLoginAttempt.getTime();
                LOG.debug("time since last failed login: " + diff);
            } else {
                LOG.debug("Login success");
                session.setAttribute(SESSION_JOCHRE_USER, user);
                Executions.sendRedirect("docs.zul");
            }
        }
    } catch (Exception e) {
        LOG.error("Failure in onClick$btnLogin", e);
        throw new RuntimeException(e);
    }
}
Also used : User(com.joliciel.jochre.security.User) Parameters(com.joliciel.jochre.security.Parameters) EntityNotFoundException(com.joliciel.jochre.EntityNotFoundException) Date(java.util.Date) EntityNotFoundException(com.joliciel.jochre.EntityNotFoundException) Session(org.zkoss.zk.ui.Session) JochreSession(com.joliciel.jochre.JochreSession) SecurityDao(com.joliciel.jochre.security.SecurityDao) Listen(org.zkoss.zk.ui.select.annotation.Listen)

Example 22 with JochreSession

use of com.joliciel.jochre.JochreSession in project jochre by urieli.

the class SplitController method doAfterCompose.

@Override
public void doAfterCompose(Window window) throws Exception {
    super.doAfterCompose(window);
    String pageTitle = Labels.getLabel("splits.title");
    winSplits.getPage().setTitle(pageTitle);
    Session session = Sessions.getCurrent();
    currentUser = (User) session.getAttribute(LoginController.SESSION_JOCHRE_USER);
    if (currentUser == null)
        Executions.sendRedirect("login.zul");
    GraphicsDao graphicsDao = GraphicsDao.getInstance(jochreSession);
    shapesToSplit = graphicsDao.findShapesToSplit(jochreSession.getLocale());
    for (Shape shape : shapesToSplit) {
        Row shapeRow = new Row();
        Image shapeImage = new Image();
        org.zkoss.image.Image convertedImage = Images.encode("shape_" + shape.getId() + ".png", shape.getImage());
        shapeImage.setContent(convertedImage);
        shapeImage.setWidth(shape.getImage().getWidth() + "px");
        shapeImage.setHeight((shape.getImage().getHeight()) + "px");
        Cell shapeCell = new Cell();
        Div shapeDiv = new Div();
        shapeDiv.setStyle("position: relative;");
        shapeCell.appendChild(shapeDiv);
        int shapeWidth = shape.getImage().getWidth();
        shapeCell.setWidth((shapeWidth + 100) + "px");
        shapeImage.addEventListener("onClick", new ShapeImageOnClickEventListener(shape, shapeDiv));
        shapeDiv.appendChild(shapeImage);
        for (Split split : shape.getSplits()) {
            Div verticalLine = new Div();
            verticalLine.setWidth("1px");
            verticalLine.setHeight("100px");
            verticalLine.setStyle("position: absolute; top:0px; left: " + split.getPosition() + "px;background-color:RED;");
            verticalLine.setId("shape" + shape.getId() + "split" + split.getPosition());
            verticalLine.addEventListener("onClick", new SplitOnClickEventListener(shape, shapeDiv, verticalLine, split));
            shapeDiv.appendChild(verticalLine);
        }
        shapeRow.appendChild(shapeCell);
        Cell letterCell = new Cell();
        Label letterLabel = new Label();
        letterLabel.setValue(shape.getLetter());
        letterCell.appendChild(letterLabel);
        letterCell.setWidth("100px");
        shapeRow.appendChild(letterCell);
        Cell detailsCell = new Cell();
        Label detailsLabel = new Label();
        JochrePage page = shape.getGroup().getRow().getParagraph().getImage().getPage();
        JochreDocument doc = page.getDocument();
        detailsLabel.setValue(doc.getName() + ", page " + page.getIndex());
        detailsCell.appendChild(detailsLabel);
        shapeRow.appendChild(detailsCell);
        splitGridRows.appendChild(shapeRow);
    }
}
Also used : Shape(com.joliciel.jochre.graphics.Shape) Label(org.zkoss.zul.Label) JochreDocument(com.joliciel.jochre.doc.JochreDocument) Image(org.zkoss.zul.Image) JochrePage(com.joliciel.jochre.doc.JochrePage) Div(org.zkoss.zul.Div) GraphicsDao(com.joliciel.jochre.graphics.GraphicsDao) Row(org.zkoss.zul.Row) Split(com.joliciel.jochre.boundaries.Split) Cell(org.zkoss.zul.Cell) JochreSession(com.joliciel.jochre.JochreSession) Session(org.zkoss.zk.ui.Session)

Example 23 with JochreSession

use of com.joliciel.jochre.JochreSession in project jochre by urieli.

the class TextController method doAfterCompose.

@Override
public void doAfterCompose(Window window) throws Exception {
    try {
        super.doAfterCompose(window);
        Session session = Sessions.getCurrent();
        currentUser = (User) session.getAttribute(LoginController.SESSION_JOCHRE_USER);
        if (currentUser == null)
            Executions.sendRedirect("login.zul");
        HttpServletRequest request = (HttpServletRequest) Executions.getCurrent().getNativeRequest();
        if (request.getParameter("imageId") != null) {
            int imageId = Integer.parseInt(request.getParameter("imageId"));
            GraphicsDao graphicsDao = GraphicsDao.getInstance(jochreSession);
            currentImage = graphicsDao.loadJochreImage(imageId);
            currentDoc = currentImage.getPage().getDocument();
            uploadPanel.setVisible(false);
            progressBox.setVisible(true);
            startRenderTimer.setRunning(true);
        } else if (request.getParameter("docId") != null) {
            int docId = Integer.parseInt(request.getParameter("docId"));
            DocumentDao documentDao = DocumentDao.getInstance(jochreSession);
            currentDoc = documentDao.loadJochreDocument(docId);
            if (request.getParameter("addPages") != null) {
                uploadPanel.setVisible(true);
                // progressBox.setVisible(false);
                lblAwaitingFile.setVisible(true);
            } else {
                uploadPanel.setVisible(false);
                progressBox.setVisible(true);
                startRenderTimer.setRunning(true);
            }
        } else {
            uploadPanel.setVisible(true);
            // progressBox.setVisible(false);
            lblAwaitingFile.setVisible(true);
        }
        if (currentDoc != null && !currentDoc.isLeftToRight())
            htmlContent.setSclass("rightToLeft");
        if (currentDoc != null) {
            documentGrid.setVisible(true);
            lblDocName.setValue(currentDoc.getName());
        }
    } catch (Exception e) {
        LOG.error("Failure in TextController$doAfterCompose", e);
        throw new RuntimeException(e);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) GraphicsDao(com.joliciel.jochre.graphics.GraphicsDao) DocumentDao(com.joliciel.jochre.doc.DocumentDao) JochreSession(com.joliciel.jochre.JochreSession) Session(org.zkoss.zk.ui.Session)

Example 24 with JochreSession

use of com.joliciel.jochre.JochreSession in project jochre by urieli.

the class LineDefinitionImplTest method testFollowInShape.

@Test
public void testFollowInShape() throws Exception {
    System.setProperty("config.file", "src/test/resources/test.conf");
    ConfigFactory.invalidateCaches();
    Config config = ConfigFactory.load();
    JochreSession jochreSession = new JochreSession(config);
    LineDefinition lineDef = new LineDefinition(0, 0);
    List<Integer> steps = new ArrayList<>();
    steps.add(2);
    steps.add(3);
    lineDef.setSteps(steps);
    int[] pixels = { // row
    0, // row
    1, // row
    1, // row
    0, // row
    0, // row
    1, // row
    1, // row
    1, // row
    0, // row
    1, // row
    1, // row
    1, // row
    0, // row
    1, // row
    1, // row
    1, // row
    0, // row
    0, // row
    1, // row
    1, // row
    0, // row
    0, // row
    1, // row
    1, // row
    0, // row
    0, // row
    1, // row
    1, // row
    0, // row
    1, // row
    1, // row
    0, // row
    0, // row
    0, // row
    0, // row
    1, // row
    1, // row
    1, // row
    1, // row
    0, // row
    0, // row
    0, // row
    0, // row
    1, // row
    1, // row
    1, // row
    0, // row
    0, // row
    0, // row
    0, // row
    1, // row
    1, // row
    1, // row
    0, // row
    0, // row
    0, // row
    1, // row
    1, // row
    1, // row
    1, // row
    1, // row
    0, // row
    0, // row
    0 };
    Shape shape = new ShapeMock(pixels, 8, 8, jochreSession);
    int[] endPoint = lineDef.followInShape(shape, 5, 5, 0, 100, 0);
    assertEquals(2, endPoint[0]);
    assertEquals(6, endPoint[1]);
    // the length
    assertEquals(3, endPoint[2]);
}
Also used : Config(com.typesafe.config.Config) ArrayList(java.util.ArrayList) JochreSession(com.joliciel.jochre.JochreSession) Test(org.junit.Test)

Example 25 with JochreSession

use of com.joliciel.jochre.JochreSession in project jochre by urieli.

the class SegmenterImplTest method testSegment.

@Test
public void testSegment() throws Exception {
    System.setProperty("config.file", "src/test/resources/test.conf");
    ConfigFactory.invalidateCaches();
    Config config = ConfigFactory.load();
    JochreSession jochreSession = new JochreSession(config);
    final int width = 12;
    final int height = 10;
    final List<Rectangle> whiteAreas = new ArrayList<Rectangle>();
    int[] pixels = // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11
    { // row 0
    0, // row 0
    1, // row 0
    0, // row 0
    0, // row 0
    0, // row 0
    0, // row 0
    0, // row 0
    0, // row 0
    0, // row 0
    0, // row 0
    0, // row 0
    0, // row 1
    0, // row 1
    1, // row 1
    1, // row 1
    0, // row 1
    0, // row 1
    0, // row 1
    1, // row 1
    1, // row 1
    0, // row 1
    1, // row 1
    1, // row 1
    1, // row 2
    0, // row 2
    0, // row 2
    1, // row 2
    0, // row 2
    0, // row 2
    0, // row 2
    0, // row 2
    1, // row 2
    0, // row 2
    0, // row 2
    0, // row 2
    1, // row 3
    0, // row 3
    0, // row 3
    1, // row 3
    0, // row 3
    1, // row 3
    0, // row 3
    0, // row 3
    0, // row 3
    0, // row 3
    1, // row 3
    0, // row 3
    1, // row 4
    0, // row 4
    0, // row 4
    1, // row 4
    0, // row 4
    1, // row 4
    1, // row 4
    0, // row 4
    0, // row 4
    0, // row 4
    0, // row 4
    0, // row 4
    0, // row 5
    0, // row 5
    0, // row 5
    0, // row 5
    0, // row 5
    0, // row 5
    0, // row 5
    0, // row 5
    0, // row 5
    0, // row 5
    0, // row 5
    0, // row 5
    0, // row 6
    0, // row 6
    0, // row 6
    1, // row 6
    1, // row 6
    1, // row 6
    0, // row 6
    1, // row 6
    1, // row 6
    0, // row 6
    1, // row 6
    1, // row 6
    1, // row 7
    0, // row 7
    0, // row 7
    0, // row 7
    0, // row 7
    1, // row 7
    0, // row 7
    0, // row 7
    1, // row 7
    0, // row 7
    0, // row 7
    0, // row 7
    1, // row 8
    0, // row 8
    0, // row 8
    1, // row 8
    0, // row 8
    1, // row 8
    0, // row 8
    0, // row 8
    1, // row 8
    0, // row 8
    0, // row 8
    0, // row 8
    0, // row 9
    0, // row 9
    0, // row 9
    0, // row 9
    0, // row 9
    0, // row 9
    0, // row 9
    0, // row 9
    1, // row 9
    0, // row 9
    1, // row 9
    0, // row 9
    1 };
    SourceImage sourceImage = new SourceImageMock(pixels, height, width, jochreSession);
    Segmenter segmenter = new Segmenter(sourceImage, jochreSession);
    LOG.debug("findContiguousShapes");
    Set<Shape> shapes = segmenter.findContiguousShapes(sourceImage);
    assertEquals(11, shapes.size());
    int i = 0;
    int j = 0;
    for (Shape shape : shapes) {
        if (i == 0) {
            assertEquals(0, shape.getTop());
            assertEquals(1, shape.getLeft());
            assertEquals(4, shape.getBottom());
            assertEquals(2, shape.getRight());
        }
        i++;
    }
    i = 0;
    for (Shape shape : shapes) {
        LOG.debug("============= Shape (" + i + ") ================");
        LOG.debug("Left = " + shape.getLeft() + ". Top = " + shape.getTop() + ". Right = " + shape.getRight() + ". Bottom = " + shape.getBottom());
        for (int y = 0; y < shape.getHeight(); y++) {
            String line = "";
            for (int x = 0; x < shape.getWidth(); x++) {
                if (shape.isPixelBlack(x, y, sourceImage.getSeparationThreshold()))
                    line += "x";
                else
                    line += "o";
            }
            LOG.debug(line);
        }
        i++;
    }
    LOG.debug("arrangeShapesInRows");
    List<RowOfShapes> rows = segmenter.groupShapesIntoRows(sourceImage, shapes, whiteAreas, false);
    assertEquals(2, rows.size());
    i = 0;
    for (RowOfShapes row : rows) {
        j = 0;
        for (Shape shape : row.getShapes()) {
            LOG.debug("============= Shape (" + i + "," + j + ") ================");
            LOG.debug("Left = " + shape.getLeft() + ". Top = " + shape.getTop() + ". Right = " + shape.getRight() + ". Bottom = " + shape.getBottom());
            for (int y = 0; y < shape.getHeight(); y++) {
                String line = "";
                for (int x = 0; x < shape.getWidth(); x++) {
                    if (shape.isPixelBlack(x, y, sourceImage.getSeparationThreshold()))
                        line += "x";
                    else
                        line += "o";
                }
                LOG.debug(line);
            }
            j++;
        }
        i++;
    }
}
Also used : Config(com.typesafe.config.Config) ArrayList(java.util.ArrayList) JochreSession(com.joliciel.jochre.JochreSession) Test(org.junit.Test)

Aggregations

JochreSession (com.joliciel.jochre.JochreSession)40 Config (com.typesafe.config.Config)34 Test (org.junit.Test)34 BufferedImage (java.awt.image.BufferedImage)20 ArrayList (java.util.ArrayList)13 InputStream (java.io.InputStream)10 Paragraph (com.joliciel.jochre.graphics.Paragraph)8 RowOfShapes (com.joliciel.jochre.graphics.RowOfShapes)8 SourceImage (com.joliciel.jochre.graphics.SourceImage)8 Segmenter (com.joliciel.jochre.graphics.Segmenter)7 Shape (com.joliciel.jochre.graphics.Shape)7 ImagePixelGrabber (com.joliciel.jochre.utils.graphics.ImagePixelGrabber)7 JochreImage (com.joliciel.jochre.graphics.JochreImage)6 Rectangle (java.awt.Rectangle)6 HashMap (java.util.HashMap)6 Session (org.zkoss.zk.ui.Session)6 BitSet (java.util.BitSet)5 JochrePage (com.joliciel.jochre.doc.JochrePage)4 SplitFeature (com.joliciel.jochre.boundaries.features.SplitFeature)3 JochreDocument (com.joliciel.jochre.doc.JochreDocument)3