Browse Source

Added image display

EyeXion 3 years ago
parent
commit
ee791c1045

+ 12
- 0
.idea/dataSources.xml View File

@@ -0,0 +1,12 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="DataSourceManagerImpl" format="xml" multifile-model="true">
4
+    <data-source source="LOCAL" name="@localhost" uuid="f82aebd1-fd1a-4e96-9405-381c0ad20dcf">
5
+      <driver-ref>mysql.8</driver-ref>
6
+      <synchronize>true</synchronize>
7
+      <jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver>
8
+      <jdbc-url>jdbc:mysql://localhost:3306</jdbc-url>
9
+      <working-dir>$ProjectFileDir$</working-dir>
10
+    </data-source>
11
+  </component>
12
+</project>

+ 2
- 1
src/main/java/app/insa/clav/Messages/MessageDisplay.java View File

@@ -14,7 +14,8 @@ public class MessageDisplay {
14 14
     private String payload;
15 15
     /**
16 16
      * Type 1 --> Normal text message
17
-     * Type 2 --> File message
17
+     * Type 2 --> File message not image
18
+     * Type 3 --> Image file (png, jpg, gif, jpeg, svg)
18 19
      */
19 20
     private int type;
20 21
 

+ 12
- 1
src/main/java/app/insa/clav/Messages/MessageDisplayFile.java View File

@@ -5,10 +5,13 @@ import java.io.File;
5 5
 public class MessageDisplayFile extends MessageDisplay{
6 6
 
7 7
     private File file;
8
+    private String ext;
8 9
 
9
-    public MessageDisplayFile(int sourceId, String date, String payload, int type, File file) {
10
+
11
+    public MessageDisplayFile(int sourceId, String date, String payload, int type, File file, String ext) {
10 12
         super(sourceId, date, payload, type);
11 13
         this.file = file;
14
+        this.ext = ext;
12 15
     }
13 16
 
14 17
     public MessageDisplayFile(File file) {
@@ -22,4 +25,12 @@ public class MessageDisplayFile extends MessageDisplay{
22 25
     public void setFile(File file) {
23 26
         this.file = file;
24 27
     }
28
+
29
+    public String getExt() {
30
+        return ext;
31
+    }
32
+
33
+    public void setExt(String ext) {
34
+        this.ext = ext;
35
+    }
25 36
 }

+ 12
- 3
src/main/java/app/insa/clav/Reseau/TCPChatConnection.java View File

@@ -142,7 +142,17 @@ public class TCPChatConnection extends Thread{
142 142
                         size -= bytes;
143 143
                     }
144 144
                     System.out.println("Reception fichier terminée");
145
-                    this.msgReceivedBufferFiles.add(new MessageDisplayFile(this.remoteUserId,msgFile.date,msgFile.payload,2,file));
145
+                    int type = 2;
146
+                    switch (msgFile.ext) {
147
+                        case "png":
148
+                        case "gif":
149
+                        case "jpeg":
150
+                        case "svg":
151
+                        case "jpg":
152
+                            type = 3;
153
+                            break;
154
+                    }
155
+                    this.msgReceivedBufferFiles.add(new MessageDisplayFile(this.remoteUserId,msgFile.date,msgFile.payload,type,file,msgFile.ext));
146 156
                     this.support.firePropertyChange("fileReceived",true,false);
147 157
                     fileOutputStream.close();
148 158
                 } catch (IOException e) {
@@ -168,8 +178,7 @@ public class TCPChatConnection extends Thread{
168 178
 
169 179
     public void sendMessageFile(MessageDisplayFile msgDisp){
170 180
         int bytes = 0;
171
-        String ext = FilenameUtils.getExtension(msgDisp.getFile().getPath());
172
-        MessageChatFile msgStartofFile = new MessageChatFile(9,this.link.getLocalAddress(),this.link.getLocalPort(),this.link.getInetAddress(),this.link.getPort(),msgDisp.getPayload(),msgDisp.getDate(),msgDisp.getFile().length(),ext);
181
+        MessageChatFile msgStartofFile = new MessageChatFile(9,this.link.getLocalAddress(),this.link.getLocalPort(),this.link.getInetAddress(),this.link.getPort(),msgDisp.getPayload(),msgDisp.getDate(),msgDisp.getFile().length(),msgDisp.getExt());
173 182
         try {
174 183
             this.objectOutStream.writeObject(msgStartofFile);
175 184
         } catch (IOException e) {

+ 56
- 5
src/main/java/app/insa/clav/UIControllers/ChatWindowController.java View File

@@ -30,6 +30,7 @@ import javafx.scene.text.TextAlignment;
30 30
 import javafx.stage.FileChooser;
31 31
 import javafx.stage.Stage;
32 32
 import javafx.stage.WindowEvent;
33
+import org.apache.commons.io.FilenameUtils;
33 34
 
34 35
 import javax.swing.*;
35 36
 import java.awt.*;
@@ -37,6 +38,7 @@ import java.beans.PropertyChangeEvent;
37 38
 import java.beans.PropertyChangeListener;
38 39
 import java.io.File;
39 40
 import java.io.IOException;
41
+import java.net.MalformedURLException;
40 42
 import java.net.URL;
41 43
 import java.text.SimpleDateFormat;
42 44
 import java.util.ArrayList;
@@ -153,9 +155,9 @@ public class ChatWindowController implements Initializable, PropertyChangeListen
153 155
                                 //Desktop.getDesktop().browseFileDirectory(msgFile.getFile());
154 156
                             }
155 157
                         });
158
+                        HBox hbox = new HBox();
159
+                        hbox.setSpacing(2.0);
156 160
                         if (item.getSourceId() == remoteUser.getId()) {
157
-                            HBox hbox = new HBox();
158
-                            hbox.setSpacing(2.0);
159 161
                             hbox.setAlignment(Pos.CENTER_RIGHT);
160 162
                             hbox.setBackground(new Background(new BackgroundFill(Paint.valueOf(Color.TRANSPARENT.toString()), null, null)));
161 163
                             setBackground(new Background(new BackgroundFill(Paint.valueOf(Color.AZURE.toString()), null, null)));
@@ -168,8 +170,7 @@ public class ChatWindowController implements Initializable, PropertyChangeListen
168 170
                             setTextAlignment(TextAlignment.RIGHT);
169 171
                             setPadding(new Insets(10, 0, 10, param.getWidth() * 0.3));
170 172
                         } else {
171
-                            HBox hbox = new HBox();
172
-                            hbox.setSpacing(2.0);
173
+                            hbox.setAlignment(Pos.CENTER_LEFT);
173 174
                             hbox.setBackground(new Background(new BackgroundFill(Paint.valueOf(Color.TRANSPARENT.toString()), null, null)));
174 175
                             setBackground(new Background(new BackgroundFill(Paint.valueOf(Color.CORNSILK.toString()), null, null)));
175 176
                             ImageView img = new ImageView();
@@ -181,6 +182,45 @@ public class ChatWindowController implements Initializable, PropertyChangeListen
181 182
                             setTextAlignment(TextAlignment.LEFT);
182 183
                             setPadding(new Insets(10, param.getWidth() * 0.3, 10, 0));
183 184
                         }
185
+                    } else if (item.getType() == 3){
186
+                        MessageDisplayFile msgFile = (MessageDisplayFile) item;
187
+                        Image imageFileSource = null;
188
+                        try {
189
+                            imageFileSource = new Image(msgFile.getFile().toURI().toURL().toExternalForm());
190
+                        } catch (MalformedURLException e) {
191
+                            e.printStackTrace();
192
+                        }
193
+                        ImageView imageFileView = new ImageView();
194
+                        imageFileView.setImage(imageFileSource);
195
+                        HBox hbox = new HBox();
196
+                        hbox.setSpacing(2.0);
197
+                        imageFileView.setPreserveRatio(true);
198
+                        imageFileView.fitWidthProperty().bind(this.widthProperty());
199
+                        if (item.getSourceId() == remoteUser.getId()) {
200
+                            hbox.setAlignment(Pos.CENTER_RIGHT);
201
+                            hbox.setBackground(new Background(new BackgroundFill(Paint.valueOf(Color.TRANSPARENT.toString()), null, null)));
202
+                            setBackground(new Background(new BackgroundFill(Paint.valueOf(Color.AZURE.toString()), null, null)));
203
+                            ImageView img = new ImageView();
204
+                            img.setImage(imageRemote);
205
+                            hbox.getChildren().addAll(imageFileView, img);
206
+                            setGraphic(hbox);
207
+                            setContentDisplay(ContentDisplay.RIGHT);
208
+                            setAlignment(Pos.CENTER_RIGHT);
209
+                            setTextAlignment(TextAlignment.RIGHT);
210
+                            setPadding(new Insets(10, 0, 10, param.getWidth() * 0.3));
211
+                        } else {
212
+                            hbox.setAlignment(Pos.CENTER_LEFT);
213
+                            hbox.setBackground(new Background(new BackgroundFill(Paint.valueOf(Color.TRANSPARENT.toString()), null, null)));
214
+                            setBackground(new Background(new BackgroundFill(Paint.valueOf(Color.CORNSILK.toString()), null, null)));
215
+                            ImageView img = new ImageView();
216
+                            img.setImage(imageSource);
217
+                            hbox.getChildren().addAll(img, imageFileView);
218
+                            setGraphic(hbox);
219
+                            setContentDisplay(ContentDisplay.LEFT);
220
+                            setAlignment(Pos.CENTER_LEFT);
221
+                            setTextAlignment(TextAlignment.LEFT);
222
+                            setPadding(new Insets(10, param.getWidth() * 0.3, 10, 0));
223
+                        }
184 224
                     }
185 225
                 }
186 226
             }
@@ -279,7 +319,18 @@ public class ChatWindowController implements Initializable, PropertyChangeListen
279 319
 
280 320
         //We send the File if the filed is not null
281 321
         if (this.filePicked != null){
282
-            MessageDisplayFile msgFile = new MessageDisplayFile(model.user.getId(),timeStamp,this.filePicked.getName(),2,this.filePicked);
322
+            String ext = FilenameUtils.getExtension(this.filePicked.getPath());
323
+            int type = 2;
324
+            switch (ext) {
325
+                case "png":
326
+                case "gif":
327
+                case "jpeg":
328
+                case "svg":
329
+                case "jpg":
330
+                    type = 3;
331
+                    break;
332
+            }
333
+            MessageDisplayFile msgFile = new MessageDisplayFile(model.user.getId(),timeStamp,this.filePicked.getName(),type,this.filePicked, ext);
283 334
             this.listMessages.add(msgFile);
284 335
             this.tcpCo.sendMessageFile(msgFile);
285 336
             this.filePicked = null;

Loading…
Cancel
Save