Procházet zdrojové kódy

Merge branch 'Ohne_Drag_and_Drop' of https://git.tk.informatik.tu-darmstadt.de/carlos.garcia/praktikum-holons.git into Ohne_Drag_and_Drop

Kevin Trometer před 8 roky
rodič
revize
4f0887199c
2 změnil soubory, kde provedl 62 přidání a 41 odebrání
  1. binární
      bin/ui/view/AddObjectPopUp.class
  2. 62 41
      src/ui/view/AddObjectPopUp.java

binární
bin/ui/view/AddObjectPopUp.class


+ 62 - 41
src/ui/view/AddObjectPopUp.java

@@ -22,6 +22,8 @@ public class AddObjectPopUp extends JDialog {
 	private AddElementPopUp addElement;
 	private JTextField textField;
 	private JTextField textField_1;
+	private File selectedFile = null;
+	private String filePath = " ";
 
 	/**
 	 * Launch the application.
@@ -58,55 +60,23 @@ public class AddObjectPopUp extends JDialog {
 			contentPanel.add(textField);
 			textField.setColumns(10);
 		}
+		{
+			textField_1 = new JTextField();
+			textField_1.setBounds(135, 51, 271, 20);
+			contentPanel.add(textField_1);
+			textField_1.setColumns(10);
+		}
 		{
 			JButton btnBrowseImage = new JButton("Browse Image");
 			btnBrowseImage.setBounds(10, 50, 112, 23);
 			contentPanel.add(btnBrowseImage);
 			btnBrowseImage.addMouseListener(new MouseAdapter() {
-				public void mouseClicked(MouseEvent e){
-		            JFileChooser FileChooser = new JFileChooser();
-		            FileNameExtensionFilter filter = new FileNameExtensionFilter("png, jpg or jpeg", "png", "jpg", "jpeg");
-		            FileChooser.setFileFilter(filter);
-		            int returnValue = FileChooser.showOpenDialog(null);
-		            if (returnValue == JFileChooser.APPROVE_OPTION){
-		                File selectedFile = FileChooser.getSelectedFile();
-		                String filePath = selectedFile.getAbsolutePath();
-		                InputStream inStream = null;
-		                OutputStream outStream = null;
-		                try{
-		                    File source =new File(filePath);
-		                    File dest =new File(System.getProperty("user.dir") + "/res/Images/", selectedFile.getName());
-		                    inStream = new FileInputStream(source);
-		                    outStream = new FileOutputStream(dest);
-
-		                    byte[] buffer = new byte[1024];
-
-		                    int length;
-		                    while ((length = inStream.read(buffer)) > 0){
-		                        outStream.write(buffer, 0, length);
-		                    }
-
-		                    if (inStream != null)inStream.close();
-		                    if (outStream != null)outStream.close();
-		                    System.out.println("File Copied..");
-		                }catch(IOException e1){
-		                    e1.printStackTrace();
-		                }
-		              //  textArea.setText("File Loaded: " + selectedFile.getName() + "\n\n\n" + "Hit 'Run Code'");
-		            }
-		            else System.out.println("Failed to Load");
-		                //UnitXMLReader.ChosenFile = filePath;
-
-		        }
+				public void mouseClicked(MouseEvent e) {
+					fileChooser();
+				}
 			});
 
 		}
-		{
-			textField_1 = new JTextField();
-			textField_1.setBounds(135, 51, 271, 20);
-			contentPanel.add(textField_1);
-			textField_1.setColumns(10);
-		}
 		{
 			JList list = new JList();
 			list.setBounds(10, 98, 242, 130);
@@ -129,6 +99,15 @@ public class AddObjectPopUp extends JDialog {
 			getContentPane().add(buttonPane, BorderLayout.SOUTH);
 			{
 				JButton okButton = new JButton("OK");
+				okButton.addMouseListener(new MouseAdapter() {
+					public void mouseClicked(MouseEvent e) {
+						if (!textField_1.getText().equals(filePath)) {
+							fileChooser();
+						} else {
+							copieFile();
+						}
+					}
+				});
 				okButton.setActionCommand("OK");
 				buttonPane.add(okButton);
 				getRootPane().setDefaultButton(okButton);
@@ -141,4 +120,46 @@ public class AddObjectPopUp extends JDialog {
 		}
 	}
 
+	protected void fileChooser() {
+		// TODO Auto-generated method stub
+		JFileChooser FileChooser = new JFileChooser();
+		FileNameExtensionFilter filter = new FileNameExtensionFilter("png, jpg or jpeg", "png", "jpg", "jpeg");
+		FileChooser.setFileFilter(filter);
+		int returnValue = FileChooser.showOpenDialog(null);
+		if (returnValue == JFileChooser.APPROVE_OPTION) {
+			selectedFile = FileChooser.getSelectedFile();
+			filePath = selectedFile.getAbsolutePath();
+			textField_1.setText(filePath);
+		} else {
+			System.out.println("Failed to Load");
+		}
+
+	}
+
+	protected void copieFile() {
+		InputStream inStream = null;
+		OutputStream outStream = null;
+		try {
+			File source = new File(filePath);
+			File dest = new File(System.getProperty("user.dir") + "/res/Images/", selectedFile.getName());
+			inStream = new FileInputStream(source);
+			outStream = new FileOutputStream(dest);
+
+			byte[] buffer = new byte[1024];
+
+			int length;
+			while ((length = inStream.read(buffer)) > 0) {
+				outStream.write(buffer, 0, length);
+			}
+
+			if (inStream != null)
+				inStream.close();
+			if (outStream != null)
+				outStream.close();
+			System.out.println("File Copied..");
+		} catch (IOException e1) {
+			e1.printStackTrace();
+		}
+	}
+
 }