1

The structure of our project strongly separates the three different parts:

  • core - data classes, POJOs, sometimes aggregated with annotations (e.g. for XML/JSON serialization)
  • plugin - plugin for a large enterprise application that is based on Swing
  • ui - additional UI components in JavaFX, does only use the core data classes

Now we want to implement drag-and-drop transfers between the UI in JavaFX and the plugin in Swing. Thanks to a question on StackOverflow, there are no more technical issues in the implementation.

However, I do not know where to put the meta information on how the data structures should be transfered through the operating system. For Swing, I need to define (or reuse) a DataFlavor, in JavaFX a DataFormat is required. Both of these classes are based on a simple string that should / must contain a MIME type, e.g. text/plain or application/x-some-custom-data. But where should this kind of information be defined?


  1. Idea: Store the DataFormat / DataFlavor directly in the class that uses it, e.g. a controller:

    class MyController {
    
        public static final DataFormat FORMAT = new DataFormat("x-some-custom-data");
    
        // ...
            ClipboardContent content = new ClipboardContent();
            content.put(FORMAT, myDataObject);
        // ...
    }
    
    // equivalent for Swing part
    

    Disadvantage: The same information needs to be stored at different places.


  1. Idea: Store the DataFormat / DataFlavor directly in the class that is transfered:

    class MyDataClass {
    
        public static final DataFormat FORMAT = new DataFormat("x-some-custom-data");
    
        public static final DataFlavor FLAVOR = ...
    
        // some fields / getters / setters ...
    
    }
    
    // ...
        ClipboardContent content = new ClipboardContent();
        content.put(MyDataClass.FORMAT, myDataObject);
    // ...
    
    // equivalent for Swing part
    

    Disadvantage: Framework-specific classes are used inside pure data classes (even if they belong to the standard Java framework).


  1. Idea: Store the information at the class that is transfered, but create the framework specific classes (DataFormat / DataFlavor) somewhere else:

    @TransferAs("application/x-some-custom-data")
    class MyDataClass {
    
    }
    
    // ...
        DataFormat format = new DataFormat(myDataObject.getClass().getAnnotation(TransferAs.class).value());
    // ...
    
    // equivalent for Swing part
    

    Disadvantage: Requires a custom non-standard annotation that may be confusing.


Which idea would you consider best-practice? Is there another approach I'm totally missing right now?

  • Idea 3 does not require annotations. Just create a public constant and use it in a thin UI wrapper. – Basilevs Aug 23 '18 at 3:46
  • Of course, that would be possible and would eliminate the disadvantage. However, I would consider this kind of information meta data and this is what annotations are for. Also, this would provide me some consistent way to access the information, using a constant I would need to follow some convention, because Java does not allow some kind of contract (interface, inheritance) for static methods and fields. – Lukas Körfer Aug 24 '18 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.