Example usage for javax.swing JFrame dispatchEvent

List of usage examples for javax.swing JFrame dispatchEvent

Introduction

In this page you can find the example usage for javax.swing JFrame dispatchEvent.

Prototype

public final void dispatchEvent(AWTEvent e) 

Source Link

Document

Dispatches an event to this component or one of its sub components.

Usage

From source file:Main.java

public static void installEscapeCloseOperation(final JFrame dialog) {
    Action dispatchClosing = new AbstractAction() {
        public void actionPerformed(ActionEvent event) {
            dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
        }//from   www  .  ja  va 2  s . c o m
    };
    JRootPane root = dialog.getRootPane();
    root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ESCAPE_KEY_STROKE, ESCAPE_KEY);
    root.getActionMap().put(ESCAPE_KEY, dispatchClosing);
}

From source file:Forms.CreateGearForm.java

private void lintSpec(final GearSpec spec) {
    LintGearSpecWorker worker = new LintGearSpecWorker(spec) {
        @Override/*from  w ww .  j  a  v a2  s. co  m*/
        protected void done() {
            super.done();

            if (result.getPassed()) {
                if (saveSpec(spec)) {
                    JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(MasterPanel);
                    frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
                }
            } else {
                showLintErrorDialog(result);
            }
        }
    };
    worker.execute();
}

From source file:com.sldeditor.test.SLDTestRunner.java

/**
 * Run the test//from   ww w. j  av a 2 s .  c o m
 *
 * @param folder the folder
 * @param testConfig the test config
 */
public void runTest(String folder, String testConfig) {
    // read JSON file data as String
    String fullPath = "/" + folder + "/test/" + testConfig;

    SldEditorTest testSuite = (SldEditorTest) ParseXML.parseFile("", fullPath, SCHEMA_RESOURCE,
            SldEditorTest.class);

    Assert.assertNotNull("Failed to read test config file : " + fullPath, testSuite);

    String testsldfile = testSuite.getTestsldfile();
    if (!testsldfile.startsWith("/")) {
        testsldfile = "/" + testsldfile;
    }

    System.out.println("Opening : " + testsldfile);

    List<XMLVendorOption> xmlVendorOptionList = testSuite.getVendorOption();
    if ((xmlVendorOptionList != null) && !xmlVendorOptionList.isEmpty()) {
        List<VersionData> versionDataList = new ArrayList<VersionData>();

        for (XMLVendorOption vo : xmlVendorOptionList) {
            try {
                String vendorOptionClassName = vo.getClassType().trim();
                Class<?> vendorOptionClass = Class.forName(vendorOptionClassName);
                VersionData versionData = VersionData.decode(vendorOptionClass, vo.getVersion());
                versionDataList.add(versionData);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        if (!versionDataList.contains(VendorOptionManager.getInstance().getDefaultVendorOptionVersionData())) {
            versionDataList.add(VendorOptionManager.getInstance().getDefaultVendorOptionVersionData());
        }
        sldEditor.setVendorOptions(versionDataList);
    }

    InputStream inputStream = SLDTestRunner.class.getResourceAsStream(testsldfile);

    if (inputStream == null) {
        Assert.assertNotNull("Failed to find sld test file : " + testsldfile, inputStream);
    } else {
        File f = null;
        try {
            f = stream2file(inputStream);
            sldEditor.openFile(f.toURI().toURL());
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        GraphicPanelFieldManager mgr = sldEditor.getFieldDataManager();

        for (XMLPanelTest test : testSuite.getPanelTests()) {
            XMLSetup selectedItem = test.getSetup();

            TreeSelectionData selectionData = new TreeSelectionData();
            selectionData.setLayerIndex(getXMLValue(selectedItem.getLayer()));
            selectionData.setStyleIndex(getXMLValue(selectedItem.getStyle()));
            selectionData.setFeatureTypeStyleIndex(getXMLValue(selectedItem.getFeatureTypeStyle()));
            selectionData.setRuleIndex(getXMLValue(selectedItem.getRule()));
            selectionData.setSymbolizerIndex(getXMLValue(selectedItem.getSymbolizer()));
            selectionData.setSymbolizerDetailIndex(getXMLValue(selectedItem.getSymbolizerDetail()));
            try {
                selectionData.setSelectedPanel(Class.forName(selectedItem.getExpectedPanel()));
            } catch (ClassNotFoundException e1) {
                Assert.fail("Unknown class : " + selectedItem.getExpectedPanel());
            }

            boolean result = sldEditor.selectTreeItem(selectionData);

            Assert.assertTrue("Failed to select tree item", result);
            PopulateDetailsInterface panel = sldEditor.getSymbolPanel();

            String panelClassName = panel.getClass().getName();
            Assert.assertEquals(panelClassName, selectedItem.getExpectedPanel());

            Assert.assertEquals("Check panel data present", panel.isDataPresent(), selectedItem.isEnabled());

            Class<?> panelId = null;
            try {
                panelId = Class.forName(selectedItem.getExpectedPanel());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            if (test.getFieldTests() != null) {
                for (XMLFieldTest testItem : test.getFieldTests()) {
                    if (testItem != null) {
                        if (testItem.getDisabledOrLiteralStringOrLiteralInt() != null) {
                            for (Object xmlTestValueObj : testItem.getDisabledOrLiteralStringOrLiteralInt()) {
                                if (xmlTestValueObj instanceof XMLSetMultiOptionGroup) {
                                    XMLSetMultiOptionGroup testValue = (XMLSetMultiOptionGroup) xmlTestValueObj;
                                    GroupIdEnum groupId = testValue.getMultiOptionGroupId();
                                    String outputText = "Checking multioption group : " + groupId;

                                    System.out.println(outputText);
                                    Assert.assertNotNull(outputText, groupId);

                                    MultiOptionGroup multiOptionGroup = mgr.getMultiOptionGroup(panelId,
                                            groupId);

                                    Assert.assertNotNull(panelId.getName() + "/" + groupId
                                            + " multi option group should exist", multiOptionGroup);

                                    multiOptionGroup.setOption(testValue.getOption());

                                    OptionGroup optionGroupSelected = multiOptionGroup.getSelectedOptionGroup();

                                    Assert.assertTrue(groupId + " should be set",
                                            optionGroupSelected.getId() == testValue.getOption());
                                } else if (xmlTestValueObj instanceof XMLSetGroup) {
                                    XMLSetGroup testValue = (XMLSetGroup) xmlTestValueObj;
                                    GroupIdEnum groupId = testValue.getGroupId();
                                    String outputText = "Checking group : " + groupId;

                                    System.out.println(outputText);
                                    Assert.assertNotNull(outputText, groupId);

                                    GroupConfig groupConfig = mgr.getGroup(panelId, groupId);

                                    Assert.assertNotNull(
                                            panelId.getName() + "/" + groupId + " group should exist",
                                            groupConfig);

                                    groupConfig.enable(testValue.isEnable());

                                    Assert.assertTrue(groupId + " should be set",
                                            groupConfig.isPanelEnabled() == testValue.isEnable());
                                } else {
                                    XMLFieldBase testValue = (XMLFieldBase) xmlTestValueObj;
                                    FieldId fieldId = new FieldId(testValue.getField());
                                    String outputText = "Checking : " + fieldId;
                                    System.out.println(outputText);
                                    Assert.assertNotNull(outputText, fieldId);

                                    try {
                                        Thread.sleep(100);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    FieldConfigBase fieldConfig = mgr.getData(panelId, fieldId);

                                    Assert.assertNotNull(String.format("Failed to field panel %s field %s",
                                            selectedItem.getExpectedPanel(), fieldId), fieldConfig);

                                    if (testValue instanceof XMLSetFieldLiteralBase) {
                                        XMLSetFieldLiteralInterface testInterface = (XMLSetFieldLiteralInterface) testValue;
                                        testInterface.accept(fieldConfig, fieldId);

                                        if (!((XMLSetFieldLiteralBase) testValue).isIgnoreCheck()) {
                                            String sldContentString = sldEditor.getSLDString();

                                            boolean actualResult = testOutput.testValue(sldContentString,
                                                    selectionData, testValue.getField(), testValue);

                                            Assert.assertTrue(fieldId + " should be set", actualResult);
                                        }
                                    } else if (testValue instanceof XMLSetFieldAttribute) {
                                        XMLSetFieldLiteralInterface testInterface = (XMLSetFieldLiteralInterface) testValue;
                                        testInterface.accept(fieldConfig, fieldId);

                                        String sldContentString = sldEditor.getSLDString();

                                        boolean actualResult = testOutput.testAttribute(sldContentString,
                                                selectionData, testValue.getField(),
                                                (XMLSetFieldAttribute) testValue);

                                        Assert.assertTrue(fieldId + " should be set", actualResult);
                                    } else if (testValue instanceof XMLFieldDisabled) {
                                        Assert.assertFalse(fieldId + " should be disabled",
                                                fieldConfig.isEnabled());
                                    } else {
                                        Assert.assertTrue(fieldId + " should be enabled",
                                                fieldConfig.isEnabled());
                                        Expression expression = null;

                                        if (fieldConfig.isValueOnly()) {
                                            String expectedValue = "";

                                            if (testValue instanceof XMLFieldLiteralBase) {
                                                Object literalValue = getLiteralValue(
                                                        (XMLFieldLiteralBase) testValue);
                                                expectedValue = String.valueOf(literalValue);
                                            } else if (testValue instanceof XMLFieldAttribute) {
                                                expectedValue = ((XMLFieldAttribute) testValue).getAttribute();
                                            } else if (testValue instanceof XMLFieldExpression) {
                                                expectedValue = ((XMLFieldExpression) testValue)
                                                        .getExpression();
                                            } else {
                                                Assert.fail(fieldId + " has unsupported type "
                                                        + testValue.getClass().getName());
                                            }

                                            String actualValue = fieldConfig.getStringValue();

                                            String msg = String.format("%s Expected : '%s' Actual : '%s'",
                                                    outputText, expectedValue, actualValue);

                                            boolean condition;
                                            if (comparingFilename(fieldId)) {
                                                condition = actualValue.endsWith(expectedValue);
                                            } else {
                                                condition = (expectedValue.compareTo(actualValue) == 0);
                                            }
                                            Assert.assertTrue(msg, condition);
                                        } else {
                                            if (colourFieldsList.contains(fieldId.getFieldId())) {
                                                FieldConfigColour fieldColour = (FieldConfigColour) fieldConfig;

                                                expression = fieldColour.getColourExpression();
                                            } else {
                                                expression = fieldConfig.getExpression();

                                                if (fieldId.getFieldId() == FieldIdEnum.SYMBOL_TYPE) {
                                                    String string = expression.toString();

                                                    expression = ff
                                                            .literal(string.replace(File.separatorChar, '/'));
                                                } else if (fieldId.getFieldId() == FieldIdEnum.FONT_FAMILY) {
                                                    // Handle the case where a font is not available on all operating systems
                                                    String string = expression.toString();

                                                    if (string.compareToIgnoreCase(DEFAULT_FONT) != 0) {
                                                        expression = ff.literal(getFontForOS());
                                                        System.out.println("Updated font family to test for : "
                                                                + expression.toString());
                                                    }
                                                }
                                            }
                                            if (expression != null) {
                                                if (testValue instanceof XMLFieldLiteralBase) {
                                                    Object literalValue = getLiteralValue(
                                                            (XMLFieldLiteralBase) testValue);

                                                    if (literalValue.getClass() == Double.class) {
                                                        checkLiteralValue(outputText, expression,
                                                                (Double) literalValue);
                                                    } else if (literalValue.getClass() == Integer.class) {
                                                        checkLiteralValue(outputText, expression,
                                                                (Integer) literalValue);
                                                    } else if (literalValue.getClass() == String.class) {
                                                        if (fieldId.getFieldId() == FieldIdEnum.FONT_FAMILY) {
                                                            // Handle the case where a font is not available on all operating systems
                                                            checkLiteralValue(outputText, expression,
                                                                    getFontForOS());
                                                        } else {
                                                            checkLiteralValue(outputText, expression,
                                                                    (String) literalValue);
                                                        }
                                                    }
                                                }
                                            } else {
                                                String actualValue;
                                                String expectedValue = fieldConfig.getStringValue();

                                                Object literalValue = getLiteralValue(
                                                        (XMLFieldLiteralBase) testValue);

                                                if (literalValue.getClass() == Double.class) {
                                                    actualValue = String.valueOf((Double) literalValue);
                                                } else if (literalValue.getClass() == Integer.class) {
                                                    actualValue = String.valueOf((Integer) literalValue);
                                                } else if (literalValue.getClass() == String.class) {
                                                    actualValue = (String) literalValue;
                                                } else {
                                                    actualValue = "";
                                                }

                                                String msg = String.format("%s Expected : '%s' Actual : '%s'",
                                                        outputText, expectedValue, actualValue);
                                                boolean condition = (expectedValue.compareTo(actualValue) == 0);
                                                Assert.assertTrue(msg, condition);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    JFrame frame = sldEditor.getApplicationFrame();
    frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}

From source file:com.sldeditor.test.unit.tool.vector.VectorToolTest.java

@Test
public void testVectorToolDBDataSource() {
    TestMissingSLDAttributes testAttribute = new TestMissingSLDAttributes();
    List<CheckAttributeInterface> checkList = new ArrayList<CheckAttributeInterface>();
    checkList.add(testAttribute);/*www.j  a v  a 2s  . c  o  m*/
    CheckAttributeFactory.setOverideCheckList(checkList);

    String testsldfile = "/polygon/sld/polygon_polygonwithdefaultlabel.sld";
    TestSLDEditor testSLDEditor = null;
    try {
        testSLDEditor = TestSLDEditor.createAndShowGUI2(null, null, true, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    RenderPanelImpl.setUnderTest(true);
    InputStream inputStream = VectorToolTest.class.getResourceAsStream(testsldfile);

    if (inputStream == null) {
        Assert.assertNotNull("Failed to find sld test file : " + testsldfile, inputStream);
    } else {
        File f = null;
        try {
            f = stream2file(inputStream);
            try {
                testSLDEditor.openFile(f.toURI().toURL());
            } catch (NullPointerException nullException) {
                nullException.printStackTrace();
                StackTraceElement[] stackTraceElements = nullException.getStackTrace();

                System.out.println(stackTraceElements[0].getMethodName());
            }

            f.delete();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    // Fields extracted from the SLD file
    DataSourceInterface dataSource = DataSourceFactory.createDataSource(null);
    Collection<PropertyDescriptor> propertyList = dataSource.getPropertyDescriptorList();

    assertEquals(2, propertyList.size());
    Map<String, PropertyDescriptor> map = new HashMap<String, PropertyDescriptor>();

    for (PropertyDescriptor property : propertyList) {
        map.put(property.getName().getLocalPart(), property);
    }
    AttributeDescriptor name = (AttributeDescriptor) map.get("name");
    assertNotNull(name);
    GeometryDescriptor geometry = (GeometryDescriptor) map.get("geom");
    assertNotNull(geometry);

    File tempFolder = Files.createTempDir();
    TestVectorTool vectorTool = new TestVectorTool(testSLDEditor);
    try {
        InputStream gpkgInputStream = VectorToolTest.class
                .getResourceAsStream("/test/sld_cookbook_polygon.gpkg");

        final File gpkgFile = new File(tempFolder, "sld_cookbook_polygon.gpkg");
        try (FileOutputStream out = new FileOutputStream(gpkgFile)) {
            IOUtils.copy(gpkgInputStream, out);
        }

        DatabaseConnection databaseConnection = DatabaseConnectionFactory
                .getConnection(gpkgFile.getAbsolutePath());
        DatabaseFeatureClassNode dbFCTreeNode = new DatabaseFeatureClassNode(null, databaseConnection,
                "sld_cookbook_polygon");

        DatabaseConnectionManager.getInstance().addNewConnection(null, databaseConnection);
        vectorTool.testSetDataSource(dbFCTreeNode);

        dataSource = DataSourceFactory.createDataSource(null);
        propertyList = dataSource.getPropertyDescriptorList();

        assertEquals(3, propertyList.size());
        map.clear();

        for (PropertyDescriptor property : propertyList) {
            map.put(property.getName().getLocalPart(), property);
        }
        name = (AttributeDescriptor) map.get("name");
        assertNotNull(name);
        geometry = (GeometryDescriptor) map.get("geometry");
        assertNotNull(geometry);
        AttributeDescriptor pop = (AttributeDescriptor) map.get("pop");
        assertNotNull(pop);

        // Create SLD from geopackage layer
        vectorTool.testImportFeatureClass(dbFCTreeNode);
        dataSource = DataSourceFactory.createDataSource(null);
        propertyList = dataSource.getPropertyDescriptorList();

        assertEquals(3, propertyList.size());
        map.clear();

        for (PropertyDescriptor property : propertyList) {
            map.put(property.getName().getLocalPart(), property);
        }
        name = (AttributeDescriptor) map.get("name");
        assertNotNull(name);
        geometry = (GeometryDescriptor) map.get("geometry");
        assertNotNull(geometry);
        pop = (AttributeDescriptor) map.get("pop");
        assertNotNull(pop);

        // Release locks
        dataSource.reset();
    } catch (IOException e) {
        e.printStackTrace();
        fail();
    }

    // Tidy up so the remaining unit tests are ok
    JFrame frame = testSLDEditor.getApplicationFrame();
    frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
    testSLDEditor = null;
    clearDown();

    // Delete the shape files we extracted
    purgeDirectory(tempFolder);
}

From source file:com.sldeditor.test.unit.tool.vector.VectorToolTest.java

/**
 * Test method for {@link com.sldeditor.tool.vector.VectorTool#VectorTool(com.sldeditor.common.SLDEditorInterface)}.
 *//*from   w  w  w  .j  av a2  s. c om*/
@Test
public void testVectorToolFileDataSource() {
    TestMissingSLDAttributes testAttribute = new TestMissingSLDAttributes();
    List<CheckAttributeInterface> checkList = new ArrayList<CheckAttributeInterface>();
    checkList.add(testAttribute);
    CheckAttributeFactory.setOverideCheckList(checkList);

    String testsldfile = "/polygon/sld/polygon_polygonwithdefaultlabel.sld";
    TestSLDEditor testSLDEditor = null;
    try {
        testSLDEditor = TestSLDEditor.createAndShowGUI2(null, null, true, null);
    } catch (Exception e) {
        e.printStackTrace();
    }

    InputStream inputStream = VectorToolTest.class.getResourceAsStream(testsldfile);

    if (inputStream == null) {
        Assert.assertNotNull("Failed to find sld test file : " + testsldfile, inputStream);
    } else {
        File f = null;
        try {
            f = stream2file(inputStream);
            try {
                testSLDEditor.openFile(f.toURI().toURL());
            } catch (NullPointerException nullException) {
                nullException.printStackTrace();
                StackTraceElement[] stackTraceElements = nullException.getStackTrace();

                System.out.println(stackTraceElements[0].getMethodName());
            }

            f.delete();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    // Fields extracted from the SLD file
    DataSourceInterface dataSource = DataSourceFactory.createDataSource(null);
    Collection<PropertyDescriptor> propertyList = dataSource.getPropertyDescriptorList();

    assertEquals(2, propertyList.size());
    Map<String, PropertyDescriptor> map = new HashMap<String, PropertyDescriptor>();

    for (PropertyDescriptor property : propertyList) {
        map.put(property.getName().getLocalPart(), property);
    }
    AttributeDescriptor name = (AttributeDescriptor) map.get("name");
    assertNotNull(name);
    GeometryDescriptor geometry = (GeometryDescriptor) map.get("geom");
    assertNotNull(geometry);

    File tempFolder = Files.createTempDir();
    TestVectorTool vectorTool = new TestVectorTool(testSLDEditor);
    try {
        // Set a shape file as a data source - that matches the SLD
        File matchingShpFile = extractShapeFile(tempFolder, "/test/sld_cookbook_polygon.zip");

        FileTreeNode fileTreeNode = new FileTreeNode(matchingShpFile.getParentFile(),
                matchingShpFile.getName());

        vectorTool.testSetDataSource(fileTreeNode);

        dataSource = DataSourceFactory.createDataSource(null);
        propertyList = dataSource.getPropertyDescriptorList();

        assertEquals(3, propertyList.size());
        map.clear();

        for (PropertyDescriptor property : propertyList) {
            map.put(property.getName().getLocalPart(), property);
        }
        name = (AttributeDescriptor) map.get("name");
        assertNotNull(name);
        geometry = (GeometryDescriptor) map.get("the_geom");
        assertNotNull(geometry);
        AttributeDescriptor pop = (AttributeDescriptor) map.get("pop");
        assertNotNull(pop);

        // Set a shape file as a data source - that does not match the SLD
        File nonMatchingShpFile = extractShapeFile(tempFolder, "/test/states.zip");

        FileTreeNode fileTreeNode2 = new FileTreeNode(nonMatchingShpFile.getParentFile(),
                nonMatchingShpFile.getName());

        vectorTool.testSetDataSource(fileTreeNode2);

        dataSource = DataSourceFactory.createDataSource(null);
        propertyList = dataSource.getPropertyDescriptorList();

        assertEquals(23, propertyList.size());
        map.clear();

        for (PropertyDescriptor property : propertyList) {
            map.put(property.getName().getLocalPart(), property);
        }
        name = (AttributeDescriptor) map.get("name");
        assertNull(name);
        geometry = (GeometryDescriptor) map.get("the_geom");
        assertNotNull(geometry);
        pop = (AttributeDescriptor) map.get("pop");
        assertNull(pop);

        assertEquals(1, testAttribute.getMissingFieldList().size());
        assertEquals("name", testAttribute.getMissingFieldList().get(0));

        // Create SLD from shape file
        vectorTool.testImportFile(fileTreeNode);
        dataSource = DataSourceFactory.createDataSource(null);
        propertyList = dataSource.getPropertyDescriptorList();

        assertEquals(3, propertyList.size());
        map.clear();

        for (PropertyDescriptor property : propertyList) {
            map.put(property.getName().getLocalPart(), property);
        }
        name = (AttributeDescriptor) map.get("name");
        assertNotNull(name);
        geometry = (GeometryDescriptor) map.get("the_geom");
        assertNotNull(geometry);
        pop = (AttributeDescriptor) map.get("pop");
        assertNotNull(pop);

        // Release locks
        dataSource.reset();
    } catch (IOException e) {
        e.printStackTrace();
        fail();
    }

    // Tidy up so the remaining unit tests are ok
    JFrame frame = testSLDEditor.getApplicationFrame();
    frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
    testSLDEditor = null;
    clearDown();

    // Delete the shape files we extracted
    purgeDirectory(tempFolder);
}