Fáze 3
Tipy

-        Pro zapínání/vypínání uživateského nástroje se hodí komponenta JToggleButton.

-        Pokud máme speciální třídu pro nějaký „vnitřní“ panel, který uživateli umožňuje nějakou akci, ale většina logiky programu je ve „vnější“ třídě (např frame), musí se tento vnější frame o dané události „dozvědět“. Pro jednoduché případy stačí v konstruktoru předat vnitřnímu panelu odkaz na vnější objekt, aby mohl ve vhodné chvíli volat určitou metodu vnějšího objektu. Např:

    public KresliciPanel(MujFrame mujFrame)

    {

        this.mujFrame = mujFrame;
    }
   
    ...
    mujFrame.zmenaPresKresliciPanel();
  

-        Když se v kódu automaticky mění obsah textového pole, může být vhodné dočasně deaktivovat jeho listener (ten bývá určený na poslouchání změn od uživatele). Lze to řešit třeba booleovskou proměnnou:

Kód upravující obsah textového pole:

listenXmlChanges = false; // deaktivuj listener

xmlTextArea.setText(novyText); // nastav text

listenXmlChanges = true; // aktivuj listener

Kód v listeneru:

if (listenXmlChanges)

{

   // reaguj na uživatelskou změnu v textu

}

 

Tipy k JAXB

Závislosti

Příklad fungujícícho nastavení:

<dependencies>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>javax.activation-api</artifactId>
        <version>1.2.0</version>
    </dependency>
</dependencies>

 

V některých případech (které se projevují zejména chybou java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass) je potřeba použít novější verzi:

                <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>javax.activation-api</artifactId>
            <version>1.2.0</version>
        </dependency>

 

Tipy pro třídy jako elipsa, obdélník, úsečka

-         @XmlRootElement(name="ellipse")
@XmlAccessorType(XmlAccessType.FIELD)
public class Elipsa

-         @XmlAttribute
private float cx;

-         @XmlAttribute(name="stroke-width")
private float strokeWidth;

-         @XmlAttribute
private String fill = "none";

-         Bezparametrový konstruktor

Tipy pro třídy jako obrázek

@XmlRootElement(name = "svg")
@XmlSeeAlso({Obdelnik.class, Elipsa.class, Usecka.class})
public class Obr
{
    public Obr()
    {
    }

    private List<Shape> shapes = new ArrayList<>();

    @XmlAttribute()
    private  String viewBox = "0 0 1000 1000";

    @XmlElementWrapper(name = "g")
    @XmlAnyElement(lax=true)
    public List<Shape> getShapes()
    {
        return shapes;
    }
}

Převod na XML a zpět, kód k dispozici

public class XmlUtils

{

    public static String getXml(Obr image)

    {

        JAXBContext ctx = null;

        try

        {

            ctx = JAXBContext.newInstance(Obr.class);

 

            Marshaller marshaller = ctx.createMarshaller();

            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            StringWriter sw = new StringWriter();

            marshaller.marshal(image, sw);

            String xmlString = sw.toString();

            return xmlString;

        } catch (JAXBException e)

        {

            throw new RuntimeException(e);

        }

    }

 

    public static Obr getImage(String xml)

    {

        try

        {

            JAXBContext ctx = JAXBContext.newInstance(Obr.class);

            Unmarshaller unmarshaller = ctx.createUnmarshaller();

            StringReader sr = new StringReader(xml);

            Obr svgImage = (Obr) unmarshaller.unmarshal(sr);

            return svgImage;

        } catch (JAXBException e)

        {

            throw new RuntimeException(e);

        }

 

    }

}