123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- package ui.view;
- import classes.AbstractCpsObject;
- import classes.HolonElement;
- import classes.HolonObject;
- import classes.Pair;
- import ui.controller.Control;
- import javax.swing.*;
- import javax.swing.border.EmptyBorder;
- import javax.swing.filechooser.FileNameExtensionFilter;
- import java.awt.*;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.io.*;
- import java.util.ArrayList;
- /**
- * Popup for adding a Holon Object to a Category.
- *
- * @author Gruppe14
- */
- public class AddObjectPopUp extends JDialog {
- private static final long serialVersionUID = 1L;
- private AddElementPopUp addElement;
- private JTextField objectName;
- private JTextField sourcePath;
- private ArrayList<HolonElement> hElements;
- private DefaultListModel listModel;
- private JList list;
- private String imagePath;
- // private HolonObject theObject;
- private Control controller;
- private File selectedFile = null;
- private String filePath = " ";
- private String givenCategory;
- private JLabel lblImagePreview;
- private AbstractCpsObject toEdit;
- private boolean editState;
- private boolean imageChanged = false;
- // /**
- // * Launch the application.
- // *
- // * @param args
- // * standard
- // */
- // public static void main(String[] args) {
- // try {
- // AddObjectPopUp dialog = new AddObjectPopUp(false, null, null);
- // dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
- // dialog.setVisible(true);
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- // }
- /**
- * Create the dialog.
- *
- * @param edit
- * true if edit
- * @param obj
- * the object
- * @param cat
- * the categorie
- */
- AddObjectPopUp(boolean edit, AbstractCpsObject obj, String cat, JFrame parentFrame) {
- toEdit = obj;
- editState = edit;
- this.setIconImage(Util.loadImage(this, "/Images/Dummy_House.png",30,30));
- setBounds(100, 100, 450, 342);
- setLocationRelativeTo(parentFrame);
- getContentPane().setLayout(new BorderLayout());
- JPanel contentPanel = new JPanel();
- contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
- getContentPane().add(contentPanel, BorderLayout.CENTER);
- contentPanel.setLayout(null);
- hElements = new ArrayList<>();
- this.setTitle(Languages.getLanguage()[58]);
- {
- JLabel lblName = new JLabel(Languages.getLanguage()[59]);
- lblName.setHorizontalAlignment(SwingConstants.CENTER);
- lblName.setBounds(28, 21, 76, 14);
- contentPanel.add(lblName);
- }
- {
- objectName = new JTextField();
- objectName.addKeyListener(new KeyListener() {
- @Override
- public void keyPressed(KeyEvent arg0) {
- }
- @Override
- public void keyReleased(KeyEvent e) {
- }
- @Override
- public void keyTyped(KeyEvent e) {
- objectName.setBackground(Color.WHITE);
- }
- });
- if (edit) {
- objectName.setText(obj.getName());
- }
- objectName.setBounds(98, 18, 172, 20);
- contentPanel.add(objectName);
- objectName.setColumns(10);
- }
- {
- JButton btnBrowseImage = new JButton(Languages.getLanguage()[60]);
- btnBrowseImage.setBounds(10, 75, 134, 23);
- contentPanel.add(btnBrowseImage);
- btnBrowseImage.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent e) {
- fileChooser();
- }
- });
- }
- {
- lblImagePreview = new JLabel("");
- lblImagePreview.setBounds(295, 3, 50, 50);
- contentPanel.add(lblImagePreview);
- }
- {
- sourcePath = new JTextField();
- sourcePath.addKeyListener(new KeyListener() {
- @Override
- public void keyPressed(KeyEvent arg0) {
- }
- @Override
- public void keyReleased(KeyEvent e) {
- }
- @Override
- public void keyTyped(KeyEvent e) {
- sourcePath.setBackground(Color.WHITE);
- }
- });
- if (edit) {
- //TODO: check why this needs a file?
- /*
- selectedFile = Util.loadStream(this,obj.getImage());
- filePath = selectedFile.getAbsolutePath();
- sourcePath.setText(filePath);
- ImageIcon icon = new ImageIcon(
- new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
- */
- lblImagePreview.setIcon(new ImageIcon(Util.loadImage(this, obj.getImage(), 50, 50)));
- }
- sourcePath.setBounds(148, 77, 271, 20);
- contentPanel.add(sourcePath);
- sourcePath.setColumns(10);
- }
- {
- JButton btnAddDefaultElement = new JButton(Languages.getLanguage()[61]);
- btnAddDefaultElement.addActionListener(actionEvent -> {
- addElement = new AddElementPopUp(parentFrame);
- addElement.setActualCps(toEdit);
- addElement.setVisible(true);
- HolonElement hl = addElement.getElement();
- hl.setSaving(new Pair<>(givenCategory, objectName.getText()));
- // if (hl != null) {
- // hl.setSav(givenCategory);
- // }
- // hl.setObj(objectName.getText());
- addElement(hl);
- });
- btnAddDefaultElement.setBounds(270, 144, 142, 23);
- contentPanel.add(btnAddDefaultElement);
- }
- {
- JScrollPane scrollPane = new JScrollPane();
- scrollPane.setBounds(10, 114, 236, 150);
- contentPanel.add(scrollPane);
- {
- listModel = new DefaultListModel();
- /*
- * HolonElement hel = new HolonElement("Test", 100, 5); String
- * name = hel.getEleName(); for (int i = 0; i < 11; i++) {
- * hel.setEleName(name + i); addElement(hel); }
- */
- list = new JList(listModel);
- scrollPane.setViewportView(list);
- }
- }
- if (edit) {
- for (HolonElement e : ((HolonObject) obj).getElements()) {
- addElement(e);
- }
- }
- {
- JButton btnNewButton = new JButton(Languages.getLanguage()[62]);
- btnNewButton.addActionListener(actionEvent -> {
- int selectedIndex = list.getSelectedIndex();
- if (selectedIndex != -1) {
- listModel.remove(selectedIndex);
- hElements.remove(selectedIndex);
- }
- });
- btnNewButton.setBounds(270, 182, 142, 27);
- contentPanel.add(btnNewButton);
- }
- {
- JPanel buttonPane = new JPanel();
- buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
- getContentPane().add(buttonPane, BorderLayout.SOUTH);
- {
- JButton okButton = new JButton("OK");
- okButton.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent e) {
- // Component frame = null;
- if (objectName.getText().length() > 0) {
- if (sourcePath.getText().equals(filePath)) {
- imagePath = filePath;
- if (imageChanged)
- copieFile();
- imageChanged = false;
- // theObject = new
- // HolonObject(objectName.getText());
- // theObject.setElements(hElements);
- // theObject.setImage(imagePath);// TODO Auto-generated catch block
- try {
- if (editState) {
- controller.delObjectCategory(givenCategory, toEdit.getName());
- hElements.forEach(ele -> ele
- .setSaving(new Pair<>(ele.getSaving().getKey(), objectName.getText())));
- controller.addObject(controller.searchCategory(givenCategory),
- objectName.getText(), hElements, imagePath);
- } else {
- controller.addObject(controller.searchCategory(givenCategory),
- objectName.getText(), hElements, imagePath);
- }
- } catch (Exception e2) {
- }
- // controller.addObjectCategory(controller.searchCategory(givenCategory),
- // theObject);
- //
- // System.out.println(theObject.getImage());
- dispose();
- } else {
- sourcePath.setBackground(new Color(255, 50, 50));
- }
- } else {
- objectName.setBackground(new Color(255, 50, 50));
- if (!sourcePath.getText().equals(filePath))
- sourcePath.setBackground(new Color(255, 50, 50));
- }
- }
- });
- okButton.setActionCommand("OK");
- buttonPane.add(okButton);
- getRootPane().setDefaultButton(okButton);
- }
- {
- JButton cancelButton = new JButton(Languages.getLanguage()[63]);
- cancelButton.setActionCommand("Cancel");
- buttonPane.add(cancelButton);
- cancelButton.addActionListener(e -> dispose());
- }
- }
- }
- // /**
- // * Get Jar Containing Folder.
- // *
- // * @param aclass aClass
- // * @return String
- // * @throws Exception Exception
- // */
- // public static String getJarContainingFolder(Class aclass) throws Exception {
- // CodeSource codeSource = aclass.getProtectionDomain().getCodeSource();
- //
- // File jarFile;
- //
- // if (codeSource.getLocation() != null) {
- // jarFile = new File(codeSource.getLocation().toURI());
- // } else {
- // String path = aclass.getResource(aclass.getSimpleName() + ".class").getPath();
- // String jarFilePath = path.substring(path.indexOf(":") + 1, path.indexOf("!"));
- // jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8");
- // jarFile = new File(jarFilePath);
- // }
- // return jarFile.getParentFile().getAbsolutePath();
- // }
- /**
- * adds a Holon Element.
- *
- * @param hl
- * the HolonElement
- */
- private void addElement(HolonElement hl) {
- hElements.add(hl);
- listModel.addElement(hl.getAmount() + "x: " + hl.getEleName() + " " + hl.getEnergyPerElement() + "U");
- }
- /**
- * Choose the file.
- */
- private void fileChooser() {
- 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();
- sourcePath.setText(filePath);
- ImageIcon icon = new ImageIcon(//TODO: ugly
-
- new ImageIcon(filePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
- lblImagePreview.setIcon(icon);
- imageChanged = true;
- } else {
- System.out.println("Failed to Load");
- }
- }
- /**
- * Copies the File.
- */
- private void copieFile() {
- InputStream inStream;
- OutputStream outStream;
- try {
- File source = new File(filePath);
- File dest = new File(System.getProperty("user.home") + "/.config/HolonGUI/Images/");
- dest.mkdirs();
- dest = new File(dest, selectedFile.getName());
- imagePath = "" + dest;
- 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);
- }
- inStream.close();
- outStream.close();
- System.out.println("File Copied..");
- } catch (IOException eex) {
- eex.printStackTrace();
- }
- }
- // /**
- // * Edit the Information.
- // *
- // * @param obj
- // * the CpsObject
- // */
- // public void editInformation(HolonObject obj) {
- // objectName.setText(obj.getName());
- // }
- // /**
- // * Return the Object.
- // *
- // * @return the CpsObject
- // */
- // public HolonObject getObject() {
- // return theObject;
- // }
- /**
- * Sets the Controller.
- *
- * @param controller
- * the controller
- */
- public void setController(Control controller) {
- this.controller = controller;
- }
- /**
- * Set the Category.
- *
- * @param cat
- * the Category
- */
- public void setCategory(String cat) {
- givenCategory = cat;
- }
- }
|