Copy & paste

De Wiki
Aller à : navigation, rechercher

GENIUS proposes an evolved "Copy & Paste" functionnality meaning its possible to store in the clipboard a complete widget rather than the values one by one. Let us see how to do it progressively on the following example ...

Creation of a widget to be copied

First, let us create a widget extending from a GPanel implementing the GListener and GReadWrite interfaces (it is mandatory) and owning some data (here two reals, one boolean and a string).

public class TestForCopyAndPaste extends GPanel implements GListener, GReadWrite {
 
  private GLabel lab;
  private GEntryReal val1;
  private GEntryReal val2;
  private GEntryString vals;
  private GCheckBox cb;
 
  public TestForCopyAndPaste () {
 
    lab = new GLabel("Label ...");
    val1 = new GEntryReal("Val1", 0.);
    val2 = new GEntryReal("Val2", 0.);
    cb = new GCheckBox("Display String");
    vals = new GEntryString("String", "");
 
  }
 
  public void generic() throws GException {
    put(lab);
    put(val1);
    put(val2);
    put(cb);
    if ( cb.isSelected() ) put(vals);
  }
 
  public void display() throws GException { generic(); }
 
  public void read() throws GException { generic(); }
  public void write() throws GException { generic(); }
 
  public void before(GEvent arg0) throws GException { }
  public void after(GEvent arg0) throws GException { }
 
}

Preparing the Copy & Paste functionnality

First, we are going to link the Copy & Paste function to the label: a contextual menu will appear after clicking on the right button of the mouse when it will be above this label. To do that, we will use the attachCopyPasteMenu() method. Two solutions are available:

  • to do it inside the constructor, meaning it will allways be active
  • to redefine the attachCopyPasteMenu() method for this widget and it will be up to the user of the widget who will decide if the functionnality will be active or not

On the example, we will choose the second option ...

  public void attachCopyPasteMenu (String title, CopyPasteOperation... operationList) {
    lab.attachCopyPasteMenu(title, operationList);
  }

Then, we will have to manage this special event in the after() method of our widget : calling the handleCopyPasteEvent() method will execute the action (Copy, Paste, ...).

  public void after(GEvent arg0) throws GException {
 
    boolean cpEvent = handleCopyPasteEvent(lab, arg0);
 
    if ( !cpEvent ) { System.out.println("Nothing to do with Copy&Paste ..."); }
    else            { System.out.println("Copy&Paste action ..."); }
 
  }

Testing it

Now we are able to call for this widget via an executable method:

  public static void main(String[] args) throws GException {
 
  TestForCopyAndPaste test = new TestForCopyAndPaste();
  // We activate the Copy&Paste functionnality ...
  // For the moment the argument "title" is not used ...
  test.attachCopyPasteMenu(null, CopyPasteOperation.Copy, CopyPasteOperation.Paste);
 
  GFrame frame = new GFrame("TestForCopyAndPasteWidget", test);
  frame.getJFrame().setPreferredSize(new Dimension(350, 200));
  frame.display();
 
}

See on the screen copies the result of such code ...

First display ...

CopyPaste1.jpg

User updating data ...

CopyPaste2.jpg

Copy & Paste these data ...

CopyPaste3.jpg

Reloading the widget ...

CopyPaste1.jpg

Paste data ...

CopyPaste4.jpg

We restored them !

CopyPaste2.jpg

Another way to do it

Rather than to use the label (or anything else) to attach the contextual menu, it is also possible to get this menu everywhere on your widget. To do it, you will just have to change these lines in the attachCopyPasteMenu() and after() methods:

  public void attachCopyPasteMenu (String title, CopyPasteOperation... operationList) {
    //lab.attachCopyPasteMenu(title, operationList);
    super.attachCopyPasteMenu(title, operationList);
  }
 
  public void after(GEvent arg0) throws GException {
 
    //boolean cpEvent = handleCopyPasteEvent(lab, arg0);
    boolean cpEvent = handleCopyPasteEvent(this, arg0);
 
}

CopyPaste5.jpg

But as sometimes, you may need to have several different "Copy & Paste" in the same GPanel : for example, one for the complete set of data and other ones for each data (or part of the data), it is important to know what you are going to copy or paste ! And it is precisely the role of the "title" argument : by given a not void character string, it will appear on the contextual menu.

  test.attachCopyPasteMenu("All the data", CopyPasteOperation.Copy, CopyPasteOperation.Paste);

CopyPaste51.jpg

More than Copy & Paste !

It is also possible to Import or Export these data into a file ans since V1.10 to reset the data. To do it, you will just have to add these operations when calling the attachCopyPasteMenu():

   test.attachCopyPasteMenu(null, CopyPasteOperation.Copy, CopyPasteOperation.Paste,
                            CopyPasteOperation.Import, CopyPasteOperation.Export, CopyPasteOperation.Reset);

Or, in order to have the whole possibilities:

   test.attachCopyPasteMenu(null, CopyPasteOperation.All);

CopyPaste6.jpg

Then a File manager will appear where we will have the possibility to choose our file.


Return to the introduction ↑ Go to the next page →