import javax.swing.DefaultListModel; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JTabbedPane; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.ListSelectionModel; import javax.swing.SpringLayout; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; @SuppressWarnings("serial") public class BeginnerJava extends JFrame { private JTabbedPane tabs; private JFileChooser fc = new JFileChooser(); private JButton create, open, delete, exportCode, saveProject, newProject, createVariable, deleteVariable, createMethod, deleteMethod, mainMethod, constructor; private JPanel template, controlPanel, logoPanel; private JFrame frame; private JList<String> displayMethod, displayVariable; private DefaultListModel<String> variableList, methodList; private boolean haveMainMethod = false; private boolean haveConstructor = false; private ArrayList<JTabbedPane> myTabs = new ArrayList<JTabbedPane>(); private ArrayList<ClassCreation> myClasses = new ArrayList<ClassCreation>(); private ArrayList<MethodCreation> myMethods = new ArrayList<MethodCreation>(); private ArrayList<VariableCreation> myVariables = new ArrayList<VariableCreation>(); private ArrayList<MethodCreation> methodNames = new ArrayList<MethodCreation>(); private ArrayList<VariableCreation> variableNames = new ArrayList<VariableCreation>(); private ArrayList<MethodCreation> main = new ArrayList<MethodCreation>(); //holds main method private ArrayList<MethodCreation> cons = new ArrayList<MethodCreation>(); //holds constructor private ArrayList<ArrayList> methodArray = new ArrayList<ArrayList>(); //holds arrays of methods for each class ClassCreation newClass; MethodCreation newMethod, name; public static void main(String[] args) { @SuppressWarnings("unused") BeginnerJava bg = new BeginnerJava(); } public BeginnerJava() { frame = new JFrame("Java for Beginners"); frame.setLayout(new BorderLayout()); tabs = new JTabbedPane(); controlPanel = controlPanel(); logoPanel = logo(); frame.add(tabs, BorderLayout.CENTER); frame.add(controlPanel, BorderLayout.NORTH); frame.add(logoPanel, BorderLayout.SOUTH); frame.setSize(400, 600); frame.setVisible(true); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); } private JPanel controlPanel() { JPanel controlPanel = new JPanel(); controlPanel.setPreferredSize(new Dimension(300,30)); //start new project ImageIcon newIcon = new ImageIcon("http://www.javaprogrammingforums.com/images/new.png"); newProject = new JButton(newIcon); newProject.setPreferredSize(new Dimension(20,20)); newProject.addActionListener(new EventHandler()); //open existing project ImageIcon openIcon = new ImageIcon("http://www.javaprogrammingforums.com/images/open.png"); open = new JButton(openIcon); open.setPreferredSize(new Dimension(20,20)); open.addActionListener(new EventHandler()); //save project ImageIcon saveIcon = new ImageIcon("http://www.javaprogrammingforums.com/images/save.png"); saveProject = new JButton(saveIcon); saveProject.setPreferredSize(new Dimension(20,20)); saveProject.addActionListener(new EventHandler()); saveProject.setEnabled(false); //create class ImageIcon createIcon = new ImageIcon("http://www.javaprogrammingforums.com/images/create.png"); create = new JButton(createIcon); create.setPreferredSize(new Dimension(20,20)); create.addActionListener(new EventHandler()); //delete class ImageIcon deleteIcon = new ImageIcon("http://www.javaprogrammingforums.com/images/delete.png"); delete = new JButton(deleteIcon); delete.setPreferredSize(new Dimension(20,20)); delete.setEnabled(false); delete.addActionListener(new EventHandler()); controlPanel.add(newProject); controlPanel.add(open); controlPanel.add(saveProject); controlPanel.add(create); controlPanel.add(delete); return controlPanel; } private JPanel logo() { JPanel panel = new JPanel(); panel.setPreferredSize(new Dimension(300,570)); panel.setLayout(new BorderLayout()); ImageIcon image = new ImageIcon("http://www.javaprogrammingforums.com/images/logo.png"); JLabel imageLabel = new JLabel(image); panel.add(imageLabel, BorderLayout.CENTER); return panel; } private JPanel makeClassTemplate() { JPanel template = new JPanel(); template.setPreferredSize(new Dimension(300,700)); SpringLayout layout = new SpringLayout(); template.setLayout(layout); JTextArea variableText = new JTextArea("Variables:"); variableText.setBackground(null); layout.putConstraint(SpringLayout.WEST, variableText, 45, SpringLayout.WEST, template); layout.putConstraint(SpringLayout.NORTH, variableText, 10, SpringLayout.NORTH, template); ImageIcon createIcon = new ImageIcon("http://www.javaprogrammingforums.com/images/create.png"); createVariable = new JButton(createIcon); createVariable.setPreferredSize(new Dimension(20,20)); createVariable.createToolTip(); createVariable.setToolTipText("Create Variable"); layout.putConstraint(SpringLayout.WEST, createVariable, 25, SpringLayout.WEST, template); layout.putConstraint(SpringLayout.NORTH, createVariable, 30, SpringLayout.NORTH, template); createVariable.addActionListener(new EventHandler()); ImageIcon deleteIcon = new ImageIcon("http://www.javaprogrammingforums.com/images/delete.png"); deleteVariable = new JButton(deleteIcon); deleteVariable.setPreferredSize(new Dimension(20,20)); deleteVariable.createToolTip(); deleteVariable.setToolTipText("Delete Variable"); deleteVariable.setEnabled(false); layout.putConstraint(SpringLayout.WEST, deleteVariable, 25, SpringLayout.WEST, template); layout.putConstraint(SpringLayout.NORTH, deleteVariable, 50, SpringLayout.NORTH, template); deleteVariable.addActionListener(new EventHandler()); variableList = new DefaultListModel<String>(); displayVariable = new JList<String>(variableList); displayVariable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); displayVariable.setLayoutOrientation(JList.HORIZONTAL_WRAP); layout.putConstraint(SpringLayout.WEST, displayVariable, 45, SpringLayout.WEST, template); layout.putConstraint(SpringLayout.NORTH, displayVariable, 30, SpringLayout.NORTH, template); displayVariable.setPreferredSize(new Dimension(300,200)); displayVariable.addListSelectionListener(new EventHandler()); JTextArea methodText = new JTextArea("Methods:"); methodText.setBackground(null); layout.putConstraint(SpringLayout.WEST, methodText, 45, SpringLayout.WEST, template); layout.putConstraint(SpringLayout.NORTH, methodText, 240, SpringLayout.NORTH, template); //create method createIcon = new ImageIcon("http://www.javaprogrammingforums.com/images/create.png"); createMethod = new JButton(createIcon); createMethod.setPreferredSize(new Dimension(20,20)); createMethod.createToolTip(); createMethod.setToolTipText("Create Method"); layout.putConstraint(SpringLayout.WEST, createMethod, 25, SpringLayout.WEST, template); layout.putConstraint(SpringLayout.NORTH, createMethod, 260, SpringLayout.NORTH, template); createMethod.addActionListener(new EventHandler()); //delete method deleteIcon = new ImageIcon("http://www.javaprogrammingforums.com/images/delete.png"); deleteMethod = new JButton(deleteIcon); deleteMethod.setPreferredSize(new Dimension(20,20)); deleteMethod.createToolTip(); deleteMethod.setToolTipText("Delete Method"); deleteMethod.setEnabled(false); layout.putConstraint(SpringLayout.WEST, deleteMethod, 25, SpringLayout.WEST, template); layout.putConstraint(SpringLayout.NORTH, deleteMethod, 280, SpringLayout.NORTH, template); deleteMethod.addActionListener(new EventHandler()); methodList = new DefaultListModel<String>(); displayMethod = new JList<String>(methodList); displayMethod.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); displayMethod.setLayoutOrientation(JList.HORIZONTAL_WRAP); layout.putConstraint(SpringLayout.WEST, displayMethod, 45, SpringLayout.WEST, template); layout.putConstraint(SpringLayout.NORTH, displayMethod, 260, SpringLayout.NORTH, template); displayMethod.setPreferredSize(new Dimension(300,200)); displayMethod.addListSelectionListener(new EventHandler()); ImageIcon export = new ImageIcon("http://www.javaprogrammingforums.com/images/save.png"); exportCode = new JButton(export); exportCode.setPreferredSize(new Dimension(20,20)); exportCode.createToolTip(); exportCode.setToolTipText("Export Code"); layout.putConstraint(SpringLayout.WEST, exportCode, 370, SpringLayout.WEST, template); layout.putConstraint(SpringLayout.NORTH, exportCode, 0, SpringLayout.NORTH, template); exportCode.addActionListener(new EventHandler()); mainMethod = new JButton("Add Main Method"); layout.putConstraint(SpringLayout.WEST, mainMethod, 193, SpringLayout.WEST, template); layout.putConstraint(SpringLayout.NORTH, mainMethod, 470, SpringLayout.NORTH, template); mainMethod.addActionListener(new EventHandler()); constructor = new JButton("Add Constructor"); layout.putConstraint(SpringLayout.WEST, constructor, 63, SpringLayout.WEST, template); layout.putConstraint(SpringLayout.NORTH, constructor, 470, SpringLayout.NORTH, template); constructor.addActionListener(new EventHandler()); template.add(createVariable); template.add(deleteVariable); template.add(displayVariable); template.add(variableText); template.add(exportCode); template.add(methodText); template.add(createMethod); template.add(deleteMethod); template.add(displayMethod); template.add(constructor); template.add(mainMethod); template.validate(); return template; } private void makeMethod() { String[] buttonTxt1 = {"public", "private", "protected"}; JComboBox<Object> methodType1 = new JComboBox<Object>(buttonTxt1); methodType1.setSelectedIndex(0); methodType1.addActionListener(new EventHandler()); String[] buttonTxt2 = {"", "static"}; JComboBox<Object> methodType2 = new JComboBox<Object>(buttonTxt2); methodType2.setSelectedIndex(0); methodType2.addActionListener(new EventHandler()); String[] buttonTxt3 = {"", "abstract" , "final"}; JComboBox<Object> methodType3 = new JComboBox<Object>(buttonTxt3); methodType3.setSelectedIndex(0); methodType3.addActionListener(new EventHandler()); String[] buttonTxt4 = {"void","int","double","long","byte","float","char","String"}; JComboBox<Object> methodType4 = new JComboBox<Object>(buttonTxt4); methodType4.setSelectedIndex(0); methodType4.addActionListener(new EventHandler()); JDialog dialog; JOptionPane optionPane = new JOptionPane(); optionPane.setLayout(new GridLayout(4,1)); optionPane.setMessage("Method Type"); optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE); optionPane.add(methodType1); optionPane.add(methodType2); optionPane.add(methodType3); optionPane.add(methodType4); dialog = optionPane.createDialog(null, "Method Type"); dialog.setVisible(true); String st = JOptionPane.showInputDialog(null, "Enter Method Name: "); String pick1 = methodType1.getSelectedItem().toString(); String pick2 = methodType2.getSelectedItem().toString(); String pick3 = methodType3.getSelectedItem().toString(); String pick4 = methodType4.getSelectedItem().toString(); String methodName = st; newMethod = new MethodCreation(pick1, pick2, pick3, pick4, methodName); myMethods.add(newMethod); methodArray.add(myMethods); name = new MethodCreation(methodName); methodNames.add(name); } private void makeVariable() { String[] buttonTxt1 = {"public", "private", "protected"}; JComboBox<Object> methodType1 = new JComboBox<Object>(buttonTxt1); methodType1.setSelectedIndex(0); methodType1.addActionListener(new EventHandler()); String[] buttonTxt2 = {"", "static"}; JComboBox<Object> methodType2 = new JComboBox<Object>(buttonTxt2); methodType2.setSelectedIndex(0); methodType2.addActionListener(new EventHandler()); String[] buttonTxt3 = {"", "abstract" , "final"}; JComboBox<Object> methodType3 = new JComboBox<Object>(buttonTxt3); methodType3.setSelectedIndex(0); methodType3.addActionListener(new EventHandler()); String[] buttonTxt4 = {"void","int","double","long","byte","float","char","String"}; JComboBox<Object> methodType4 = new JComboBox<Object>(buttonTxt4); methodType4.setSelectedIndex(0); methodType4.addActionListener(new EventHandler()); JDialog dialog; JOptionPane optionPane = new JOptionPane(); optionPane.setLayout(new GridLayout(4,1)); optionPane.setMessage("Method Type"); optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE); optionPane.add(methodType1); optionPane.add(methodType2); optionPane.add(methodType3); optionPane.add(methodType4); dialog = optionPane.createDialog(null, "Method Type"); dialog.setVisible(true); String st = JOptionPane.showInputDialog(null, "Enter Method Name: "); String pick1 = methodType1.getSelectedItem().toString(); String pick2 = methodType2.getSelectedItem().toString(); String pick3 = methodType3.getSelectedItem().toString(); String pick4 = methodType4.getSelectedItem().toString(); String methodName = st; newMethod = new MethodCreation(pick1, pick2, pick3, pick4, methodName); myMethods.add(newMethod); methodArray.add(myMethods); name = new MethodCreation(methodName); methodNames.add(name); } private void createClass() { //public or private String[] buttonTxt1 = {"public", "private"}; JComboBox<Object> classType1 = new JComboBox<Object>(buttonTxt1); classType1.setSelectedIndex(0); classType1.addActionListener(new EventHandler()); //abstract, final , or neither String[] buttonTxt2 = {"","abstract","final"}; JComboBox<Object> classType2 = new JComboBox<Object>(buttonTxt2); classType2.setSelectedIndex(0); classType2.addActionListener(new EventHandler()); JDialog dialog; JOptionPane optionPane = new JOptionPane(); optionPane.setLayout(new GridLayout(2,1)); methodList = new DefaultListModel<String>(); variableList = new DefaultListModel<String>(); optionPane.setMessage("Class Type"); optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE); optionPane.add(classType1); optionPane.add(classType2); dialog = optionPane.createDialog(null, "Class Type"); dialog.setVisible(true); String st = JOptionPane.showInputDialog(null, "Enter Class Name:"); Pattern whiteSpace = Pattern.compile("\\s"); Matcher matcher = whiteSpace.matcher(st); boolean found = matcher.find(); String className = st; String pick1 = classType1.getSelectedItem().toString(); String pick2 = classType2.getSelectedItem().toString(); if(found == true || st.equals("")) { //will not create a new tab unless there are no spaces JOptionPane.showMessageDialog(null, "Class name cannot be empty or contain spaces or symbols", "Error", JOptionPane.WARNING_MESSAGE); st = JOptionPane.showInputDialog(null, "Enter Class Name:"); } if(!st.equals("")) { logoPanel.setVisible(false); newClass = new ClassCreation(pick1, pick2, className); template = makeClassTemplate(); tabs.add(st, template); tabs.setSelectedComponent(template); myTabs.add(tabs); myClasses.add(newClass); } } private class EventHandler implements ActionListener, ListSelectionListener{ @Override public void actionPerformed(ActionEvent e) { Object b = e.getSource(); String str = e.getActionCommand(); if(b.equals(create)) { myMethods.clear(); methodNames.clear(); createClass(); } if(tabs.getTabCount()>=1) { //enables delete & save if there is at least one tab delete.setEnabled(true); saveProject.setEnabled(true); } if(b.equals(delete)) { tabs.remove(tabs.getSelectedIndex()); if(tabs.getTabCount()<1){ delete.setEnabled(false); saveProject.setEnabled(false); logoPanel.setVisible(true); } } if(b.equals(open)) { int returnVal = fc.showOpenDialog(BeginnerJava.this); if(returnVal == JFileChooser.APPROVE_OPTION) { String file = fc.getSelectedFile().getPath(); try { openFile(file); } catch (ClassNotFoundException | IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } if(b.equals(exportCode)) { String name = tabs.getTitleAt(tabs.getSelectedIndex()); File outFile = new File(name + ".java"); if(outFile.exists()) { } PrintWriter textStream; try { textStream = new PrintWriter(outFile); //calls ClassCreation's toString textStream.println(newClass); if(methodArray.size() != 0) { if(methodArray.get(tabs.getSelectedIndex()).size() <= 1) { for(int i = 0; i < methodArray.get(tabs.getSelectedIndex()).size(); i++) //gets currently selected tabs ArrayList that is filled with it's methods if(methodArray.get(i).size() != 0) { textStream.println(methodArray.get(i).get(i).toString()); } } else { for(int i = 0; i < methodArray.get(tabs.getSelectedIndex()-1).size(); i++) //gets currently selected tabs ArrayList that is filled with it's methods if(methodArray.get(i).size() != 0) { textStream.println(methodArray.get(i).get(i).toString()); } } } if(main.size() != 0) textStream.println(main.get(0).mainMethod()); if(cons.size() != 0) textStream.println(cons.get(0).constructor(name)); textStream.println("}"); haveMainMethod = false; haveConstructor = false; textStream.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } } if(b.equals(createMethod)) { makeMethod(); methodList.clear(); deleteMethod.setEnabled(true); for(int i = 0; i < methodNames.size(); i++) { methodList.addElement(methodNames.get(i).getName()); } if(haveMainMethod == true) methodList.addElement("main"); if(haveConstructor == true) methodList.addElement("constructor"); } if(b.equals(deleteMethod)) { int index = displayMethod.getSelectedIndex(); methodList.remove(index); myMethods.remove(index); if(myMethods.size()<1) deleteMethod.setEnabled(false); } if(b.equals(createVariable)) { makeVariable(); variableList.clear(); deleteVariable.setEnabled(true); for(int i = 0; i < variableNames.size(); i++) { variableList.addElement(variableNames.get(i).getName()); } } if(b.equals(deleteVariable)) { } if(b.equals(newProject)) { methodArray.clear(); myClasses.clear(); myMethods.clear(); myVariables.clear(); tabs.removeAll(); } if(str.equals("Add Main Method")) { //creates main method newMethod = new MethodCreation(); mainMethod.setEnabled(false); deleteMethod.setEnabled(true); haveMainMethod = true; main.add(newMethod); methodList.addElement("main"); } if(str.equals("Add Constructor")) { //creates constructor newMethod = new MethodCreation(); constructor.setEnabled(false); deleteMethod.setEnabled(true); haveConstructor = true; cons.add(newMethod); methodList.addElement("constructor"); } } @Override public void valueChanged(ListSelectionEvent arg0) { // TODO Auto-generated method stub } } private void openFile(String file) throws ClassNotFoundException, IOException { } }