« Copy & paste » : différence entre les versions
(Page créée avec « TBW ... Return to the introduction ↑ Go to the next page → ») |
Aucun résumé des modifications |
||
Ligne 1 : | Ligne 1 : | ||
<font color=#556B2F>'''GENIUS'''</font> 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 [{{PathCurrentJavaDoc}}/fr/cnes/genius/lowLevel/GPanel.html GPanel] implementing the [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GListener.html GListener] and [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GReadWrite.html GReadWrite] interfaces (<u>it is mandatory</u>) and owning some data (here two reals, one boolean and a string). | |||
<syntaxhighlight lang="java"> | |||
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 { } | |||
} | |||
</syntaxhighlight> | |||
== 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 <font color=#4169E1>attachCopyPasteMenu()</font> method. Two solutions are available: | |||
* to do it inside the constructor, meaning it will allways be active | |||
* to redefine the <font color=#4169E1>attachCopyPasteMenu()</font> 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 seconde option ... | |||
<syntaxhighlight lang="java"> | |||
public void attachCopyPasteMenu (String title, CopyPasteOperation... operationList) { | |||
lab.attachCopyPasteMenu(title, operationList); | |||
} | |||
</syntaxhighlight> | |||
Then, we will have to manage this special event in the <font color=#4169E1>after()</font> method of our widget : calling the <font color=#4169E1>handleCopyPasteEvent()</font> method will execute the action (Copy, Paste, ...). | |||
<syntaxhighlight lang="java"> | |||
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 ..."); } | |||
} | |||
</syntaxhighlight> | |||
== Testing it == | |||
Now we are able to call for this widget via an executable method: | |||
<syntaxhighlight lang="java"> | |||
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(); | |||
} | |||
</syntaxhighlight> | |||
See on the screen copies the result of such code ... | |||
First display ... [[File:CopyPaste1.jpg]] | |||
User updating data ... [[File:CopyPaste2.jpg]] | |||
Copy & Paste these data ... [[File:CopyPaste3.jpg]] | |||
Reloading the widget ... [[File:CopyPaste1.jpg]] | |||
Paste data ... [[File:CopyPaste4.jpg]] | |||
We restored them ! [[File: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 <font color=#4169E1>attachCopyPasteMenu()</font> and <font color=#4169E1>after()</font> methods: | |||
<syntaxhighlight lang="java"> | |||
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); | |||
} | |||
</syntaxhighlight> | |||
[[File:CopyPaste5.jpg]] | |||
But as sometimes, you may need to have several different "**Copy & Paste**" in the same [{{PathCurrentJavaDoc}}/fr/cnes/genius/lowLevel/GPanel.html 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. | |||
<syntaxhighlight lang="java"> | |||
test.attachCopyPasteMenu("All the data", CopyPasteOperation.Copy, CopyPasteOperation.Paste); | |||
</syntaxhighlight> | |||
[[File:CopyPaste51.jpg]] | |||
== More than Copy & Paste ! == | |||
It is also possible to Import or Export these data into a file. To do it, you will just have to add these operations when calling the <font color=#4169E1>attachCopyPasteMenu()</font>: | |||
<syntaxhighlight lang="java"> | |||
test.attachCopyPasteMenu(null, CopyPasteOperation.Copy, CopyPasteOperation.Paste, | |||
CopyPasteOperation.Import, CopyPasteOperation.Export); | |||
</syntaxhighlight> | |||
Or, in order to have the whole possibilities: | |||
<syntaxhighlight lang="java"> | |||
test.attachCopyPasteMenu(null, CopyPasteOperation.All); | |||
</syntaxhighlight> | |||
[[File:CopyPaste6.jpg]] | |||
Then a File manager will appear where we will have the possibility to choose our file. | |||
[[WELCOME_TO_THE_GENIUS_WIKI|Return to the introduction ↑]] | [[WELCOME_TO_THE_GENIUS_WIKI|Return to the introduction ↑]] | ||
[[Plots|Go to the next page →]] | [[Plots|Go to the next page →]] |
Version du 5 mai 2017 à 12:14
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 seconde 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 ...
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);
}
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);
More than Copy & Paste !
It is also possible to Import or Export these data into a file. 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);
Or, in order to have the whole possibilities:
test.attachCopyPasteMenu(null, CopyPasteOperation.All);
Then a File manager will appear where we will have the possibility to choose our file.