Java tutorial
/** * Copyright 2014/2015 JeremGamer * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package io.github.jeremgamer.editor.panels.components; import io.github.jeremgamer.editor.Editor; import io.github.jeremgamer.editor.files.PanelSave; import io.github.jeremgamer.editor.panels.Buttons; import io.github.jeremgamer.editor.panels.Labels; import io.github.jeremgamer.editor.panels.Others; import io.github.jeremgamer.editor.panels.Panels; import io.github.jeremgamer.editor.panels.components.PanelsPanel.GridLayoutEditor.GridComponentPanel; import io.github.jeremgamer.resources.images.ImageGetter; import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import javax.imageio.ImageIO; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JSpinner; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SpinnerModel; import javax.swing.SpinnerNumberModel; import javax.swing.event.CaretListener; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.filechooser.FileNameExtensionFilter; import org.apache.commons.io.IOUtils; public class PanelsPanel extends JPanel implements ComponentEditor { /** * */ private static final long serialVersionUID = -8239018737876710239L; private Thread t; private Thread t2; private Thread t3; private ArrayList<GridComponentPanel> gridList = new ArrayList<GridComponentPanel>(); private PanelSave ps; private static JTextField name = new JTextField(); private JComboBox<String> layout = new JComboBox<String>(); private CardLayout cl = new CardLayout(); String[] listContent = { "basic", "border", "boxLine", "boxPage", "grid", "card" }; JPanel advanced = new JPanel(cl); BorderLayoutEditor brdle = new BorderLayoutEditor(); BasicLayoutEditor ble = new BasicLayoutEditor(); LineLayoutEditor lle = new LineLayoutEditor(); RowLayoutEditor rle = new RowLayoutEditor(); GridLayoutEditor gle = new GridLayoutEditor(); CardLayoutEditor cle = new CardLayoutEditor(); static JComboBox<String> compList = new JComboBox<String>(); static JComboBox<String> compList2 = new JComboBox<String>(); static JComboBox<String> compList3 = new JComboBox<String>(); static JComboBox<String> panelList = new JComboBox<String>(); static JButton addBasic = null; static JButton addLine = null; static JButton addRow = null; static JButton addCard = null; private JCheckBox insetsEnabled = new JCheckBox("Activs"); private SpinnerModel model = new SpinnerNumberModel(1, 1, 100, 1); private JSpinner insetHz = new JSpinner(model); private SpinnerModel model2 = new SpinnerNumberModel(1, 1, 100, 1); private JSpinner insetVt = new JSpinner(model2); private static JCheckBox webEnabled = new JCheckBox("Activ"); private JTextField adress = new JTextField(); private JCheckBox opaque = new JCheckBox("Opaque"); private ColorPanel cp = new ColorPanel(null); private static JComboBox<String> topBox = new JComboBox<String>(); private static JComboBox<String> leftBox = new JComboBox<String>(); private static JComboBox<String> centerBox = new JComboBox<String>(); private static JComboBox<String> rightBox = new JComboBox<String>(); private static JComboBox<String> bottomBox = new JComboBox<String>(); private JButton remove = null; private JLabel nameBackground = new JLabel(""); private JRadioButton size = new JRadioButton("Conserver la taille"); private JRadioButton size2 = new JRadioButton("tirer"); private ButtonGroup bg = new ButtonGroup(); private JButton browseImage = new JButton("Parcourir..."); private JCheckBox prefSizeEnabled = new JCheckBox("Activ"); private SpinnerModel model3 = new SpinnerNumberModel(200, 0, 9999, 1); private JSpinner prefWidth = new JSpinner(model3); private SpinnerModel model4 = new SpinnerNumberModel(200, 0, 9999, 1); private JSpinner prefHeight = new JSpinner(model4); private JFrame frame; public PanelsPanel(JFrame frame, final PanelSave ps) { this.ps = ps; this.frame = frame; this.setSize(new Dimension(395, frame.getHeight() - 27 - 23)); this.setLocation(300, 0); this.setBorder(BorderFactory.createTitledBorder("Edition du panneau")); JPanel content = new JPanel(); JScrollPane scroll = new JScrollPane(content); scroll.getVerticalScrollBar().setUnitIncrement(Editor.SCROLL_SPEED); scroll.setBorder(null); content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS)); scroll.setPreferredSize(new Dimension(382, frame.getHeight() - 27 - 46 - 20)); JPanel namePanel = new JPanel(); name.setPreferredSize(new Dimension(this.getWidth() - 280, 30)); name.setEditable(false); namePanel.add(new JLabel("Nom :")); namePanel.add(name); namePanel.add(Box.createRigidArea(new Dimension(10, 1))); layout.addItem("Basique"); layout.addItem("Bordures"); layout.addItem("Ligne"); layout.addItem("Colonne"); layout.addItem("Grille"); layout.addItem("Empil"); layout.setPreferredSize(new Dimension(110, 30)); layout.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { @SuppressWarnings("unchecked") JComboBox<String> combo = (JComboBox<String>) event.getSource(); cl.show(advanced, listContent[combo.getSelectedIndex()]); ps.set("layout", combo.getSelectedIndex()); ActionPanel.updateLists(); } }); namePanel.add(new JLabel("Disposition :")); namePanel.add(layout); namePanel.setPreferredSize(new Dimension(365, 50)); namePanel.setMaximumSize(new Dimension(365, 50)); content.add(namePanel); advanced.setPreferredSize(new Dimension(365, 300)); advanced.setMaximumSize(new Dimension(365, 300)); advanced.add(ble, listContent[0]); advanced.add(brdle, listContent[1]); advanced.add(lle, listContent[2]); advanced.add(rle, listContent[3]); advanced.add(gle, listContent[4]); advanced.add(cle, listContent[5]); content.add(advanced); topBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { @SuppressWarnings("unchecked") JComboBox<String> combo = (JComboBox<String>) event.getSource(); ps.set("border.top", combo.getSelectedItem()); } }); leftBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { @SuppressWarnings("unchecked") JComboBox<String> combo = (JComboBox<String>) event.getSource(); ps.set("border.left", combo.getSelectedItem()); } }); centerBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { @SuppressWarnings("unchecked") JComboBox<String> combo = (JComboBox<String>) event.getSource(); ps.set("border.center", combo.getSelectedItem()); } }); rightBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { @SuppressWarnings("unchecked") JComboBox<String> combo = (JComboBox<String>) event.getSource(); ps.set("border.right", combo.getSelectedItem()); } }); bottomBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { @SuppressWarnings("unchecked") JComboBox<String> combo = (JComboBox<String>) event.getSource(); ps.set("border.bottom", combo.getSelectedItem()); } }); JPanel prefSize = new JPanel(); prefSize.setPreferredSize(new Dimension(365, 110)); prefSize.setMaximumSize(new Dimension(365, 110)); prefSize.setBorder(BorderFactory.createTitledBorder("Taille prfre")); JPanel prefSizePanel = new JPanel(); prefSizePanel.setLayout(new GridLayout(2, 4)); prefSizePanel.setPreferredSize(new Dimension(300, 55)); prefSizePanel.setMaximumSize(new Dimension(300, 55)); prefSizePanel.add(prefSizeEnabled); prefSizePanel.add(new JLabel("")); prefSizePanel.add(new JLabel("")); prefSizePanel.add(new JLabel("(en pixels)")); prefSizePanel.add(new JLabel("Largeur :")); prefSizePanel.add(prefWidth); prefSizePanel.add(new JLabel("Hauteur :")); prefSizePanel.add(prefHeight); prefWidth.setEnabled(false); prefHeight.setEnabled(false); prefSizeEnabled.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { JCheckBox check = (JCheckBox) event.getSource(); ps.set("preferredSize", check.isSelected()); prefWidth.setEnabled(check.isSelected()); prefHeight.setEnabled(check.isSelected()); } }); prefWidth.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent event) { JSpinner spinner = (JSpinner) event.getSource(); ps.set("preferredWidth", spinner.getValue()); } }); prefHeight.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent event) { JSpinner spinner = (JSpinner) event.getSource(); ps.set("preferredHeight", spinner.getValue()); } }); prefSize.add(prefSizePanel); content.add(prefSize); JPanel insetsPanel = new JPanel(); insetsPanel.setBorder(BorderFactory.createTitledBorder("carts")); insetsPanel.setPreferredSize(new Dimension(365, 100)); insetsPanel.setMaximumSize(new Dimension(365, 100)); JPanel insetsContent = new JPanel(); insetsContent.setLayout(new BoxLayout(insetsContent, BoxLayout.PAGE_AXIS)); JPanel insetInput = new JPanel(); insetInput.setLayout(new GridLayout(2, 4)); insetInput.add(insetsEnabled); insetInput.add(new JLabel("")); insetInput.add(new JLabel("")); insetInput.add(new JLabel("(en pixels)")); insetInput.add(new JLabel("Horizontaux :")); insetInput.add(insetHz); insetInput.add(new JLabel("Verticaux :")); insetInput.add(insetVt); insetsEnabled.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { JCheckBox check = (JCheckBox) event.getSource(); if (check.isSelected()) { insetHz.setEnabled(true); insetVt.setEnabled(true); ps.set("insets", true); } else { insetHz.setEnabled(true); insetVt.setEnabled(true); ps.set("insets", false); } } }); insetHz.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent event) { JSpinner spinner = (JSpinner) event.getSource(); ps.set("insets.horizontal", spinner.getValue()); } }); insetVt.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent event) { JSpinner spinner = (JSpinner) event.getSource(); ps.set("insets.vertical", spinner.getValue()); } }); insetsContent.add(insetInput); insetsPanel.add(insetsContent); content.add(insetsPanel); JPanel web = new JPanel(); web.setPreferredSize(new Dimension(365, 100)); web.setMaximumSize(new Dimension(365, 100)); web.setBorder(BorderFactory.createTitledBorder("Page Web")); JPanel webContent = new JPanel(); webContent.setLayout(new BorderLayout()); webContent.add(webEnabled, BorderLayout.NORTH); webEnabled.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JCheckBox check = (JCheckBox) e.getSource(); ps.set("web", check.isSelected()); if (check.isSelected() == true) { layout.setSelectedIndex(0); layout.setEnabled(false); ble.removeAllComponents(); ble.disableComponents(); adress.setEnabled(true); } else { ble.enableComponents(); layout.setEnabled(true); adress.setEnabled(false); } } }); JPanel webInput = new JPanel(); webInput.add(new JLabel("Adresse :")); adress.setPreferredSize(new Dimension(250, 30)); CaretListener caretUpdate = new CaretListener() { public void caretUpdate(javax.swing.event.CaretEvent e) { JTextField text = (JTextField) e.getSource(); ps.set("web.adress", text.getText()); } }; adress.addCaretListener(caretUpdate); webInput.add(adress); webContent.add(webInput, BorderLayout.CENTER); web.add(webContent); JPanel background = new JPanel(); BorderLayout bLayout = new BorderLayout(); bLayout.setVgap(12); background.setLayout(bLayout); background.setBorder(BorderFactory.createTitledBorder("Couleur de fond")); background.setPreferredSize(new Dimension(365, 210)); background.setMaximumSize(new Dimension(365, 210)); cp.setPreferredSize(new Dimension(347, 145)); cp.setMaximumSize(new Dimension(347, 145)); opaque.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JCheckBox check = (JCheckBox) e.getSource(); ps.set("background.opaque", check.isSelected()); cp.enableComponents(check.isSelected()); } }); background.add(opaque, BorderLayout.NORTH); background.add(cp, BorderLayout.CENTER); JPanel image = new JPanel(); image.setBorder(BorderFactory.createTitledBorder("Image de fond")); image.setPreferredSize(new Dimension(365, 125)); image.setMaximumSize(new Dimension(365, 125)); image.setLayout(new BorderLayout()); try { remove = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png")))); } catch (IOException e) { e.printStackTrace(); } remove.setEnabled(false); remove.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { File img = new File( "projects/" + Editor.getProjectName() + "/panels/" + name.getText() + "/background.png"); if (img.exists()) { img.delete(); } browseImage.setEnabled(true); } }); JPanel top = new JPanel(); browseImage.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JButton button = (JButton) e.getSource(); String path = null; JFileChooser chooser = new JFileChooser(Editor.lastPath); FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "png", "gif", "jpeg", "bmp"); chooser.setFileFilter(filter); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int option = chooser.showOpenDialog(null); if (option == JFileChooser.APPROVE_OPTION) { path = chooser.getSelectedFile().getAbsolutePath(); Editor.lastPath = chooser.getSelectedFile().getParent(); copyImage(new File(path), "background.png"); nameBackground.setText(new File(path).getName()); ps.set("background.image", new File(path).getName()); button.setEnabled(false); size.setEnabled(true); size2.setEnabled(true); remove.setEnabled(true); } } }); bg.add(size); bg.add(size2); JPanel sizePanel = new JPanel(); sizePanel.setLayout(new BoxLayout(sizePanel, BoxLayout.PAGE_AXIS)); size.setEnabled(false); size2.setEnabled(false); sizePanel.add(size); sizePanel.add(size2); size.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { ps.set("background.size", 0); } }); size2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { ps.set("background.size", 1); } }); top.add(browseImage); top.add(sizePanel); remove.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { JButton button = (JButton) event.getSource(); new File("projects/" + Editor.getProjectName() + "/panels/" + name.getText() + "/background.png") .delete(); nameBackground.setText(""); ps.set("background.image", ""); button.setEnabled(false); } }); nameBackground.setFont(new Font("Sans Serif", Font.PLAIN, 15)); JPanel center = new JPanel(new BorderLayout()); center.add(nameBackground, BorderLayout.CENTER); center.add(remove, BorderLayout.EAST); image.add(top, BorderLayout.NORTH); image.add(center, BorderLayout.CENTER); content.add(web); content.add(background); content.add(image); this.add(scroll); } @Override public void show() { t = new Thread(new Show(frame)); t2 = new Thread(new Hide(frame)); if (t2.isAlive() || t.isAlive()) { t2.interrupt(); } else { t.start(); } } @Override public void hide() { save(); t = new Thread(new Show(frame)); t2 = new Thread(new Hide(frame)); if (t.isAlive() || t2.isAlive()) { t.interrupt(); } else { t2.start(); } } @Override public void hideThenShow() { save(); t = new Thread(new Show(frame)); t2 = new Thread(new Hide(frame)); t3 = new Thread(new HideThenShow(frame)); if (t.isAlive() || t2.isAlive()) { t.interrupt(); t2.interrupt(); } else { t3.start(); } } @Override public void load(File file) { try { ps.load(file); } catch (IOException e) { e.printStackTrace(); } name.setText(ps.getString("name")); layout.setSelectedIndex(ps.getInt("layout")); ble.removeAllComponents(); int i = 0; for (String s : ps.getSectionsBeginningWith("flow.")) { ble.createComponent(i, ps.getString(s)); i++; } for (String s : ps.getSectionsBeginningWith("flow.")) { ps.removeSection(s); } lle.removeAllComponents(); i = 0; for (String s : ps.getSectionsBeginningWith("line.")) { lle.createComponent(i, ps.getString(s)); i++; } for (String s : ps.getSectionsBeginningWith("line.")) { ps.removeSection(s); } rle.removeAllComponents(); i = 0; for (String s : ps.getSectionsBeginningWith("row.")) { rle.createComponent(i, ps.getString(s)); i++; } for (String s : ps.getSectionsBeginningWith("row.")) { ps.removeSection(s); } i = 0; for (String s : ps.getSectionsBeginningWith("card.")) { cle.createComponent(i, ps.getString(s)); i++; } for (String s : ps.getSectionsBeginningWith("card.")) { ps.removeSection(s); } topBox.setSelectedItem(ps.getString("border.top")); leftBox.setSelectedItem(ps.getString("border.left")); centerBox.setSelectedItem(ps.getString("border.center")); rightBox.setSelectedItem(ps.getString("border.right")); bottomBox.setSelectedItem(ps.getString("border.bottom")); gridList.clear(); gle.removeAllGridComponents(); gle.setLines(ps.getInt("gridBounds.lines")); gle.setCols(ps.getInt("gridBounds.cols")); if (ps.getSectionsBeginningWith("grid.").length > 0) { for (String s : ps.getSectionsBeginningWith("grid.")) { gle.addComponentPanel(ps.getString(s)); } } else { gle.addComponentPanel("Aucun"); } for (String s : ps.getSectionsBeginningWith("grid.")) { ps.removeSection(s); } cle.removeAllComponents(); prefSizeEnabled.setSelected(ps.getBoolean("preferredSize")); prefWidth.setEnabled(ps.getBoolean("preferredSize")); prefWidth.setValue(ps.getInt("preferredWidth")); prefHeight.setEnabled(ps.getBoolean("preferredSize")); prefHeight.setValue(ps.getInt("preferredHeight")); insetsEnabled.setSelected(ps.getBoolean("insets")); insetHz.setEnabled(ps.getBoolean("insets")); insetVt.setEnabled(ps.getBoolean("insets")); insetHz.setValue(ps.getInt("insets.horizontal")); insetVt.setValue(ps.getInt("insets.vertical")); layout.setEnabled(!ps.getBoolean("web")); webEnabled.setSelected(ps.getBoolean("web")); if (ps.getBoolean("web")) { layout.setSelectedIndex(0); ble.removeAllComponents(); ble.disableComponents(); } adress.setEnabled(ps.getBoolean("web")); adress.setText(ps.getString("web.adress")); opaque.setSelected(ps.getBoolean("background.opaque")); cp.enableComponents(ps.getBoolean("background.opaque")); cp.setRed(ps.getInt("background.red")); cp.setGreen(ps.getInt("background.green")); cp.setBlue(ps.getInt("background.blue")); if (new File("projects/" + Editor.getProjectName() + "/panels/" + name.getText() + "/background.png") .exists()) { browseImage.setEnabled(false); size.setEnabled(true); size2.setEnabled(true); remove.setEnabled(true); } if (ps.getInt("background.size") == 0) { size.setSelected(true); } else { size2.setSelected(true); } nameBackground.setText(ps.getString("background.image")); } @Override public void save() { int i = 0; for (GridComponentPanel gcp : gle.getGridPanels()) { ps.createSection("grid." + i, gcp.getSelectedComponent()); i++; } i = 0; for (BasicLayoutEditor.ComponentPanel bcp : ble.getBasicComponents()) { ps.createSection("flow." + i, bcp.getTitle()); i++; } i = 0; for (LineLayoutEditor.ComponentPanel lcp : lle.getLineComponents()) { ps.createSection("line." + i, lcp.getTitle()); i++; } i = 0; for (RowLayoutEditor.ComponentPanel lcp : rle.getRowComponents()) { ps.createSection("row." + i, lcp.getTitle()); i++; } i = 0; for (CardLayoutEditor.ComponentPanel lcp : cle.getCardComponents()) { ps.createSection("card." + i, lcp.getTitle()); i++; } ps.set("background.red", cp.getRed()); ps.set("background.green", cp.getGreen()); ps.set("background.blue", cp.getBlue()); try { if (!name.getText().equals("") || name.getText() != null) { ps.save(new File("projects/" + Editor.getProjectName() + "/panels/" + name.getText() + "/" + name.getText() + ".rbd")); } } catch (IOException e) { e.printStackTrace(); } for (String s : ps.getSectionsBeginningWith("flow.")) { ps.removeSection(s); } for (String s : ps.getSectionsBeginningWith("line.")) { ps.removeSection(s); } for (String s : ps.getSectionsBeginningWith("row.")) { ps.removeSection(s); } for (String s : ps.getSectionsBeginningWith("card.")) { ps.removeSection(s); } for (String s : ps.getSectionsBeginningWith("grid.")) { ps.removeSection(s); } } public void copyImage(File file, String name) { try { @SuppressWarnings("static-access") FileOutputStream fos = new FileOutputStream(new File( "projects/" + Editor.getProjectName() + "/panels/" + this.name.getText() + "/" + name)); FileInputStream fis = new FileInputStream(file.getAbsolutePath()); IOUtils.copy(fis, fos); fos.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } @Override public String getFileName() { return name.getText(); } @SuppressWarnings("static-access") @Override public void setFileName(String name) { this.name.setText(name); } public static void updateLists() { compList.removeAllItems(); compList2.removeAllItems(); compList3.removeAllItems(); for (String s : Panels.getPanels()) { if (!s.equals(name.getText())) { compList.addItem(s + " (Panneau)"); compList2.addItem(s + " (Panneau)"); compList3.addItem(s + " (Panneau)"); } } for (String s : Others.getComponentsArray()) { compList.addItem(s); compList2.addItem(s); compList3.addItem(s); } for (String s : Buttons.getButtons()) { compList.addItem(s + " (Bouton)"); compList2.addItem(s + " (Bouton)"); compList3.addItem(s + " (Bouton)"); } for (String s : Labels.getLabels()) { compList.addItem(s + " (Label)"); compList2.addItem(s + " (Label)"); compList3.addItem(s + " (Label)"); } if (compList.getSelectedItem() == null) { addBasic.setEnabled(false); compList.setEnabled(false); } else { if (!webEnabled.isSelected()) { addBasic.setEnabled(true); compList.setEnabled(true); } } if (compList2.getSelectedItem() == null) { addLine.setEnabled(false); compList2.setEnabled(false); } else { addLine.setEnabled(true); compList2.setEnabled(true); } if (compList3.getSelectedItem() == null) { addRow.setEnabled(false); compList3.setEnabled(false); } else { addRow.setEnabled(true); compList3.setEnabled(true); } topBox.removeAllItems(); leftBox.removeAllItems(); centerBox.removeAllItems(); rightBox.removeAllItems(); bottomBox.removeAllItems(); topBox.addItem("Aucun"); leftBox.addItem("Aucun"); centerBox.addItem("Aucun"); rightBox.addItem("Aucun"); bottomBox.addItem("Aucun"); for (String s : Panels.getPanels()) { if (!s.equals(name.getText())) { topBox.addItem(s + " (Panneau)"); leftBox.addItem(s + " (Panneau)"); centerBox.addItem(s + " (Panneau)"); rightBox.addItem(s + " (Panneau)"); bottomBox.addItem(s + " (Panneau)"); } } for (String s : Others.getComponentsArray()) { topBox.addItem(s); leftBox.addItem(s); centerBox.addItem(s); rightBox.addItem(s); bottomBox.addItem(s); } for (String s : Buttons.getButtons()) { topBox.addItem(s + " (Bouton)"); leftBox.addItem(s + " (Bouton)"); centerBox.addItem(s + " (Bouton)"); rightBox.addItem(s + " (Bouton)"); bottomBox.addItem(s + " (Bouton)"); } for (String s : Labels.getLabels()) { topBox.addItem(s + " (Label)"); leftBox.addItem(s + " (Label)"); centerBox.addItem(s + " (Label)"); rightBox.addItem(s + " (Label)"); bottomBox.addItem(s + " (Label)"); } panelList.removeAllItems(); panelList.setPreferredSize(new Dimension(150, 32)); for (String s : Panels.getPanels()) { if (!s.equals(name.getText())) { panelList.addItem(s); } } if (panelList.getSelectedItem() == null) { addCard.setEnabled(false); panelList.setEnabled(false); } else { addCard.setEnabled(true); panelList.setEnabled(true); } } class BasicLayoutEditor extends JPanel { /** * */ private static final long serialVersionUID = -1033084180023672669L; private ArrayList<ComponentPanel> flowList = new ArrayList<ComponentPanel>(); JPanel components = new JPanel(); JScrollPane scroll = new JScrollPane(components); BasicLayoutEditor() { this.setPreferredSize(new Dimension(365, 300)); this.setMaximumSize(new Dimension(365, 300)); this.setBorder(BorderFactory.createTitledBorder("Disposition basique")); this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); scroll.getVerticalScrollBar().setUnitIncrement(Editor.SCROLL_SPEED); try { addBasic = new JButton("Ajouter un composant", new ImageIcon(ImageIO.read(ImageGetter.class.getResource("add.png")))); } catch (IOException e) { e.printStackTrace(); } JPanel addPanel = new JPanel(); addPanel.setPreferredSize(new Dimension(365, 50)); addPanel.setMaximumSize(new Dimension(365, 50)); addPanel.add(addBasic); compList.removeAllItems(); compList.setPreferredSize(new Dimension(150, 32)); for (String s : Others.getComponentsArray()) { compList.addItem(s); } for (String s : Buttons.getButtons()) { compList.addItem(s + " (Bouton)"); } for (String s : Labels.getLabels()) { compList.addItem(s + " (Label)"); } addPanel.add(compList); this.add(addPanel); components.setLayout(new BoxLayout(components, BoxLayout.PAGE_AXIS)); scroll.setBorder(null); this.add(scroll); addBasic.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { ComponentPanel comp = new ComponentPanel(flowList.size(), (String) compList.getSelectedItem()); flowList.add(comp); components.add(comp); components.revalidate(); components.repaint(); } }); } public void enableComponents() { addBasic.setEnabled(true); compList.setEnabled(true); } public void disableComponents() { addBasic.setEnabled(false); compList.setEnabled(false); } void removeAllComponents() { components.removeAll(); flowList.clear(); } public ArrayList<ComponentPanel> getBasicComponents() { return flowList; } void createComponent(int number, String comp) { ComponentPanel newComp = new ComponentPanel(number, comp); components.add(newComp); flowList.add(newComp); } class ComponentPanel extends JPanel { /** * */ private static final long serialVersionUID = 8403197898011255437L; int number; JLabel title = new JLabel(""); ComponentPanel(int number, String comp) { this.number = number; this.setPreferredSize(new Dimension(320, 75)); this.setMaximumSize(new Dimension(320, 75)); this.setLayout(new BorderLayout()); this.setBorder(BorderFactory.createTitledBorder("Composant n" + number)); title.setFont(new Font("Sans Serif", Font.BOLD, 14)); title.setText(comp); JButton remove = null; try { remove = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png")))); } catch (IOException e) { e.printStackTrace(); } remove.addActionListener(new RemoveListenerBasic()); this.add(title, BorderLayout.WEST); this.add(remove, BorderLayout.EAST); } int getId() { return number; } void updateNumber(int id) { number = id; this.setBorder(BorderFactory.createTitledBorder("Composant n" + id)); } void setTitle(String title) { this.title.setText(title); } public String getTitle() { return title.getText(); } class RemoveListenerBasic implements ActionListener { @Override public void actionPerformed(ActionEvent event) { JButton button = (JButton) event.getSource(); flowList.remove(number); JPanel parent = (JPanel) button.getParent().getParent(); parent.remove(button.getParent()); parent.revalidate(); parent.repaint(); int i = 0; for (Component cp : components.getComponents()) { if (cp instanceof ComponentPanel) { ((ComponentPanel) cp).updateNumber(i); i++; } } } } } } class BorderLayoutEditor extends JPanel { /** * */ private static final long serialVersionUID = -9048002885849835051L; JTextArea description = new JTextArea( "La disposition par bords aura un rendu similaire celui ci-dessus. Vous pouvez ne pas ajouter de composant sur chaque bord." + " Les composants se redimensionneront par eux-mme en fonction du bord qui leur est attribu et en fonction de la taille du panneau."); BorderLayoutEditor() { this.setPreferredSize(new Dimension(365, 300)); this.setMaximumSize(new Dimension(365, 300)); this.setBorder(BorderFactory.createTitledBorder("Disposition par bords")); this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); JPanel preview = new JPanel(new BorderLayout()); preview.setPreferredSize(new Dimension(365, 160)); preview.setMaximumSize(new Dimension(365, 160)); JPanel top = new JPanel(new BorderLayout()); top.setBorder(BorderFactory.createLoweredSoftBevelBorder()); top.setPreferredSize(new Dimension(365, 40)); top.add(topBox, BorderLayout.CENTER); JPanel left = new JPanel(new BorderLayout()); left.setBorder(BorderFactory.createLoweredSoftBevelBorder()); left.setPreferredSize(new Dimension(100, 40)); left.add(leftBox, BorderLayout.CENTER); JPanel center = new JPanel(new BorderLayout()); center.setBorder(BorderFactory.createLoweredSoftBevelBorder()); center.setPreferredSize(new Dimension(120, 40)); center.add(centerBox, BorderLayout.CENTER); JPanel right = new JPanel(new BorderLayout()); right.setBorder(BorderFactory.createLoweredSoftBevelBorder()); right.setPreferredSize(new Dimension(100, 40)); right.add(rightBox, BorderLayout.CENTER); JPanel bottom = new JPanel(new BorderLayout()); bottom.setBorder(BorderFactory.createLoweredSoftBevelBorder()); bottom.setPreferredSize(new Dimension(365, 40)); bottom.add(bottomBox, BorderLayout.CENTER); preview.add(top, BorderLayout.NORTH); preview.add(left, BorderLayout.WEST); preview.add(center, BorderLayout.CENTER); preview.add(right, BorderLayout.EAST); preview.add(bottom, BorderLayout.SOUTH); this.add(preview); this.add(Box.createRigidArea(new Dimension(0, 10))); description.setFocusable(false); description.setLineWrap(true); description.setWrapStyleWord(true); description.setEditable(false); description.setBackground(new Color(0, 0, 0, 0)); description.setBorder(null); this.add(description); } } class LineLayoutEditor extends JPanel { /** * */ private static final long serialVersionUID = -1033084180023672669L; private ArrayList<ComponentPanel> lineList = new ArrayList<ComponentPanel>(); JPanel components = new JPanel(); JScrollPane scroll = new JScrollPane(components); LineLayoutEditor() { this.setPreferredSize(new Dimension(365, 300)); this.setMaximumSize(new Dimension(365, 300)); this.setBorder(BorderFactory.createTitledBorder("Disposition en ligne")); this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); scroll.getVerticalScrollBar().setUnitIncrement(Editor.SCROLL_SPEED); try { addLine = new JButton("Ajouter un composant", new ImageIcon(ImageIO.read(ImageGetter.class.getResource("add.png")))); } catch (IOException e) { e.printStackTrace(); } JPanel addPanel = new JPanel(); addPanel.setPreferredSize(new Dimension(365, 50)); addPanel.setMaximumSize(new Dimension(365, 50)); addPanel.add(addLine); compList2.removeAllItems(); compList2.setPreferredSize(new Dimension(150, 32)); for (String s : Others.getComponentsArray()) { compList2.addItem(s); } for (String s : Buttons.getButtons()) { compList2.addItem(s + " (Bouton)"); } for (String s : Labels.getLabels()) { compList2.addItem(s + " (Label)"); } addPanel.add(compList2); this.add(addPanel); components.setLayout(new BoxLayout(components, BoxLayout.PAGE_AXIS)); scroll.setBorder(null); this.add(scroll); addLine.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { ComponentPanel comp = new ComponentPanel(lineList.size(), (String) compList2.getSelectedItem()); lineList.add(comp); components.add(comp); components.revalidate(); components.repaint(); } }); } public ArrayList<ComponentPanel> getLineComponents() { return lineList; } void removeAllComponents() { components.removeAll(); lineList.clear(); } void createComponent(int number, String comp) { ComponentPanel newComp = new ComponentPanel(number, comp); components.add(newComp); lineList.add(newComp); } class ComponentPanel extends JPanel { /** * */ private static final long serialVersionUID = 8403197898011255437L; int number; JLabel title = new JLabel(""); ComponentPanel(int number, String comp) { this.number = number; this.setPreferredSize(new Dimension(320, 75)); this.setMaximumSize(new Dimension(320, 75)); this.setLayout(new BorderLayout()); this.setBorder(BorderFactory.createTitledBorder("Composant n" + number)); title.setFont(new Font("Sans Serif", Font.BOLD, 14)); title.setText(comp); JButton remove = null; try { remove = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png")))); } catch (IOException e) { e.printStackTrace(); } remove.addActionListener(new RemoveListenerLine()); this.add(title, BorderLayout.WEST); this.add(remove, BorderLayout.EAST); } int getId() { return number; } void updateNumber(int id) { number = id; this.setBorder(BorderFactory.createTitledBorder("Composant n" + id)); } void setTitle(String title) { this.title.setText(title); } public String getTitle() { return title.getText(); } class RemoveListenerLine implements ActionListener { @Override public void actionPerformed(ActionEvent event) { JButton button = (JButton) event.getSource(); lineList.remove(number); JPanel parent = (JPanel) button.getParent().getParent(); parent.remove(button.getParent()); parent.revalidate(); parent.repaint(); int i = 0; for (Component cp : components.getComponents()) { if (cp instanceof ComponentPanel) { ((ComponentPanel) cp).updateNumber(i); i++; } } } } } } class RowLayoutEditor extends JPanel { /** * */ private static final long serialVersionUID = -1033084180023672669L; private ArrayList<ComponentPanel> rowList = new ArrayList<ComponentPanel>(); JPanel components = new JPanel(); JScrollPane scroll = new JScrollPane(components); RowLayoutEditor() { this.setPreferredSize(new Dimension(365, 300)); this.setMaximumSize(new Dimension(365, 300)); this.setBorder(BorderFactory.createTitledBorder("Disposition en colonne")); this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); scroll.getVerticalScrollBar().setUnitIncrement(Editor.SCROLL_SPEED); try { addRow = new JButton("Ajouter un composant", new ImageIcon(ImageIO.read(ImageGetter.class.getResource("add.png")))); } catch (IOException e) { e.printStackTrace(); } JPanel addPanel = new JPanel(); addPanel.setPreferredSize(new Dimension(365, 50)); addPanel.setMaximumSize(new Dimension(365, 50)); addPanel.add(addRow); compList3.removeAllItems(); compList3.setPreferredSize(new Dimension(150, 32)); for (String s : Others.getComponentsArray()) { compList3.addItem(s); } for (String s : Buttons.getButtons()) { compList3.addItem(s + " (Bouton)"); } for (String s : Labels.getLabels()) { compList3.addItem(s + " (Label)"); } addPanel.add(compList3); this.add(addPanel); components.setLayout(new BoxLayout(components, BoxLayout.PAGE_AXIS)); scroll.setBorder(null); this.add(scroll); addRow.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { ComponentPanel comp = new ComponentPanel(rowList.size(), (String) compList3.getSelectedItem()); rowList.add(comp); components.add(comp); components.revalidate(); components.repaint(); } }); } void removeAllComponents() { components.removeAll(); } void createComponent(int number, String comp) { ComponentPanel newComp = new ComponentPanel(number, comp); components.add(newComp); rowList.add(newComp); } public ArrayList<ComponentPanel> getRowComponents() { return rowList; } class ComponentPanel extends JPanel { /** * */ private static final long serialVersionUID = 8403197898011255437L; int number; JLabel title = new JLabel(""); ComponentPanel(int number, String comp) { this.number = number; this.setPreferredSize(new Dimension(320, 75)); this.setMaximumSize(new Dimension(320, 75)); this.setLayout(new BorderLayout()); this.setBorder(BorderFactory.createTitledBorder("Composant n" + number)); title.setFont(new Font("Sans Serif", Font.BOLD, 14)); title.setText(comp); JButton remove = null; try { remove = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png")))); } catch (IOException e) { e.printStackTrace(); } remove.addActionListener(new RemoveListenerLine()); this.add(title, BorderLayout.WEST); this.add(remove, BorderLayout.EAST); } int getId() { return number; } void updateNumber(int id) { number = id; this.setBorder(BorderFactory.createTitledBorder("Composant n" + id)); } void setTitle(String title) { this.title.setText(title); } public String getTitle() { return title.getText(); } class RemoveListenerLine implements ActionListener { @Override public void actionPerformed(ActionEvent event) { JButton button = (JButton) event.getSource(); rowList.remove(number); JPanel parent = (JPanel) button.getParent().getParent(); parent.remove(button.getParent()); parent.revalidate(); parent.repaint(); int i = 0; for (Component cp : components.getComponents()) { if (cp instanceof ComponentPanel) { ((ComponentPanel) cp).updateNumber(i); i++; } } } } } } class GridLayoutEditor extends JPanel { /** * */ private static final long serialVersionUID = -3264767648112009685L; private SpinnerModel model = new SpinnerNumberModel(1, 1, 100, 1); private SpinnerModel model2 = new SpinnerNumberModel(1, 1, 100, 1); private JSpinner lines = new JSpinner(model); private JSpinner cols = new JSpinner(model2); private GridLayout gl = new GridLayout(1, 1); JPanel preview = new JPanel(gl); JScrollPane scroll = new JScrollPane(preview); LineChangeListener lineListener = new LineChangeListener(); ColChangeListener colListener = new ColChangeListener(); GridLayoutEditor() { this.setPreferredSize(new Dimension(365, 300)); this.setMaximumSize(new Dimension(365, 300)); this.setBorder(BorderFactory.createTitledBorder("Disposition en grille")); this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); scroll.getVerticalScrollBar().setUnitIncrement(Editor.SCROLL_SPEED); JPanel spinners = new JPanel(); spinners.add(new JLabel("Lignes :")); spinners.add(lines); spinners.add(new JLabel("Colonnes :")); spinners.add(cols); spinners.setPreferredSize(new Dimension(360, 55)); spinners.setMaximumSize(new Dimension(360, 55)); this.add(spinners); scroll.setBorder(null); this.add(scroll); } void removeAllGridComponents() { lines.removeChangeListener(lineListener); cols.removeChangeListener(colListener); gridList.clear(); preview.removeAll(); } void addComponentPanel(String item) { GridComponentPanel gcp = new GridComponentPanel(); gcp.setSelectedComponent(item); preview.add(gcp); gridList.add(gcp); preview.revalidate(); preview.repaint(); } void setLines(int amount) { lines.setValue(amount); gl.setRows(amount); preview.revalidate(); preview.repaint(); lines.addChangeListener(lineListener); } void setCols(int amount) { cols.setValue(amount); gl.setColumns(amount); preview.revalidate(); preview.repaint(); cols.addChangeListener(colListener); } class LineChangeListener implements ChangeListener { @Override public void stateChanged(ChangeEvent e) { JSpinner spinner = (JSpinner) e.getSource(); if (ps.getInt("gridBounds.lines") > (int) spinner.getValue()) { int i = 1; gl.setRows((int) spinner.getValue()); while (preview.getComponentCount() > ((int) lines.getValue() * (int) cols.getValue())) { if (i % (int) lines.getValue() == 0 && i < gridList.size()) { preview.remove(gridList.get(i)); gridList.remove(i); preview.revalidate(); preview.repaint(); } i++; } } else { gl.setRows((int) spinner.getValue()); for (int i = preview.getComponentCount(); i < ((int) lines.getValue() * (int) cols.getValue()); i++) { preview.add(new GridComponentPanel()); preview.revalidate(); preview.repaint(); } } ps.set("gridBounds.lines", spinner.getValue()); } } class ColChangeListener implements ChangeListener { @Override public void stateChanged(ChangeEvent e) { JSpinner spinner = (JSpinner) e.getSource(); if (ps.getInt("gridBounds.cols") > (int) spinner.getValue()) { int i = 1; gl.setColumns((int) spinner.getValue()); while (preview.getComponentCount() > ((int) lines.getValue() * (int) cols.getValue())) { if (i % (int) cols.getValue() == 0 && i < gridList.size()) { preview.remove(gridList.get(i)); gridList.remove(i); preview.revalidate(); preview.repaint(); } i++; } } else { gl.setColumns((int) spinner.getValue()); for (int i = preview.getComponentCount(); i < ((int) lines.getValue() * (int) cols.getValue()); i++) { preview.add(new GridComponentPanel()); preview.revalidate(); preview.repaint(); } } ps.set("gridBounds.cols", spinner.getValue()); } } class GridComponentPanel extends JPanel { /** * */ private static final long serialVersionUID = 5436264953045529720L; private JComboBox<String> components = new JComboBox<String>(); GridComponentPanel() { gridList.add(this); this.setPreferredSize(new Dimension(100, 45)); this.setLayout(new BorderLayout()); this.setBorder(BorderFactory.createLoweredSoftBevelBorder()); components.removeAllItems(); components.addItem("Aucun"); for (String s : Panels.getPanels()) { if (!s.equals(name.getText())) { components.addItem(s + " (Panneau)"); } } for (String s : Others.getComponentsArray()) { components.addItem(s); } for (String s : Buttons.getButtons()) { components.addItem(s + " (Bouton)"); } for (String s : Labels.getLabels()) { components.addItem(s + " (Label)"); } this.add(components, BorderLayout.CENTER); } void updateList() { components.removeAllItems(); components.addItem("Aucun"); for (String s : Panels.getPanels()) { if (!s.equals(name.getText())) { components.addItem(s + " (Panneau)"); } } for (String s : Others.getComponentsArray()) { components.addItem(s); } for (String s : Buttons.getButtons()) { components.addItem(s + " (Bouton)"); } for (String s : Labels.getLabels()) { components.addItem(s + " (Label)"); } } String getSelectedComponent() { return String.valueOf(components.getSelectedItem()); } void setSelectedComponent(String comp) { components.setSelectedItem(comp); } } GridComponentPanel[] getGridPanels() { ArrayList<GridComponentPanel> list = new ArrayList<GridComponentPanel>(); for (Component comp : preview.getComponents()) { if (comp instanceof GridComponentPanel) { list.add((GridComponentPanel) comp); } } return list.toArray(new GridComponentPanel[0]); } } class CardLayoutEditor extends JPanel { /** * */ private static final long serialVersionUID = 7628278048962473646L; private ArrayList<ComponentPanel> cardList = new ArrayList<ComponentPanel>(); JPanel components = new JPanel(); JScrollPane scroll = new JScrollPane(components); CardLayoutEditor() { this.setPreferredSize(new Dimension(365, 300)); this.setMaximumSize(new Dimension(365, 300)); this.setBorder(BorderFactory.createTitledBorder("Disposition empile")); this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); scroll.getVerticalScrollBar().setUnitIncrement(Editor.SCROLL_SPEED); try { addCard = new JButton("Ajouter une couche", new ImageIcon(ImageIO.read(ImageGetter.class.getResource("add.png")))); } catch (IOException e) { e.printStackTrace(); } JPanel addPanel = new JPanel(); addPanel.setPreferredSize(new Dimension(365, 50)); addPanel.setMaximumSize(new Dimension(365, 50)); addPanel.add(addCard); panelList.setPreferredSize(new Dimension(150, 32)); addPanel.add(panelList); this.add(addPanel); components.setLayout(new BoxLayout(components, BoxLayout.PAGE_AXIS)); scroll.setBorder(null); this.add(scroll); addCard.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { ComponentPanel comp = new ComponentPanel(cardList.size(), (String) panelList.getSelectedItem()); cardList.add(comp); components.add(comp); components.revalidate(); components.repaint(); } }); } void removeAllComponents() { components.removeAll(); cardList.clear(); } void createComponent(int number, String comp) { ComponentPanel newComp = new ComponentPanel(number, comp); components.add(newComp); cardList.add(newComp); } public ArrayList<ComponentPanel> getCardComponents() { return cardList; } class ComponentPanel extends JPanel { /** * */ private static final long serialVersionUID = 8403197898011255437L; int number; JLabel title = new JLabel(""); ComponentPanel(int number, String comp) { this.number = number; this.setPreferredSize(new Dimension(320, 75)); this.setMaximumSize(new Dimension(320, 75)); this.setLayout(new BorderLayout()); this.setBorder(BorderFactory.createTitledBorder("Couche n" + number)); title.setFont(new Font("Sans Serif", Font.BOLD, 14)); title.setText(comp); JButton remove = null; try { remove = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png")))); } catch (IOException e) { e.printStackTrace(); } remove.addActionListener(new RemoveListenerLine()); this.add(title, BorderLayout.WEST); this.add(remove, BorderLayout.EAST); } int getId() { return number; } void updateNumber(int id) { number = id; this.setBorder(BorderFactory.createTitledBorder("Couche n" + id)); } void setTitle(String title) { this.title.setText(title); } public String getTitle() { return title.getText(); } class RemoveListenerLine implements ActionListener { @Override public void actionPerformed(ActionEvent event) { JButton button = (JButton) event.getSource(); cardList.remove(number); JPanel parent = (JPanel) button.getParent().getParent(); parent.remove(button.getParent()); parent.revalidate(); parent.repaint(); int i = 0; for (Component cp : components.getComponents()) { if (cp instanceof ComponentPanel) { ((ComponentPanel) cp).updateNumber(i); i++; } } } } } } }