123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- package ui.view;
- import classes.AbstractCanvasObject;
- import classes.HolonBattery;
- import classes.HolonElement;
- import classes.HolonObject;
- import classes.Pair;
- import ui.controller.Control;
- import java.lang.NumberFormatException;
- import ui.model.Model;
- import utility.Util;
- 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 AbstractCanvasObject toEdit;
- private boolean editState;
- private boolean imageChanged = false;
- private Model model;
- /**
- * Launch the application.
- *
- * @param args
- * standard
- */
- public static void main(String[] args) {
- try {
- AddObjectPopUp dialog = new AddObjectPopUp(false,new HolonBattery("jo") , "hei", 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, AbstractCanvasObject obj, String cat, JFrame parentFrame) {
- if(obj instanceof HolonBattery)
- {
- BatteryPopUp(edit, obj, cat, parentFrame);
- return;
- }
- toEdit = obj;
- editState = edit;
- this.model=model;
- this.setIconImage(Util.loadImage("/Images/Holeg.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(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, model);
- 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());
- }
- }
- }
- protected void BatteryPopUp(boolean edit, AbstractCanvasObject obj, String cat, JFrame parentFrame) {
- //TODO: Click mich <3
- HolonBattery editBat = (HolonBattery) obj;
- //Window Settings
- this.setIconImage(Util.loadImage("/Images/battery.png",30,30));
- this.setTitle("Edit Battery");
- setBounds(0, 0, 285, 290);
- setLocationRelativeTo(parentFrame);
-
- //Labels, TextFiels, Buttons
- JPanel myPanel = new JPanel();
- myPanel.setLayout(null);
- int beginBoxX = 140, beginLabelX = 30;
- int beginColumAtY = 20, newColumY = 40;
- JLabel batteryNameLabel = new JLabel("Name:");
- JTextField batteryNameBox = new JTextField(10);
- batteryNameBox.setText(editBat.getName());
- JLabel batteryInRateLabel = new JLabel("In ratio:");
- JTextField batteryInRateBox = new JTextField(10);
- batteryInRateBox.setText(Float.toString(editBat.getInRatio()));
- JLabel batteryOutRateLabel = new JLabel("Out ratio:");
- JTextField batteryOutRateBox = new JTextField(10);
- batteryOutRateBox.setText(Float.toString(editBat.getOutRatio()));
- JLabel batteryCapasityLabel = new JLabel("Capacity:");
- JTextField batteryCapasityBox = new JTextField(10);
- batteryCapasityBox.setText(Float.toString(editBat.getCapacity()));
- JLabel batterySOCLabel = new JLabel("State of charge:");
- JTextField batterySOCBox = new JTextField(10);
- batterySOCBox.setText(Float.toString(editBat.getInitialStateOfCharge()));
-
-
- batteryNameLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
- batteryNameBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
- myPanel.add(batteryNameLabel);
- myPanel.add(batteryNameBox);
- beginColumAtY += newColumY;
-
- batteryInRateLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
- batteryInRateBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
- myPanel.add(batteryInRateLabel);
- myPanel.add(batteryInRateBox);
- beginColumAtY += newColumY;
-
- batteryOutRateLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
- batteryOutRateBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
- myPanel.add(batteryOutRateLabel);
- myPanel.add(batteryOutRateBox);
- beginColumAtY += newColumY;
-
- batteryCapasityLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
- batteryCapasityBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
- myPanel.add(batteryCapasityLabel);
- myPanel.add(batteryCapasityBox);
- beginColumAtY += newColumY;
-
- batterySOCLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
- batterySOCBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
- myPanel.add(batterySOCLabel);
- myPanel.add(batterySOCBox);
-
- //Save , Cancel
- JPanel bottomPanel = new JPanel();
- bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
-
- JButton saveButton = new JButton("Save");
- bottomPanel.add(saveButton);
-
- saveButton.addActionListener(e -> {
- //SaveButten Event:
- if(batteryNameBox.getText().isEmpty()){ //Check for valid name
- JOptionPane.showMessageDialog(getContentPane(), "Name is empty." , "Wrong input" , JOptionPane.WARNING_MESSAGE);
- }else{
- editBat.setName(batteryNameBox.getText());
- try{//Check for floats inputs
- float changedInRatio = Float.valueOf(batteryInRateBox.getText());
- float changedOutRatio = Float.valueOf(batteryOutRateBox.getText());
- float changedCapasity = Float.valueOf(batteryCapasityBox.getText());
- float changedSOC = Float.valueOf(batterySOCBox.getText());
- //Saving:
- editBat.setInRatio(changedInRatio);
- editBat.setOutRatio(changedOutRatio);
- editBat.setCapacity(changedCapasity);
- editBat.setInitialStateOfCharge(changedSOC);
- try {
- controller.saveCategory();
- controller.notifyAll();
- } catch (Exception e1) { //Controller Exceptions
- }
- dispose();
- }catch(NumberFormatException FloatException){
- String wrong= FloatException.getMessage();
- String message;
- if(wrong.startsWith("empty")){
- message = "A field is empty.";
- }else{
- message = wrong.substring(18) + " is not a valid Number.";
- }
- JOptionPane.showMessageDialog(getContentPane(), message, "Wrong input" , JOptionPane.WARNING_MESSAGE);
- }
- }
- });
-
- JButton cancelButton = new JButton("Cancel");
- bottomPanel.add(cancelButton);
- cancelButton.addActionListener(e -> dispose());
-
-
-
- getContentPane().setLayout(new BorderLayout());
- getContentPane().add(myPanel, BorderLayout.CENTER);
- getContentPane().add(bottomPanel, BorderLayout.SOUTH);
- }
- // /**
- // * 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;
- }
- }
|