Commit 02c44758abfb5eb59eca9fe012c197395e1e52e4
1 parent
08cc28cc
finish ihm
Showing
7 changed files
with
329 additions
and
235 deletions
Show diff stats
essai.ser renamed to grid.data
No preview for this file type
src/app/ApplicationDeserialization.java deleted
@@ -1,38 +0,0 @@ | @@ -1,38 +0,0 @@ | ||
1 | -package app; | ||
2 | - | ||
3 | -import java.io.File; | ||
4 | -import java.io.FileInputStream; | ||
5 | -import java.io.IOException; | ||
6 | -import java.io.ObjectInputStream; | ||
7 | -import java.util.List; | ||
8 | - | ||
9 | -import kernel.Cell; | ||
10 | -import kernel.Grid; | ||
11 | - | ||
12 | -public class ApplicationDeserialization { | ||
13 | - public static void main(String[] args) throws IOException, ClassNotFoundException { | ||
14 | - File fichier = new File("essai.ser") ; | ||
15 | - | ||
16 | - // ouverture d'un flux sur un fichier | ||
17 | - ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fichier)) ; | ||
18 | - | ||
19 | - // désérialization de l'objet | ||
20 | - Grid grid = (Grid)ois.readObject() ; | ||
21 | - // Affichage | ||
22 | - List<Cell> cells = grid.getCells(); | ||
23 | - | ||
24 | - System.out.println("Affichage des valeurs :"); | ||
25 | - for (Cell cell : cells) | ||
26 | - System.out.println(cell.getId() + ": " + cell.getValue()); | ||
27 | - | ||
28 | - System.out.println("Affichage des formules :"); | ||
29 | - for (Cell cell : cells) | ||
30 | - System.out.println(cell.getId() + ": " + cell.toString()); | ||
31 | - | ||
32 | - System.out.println("Affichage des formules développées :"); | ||
33 | - for (Cell cell : cells) | ||
34 | - System.out.println(cell.getId() + ": " + cell.getDevelopedFormula()); | ||
35 | - ois.close(); | ||
36 | - } | ||
37 | - | ||
38 | -} |
src/ihm/TablooProto.java
@@ -24,53 +24,69 @@ import javax.swing.table.DefaultTableCellRenderer; | @@ -24,53 +24,69 @@ import javax.swing.table.DefaultTableCellRenderer; | ||
24 | import javax.swing.table.TableColumn; | 24 | import javax.swing.table.TableColumn; |
25 | 25 | ||
26 | import kernel.Grid; | 26 | import kernel.Grid; |
27 | -import kernel.exception.CellNotFoundException; | ||
28 | -import kernel.exception.InvalidIntervalException; | 27 | +import kernel.exception.*; |
28 | +import kernel.function.Average; | ||
29 | +import kernel.function.Sum; | ||
30 | +import kernel.operation.Addition; | ||
31 | +import kernel.operation.Division; | ||
32 | +import kernel.operation.Multiplication; | ||
33 | +import kernel.operation.Subtraction; | ||
34 | + | ||
35 | +import javax.swing.*; | ||
36 | +import javax.swing.table.AbstractTableModel; | ||
37 | +import javax.swing.table.DefaultTableCellRenderer; | ||
38 | +import javax.swing.table.TableColumn; | ||
39 | +import java.awt.*; | ||
40 | +import java.awt.event.WindowAdapter; | ||
41 | +import java.awt.event.WindowEvent; | ||
42 | +import java.io.IOException; | ||
43 | +import java.util.Arrays; | ||
44 | +import java.util.List; | ||
45 | +import java.util.Objects; | ||
46 | +import java.util.regex.Matcher; | ||
47 | +import java.util.regex.Pattern; | ||
48 | +import java.util.stream.Collectors; | ||
29 | 49 | ||
30 | public class TablooProto extends JPanel { | 50 | public class TablooProto extends JPanel { |
31 | - | ||
32 | - /** | ||
33 | - * | ||
34 | - */ | ||
35 | - private static final long serialVersionUID = 1L; | ||
36 | - | 51 | + |
52 | + static Grid grid; | ||
53 | + | ||
37 | // Fourni: ne rien changer. | 54 | // Fourni: ne rien changer. |
38 | - public TablooProto() throws FileNotFoundException, ClassNotFoundException, IOException { | ||
39 | - super(new GridLayout(1, 0)); | ||
40 | - | ||
41 | - // modele de donnees | ||
42 | - // cf. plus loin la inner classe MyTableModel a modifier... | ||
43 | - MyTableModel tableModel = new MyTableModel(); | ||
44 | - | ||
45 | - // la JTable et ses parametres | ||
46 | - JTable table = new JTable(tableModel); | ||
47 | - table.setPreferredScrollableViewportSize(new Dimension(1000, 500)); | ||
48 | - table.setGridColor(Color.BLACK); | ||
49 | - table.setShowGrid(true); | ||
50 | - | ||
51 | - // on ajoute un scroll | ||
52 | - JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); | ||
53 | - add(scrollPane); | ||
54 | - table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); | ||
55 | - | ||
56 | - // parametrage de la 1ere ligne = noms des colonnes | ||
57 | - ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER); | ||
58 | - | ||
59 | - // parametrage de la 1ere colonne consacree a la numerotation des lignes | ||
60 | - TableColumn tm = table.getColumnModel().getColumn(0); | ||
61 | - tm.setPreferredWidth(tm.getPreferredWidth() * 2 / 3); | ||
62 | - tm.setCellRenderer(new PremiereColonneSpecificRenderer(Color.LIGHT_GRAY)); | ||
63 | - | ||
64 | - } | ||
65 | - | ||
66 | - // Inner class pour changer l'aspect de la premiere colonne consacree a la numerotation des lignes | ||
67 | - // Fourni: ne rien changer. | ||
68 | - class PremiereColonneSpecificRenderer extends DefaultTableCellRenderer { | ||
69 | - | ||
70 | - /** | ||
71 | - * | 55 | + public TablooProto(Grid grid) throws ClassNotFoundException, IOException { |
56 | + super(new GridLayout(1, 0)); | ||
57 | + | ||
58 | + // modele de donnees | ||
59 | + // cf. plus loin la inner classe MyTableModel a modifier... | ||
60 | + MyTableModel tableModel = new MyTableModel(grid); | ||
61 | + | ||
62 | + // la JTable et ses parametres | ||
63 | + JTable table = new JTable(tableModel); | ||
64 | + table.setPreferredScrollableViewportSize(new Dimension(1000, 500)); | ||
65 | + table.setGridColor(Color.BLACK); | ||
66 | + table.setShowGrid(true); | ||
67 | + | ||
68 | + // on ajoute un scroll | ||
69 | + JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); | ||
70 | + add(scrollPane); | ||
71 | + table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); | ||
72 | + | ||
73 | + // parametrage de la 1ere ligne = noms des colonnes | ||
74 | + ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER); | ||
75 | + | ||
76 | + // parametrage de la 1ere colonne consacree a la numerotation des lignes | ||
77 | + TableColumn tm = table.getColumnModel().getColumn(0); | ||
78 | + tm.setPreferredWidth(tm.getPreferredWidth() * 2 / 3); | ||
79 | + tm.setCellRenderer(new PremiereColonneSpecificRenderer(Color.LIGHT_GRAY)); | ||
80 | + | ||
81 | + } | ||
82 | + | ||
83 | + // Inner class pour changer l'aspect de la premiere colonne consacree a la numerotation des lignes | ||
84 | + // Fourni: ne rien changer. | ||
85 | + class PremiereColonneSpecificRenderer extends DefaultTableCellRenderer { | ||
86 | + | ||
87 | + /** | ||
88 | + * | ||
72 | */ | 89 | */ |
73 | - private static final long serialVersionUID = 1L; | ||
74 | Color couleur; | 90 | Color couleur; |
75 | 91 | ||
76 | public PremiereColonneSpecificRenderer(Color couleur) { | 92 | public PremiereColonneSpecificRenderer(Color couleur) { |
@@ -98,145 +114,212 @@ public class TablooProto extends JPanel { | @@ -98,145 +114,212 @@ public class TablooProto extends JPanel { | ||
98 | * | 114 | * |
99 | */ | 115 | */ |
100 | private static final long serialVersionUID = 1L; | 116 | private static final long serialVersionUID = 1L; |
101 | - // TODO | ||
102 | - // remplacer ce tableau en dur du prototype par la grille serialisee: | ||
103 | - kernel.Grid calc; | ||
104 | - //String[][] calc; | ||
105 | - | ||
106 | - MyTableModel() throws FileNotFoundException, IOException, ClassNotFoundException { | ||
107 | - // TODO: remplacer cette initialisation par le chargement de la grille serialisee | ||
108 | - File fichier = new File("grille.ser") ; | ||
109 | - ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fichier)) ; | ||
110 | - calc = (Grid)ois.readObject() ; | ||
111 | - ois.close(); | ||
112 | - //calc = new String[this.getRowCount()][this.getColumnCount()]; | ||
113 | - //for (int ligne =0; ligne < calc.length; ligne++) | ||
114 | - //for (int colonne=0; colonne < calc[ligne].length; colonne++) | ||
115 | - //calc[ligne][colonne] = ""; | ||
116 | - } | ||
117 | - | ||
118 | - @Override | ||
119 | - // Standard: doit retourner le nbre de colonnes de la JTable | ||
120 | - public int getColumnCount() { | ||
121 | - // TODO: remplacer par le nbre de colonnes de la grille | ||
122 | - // + 1 pour la colonne 0 consacrée aux numeros de ligne) | ||
123 | - return calc.getTotalColumn()+1; | ||
124 | - } | ||
125 | - | ||
126 | - @Override | ||
127 | - // Standard: doit retourner le nbre de lignes de la JTable | ||
128 | - public int getRowCount() { | ||
129 | - // TODO: remplacer par le nbre de lignes de la grille | ||
130 | - return calc.getTotalLine(); | ||
131 | - } | ||
132 | - | ||
133 | - // Standard: doit renvoyer le nom de la colonne a afficher en tete | ||
134 | - // Fourni: ne rien changer. | ||
135 | - @Override | ||
136 | - public String getColumnName(int col) { | ||
137 | - if (col == 0) { | ||
138 | - return ""; // colonne consacrée aux numeros de ligne | ||
139 | - } else { | ||
140 | - return "" + (char) ((int) ('A') + col - 1); | ||
141 | - } | ||
142 | - } | ||
143 | - | ||
144 | - // Utilitaire interne fourni (ne rien changer) | ||
145 | - // Retourne le nom d'une case a partir de ses coordonnees dans la JTable. | ||
146 | - String getNomCase(int row, int col) { | ||
147 | - return this.getColumnName(col) + String.valueOf(row + 1); // row commence a 0 | ||
148 | - } | ||
149 | - | ||
150 | - @Override | ||
151 | - // Standard: doit renvoyer le contenu a afficher de la case correspondante | ||
152 | - public Object getValueAt(int row, int col) { | ||
153 | - if (col == 0) { | ||
154 | - // Fourni: ne rien changer. | ||
155 | - // en colonne 0 : numeros de lignes | ||
156 | - return "" + String.valueOf(row + 1); | ||
157 | - } else { | ||
158 | - // TODO: remplacer par le contenu + la valeur | ||
159 | - // de la case de nom getNomCase(row, col) | ||
160 | - // dans la grille (comme dans la figure 1 du sujet). | ||
161 | - try { | ||
162 | - return "" + calc.getDevelopedFormula(this.getColumnName(col), row+1)+"="+calc.getValue(this.getColumnName(col), row+1); | 117 | + private Grid grid; |
118 | + | ||
119 | + MyTableModel(Grid grid) { | ||
120 | + this.grid = grid; | ||
121 | + } | ||
122 | + | ||
123 | + @Override | ||
124 | + // Standard: doit retourner le nbre de colonnes de la JTable | ||
125 | + public int getColumnCount() { | ||
126 | + return grid.getTotalColumn() + 1; | ||
127 | + } | ||
128 | + | ||
129 | + @Override | ||
130 | + // Standard: doit retourner le nbre de lignes de la JTable | ||
131 | + public int getRowCount() { | ||
132 | + return grid.getTotalRow(); | ||
133 | + } | ||
134 | + | ||
135 | + // Standard: doit renvoyer le nom de la colonne a afficher en tete | ||
136 | + // Fourni: ne rien changer. | ||
137 | + @Override | ||
138 | + public String getColumnName(int col) { | ||
139 | + if (col == 0) { | ||
140 | + return ""; // colonne consacrée aux numeros de ligne | ||
141 | + } else { | ||
142 | + return "" + (char) ((int) ('A') + col - 1); | ||
143 | + } | ||
144 | + } | ||
145 | + | ||
146 | + // Utilitaire interne fourni (ne rien changer) | ||
147 | + // Retourne le nom d'une case a partir de ses coordonnees dans la JTable. | ||
148 | + private String getNomCase(int row, int col) { | ||
149 | + return this.getColumnName(col) + (row + 1); // row commence a 0 | ||
150 | + } | ||
151 | + | ||
152 | + @Override | ||
153 | + // Standard: doit renvoyer le contenu a afficher de la case correspondante | ||
154 | + public Object getValueAt(int row, int col) { | ||
155 | + if (col == 0) { | ||
156 | + // Fourni: ne rien changer. | ||
157 | + // en colonne 0 : numeros de lignes | ||
158 | + return "" + (row + 1); | ||
159 | + } else { | ||
160 | + try { | ||
161 | + return grid.getDevelopedFormula(this.getColumnName(col), row + 1) + "=" + grid.getValue(this.getColumnName(col), row + 1); | ||
163 | } catch (CellNotFoundException e) { | 162 | } catch (CellNotFoundException e) { |
164 | // TODO Auto-generated catch block | 163 | // TODO Auto-generated catch block |
165 | 164 | ||
166 | } | 165 | } |
167 | } | 166 | } |
168 | return ""; | 167 | return ""; |
169 | - } | ||
170 | - | ||
171 | - // Standard. | ||
172 | - // Fourni: ne rien changer. | ||
173 | - @Override | ||
174 | - public Class getColumnClass(int c) { | ||
175 | - return getValueAt(0, c).getClass(); | ||
176 | - } | ||
177 | - | ||
178 | - // Standard: determine si une case est editable ou non. | ||
179 | - // Fourni: ne rien changer. | ||
180 | - // Seules les cases de la 1er colonne ne le sont pas | ||
181 | - // (consacrees a la numerotation des lignes) | ||
182 | - @Override | ||
183 | - public boolean isCellEditable(int row, int col) { | ||
184 | - if (col < 1) { | ||
185 | - return false; // col 0 consacree a la numerotation des lignes (non editable) | ||
186 | - } else { | ||
187 | - return true; | ||
188 | - } | ||
189 | - } | ||
190 | - | ||
191 | - | ||
192 | - // Standard: l'utilisateur a entré une valeur dans une case, | ||
193 | - // mettre a jour le modèle de donnees connecte. | ||
194 | - // L'utilisateur a modifie une case. | ||
195 | - // Si c'est une valeur numerique (sinon ne rien faire) | ||
196 | - // - modifier la case correspondante dans la grille si cette case existe | ||
197 | - // - ajouter la case correspondante dans la grille | ||
198 | - @Override | ||
199 | - public void setValueAt(Object value, int row, int col) { | ||
200 | - | ||
201 | - // TODO remplacer par le code correspondant | ||
202 | - if (value instanceof String) { | ||
203 | - if (calc.getCellsId().contains(this.getNomCase(row, col))) | 168 | + } |
169 | + | ||
170 | + // Standard. | ||
171 | + // Fourni: ne rien changer. | ||
172 | + @Override | ||
173 | + public Class getColumnClass(int c) { | ||
174 | + return getValueAt(0, c).getClass(); | ||
175 | + } | ||
176 | + | ||
177 | + // Standard: determine si une case est editable ou non. | ||
178 | + // Fourni: ne rien changer. | ||
179 | + // Seules les cases de la 1er colonne ne le sont pas | ||
180 | + // (consacrees a la numerotation des lignes) | ||
181 | + @Override | ||
182 | + public boolean isCellEditable(int row, int col) { | ||
183 | + return col > 0; | ||
184 | + } | ||
185 | + | ||
186 | + // Standard: l'utilisateur a entré une valeur dans une case, | ||
187 | + // mettre a jour le modèle de donnees connecte. | ||
188 | + // L'utilisateur a modifie une case. | ||
189 | + // Si c'est une valeur numerique | ||
190 | + // - modifier la case correspondante dans la grille si cette case existe | ||
191 | + // - ajouter la case correspondante dans la grille | ||
192 | + @Override | ||
193 | + public void setValueAt(Object input, int row, int col) { | ||
194 | + Formula formula = new Addition(null, null); | ||
195 | + double value = 0; | ||
196 | + int cellType; | ||
197 | + | ||
198 | + if (input instanceof String && !((String) input).isEmpty()) { | ||
199 | + String text = (String) input; | ||
200 | + try { | ||
204 | try { | 201 | try { |
205 | - calc.setValue(this.getColumnName(col), row+1, Double.parseDouble((String)value)); | ||
206 | - } catch (CellNotFoundException e) { | ||
207 | - // TODO Auto-generated catch block | ||
208 | - e.printStackTrace(); | 202 | + // Récupération de la valeur |
203 | + value = Double.parseDouble(text); | ||
204 | + cellType = 0; | ||
205 | + } catch (NumberFormatException exception) { | ||
206 | + // Récupération de la formule | ||
207 | + formula = this.generateFormulaWithString(text); | ||
208 | + cellType = 1; | ||
209 | } | 209 | } |
210 | - else | ||
211 | - try { | ||
212 | - calc.createCell(this.getColumnName(col), row+1, Double.parseDouble((String)value)); | ||
213 | - } catch (InvalidIntervalException e) { | ||
214 | - // TODO Auto-generated catch block | ||
215 | - | 210 | + |
211 | + String cellName = this.getNomCase(row, col); | ||
212 | + switch (cellType) { | ||
213 | + case 0: | ||
214 | + if (this.grid.cellExist(cellName)) | ||
215 | + this.grid.setValue(this.getColumnName(col), row + 1, value); | ||
216 | + else | ||
217 | + this.grid.createCell(this.getColumnName(col), row + 1, value); | ||
218 | + break; | ||
219 | + case 1: | ||
220 | + if (this.grid.cellExist(cellName)) | ||
221 | + this.grid.setFormula(this.getColumnName(col), row + 1, formula); | ||
222 | + else | ||
223 | + this.grid.createCell(this.getColumnName(col), row + 1, formula); | ||
224 | + break; | ||
216 | } | 225 | } |
217 | - } | ||
218 | - // Ne pas modifier : | ||
219 | - // mise a jour automatique de l'affichage suite a la modification | ||
220 | - fireTableCellUpdated(row, col); | ||
221 | - } | ||
222 | - } | ||
223 | - // Fin de la inner class MyTableModel | ||
224 | - | ||
225 | - // Exécution de l'interface graphique a partir d'un terminal. | ||
226 | - // TODO: parametrer le tout par un fichier de grille serialisee. | ||
227 | - public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, IOException { | ||
228 | - // TODO: parametrer le tableur par un fichier de grille serialisee | ||
229 | - // a charger comme modele de donnees. | ||
230 | - TablooProto tableur = new TablooProto(); | ||
231 | - | ||
232 | - // Creation de l'application et lancement | ||
233 | - // Fourni: ne rien changer. | ||
234 | - JFrame frame = new JFrame("TABLO"); | ||
235 | - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
236 | - tableur.setOpaque(true); | ||
237 | - frame.setContentPane(tableur); | ||
238 | - frame.pack(); | ||
239 | - frame.setVisible(true); | ||
240 | - | ||
241 | - } | 226 | + } catch (CellNotFoundException | BadSyntaxException | CreateCycleException | InvalidIntervalException e) { |
227 | + JOptionPane.showMessageDialog(null, e.getMessage(), "Attention", JOptionPane.INFORMATION_MESSAGE); | ||
228 | + } | ||
229 | + } else { | ||
230 | + try { | ||
231 | + this.grid.deleteCell(this.getColumnName(col), row + 1); | ||
232 | + } catch (CannotDeleteCellException e) { | ||
233 | + JOptionPane.showMessageDialog(null, e.getMessage(), "Attention", JOptionPane.INFORMATION_MESSAGE); | ||
234 | + } | ||
235 | + } | ||
236 | + | ||
237 | + // mise a jour automatique de l'affichage de toute la table suite a la modification | ||
238 | + fireTableDataChanged(); | ||
239 | + } | ||
240 | + | ||
241 | + private Formula generateFormulaWithString(String input) throws CellNotFoundException, BadSyntaxException { | ||
242 | + Pattern functionPattern = Pattern.compile("=([A-Z]+)\\(([A-Z0-9,]+)\\)"); | ||
243 | + Matcher functionMatcher = functionPattern.matcher(input); | ||
244 | + | ||
245 | + if (functionMatcher.matches()) { | ||
246 | + List<Cell> cells = Arrays.stream(functionMatcher.group(2).split(",")) | ||
247 | + .map(c -> grid.getCell(c)) | ||
248 | + .collect(Collectors.toList()); | ||
249 | + | ||
250 | + if (cells.contains(null)) | ||
251 | + throw new CellNotFoundException(); | ||
252 | + | ||
253 | + cells = cells.stream().filter(Objects::nonNull).collect(Collectors.toList()); | ||
254 | + | ||
255 | + switch (functionMatcher.group(1)) { | ||
256 | + case "SUM": | ||
257 | + case "SOMME": | ||
258 | + return new Sum(cells); | ||
259 | + case "AVERAGE": | ||
260 | + case "MOYENNE": | ||
261 | + return new Average(cells); | ||
262 | + } | ||
263 | + } else { | ||
264 | + Pattern binaryOperationPattern = Pattern.compile("=([A-Z]+[0-9]+)([+\\-*/])([A-Z]+[0-9]+)"); | ||
265 | + Matcher binaryOperationMatcher = binaryOperationPattern.matcher(input); | ||
266 | + | ||
267 | + if (!binaryOperationMatcher.matches()) | ||
268 | + throw new BadSyntaxException(); | ||
269 | + | ||
270 | + Cell leftCell = grid.getCell(binaryOperationMatcher.group(1)); | ||
271 | + Cell rightCell = grid.getCell(binaryOperationMatcher.group(3)); | ||
272 | + | ||
273 | + if (leftCell == null || rightCell == null) | ||
274 | + throw new CellNotFoundException(); | ||
275 | + | ||
276 | + switch (binaryOperationMatcher.group(2)) { | ||
277 | + case "+": | ||
278 | + return new Addition(leftCell, rightCell); | ||
279 | + case "-": | ||
280 | + return new Subtraction(leftCell, rightCell); | ||
281 | + case "*": | ||
282 | + return new Multiplication(leftCell, rightCell); | ||
283 | + case "/": | ||
284 | + return new Division(leftCell, rightCell); | ||
285 | + } | ||
286 | + } | ||
287 | + | ||
288 | + return null; | ||
289 | + } | ||
290 | + } | ||
291 | + // Fin de la inner class MyTableModel | ||
292 | + | ||
293 | + // Exécution de l'interface graphique a partir d'un terminal. | ||
294 | + public static void main(String[] args) throws ClassNotFoundException, IOException { | ||
295 | + try { | ||
296 | + grid = Grid.load(); | ||
297 | + } catch (IOException | ClassNotFoundException e) { | ||
298 | + grid = new Grid(); | ||
299 | + } | ||
300 | + // a charger comme modele de donnees. | ||
301 | + TablooProto tableur = new TablooProto(grid); | ||
302 | + | ||
303 | + // Creation de l'application et lancement | ||
304 | + // Fourni: ne rien changer. | ||
305 | + JFrame frame = new JFrame("TABLO"); | ||
306 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
307 | + tableur.setOpaque(true); | ||
308 | + frame.setContentPane(tableur); | ||
309 | + frame.pack(); | ||
310 | + frame.setVisible(true); | ||
311 | + | ||
312 | + // Sauvegarde de la grille lors de la fermeture de l'application | ||
313 | + frame.addWindowListener(new WindowAdapter() { | ||
314 | + @Override | ||
315 | + public void windowClosing(WindowEvent we) { | ||
316 | + try { | ||
317 | + grid.save(); | ||
318 | + } catch (IOException e) { | ||
319 | + System.out.println(e.getMessage()); | ||
320 | + } | ||
321 | + System.exit(0); | ||
322 | + } | ||
323 | + }); | ||
324 | + } | ||
242 | } | 325 | } |
src/kernel/Cell.java
@@ -65,6 +65,13 @@ public class Cell implements Serializable { | @@ -65,6 +65,13 @@ public class Cell implements Serializable { | ||
65 | this.value = this.formula.eval(); | 65 | this.value = this.formula.eval(); |
66 | } | 66 | } |
67 | 67 | ||
68 | + public void updateUsedIn() { | ||
69 | + if (this.containFormula()) { | ||
70 | + for (Cell cell : this.formula.getUtilisedCells()) | ||
71 | + cell.getUsedIn().remove(this); | ||
72 | + } | ||
73 | + } | ||
74 | + | ||
68 | public boolean containFormula() { | 75 | public boolean containFormula() { |
69 | return this.formula != null; | 76 | return this.formula != null; |
70 | } | 77 | } |
src/kernel/Grid.java
1 | package kernel; | 1 | package kernel; |
2 | 2 | ||
3 | +import kernel.exception.CannotDeleteCellException; | ||
3 | import kernel.exception.CellNotFoundException; | 4 | import kernel.exception.CellNotFoundException; |
4 | import kernel.exception.CreateCycleException; | 5 | import kernel.exception.CreateCycleException; |
5 | import kernel.exception.InvalidIntervalException; | 6 | import kernel.exception.InvalidIntervalException; |
6 | 7 | ||
7 | -import java.io.Serializable; | 8 | +import java.io.*; |
8 | import java.util.ArrayList; | 9 | import java.util.ArrayList; |
9 | import java.util.HashMap; | 10 | import java.util.HashMap; |
10 | import java.util.List; | 11 | import java.util.List; |
@@ -13,32 +14,32 @@ import java.util.Map; | @@ -13,32 +14,32 @@ import java.util.Map; | ||
13 | public class Grid implements Serializable { | 14 | public class Grid implements Serializable { |
14 | 15 | ||
15 | private static final long serialVersionUID = 1L; | 16 | private static final long serialVersionUID = 1L; |
16 | - private static final int MAX_LIGNES = 20; | ||
17 | - private static final String MAX_COLONNES = "Z"; | 17 | + private static final int MAX_ROWS = 20; |
18 | + private static final String MAX_COLUMNS = "Z"; | ||
18 | private Map<String, Cell> cells = new HashMap<>(); | 19 | private Map<String, Cell> cells = new HashMap<>(); |
19 | public static LanguageEnum language = LanguageEnum.FR; | 20 | public static LanguageEnum language = LanguageEnum.FR; |
20 | - | 21 | + |
21 | 22 | ||
22 | public void createCell(String column, int line, double value) throws InvalidIntervalException { | 23 | public void createCell(String column, int line, double value) throws InvalidIntervalException { |
23 | column = column.toUpperCase(); | 24 | column = column.toUpperCase(); |
24 | - | 25 | + |
25 | if (!validateInterval(column, line)) | 26 | if (!validateInterval(column, line)) |
26 | throw new InvalidIntervalException(); | 27 | throw new InvalidIntervalException(); |
27 | String id = this.getCellId(column, line); | 28 | String id = this.getCellId(column, line); |
28 | Cell cell = new Cell(column, line, value); | 29 | Cell cell = new Cell(column, line, value); |
29 | this.cells.put(id, cell); | 30 | this.cells.put(id, cell); |
30 | - | 31 | + |
31 | } | 32 | } |
32 | 33 | ||
33 | public void createCell(String column, int line, Formula formula) throws CreateCycleException, InvalidIntervalException { | 34 | public void createCell(String column, int line, Formula formula) throws CreateCycleException, InvalidIntervalException { |
34 | column = column.toUpperCase(); | 35 | column = column.toUpperCase(); |
35 | - | 36 | + |
36 | if (!validateInterval(column, line)) | 37 | if (!validateInterval(column, line)) |
37 | throw new InvalidIntervalException(); | 38 | throw new InvalidIntervalException(); |
38 | String id = this.getCellId(column, line); | 39 | String id = this.getCellId(column, line); |
39 | Cell cell = new Cell(column, line, formula); | 40 | Cell cell = new Cell(column, line, formula); |
40 | this.cells.put(id, cell); | 41 | this.cells.put(id, cell); |
41 | - | 42 | + |
42 | } | 43 | } |
43 | 44 | ||
44 | public void setValue(String column, int line, double value) throws CellNotFoundException { | 45 | public void setValue(String column, int line, double value) throws CellNotFoundException { |
@@ -63,10 +64,6 @@ public class Grid implements Serializable { | @@ -63,10 +64,6 @@ public class Grid implements Serializable { | ||
63 | return new ArrayList<>(this.cells.values()); | 64 | return new ArrayList<>(this.cells.values()); |
64 | } | 65 | } |
65 | 66 | ||
66 | - public List<String> getCellsId() { | ||
67 | - return new ArrayList<>(this.cells.keySet()); | ||
68 | - } | ||
69 | - | ||
70 | public double getValue(String column, int line) throws CellNotFoundException { | 67 | public double getValue(String column, int line) throws CellNotFoundException { |
71 | return this.getCell(column, line).getValue(); | 68 | return this.getCell(column, line).getValue(); |
72 | } | 69 | } |
@@ -85,26 +82,59 @@ public class Grid implements Serializable { | @@ -85,26 +82,59 @@ public class Grid implements Serializable { | ||
85 | 82 | ||
86 | 83 | ||
87 | public int getTotalColumn() { | 84 | public int getTotalColumn() { |
88 | - return MAX_COLONNES.charAt(0) - (int) 'A' + 1; | ||
89 | - //return convertStringToInt(MAX_COLONNES); | 85 | + return MAX_COLUMNS.charAt(0) - (int) 'A' + 1; |
90 | } | 86 | } |
91 | 87 | ||
92 | - public int getTotalLine() { | ||
93 | - return MAX_LIGNES; | 88 | + public int getTotalRow() { |
89 | + return MAX_ROWS; | ||
94 | } | 90 | } |
95 | 91 | ||
96 | private boolean validateInterval(String column, int line) { | 92 | private boolean validateInterval(String column, int line) { |
97 | - return line >= 1 && line <= MAX_LIGNES && convertStringToInt(column)>=convertStringToInt("A") && convertStringToInt(column)<=convertStringToInt(MAX_COLONNES); | 93 | + return line >= 1 && line <= MAX_ROWS && convertStringToInt(column) >= convertStringToInt("A") && convertStringToInt(column) <= convertStringToInt(MAX_COLUMNS); |
94 | + } | ||
95 | + | ||
96 | + public boolean cellExist(String key) { | ||
97 | + return this.cells.containsKey(key); | ||
98 | } | 98 | } |
99 | 99 | ||
100 | - private int convertStringToInt(String str){ | ||
101 | - int ascii=0; | ||
102 | - for(int i = 0; i < str.length(); i++){ // while counting characters if less than the length add one | 100 | + public void deleteCell(String column, int line) throws CannotDeleteCellException { |
101 | + String id = this.getCellId(column, line); | ||
102 | + | ||
103 | + if (this.cellExist(id)) { | ||
104 | + Cell cell = this.getCell(id); | ||
105 | + | ||
106 | + if (!cell.getUsedIn().isEmpty()) | ||
107 | + throw new CannotDeleteCellException(); | ||
108 | + | ||
109 | + cell.updateUsedIn(); | ||
110 | + this.cells.remove(this.getCellId(column, line)); | ||
111 | + } | ||
112 | + } | ||
113 | + | ||
114 | + private int convertStringToInt(String str) { | ||
115 | + int ascii = 0; | ||
116 | + for (int i = 0; i < str.length(); i++) { // while counting characters if less than the length add one | ||
103 | char character = str.charAt(i); // start on the first character | 117 | char character = str.charAt(i); // start on the first character |
104 | - ascii = ascii+(int) character; | ||
105 | - | 118 | + ascii = ascii + (int) character; |
119 | + | ||
106 | } | 120 | } |
107 | return ascii; | 121 | return ascii; |
108 | } | 122 | } |
109 | 123 | ||
124 | + public void save() throws IOException { | ||
125 | + File file = new File("grid.data"); | ||
126 | + | ||
127 | + ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(file)); | ||
128 | + stream.writeObject(this); | ||
129 | + stream.close(); | ||
130 | + } | ||
131 | + | ||
132 | + public static Grid load() throws IOException, ClassNotFoundException { | ||
133 | + File file = new File("grid.data"); | ||
134 | + | ||
135 | + ObjectInputStream stream = new ObjectInputStream(new FileInputStream(file)); | ||
136 | + | ||
137 | + return (Grid) stream.readObject(); | ||
138 | + } | ||
139 | + | ||
110 | } | 140 | } |
src/kernel/exception/CannotDeleteCellException.java
0 → 100644
src/kernel/exception/CellNotFoundException.java
1 | package kernel.exception; | 1 | package kernel.exception; |
2 | 2 | ||
3 | public class CellNotFoundException extends Exception { | 3 | public class CellNotFoundException extends Exception { |
4 | + | ||
5 | + public CellNotFoundException() { | ||
6 | + super("Vous voulez utiliser une cellule qui n'est pas encore créée."); | ||
7 | + } | ||
4 | } | 8 | } |