1

GUI example


I would like to introduce the GUI similar to the previous example.

  • On the left side, there are a collection of paragraphs (instances of the Paragraph class) that contain a collection of words (Strings). As you can see – in the text, there are highlighted text fragments (green, orange, blue).
  • The text fragment is subcollection of words from concrete paragraph.
  • On the right side, there are a collection of facts (instances of the Fact class).
  • Each fact is associated with at least one text fragment (there may be more – e.g. Fact #2).

Code fragments:

public class Paragraph {
    private List<String> words;
    …
}

public class Fact {
    private String text;
    …
}

I wish that this will be an interactive GUI (user can click on the text fragment => e.g. corresponding fact will be pop-uped / user can click on the fact => e.g. only corresponding text fragments stay highlighted).

Due to this, it is necessary that each text fragment knows corresponding fact and vice versa.

How to achieve the linkage between the text fragments and the corresponding fact in Java and what are appropriate data-structures?

I mean the data model of this linkage, not the GUI implementation. I suppose the word count is much higher than the number of text fragments.

David
  • 527
  • 1
  • 8
  • 21
  • http://stackoverflow.com/questions/9783020/bidirectional-map – PM 77-1 Dec 14 '16 at 22:07
  • @PM77-1 Yes, something like the bidirectional map I will need. Actually, I have problem with the proposal of datastructure for **text fragments**. How to handle this information? I thinking of special **class** for each String **word**, but the most part of words aren't part of some text fragment. – David Dec 14 '16 at 22:12
  • @user3437460 Yes, if you mean reference back to **fact** from concrete **text fragment**. – David Dec 14 '16 at 22:15
  • Can one paragraph relate to multiple facts? – user3437460 Dec 14 '16 at 22:17
  • @user3437460 Yes, but the relation is between the concrete **text fragment = group of words** (not whole paragraph) and the **fact**. One paragraph can contain multiple **text fragments.** – David Dec 14 '16 at 22:21

1 Answers1

1

Assuming 1 fact can have multiple paragraphs, but 1 paragraph can only belongs to 1 fact. I will implement the classes as something like:

class Fact{

    private ArrayList<ParagraphText> paraTexts;
    private String text;        
    //any other attributes

    public Fact(){
        //initialization..
    }

    public void addParaText(ParagraphText p){
        paraTexts.add(p);
        p.linkWithFact(this);  //remember which fact p belongs to
    }

    public void removeParaText(ParagraphText p){
        if(paraTexts.contains(p)){
            p.unlinkFact();
            paraTexts.remove(p);
        }
    }
}

Whenever you add a paragraph's text to the fact, the paragraph's text itself remembers which fact it belongs to.

class ParagraphText{

    private int startPos;
    private int endPos;
    private Fact fact;
    //any other attributes

    public ParagraphText(int startPos, int endPos){
        this.startPos = startPos;
        this.endPos = endPos;
        //any other initializations
    }

    public void linkWithFact(Fact fact){
        this.fact = fact;
    }

    public void unlinkFact(){
        this.fact = null;
    }
}
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • @David let me know whether it helps. – user3437460 Dec 14 '16 at 22:36
  • Thank you, but I think that we have a different view of the **paragraph**. The **paragraph** in this context is only something like the wrapper of collection of **words**. The **fact** is not related to the entire **paragraph**, but is related only to the some words of paragraph. The *Fact #1* from my exmple is related to the **7. and 8. word** from **the first paragraph**. Sorry, maybe it is not clearly from my intro description. – David Dec 14 '16 at 22:38
  • @David I tried to use shorter names for readability. It does not mean that it is the entire paragraph, else I would not have included the start and end positions. I have edited the naming for clarity. – user3437460 Dec 14 '16 at 22:42