summaryrefslogtreecommitdiff
path: root/trunk/users/hoeken/gcode-host-old/src/org/reprap/gui/Preferences.java
blob: 1ae69a84d7d6acaa8cbcfefbcdff5dc39c2f25ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
package org.reprap.gui;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
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;

/**
 * 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
 * regretably can't contain unescaped 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.
 * 
 * TODO: make booleans use check boxes, not "true" or "false".
 */

//Boxes must contain one of three types:

enum Category
{
	number, string, bool;
}

public class Preferences extends javax.swing.JDialog {
	
	// Pixel dimensions of boxes and things
	
	static private final int gx = 10;    // Horizontal gap
	static private final int gy = 5;     // Vertical gap
	static private final int tx = 9;     // Character X width (average)
	static private final int ty = 20;    // Character Y height
	static private final int bx = 80;    // Button X width
	static private final int by = 20;    // Button Y height
	static private final int taby = 50;  // Tab Y height
	static private final int maxy = 700; // Maximum Y height
	
	// Load of arrays for all the stuff...
	
	private int extruderCount;
	JLabel[] globals;              // Array of JLabels for the general key names
	JTextField[] globalValues;     // Array of JTextFields for the general variables
	Category[] globalCats;         // What are they?
	JLabel[][] extruders;          // Array of Arrays of JLabels for the extruders' key names
	JTextField[][] extruderValues; // Array of Arrays of JTextFields for the extruders' variables
	Category[][] extruderCats;     // What are they?
	int longestGlobal;             // The longest string in the global list
	int longestGlobalVal;          // The longest value in the global list
	int longestExtruders[];        // The longest strings in the extuder lists
	int longestExtruderVals[];     // The longest strings in the extuder lists
	int columns;                   // The number of menu columns to use

	// Get the show on the road...
	
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		Preferences inst = new Preferences(frame);
		inst.setVisible(true);
	}
	

	/**
	 * 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);
	}
	
	/**
	 * 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];
				JTextField[] 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(JFrame frame) 
	{
		super(frame);
		
		columns = 1; // Default
		
		// 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);
			longestGlobal = jLabelListLongest(globals);
			longestGlobalVal = jTextFieldListLongest(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 JTextField[extruderCount][];
		longestExtruders = new int[extruderCount];
		longestExtruderVals = new int[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);
				longestExtruders[i] = jLabelListLongest(extruders[i]);
				extruderValues[i]= makeValues(extruders[i]);
				extruderCats[i] = categorise(extruderValues[i]);
				longestExtruderVals[i] = jTextFieldListLongest(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);
	}
	
	/**
	 * Set up the panels with all the right boxes in
	 *
	 */
	private void initGUI() 
	{
		JButton jButtonOK;     // Save and exit
		JButton jButtonCancel; // Exit without save

		// Work out overall dimensions

		int ypane = yAll();		
		if(ypane > maxy)
		{
			columns = 1 + ypane/maxy;
			ypane = yAll();
		}
		int xall = (xAll() + 2*gx)*columns + gx;
		int yall = ypane + by + 3*gy + taby;

		// Put it all together

		try {
			// Start with the buttons

			jButtonOK = new JButton();
			getContentPane().add(jButtonOK);
			jButtonOK.setText("OK");
			jButtonOK.setBounds(xall - (3*gx + bx), ypane + by + 2*gy, bx, by);
			jButtonOK.addMouseListener(new MouseAdapter() 
			{
				public void mouseClicked(MouseEvent evt) 
				{
					jButtonOKMouseClicked(evt);
				}
			});


			jButtonCancel = new JButton();
			getContentPane().add(jButtonCancel);
			jButtonCancel.setText("Cancel");
			jButtonCancel.setBounds(3*gx, ypane + by + 2*gy, bx, by);
			jButtonCancel.addMouseListener(new MouseAdapter() 
			{
				public void mouseClicked(MouseEvent evt) 
				{
					jButtonCancelMouseClicked(evt);
				}
			});

			// We'll have a tab for the globals, then one 
			// for each extruder

			JTabbedPane jTabbedPane1 = new JTabbedPane();
			getContentPane().add(jTabbedPane1);
			jTabbedPane1.setBounds(gx, gy, xall, yall);


			// Do the global panel

			JPanel jPanelGeneral = new JPanel();
			jTabbedPane1.addTab("Globals", null, jPanelGeneral, null);
			jPanelGeneral.setPreferredSize(new java.awt.Dimension(xall, ypane));
			jPanelGeneral.setLayout(null);

			// Start top left

			int x = 0;
			int y = gy;
			int xw;     // X coordinate of the edit boxes
			xw = longestGlobal*tx + 2*gx;
			int i = 0;
			int lim;
			
			for(int c = 1; c <= columns; c++)
			{
				if(c == columns)
					lim = globals.length%(1 + globals.length/columns);
				else
					lim = 1 + globals.length/columns;
				y = gy;
				for(int d = 0; d < lim; d++)
				{
					globals[i].setBounds(x + gx, y, longestGlobal*tx, ty);
					jPanelGeneral.add(globals[i]);
					globalValues[i].setBounds(x + xw, y, longestGlobalVal*tx, ty);
					jPanelGeneral.add(globalValues[i]);
					y = y + ty + gy;
					i++;
				}
				x += (longestGlobal + longestGlobalVal)*tx + 3*gx;
			}

			// Do all the extruder panels

			for(int j = 0; j < extruderCount; j++)
			{
				JLabel[] keys = extruders[j];
				JTextField[] values = extruderValues[j];

				JPanel jPanelExtruder = new JPanel();
				jTabbedPane1.addTab("Extruder" + j, null, jPanelExtruder, null);
				jPanelExtruder.setLayout(null);
				jPanelExtruder.setPreferredSize(new java.awt.Dimension(xall, ypane));
				
				
				x = 0;
				y = gy;
				xw = longestExtruders[j]*tx + 2*gx;
				i = 0;
				
				for(int c = 1; c <= columns; c++)
				{
					if(c == columns)
						lim = keys.length%(1 + keys.length/columns);
					else
						lim = 1 + keys.length/columns;
					y = gy;
					for(int d = 0; d < lim; d++)
					{
						keys[i].setBounds(x + gx, y, longestExtruders[j]*tx, ty);
						jPanelExtruder.add(keys[i]);
						values[i].setBounds(x + xw, y, longestExtruderVals[j]*tx, ty);
						jPanelExtruder.add(values[i]);
						y = y + ty + gy;
						i++;
					}
					x += (longestExtruders[j] + longestExtruderVals[j])*tx + 3*gx;
				}
			}	
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// Wrap it all up
		getContentPane().setLayout(null);
		setTitle("RepRap Preferences");
		setSize(xall, yall);
	}
	
	/**
	 * What to do when OK is clicked
	 * @param evt
	 */
	private void jButtonOKMouseClicked(MouseEvent evt) {
		// Update all preferences
		savePreferences();
		dispose();
	}
	
	/**
	 * What to do when Cancel is clicked
	 * @param evt
	 */
	private void jButtonCancelMouseClicked(MouseEvent evt) {
		// Close without saving
		dispose();
	}
	
	/**
	 * Find the character count of the longest string in a label (key)
	 * @param a
	 * @return
	 */
	private int jLabelListLongest(JLabel[] a)
	{
		int result = 0;
		for(int i = 0; i < a.length; i++)
		{
			int len = a[i].getText().length();
			if(len > result)
				result = len;
		}
		return result;
	}
	
	/**
	 * Find the character count of the longest string in a value 
	 * @param a
	 * @return
	 */
	private int jTextFieldListLongest(JTextField[] a)
	{
		int result = 0;
		for(int i = 0; i < a.length; i++)
		{
			int len = a[i].getText().length();
			if(len > result)
				result = len;
		}
		return result;
	}

	/**
	 * Work out the pixel width of a key + value pair
	 * @param longestLab
	 * @param longestVal
	 * @return
	 */
	private int xSize(int longestLab, int longestVal)
	{	
		return tx*(longestLab + longestVal) + 3*gx;
	}
	
	/**
	 * Work out the pixel height of an array of values and keys
	 * @param listLen
	 * @return
	 */
	private int ySize(int listLen)
	{	
		return (gy + ty)*listLen + gy;
	}
	
	/**
	 * Work out the widest width of all the lists
	 * @return
	 */
	private int xAll()
	{
		int result = xSize(longestGlobal, longestGlobalVal);
		for(int i = 0; i < extruderCount; i++)
		{
			int x = xSize(longestExtruders[i], longestExtruderVals[i]);
			if(x > result) 
				result = x;
		}
		return result;
	}
	
	/**
	 * Work out the heighest height of all the lists
	 * @return
	 */
	private int yAll()
	{
		int result = ySize(1 + globals.length/columns);
		for(int i = 0; i < extruderCount; i++)
		{
			int y = ySize(1 + extruders[i].length/columns);
			if(y > result) 
				result = y;
		}		
		return result;
	}
	
	/**
	 * Take an array of strings and turn them into labels (right justified).
	 * @param a
	 * @return
	 */
	private JLabel[] makeLabels(String[] a)
	{
		JLabel[] result = new JLabel[a.length];
		for(int i = 0; i < a.length; i++)
		{
			result[i] = new JLabel();
			result[i].setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
			result[i].setText(a[i]);
		}
		return result;
	}	
	
	/**
	 * Take an array of labels and use their string values as keys to look up
	 * the corresponding values.  Make those into an array of editable boxes.
	 * @param a
	 * @return
	 */
	private JTextField[] makeValues(JLabel[] a)
	{
		JTextField[] result = new JTextField[a.length];
		for(int i = 0; i < a.length; i++)
		{
			try{
				result[i] = new JTextField();
				result[i].setText(loadString(a[i].getText()));
			} catch (Exception ex)
			{
				ex.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * Is a string saying a boolean?
	 * @param s
	 * @return
	 */
	private boolean isBoolean(String s)
	{
		if(s.equalsIgnoreCase("true"))
			return true;
		if(s.equalsIgnoreCase("false"))
			return true;
		return false;
	}
	
	/**
	 * Is a string a number (int or double)?
	 * 
	 * There must be a better way to do this; also this doesn't allow
	 * for 1.3e-5...
	 * 
	 * @param s
	 * @return
	 */
	private boolean isNumber(String s)
	{
		// Bulletproofing.
		if ((s==null)||(s.length()==0)) return false;
		
		int start = 0;
		
		while(Character.isSpaceChar(s.charAt(start)))
			start++;
		
		if(s.charAt(start) == '-' || s.charAt(start) == '+')
			start++;
		
		// Last we checked, only one decimal point allowed per number.
		int dotCount = 0;
		for(int i = start; i < s.length(); i++)
		{
			char c = s.charAt(i);
			if(!Character.isDigit(c))
			{
				 if(c != '.')
					return false;
				 else
				 {
					 dotCount++;
					 if(dotCount > 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(JTextField[] a)
	{
		Category[] result = new Category[a.length];
		for(int i = 0; i < a.length; i++)
			result[i] = category(a[i].getText());
		
		return result;
	}
}