Salut à tous,

ça fait quelques jours que je planche sur mon problème et pour l'instant je n'ai pas de solution. Même après maintes recherches sur le net, je ne trouve pas la solution à mon problème, c'est peut-être un problème de conceptualisation ?

J'ai donc développé une application : un mini SIG pour simplifier. Cette application comme tout SIG permet de surfer sur la carte géographique en mémoire (c'est PanelMaps qui gère ça). Au bout d'un moment, je souhaite avoir la possibilité de sélectionner un type d'objet (carte, polygone, légende, etc ...) via un clic souris et que le résultat soit retourné(PanelChoices) à la procédure appelante.

Je donne un exemple : J'ai deux cartes qui sont affichées et je voudrais en sauvegarder une. PanelChoices me propose de choisir la carte à enregistrer soit dans une liste, soit par clic. Je décide donc de la sélectionner par un clic souris. Mais c'est la que mon problème intervient. Comment forcer PanelChoices à attendre un clic sur PanelMaps pour que PanelChoices renvoie après à la procédure de sauvegarde, l'identifiant de la carte à sauvegarder(BlockDatas ici).

J'ai essayé quelques méthodes comme une boucle while avec un sleep, j'ai réfléchi à une méthode via un thread.wait() mais j'ai du mal à voir comment mettre ça en place, j'ai regardé du côté des mutex(reentrantlock) mais là aussi je n'ai rien trouvé. J'ai donc besoin d'un petit coup de main SVP parce que je bloque méchamment.

Petite remarque : Je sais que le code n'est pas propre mais pour l'instant je me concentre les fonctionnalités, je ferais du nettoyage un peu plus tard.

Merci d'avance

Code de PanelMaps
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
 
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Dimension;
 
public class PanelMaps extends JPanel {
 
 public static int panelHeight;
 public static int panelWidth;
 public static int Xc, Yc, Xc_init, Yc_init, scale, scale_init;
 public static int Xmin, Xmax, Ymin, Ymax;
 public static final int PANELMAPS_CLIC_MODE = 0;
 public static final int SELECT_BY_CLIC_MODE = 1;
 public static int clicMode = PANELMAPS_CLIC_MODE;
 private static int mouseX = 0;
 private static int mouseY = 0;
 private static final long serialVersionUID = 1L;
 private static boolean popupMenuWasVisible = false;
 private static PanelPopupMenu panelMenu = new PanelPopupMenu();
 private static int currentFont = Accessibility.currentFont;
 public static KeyAdapter ka = new KeyAdapter() {
  public void keyPressed(KeyEvent e){
   if (clicMode == PANELMAPS_CLIC_MODE) {
    if (e.isControlDown()) {
     int k = e.getKeyCode();
     switch (k) {
      case Datas.HOTKEY_GOTO:
       MainWindow.hides(Datas.FOCUS_PANELMAPS);
       PanelGoto.initialize();
       break;
      case Datas.HOTKEY_CAPTION:
       MainWindow.hides(Datas.FOCUS_PANELMAPS);
       PanelCaption.toggle();
       break;
      case Datas.HOTKEY_GETOUT:
       MainWindow.hides(Datas.FOCUS_PANELMAPS);
       PanelGetOut.initialize();
       break;
      case Datas.HOTKEY_SEARCH:
       MainWindow.hides(Datas.FOCUS_PANELMAPS);
       PanelSearch.initialize();
       break;
      case Datas.HOTKEY_SLIDERS:
       MainWindow.hides(Datas.FOCUS_PANELMAPS);
       PanelSliders.toggle();
       break;
      case Datas.HOTKEY_BUTTONS:
       MainWindow.hides(Datas.FOCUS_PANELMAPS);
       PanelButtons.toggle();
       break;
     }
    } else {
     if (e.getKeyCode() == Datas.HOTKEY_EXIT) {
      tryGetOut();
     }
    }
   } else if (clicMode == SELECT_BY_CLIC_MODE) {
    if (e.getKeyCode() == Datas.HOTKEY_EXIT) {
     PanelChoices.clicCoords = null;
     PanelChoices.unlock();
    }
   }
  }
 };
 public void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2d = (Graphics2D) g;
  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
  g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED);
  g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
  g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
  Datas.listObjDisplayed.clear();
  g2d.setColor(new Color(255, 255, 255, 255));
  g2d.fillRect(0,0,panelWidth,panelHeight);
  for (int i=0; i<Datas.listBlockDatas.size(); i++) {
   BlockDatas bd = Datas.listBlockDatas.get(i);
   if (bd.nbObjects == bd.listObjects.size()) {
    Identifiant.draw(g2d, bd);
   } else {
    if (ProgressWindow.isActive()) {
     ProgressWindow.addValue(bd.nbObjects);
    }
   }
  }
  System.gc();
 }
 public static void repaintMaps() {
  ProgressWindow.initialize(Datas.nbObjects, "Affichage en cours ...");
  MainWindow.mapsPanel.repaint();
  ProgressWindow.clean();
  focusRequested();
 }
 public static void focusRequested() {
  if (currentFont != Accessibility.currentFont) {
    PanelButtons.redraw();
    PanelSliders.redraw();
    PanelMessage.redraw();
    PanelCaption.redraw();
    currentFont = Accessibility.currentFont;
  }
  MainWindow.mapsPanel.requestFocus();
 }
 public static void initMouse() {
  MainWindow.mapsPanel.addMouseListener(new MouseAdapter() {
   public void mouseClicked(MouseEvent mlc) {
    if (clicMode == PANELMAPS_CLIC_MODE) {
     if (mlc.getButton() == MouseEvent.BUTTON1) {
      leftMouseClicked(mlc);
     }
     if (mlc.getButton() == MouseEvent.BUTTON3) {
      rightMouseClicked(mlc);
     }
    } else if (clicMode == SELECT_BY_CLIC_MODE) {
     int xmouse = mlc.getX();
     int ymouse = mlc.getY();
     int xm = (xmouse-panelWidth/2)*Datas.scale[scale]+Xc;
     int ym = (panelHeight/2-ymouse)*Datas.scale[scale]+Yc;
     PanelChoices.clicCoords = new Dimension(xm, ym);
     PanelChoices.unlock();
    }
   }
   public void mousePressed(MouseEvent mlp) {
    if (mlp.getButton() == MouseEvent.BUTTON1) {
     leftMousePressed(mlp);
    }
   }
   public void mouseReleased(MouseEvent mlr) {
    if (mlr.getButton() == MouseEvent.BUTTON1) {
     leftMouseReleased(mlr);
    }
   }
  });
  MainWindow.mapsPanel.addMouseWheelListener(new MouseWheelListener() {
   public void mouseWheelMoved(MouseWheelEvent mwl) {
    calcWheel(mwl);
   }
  });
  MainWindow.mapsPanel.addKeyListener(ka);
 }
 public static void boundingBoxPanel() {
  Xmin = Xc-(panelWidth/2 * Datas.scale[scale]);
  Xmax = Xc+(panelWidth/2 * Datas.scale[scale]);
  Ymin = Yc-(panelHeight/2 * Datas.scale[scale]);
  Ymax = Yc+(panelHeight/2 * Datas.scale[scale]);
 }
 public static boolean isDrawable(int xmin_obj, int ymin_obj, int xmax_obj, int ymax_obj) {
  boolean draw = true;
  if (xmin_obj > Xmax)
   draw = false;
  if (ymin_obj > Ymax)
   draw = false;
  if (xmax_obj < Xmin)
   draw = false;
  if (ymax_obj < Ymin)
   draw = false;
  return(draw);
 }
 public static boolean isNotToSmall(int semilargeX, int semilargeY) {
  boolean draw = true;
  if (semilargeX<Datas.PIXELS_MIN_SIZE_TO_DRAW*Datas.scale[scale])
   draw = false;
  if (semilargeY<Datas.PIXELS_MIN_SIZE_TO_DRAW*Datas.scale[scale])
   draw = false;
  return(draw);
 }
 public static void calcWheel(MouseWheelEvent mwl) {
  int mvt = mwl.getWheelRotation();
  if (mvt < 0) {
   zoomIn();
  }
  if (mvt > 0) {
   zoomOut();
  }
 }
 public static void zoomIn() {
  scale--;
  if (scale < 0)
   scale = 0;
  else {
   boundingBoxPanel();
   Identifiant.mustRedrawAll();
   repaintMaps();
  }
 }
 public static void zoomOut() {
  scale++;
  if (scale > Datas.maxscale)
   scale = Datas.maxscale;
  else {
   boundingBoxPanel();
   Identifiant.mustRedrawAll();
   repaintMaps();
  }
 }
 public static void leftMousePressed(MouseEvent e) {
  if (panelMenu.isVisible()) {
   popupMenuWasVisible = true;
   panelMenu.hide();
  } else {
   popupMenuWasVisible = false;
  }
  if ((mouseX != 0) || (mouseY != 0)) {
   PanelOutput po = new PanelOutput("Erreur", MainWindow.cadre, PanelOutput.ERROR_MODE);
   po.addEntry("ERREUR[MapsPanel/leftMousePressed] : souris mal initialisée");
   po.display();
  } else {
   mouseX = e.getX();
   mouseY = e.getY();
  }
 }
 public static void leftMouseReleased(MouseEvent e) {
  if ((mouseX == 0) && (mouseY == 0)) {
   PanelOutput po = new PanelOutput("Erreur", MainWindow.cadre, PanelOutput.ERROR_MODE);
   po.addEntry("ERREUR[MapsPanel/leftMouseReleased] : souris mal initialisée");
   po.display();
  } else {
   int xx = e.getX();
   int yy = e.getY();
   if ((mouseX != xx) || (mouseY != yy))
    calcDrag(mouseX-e.getX(),e.getY()-mouseY);
   mouseX = 0;
   mouseY = 0;
  }
 }
 public static void leftMouseClicked(MouseEvent e) {
  if (!popupMenuWasVisible) {
   int xmouse = e.getX();
   int ymouse = e.getY();
   int i, j;
   int xm = (xmouse-panelWidth/2)*Datas.scale[scale]+Xc;
   int ym = (panelHeight/2-ymouse)*Datas.scale[scale]+Yc;
   PanelInfo.displayCoords(xm,ym);
   for (i=0; i<Datas.listObjDisplayed.size(); i++) {
    Objects o = Datas.listObjDisplayed.get(i);
    if (o.isClicked(xm, ym, xmouse, ymouse)) {
     PanelInfo.displayInfo(o.ptrInfodata);
    }
   }
  }
 }
 public static void calcDrag(int xx, int yy) {
  Xc = Xc + xx*Datas.scale[scale];
  Yc = Yc + yy*Datas.scale[scale];
  boundingBoxPanel();
  Identifiant.mustRedrawAll();
  repaintMaps();
 }
 public static void reinitialize() {
  Xc = Xc_init;
  Yc = Yc_init;
  scale = scale_init;
  boundingBoxPanel();
  Identifiant.mustRedrawAll();
  repaintMaps();
 }
 public static void hidePopupMenu() {
  panelMenu.hide();
 }
 public static void tryGetOut() {
  if (panelMenu.isVisible()) {
   panelMenu.hide();
  } else {
   if (PanelInfo.isVisible()) {
    PanelInfo.hide();
   } else {
    PanelGetOut.initialize();
   }
  }
 }
 public static void getNewBounds() {
  BlockDatas bd = Datas.listBlockDatas.get(0);
  int xmin = bd.x0 - bd.semiLargeX;
  int xmax = bd.x0 + bd.semiLargeX;
  int ymin = bd.y0 - bd.semiLargeY;
  int ymax = bd.y0 + bd.semiLargeY;
  int temp;
  for (int i=1; i<Datas.listBlockDatas.size(); i++) {
   bd = Datas.listBlockDatas.get(i);
   temp = bd.x0 - bd.semiLargeX;
   if (temp < xmin)
    xmin = temp;
   temp = bd.x0 + bd.semiLargeX;
   if (temp > xmax)
    xmax = temp;
   temp = bd.y0 - bd.semiLargeY;
   if (temp < ymin)
    ymin = temp;
   temp = bd.y0 + bd.semiLargeY;
   if (temp > ymax)
    ymax = temp;
  }
  Datas.x0 = (xmin + xmax) / 2;
  Datas.y0 = (ymin + ymax) / 2;
  Datas.semiLargeX = (xmax - xmin) / 2;
  Datas.semiLargeY = (ymax - ymin) / 2;
 }
 public static void rightMouseClicked(MouseEvent e) {
  int xmouse = e.getX();
  int ymouse = e.getY();
  if (!panelMenu.isInitialized()) {
   panelMenu.init();
   MouseAdapter ma = new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
     panelMenu.hide();
     PanelButtons.toggle();
    }
   };
   panelMenu.add("Commandes", true, ma);
   ma = new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
     panelMenu.hide();
     PanelCaption.toggle();
    }
   };
   panelMenu.add("Légende", true, ma);
   ma = new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
     panelMenu.hide();
     PanelSliders.toggle();
    }
   };
   panelMenu.add("Calques", true, ma);
   panelMenu.addSeparator();
   ma = new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
     panelMenu.hide();
     PanelManageMapFiles.addFileToWorkSpace();
    }
   };
   panelMenu.add("Superposer carte", true, ma);
   panelMenu.addSeparator();
   ma = new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
     panelMenu.hide();
    }
   };
   panelMenu.add("Annuler", true, ma);
  }
  panelMenu.show(xmouse, ymouse);
 }
}
Code de PanelChoices
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
 
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
 
public class PanelChoices {
 
 private static int mode;
 private static PanelQueries panelQuery;
 private static Object result;
 public static final int CHOICE_MAP = 1;
 public static final int CHOICE_OBJECT = 2;
 public static final int CHOICE_SLIDER = 3;
 public static final int CHOICE_CAPTION = 4;
 public static final int CHOICE_INFODATA = 5;
 public static Dimension clicCoords;
 private static boolean panelButtonsVisible;
 private static boolean panelSlidersVisible;
 private static boolean panelCaptionVisible;
 private static boolean panelInfoVisible;
 private static boolean locked = false;
 
 public static Object initialize(String title, int m, String message) {
  mode = m;
 
  if (Datas.listBlockDatas.size() == 1) {
   BlockDatas bd = Datas.listBlockDatas.get(0);
   switch (mode) {
    case CHOICE_MAP:
     return(bd);
    case CHOICE_OBJECT:
     if (bd.listObjects.size() == 1) {
      clean();
      return(bd.listObjects.get(0));
     }
     break;
    case CHOICE_SLIDER:
     if (bd.listIdentifiants.size() == 1) {
      clean();
      return(bd.listIdentifiants.get(0));
     }
     break;
    case CHOICE_CAPTION:
     if (bd.listCaptions.size() == 1) {
      clean();
      return(bd.listCaptions.get(0));
     }
     break;
    case CHOICE_INFODATA:
     if (bd.listInfodatas.size() == 1) {
      clean();
      return(bd.listInfodatas.get(0));
     }
     break;
    default:
     PanelOutput po = new PanelOutput("ERREUR", MainWindow.cadre, PanelOutput.WARNING_MODE);
     po.addEntry("ERREUR[PanelChoices/display] : mode inconnu (1)");
     po.display();
     clean();
     return(null);
   }
  }
  switch (mode) {
   case CHOICE_MAP:
    panelQuery = new PanelQueries(title, "OK", "Annuler", MainWindow.cadre, PanelQueries.MODE_JCOMBOBOX);
    break;
   case CHOICE_OBJECT:
    panelQuery = new PanelQueries(title, "OK", "Annuler", MainWindow.cadre, PanelQueries.MODE_JCOMBOBOX);
    break;
   case CHOICE_SLIDER:
    panelQuery = new PanelQueries(title, "OK", "Annuler", MainWindow.cadre, PanelQueries.MODE_JCOMBOBOX);
    break;
   case CHOICE_CAPTION:
    panelQuery = new PanelQueries(title, "OK", "Annuler", MainWindow.cadre, PanelQueries.MODE_JCOMBOBOX);
    break;
   case CHOICE_INFODATA:
    panelQuery = new PanelQueries(title, "OK", "Annuler", MainWindow.cadre, PanelQueries.MODE_JCOMBOBOX);
    break;
   default:
    PanelOutput po = new PanelOutput("ERREUR", MainWindow.cadre, PanelOutput.WARNING_MODE);
    po.addEntry("ERREUR[PanelChoices/PanelChoices] : mode inconnu");
    po.display();
    clean();
    return(null);
  }
  panelQuery.addEntry("Sélectionner à partir d'une liste");
  panelQuery.addEntry("Sélectionner à partir d'un clic sur la carte");
  result = panelQuery.display();
  if (result == null) {
   clean();
   return(null);
  }
  int ptr = (Integer)result;
  PanelMessage.set(message);
  switch (mode) {
   case CHOICE_MAP:
    if (ptr ==0) {
 
    } else {
 
    }
    break;
   case CHOICE_OBJECT:
    if (ptr ==0) {
 
    } else {
     lock();
     PanelMessage.clear();
     if (clicCoords == null) {
      clean();
      return(null);
     }
     PanelInfo.displayCoords(clicCoords.width, clicCoords.height);
     clean();
     return(null);
    }
    break;
   case CHOICE_SLIDER:
    if (ptr ==0) {
 
    } else {
 
    }
    break;
   case CHOICE_CAPTION:
    if (ptr ==0) {
 
    } else {
 
    }
    break;
   case CHOICE_INFODATA:
    if (ptr ==0) {
 
    } else {
 
    }
    break;
   default:
    PanelOutput po = new PanelOutput("ERREUR", MainWindow.cadre, PanelOutput.WARNING_MODE);
    po.addEntry("ERREUR[PanelChoices/PanelChoices] : mode inconnu");
    po.display();
    PanelMessage.clear();
    clean();
    return(null);
  }
  return(null);
 }
 
 private static void clean() {
  panelQuery = null;
  clicCoords = null;
  result = null;
 }
 
 private static void lock() {
  panelButtonsVisible = PanelButtons.isPanelVisible();
  panelSlidersVisible = PanelSliders.isPanelVisible();
  panelCaptionVisible = PanelCaption.isPanelVisible();
  panelInfoVisible = PanelInfo.isPanelVisible();
  if (panelButtonsVisible) {
   PanelButtons.setPanelVisible(false);
  }
  if (panelSlidersVisible) {
   PanelSliders.setPanelVisible(false);
  }
  if (panelCaptionVisible) {
   PanelCaption.setPanelVisible(false);
  }
  if (panelInfoVisible) {
   PanelInfo.setPanelVisible(false);
  }
  PanelMaps.clicMode = PanelMaps.SELECT_BY_CLIC_MODE;
  locked = true;
  MainWindow.mapsPanel.repaint();
  while (locked) {
   try {
    Thread.sleep(500);
   } catch (InterruptedException ie) {
    System.out.println("Erreur de thread");
   }
  }
 }
 
 public static void unlock() {
  PanelMaps.clicMode = PanelMaps.PANELMAPS_CLIC_MODE;
  if (panelButtonsVisible) {
   PanelButtons.setPanelVisible(true);
  }
  if (panelSlidersVisible) {
   PanelSliders.setPanelVisible(true);
  }
  if (panelCaptionVisible) {
   PanelCaption.setPanelVisible(true);
  }
  if (panelInfoVisible) {
   PanelInfo.setPanelVisible(true);
  }
  locked = false;
 }
}