package org.reprap.gui; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.GridLayout; import java.io.File; import java.io.IOException; import java.lang.System; import java.util.Arrays; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.JTextField; import javax.swing.JScrollPane; import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import javax.swing.JComboBox; import javax.swing.Box; import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * This reads in the preferences file and constructs a set of menus from it to allow entries * to be edited. * * Preference keys either start with the string "Extruder" followed by a number * and an underscore (that is, they look like "Extruder3_temp(C)") in which case they * are assumed to be a characteristic of the extruder with that number; or they don't, * in which case they are assumed to be global characteristics of the entire machine. * * The keys should end with their dimensions: "Extruder3_temp(C)", "Axis2Scale(steps/mm)", but * regrettably can't contain un-escaped space characters (see java.util.Properties). * * Some weak type checking is done to prevent obvious crassness being put in the edit * boxes. This is done at save time and prevents the junk being written, but doesn't give * a chance to correct it. * * Extensively adapted from Simon's old version by Adrian to construct itself from * the preferences file. * */ //Boxes must contain one of three types: enum Category { number, string, bool; } public class Preferences extends JFrame { private static final long serialVersionUID = 1L; // Load of arrays for all the stuff... private int extruderCount; private JLabel[] globals; // Array of JLabels for the general key names private PreferencesValue[] globalValues; // Array of JTextFields for the general variables private Category[] globalCats; // What are they? private JLabel[][] extruders; // Array of Arrays of JLabels for the extruders' key names private PreferencesValue[][] extruderValues; // Array of Arrays of JTextFields for the extruders' variables private Category[][] extruderCats; // What are they? // Get the show on the road... public static void main(String[] args) { new Preferences(); } /** * Get the value corresponding to name from the preferences file * @param name * @return String */ private String loadString(String name) throws IOException { return org.reprap.Preferences.loadGlobalString(name); } /** * Save the value corresponding to name to the preferences file * @param name * @param value */ private void saveString(String name, String value) throws IOException { org.reprap.Preferences.setGlobalString(name, value); } public void updatePreferencesValues() { try { for(int i = 0; i < globals.length; i++) { globalValues[i].setText(loadString(globals[i].getText())); } for(int j = 0; j < extruderCount; j++) { JLabel[] enames = extruders[j]; for(int i = 0; i < enames.length; i++) extruderValues[j][i].setText(loadString(enames[i].getText())); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Updating preferences: " + ex); ex.printStackTrace(); } } /** * Save the lot to the preferences file * */ public void savePreferences() { try { for(int i = 0; i < globals.length; i++) { String s = globalValues[i].getText(); if(category(s) != globalCats[i]) System.err.println("Dud format for " + globals[i].getText() + ": " + s); else saveString(globals[i].getText(), s); } for(int j = 0; j < extruderCount; j++) { JLabel[] enames = extruders[j]; PreferencesValue[] evals = extruderValues[j]; Category[] cats = extruderCats[j]; for(int i = 0; i < enames.length; i++) { String s = evals[i].getText(); if(category(s) != cats[i]) System.err.println("Dud format for " + enames[i].getText() + ": " + s); else saveString(enames[i].getText(), s); } } org.reprap.Preferences.saveGlobal(); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Saving preferences: " + ex); ex.printStackTrace(); } } /** * Constructor loads all the information from the preferences file, * converts it into arrays of JPanels and JTextFields, then builds the * menus from them. * * @param frame */ public Preferences() { // Start with everything that isn't an extruder value. try { String[] g = org.reprap.Preferences.notStartsWith("Extruder"); Arrays.sort(g); globals = makeLabels(g); globalValues = makeValues(globals); globalCats = categorise(globalValues); }catch (Exception ex) { System.err.println("Preferences window: Can't load the globals!"); ex.printStackTrace(); } // Next we need to know how many extruders we've got. try{ extruderCount = Integer.parseInt(loadString("NumberOfExtruders")); } catch (Exception ex) { System.err.println("Preferences window: Can't load the extruder count!"); ex.printStackTrace(); } // Now build a set of arrays for each extruder in turn. extruders= new JLabel[extruderCount][]; extruderValues= new PreferencesValue[extruderCount][]; extruderCats = new Category[extruderCount][]; try { for(int i = 0; i < extruderCount; i++) { String[] a = org.reprap.Preferences.startsWith("Extruder" + i); Arrays.sort(a); extruders[i] = makeLabels(a); extruderValues[i]= makeValues(extruders[i]); extruderCats[i] = categorise(extruderValues[i]); } }catch (Exception ex) { System.err.println("Preferences window: Can't load extruder(s)!"); ex.printStackTrace(); } // Paint the lot on the screen... initGUI(); //Utility.centerWindowOnParent(this, frame); } private JButton OKButton() { JButton jButtonOK = new JButton(); jButtonOK.setText("OK"); jButtonOK.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { jButtonOKMouseClicked(evt); } }); return jButtonOK; } private JButton CancelButton() { JButton jButtonCancel = new JButton(); jButtonCancel.setText("Cancel"); jButtonCancel.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { jButtonCancelMouseClicked(evt); } }); return jButtonCancel; } private void addValueToPanel(PreferencesValue value, JPanel panel) { if(isBoolean(value.getText())) { value.makeBoolean(); panel.add(value.getObject()); } else panel.add(value.getObject()); } /** * Set up the panels with all the right boxes in * */ private void initGUI() { setSize(600, 700); //Dimension box = new Dimension(30, 10); // Put it all together try { // combobox with buttons for selecting config files JPanel panel = new JPanel(); String[] configfiles = { "reprap.properties" }; File dir = new File( org.reprap.Preferences.getProbsFolderPath()); if (dir.list() != null) { configfiles = dir.list(); for (int i=0; i 1) return false; } } } return true; } /** * Find if a string is a boolean, a number, or a string * @param s * @return */ private Category category(String s) { if(isBoolean(s)) return Category.bool; if(isNumber(s)) return Category.number; return Category.string; } /** * Generate an array of categories corresponsing to the text in * an array of edit boxes so they can be checked later. * @param a * @return */ private Category[] categorise(PreferencesValue[] a) { Category[] result = new Category[a.length]; for(int i = 0; i < a.length; i++) result[i] = category(a[i].getText()); return result; } }