3535import org .jfree .data .xy .XYSeries ;
3636import org .jfree .data .xy .XYSeriesCollection ;
3737
38+ import calib .MapCal ;
39+ import calib .Variable ;
3840import gui .Ihm ;
41+ import gui .MapView ;
3942import log .Formula ;
4043import log .Measure ;
4144
@@ -123,11 +126,36 @@ public boolean canImport(TransferSupport support) {
123126 @ Override
124127 public boolean importData (TransferSupport supp ) {
125128 Transferable t = supp .getTransferable ();
129+
126130 String data = "" ;
127131 try {
128132 data = (String ) t .getTransferData (DataFlavor .stringFlavor );
129133
130- if (!data .equals (Toolkit .getDefaultToolkit ().getSystemClipboard ().getContents (null ).getTransferData (DataFlavor .stringFlavor ))) {
134+ MapView mapView = ((Ihm ) DialNewFormula .this .getParent ()).getMapView ();
135+
136+ Variable var = null ;
137+
138+ if (mapView != null && mapView .isVisible ()) {
139+
140+ MapCal calib = mapView .findSelectedCal ();
141+
142+ if (calib != null && calib .isUsedByFormula ()) {
143+ var = calib .getVariable (data );
144+ } else {
145+ int res = JOptionPane .showConfirmDialog (null ,
146+ "La fichier \" " + calib + "\" " + " n'a pas \u00e9 t\u00e9 défini comme r\u00e9 f\u00e9 rence, le faire?" , "INFO" ,
147+ JOptionPane .YES_NO_OPTION );
148+ if (res == JOptionPane .YES_OPTION ) {
149+ mapView .setCalForFormula (calib .getName ());
150+ var = calib .getVariable (data );
151+ } else {
152+ return false ;
153+ }
154+ }
155+ }
156+
157+ if (!data .equals (Toolkit .getDefaultToolkit ().getSystemClipboard ().getContents (null ).getTransferData (DataFlavor .stringFlavor ))
158+ && var == null ) {
131159 data = "#" + data + "#" ;
132160 }
133161
@@ -139,7 +167,13 @@ public boolean importData(TransferSupport supp) {
139167 e .printStackTrace ();
140168 }
141169
142- formulaText .insert (data , formulaText .getCaretPosition ());
170+ int caretPosition = formulaText .getCaretPosition ();
171+
172+ if (checkAccolades (caretPosition )) {
173+ formulaText .insert (data .replace ("#" , "" ), formulaText .getCaretPosition ());
174+ } else {
175+ formulaText .insert (data , formulaText .getCaretPosition ());
176+ }
143177
144178 return true ;
145179 }
@@ -191,7 +225,7 @@ public boolean importData(TransferSupport supp) {
191225 @ Override
192226 public void actionPerformed (ActionEvent arg0 ) {
193227
194- Formula newFormula = new Formula (txtName .getText (), txtUnit .getText (), formulaText .getText (), ihm .getLog ());
228+ Formula newFormula = new Formula (txtName .getText (), txtUnit .getText (), formulaText .getText (), ihm .getLog (), ihm . getSelectedCal () );
195229 if (ihm .getLog () != null && newFormula .isValid ()) {
196230 XYSeriesCollection dataset = (XYSeriesCollection ) chartPanel .getChart ().getXYPlot ().getDataset ();
197231 XYSeries serie = dataset .getSeries (0 );
@@ -224,7 +258,7 @@ public void actionPerformed(ActionEvent arg0) {
224258 public void actionPerformed (ActionEvent arg0 ) {
225259
226260 if (formula == null ) {
227- Formula newFormula = new Formula (txtName .getText (), txtUnit .getText (), formulaText .getText (), ihm .getLog ());
261+ Formula newFormula = new Formula (txtName .getText (), txtUnit .getText (), formulaText .getText (), ihm .getLog (), ihm . getSelectedCal () );
228262 if (!ihm .getListFormula ().contains (newFormula )) {
229263 if (newFormula .isValid ()) {
230264 ihm .addMeasure (newFormula );
@@ -237,7 +271,7 @@ public void actionPerformed(ActionEvent arg0) {
237271 formula .setName (txtName .getText ());
238272 formula .setUnit (txtUnit .getText ());
239273 formula .setExpression (formulaText .getText ());
240- formula .calculate (ihm .getLog ());
274+ formula .calculate (ihm .getLog (), ihm . getSelectedCal () );
241275 dispose ();
242276 }
243277
@@ -273,12 +307,42 @@ public void actionPerformed(ActionEvent arg0) {
273307 setVisible (true );
274308 }
275309
310+ private boolean checkAccolades (int caretPosition ) {
311+
312+ final char accoladeOuvrante = '{' ;
313+ final char accoladeFermante = '}' ;
314+
315+ final String text = formulaText .getText ();
316+ final int textLength = text .length ();
317+
318+ if (text .isEmpty () || textLength == caretPosition || caretPosition == 0 ) {
319+ return false ;
320+ }
321+
322+ int idx1 = caretPosition ;
323+ int idx2 = caretPosition ;
324+
325+ do {
326+ if (text .charAt (idx1 --) == accoladeOuvrante ) {
327+ break ;
328+ }
329+ } while (idx1 == 0 );
330+
331+ do {
332+ if (text .charAt (idx2 ++) == accoladeFermante ) {
333+ break ;
334+ }
335+ } while (idx2 == textLength - 1 );
336+
337+ return idx2 > idx1 && idx1 != 0 && idx2 != textLength - 1 ;
338+ }
339+
276340 private final JPanel createPad () {
277- String [] tab_string = new String [] { "^" , "sqrt()" , "cos()" , "sin()" , "tan()" , "+" , "-" , "*" , "/" , "." , "0" , "1" , "2" , "3" , "4" , "5" , "6 " ,
278- "7" , "8" , "9" };
341+ String [] tab_string = new String [] { "SCALAIRE{}" , "TABLE1D{,}" , "TABLE2D{,,}" , " ^" , "sqrt()" , "cos()" , "sin()" , "tan()" , "+" , "-" , "*" , "/" ,
342+ "." , "0" , "1" , "2" , "3" , "4" , "5" , "6" , " 7" , "8" , "9" };
279343 JButton [] tab_button = new JButton [tab_string .length ];
280344
281- JPanel pad = new JPanel (new GridLayout (4 , 5 ));
345+ JPanel pad = new JPanel (new GridLayout (6 , 5 ));
282346
283347 for (int i = 0 ; i < tab_string .length ; i ++) {
284348 tab_button [i ] = new JButton (tab_string [i ]);
@@ -296,6 +360,13 @@ public void actionPerformed(ActionEvent e) {
296360 String str = ((JButton ) e .getSource ()).getText ();
297361 formulaText .insert (str , formulaText .getCaretPosition ());
298362
363+ int caretPosition = formulaText .getCaretPosition ();
364+
365+ if (str .indexOf ('(' ) > -1 || str .indexOf ('{' ) > -1 ) {
366+ caretPosition --;
367+ }
368+ formulaText .requestFocusInWindow ();
369+ formulaText .setCaretPosition (caretPosition );
299370 }
300371
301372 }
0 commit comments