/
HappyHackingConverter.java
1198 lines (1071 loc) · 36.4 KB
/
HappyHackingConverter.java
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.border.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit;
import java.util.*;
import java.lang.Double;
import java.lang.StringBuffer;
import javax.swing.border.BevelBorder;
import java.awt.Dimension;
import javax.swing.event.*;
import java.text.DecimalFormat;
import java.math.RoundingMode;
/**
* A very simple toy application with an intuitive UI.
* We start with a simple class which will contain main,
* create and launch the gui and serve as a container
* for the other classes specific to this project.
* And lets use easily remembered variables names like:
*/
class HappyHackingConverter
{
/**
* First we declare all global varibles.
* Like the non digit characters we'll allow
* past our numerical filters.
*/
private static String minus = "-";
private static String blank = "";
/**
* I would describe the default widget colors used as soul numbingly gray.
* So instead we are going to use this variable as the background color throughout our toy app.
*/
private final static Color white = new Color(255,255,255);
/**
* All classes in this toy app will be contained in this class.
* As they are specific to this projet and should have access to shared variables like the background color.
* And if we do decide to share them with other projects we can always refactor them out.
*/
/**
* A single instance of this class will be shared
* between all units of any one type.
* Thus centemeters, meters, kilometers, etc share one instance.
*/
class ConvertibleValue
{
public ConvertibleValue()
{
value = 1; //Default
}
public double getValue()
{
return value;
}
public void setValue(double v)
{
value = v;
}
protected double value;
}
/**
* This interface defiens the method used to
* convert between units. There will always
* be one base unit. For example the base
* unit for distances is the meter.
* All conversion will be from and to meters.
* So getDisplayValue will convert from meters
* to which ever unit system is needed for display.
* And getValueToBeSet will conver to meters.
* getFactor is a conveninece method and returns
* the factor used in conversions.
*/
interface Converter
{
public double getDisplayValue(double value);
public double getValueToBeSet(double value);
public double getFactor();
}
/**
* An enum to handle conversion between distance units.
* All enums implicitly extend java.lang.Enum,
* and since Java does not support multiple inheritance,
* an enum cannot extend anything else.
* So there will be some code duplication between unit enums.
* The base unit is the meter. All values are converted
* either from or to meters.
*/
enum DistanceMultipliers implements Converter
{
MILIMETERS(1000),
CENTIMETERS(100),
METERS(1), //Base unit. so the factor is 1.
KILOMETERS(0.001),
INCHES(39.3701),
FEET(3.28084),
YARDS(1.09361),
MILES(0.00062137273);
private final double factor;
private DistanceMultipliers(double f)
{
factor = f;
}
public double getDisplayValue(double value)
{
return (value * factor);
}
public double getValueToBeSet(double value)
{
return (value / factor);
}
public double getFactor()
{
return factor;
}
}
/**
* An enum to handle conversion between weight and volume units.
* The volum units only regard water.
* The base unit is the kilogram, which for water is
* conveniently equal to 1 liter.
*/
enum WeightMultipliers implements Converter
{
MILLIGRAM(1000000),
GRAM(1000),
KILOGRAM(1), //Base unit
METRIC_TON(0.001), //It's almost as if the metric units could share the factors.
LITER(1), //Water
MILLILITER(1000),
OUNZE(35.274),
TORY_OUNCE(32.1507466),
POUND(2.20462262185),
LONG_TON(0.000984207), //United Kingdom
UK_GALON(0.219969),
UK_QUART(0.879877),
UK_PINT(1.75975),
UK_TBL_SPN(56.3121),
UK_TSPN(168.936),
STONE(0.157473),
SHORT_TON(0.00110231), //United States
US_GALON(0.264172),
US_QUART(1.05669),
US_PINT(2.11338),
US_CUP(4.22675), //US cups and Metric cups are within a fudge factor of each other.
US_TBL_SPN(67.628),
US_TSPN(202.884);
private final double factor;
private WeightMultipliers(double f)
{
factor = f;
}
public double getDisplayValue(double value)
{
return (value * factor);
}
public double getValueToBeSet(double value)
{
return (value / factor);
}
public double getFactor()
{
return factor;
}
}
/**
* A much simpler enum since there are only two units.
*/
enum TemperatureScales
{
CENTIGRADE, FAHRENHEIT
}
/**
* Since there are only two units
* this class will implement the Converter
* methods by using a simple swtich
* to convert between Celsius and Faherheit.
* The base unit is Celsius.
*/
class TemperatureFactors implements Converter
{
TemperatureScales scale;
public TemperatureFactors(TemperatureScales s)
{
scale = s;
}
public double getDisplayValue(double value)
{
switch(scale)
{
case CENTIGRADE:
return value;
case FAHRENHEIT:
return ((value * (9.0/5.0)) + 32.0);
default:
return value;
}
}
public double getValueToBeSet(double value)
{
switch(scale)
{
case CENTIGRADE:
return value;
case FAHRENHEIT:
return ((value - 32.0) * (5.0/9.0));
default:
return value;
}
}
public double getFactor()
{
//FIXME: We should probably throw here.
return 0; //Not an actual multiplilcative factor.
}
}
/**
* A frame class to contain all the visual components.
* This class also provides a way to force the redrawing
* of everthing inside it. As the user modifies any unit
* all other units will be updated and forced to redraw.
*/
class CFrame extends JFrame
{
public CFrame()
{
super("Converter between Imperial and Metric.");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenu menu = new JMenu("Application");
menu.setBackground(white);
menu.setMnemonic(KeyEvent.VK_A);
//quit item
JMenuItem item = new JMenuItem("Quit");
item.setBackground(white);
item.setMnemonic(KeyEvent.VK_Q);
item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.gc();
System.exit(0);
}
}
);
menu.add(item);
JMenuBar menuBar = new JMenuBar();
menuBar.setBackground(white);
menuBar.add(menu);
setJMenuBar(menuBar);
setVisible(true);
}//end of CFrame constructor
/*
* Called to force redrawing of everything.
*/
public void forceRedraw()
{
getContentPane().validate();
getContentPane().repaint();
}
}//end of CFrame class.
/**
* We will want to prevent input of invalid values.
* A validator will allow us to correct invalid input when the widget is about to lose focus.
* How ever what we want is to prevent the appearence of invalid input at all.
* This is why we need a customized document filter.
* This class also takes a refernce to the shared convertible value instance.
* A multiplier instance to do the actual conversions.
* And a pointer to the parent frame so that this instance can force the redraw of
* everything contained in the frame.
*/
class DocumentTemperatureFilter extends DocumentFilter
{
public DocumentTemperatureFilter(ConvertibleValue cv, Converter m, CFrame frame)
{
super();
updateValue = true; //The shared value instance will be updated when this is true.
cValue = cv;
multiplier = m;
topContainer = frame;
}
protected ConvertibleValue cValue; //Reference to the shared instance.
protected Converter multiplier;
protected CFrame topContainer;
public Converter getConverter()
{
return multiplier;
}
/**
* This function inserts or replaces the user's input in a string buffer, to create the text which would be on screen if we allowed it.
* We declare to throw bad location due to doc.getText, but since the paramters are coming from the farmework, that should never happen.... in theory.
* If insert is true we insert, if false we replace.
* Length parameter only used when replacing.
*/
private StringBuffer getTextPrototype(boolean insert, FilterBypass fb, int offs,
String str, int length) throws BadLocationException
{
Document doc = fb.getDocument();
String text = doc.getText(0, doc.getLength());
StringBuffer sb = new StringBuffer(text);
if(insert)
{
sb.insert(offs, str);
}
else
{
sb.replace(offs, offs+length, str);
}
return sb;
}//end of getTextPrototype
/**
* This function attempts to convert the text to a double.
* It returns a valid double if successful, NaN otherwise.
*/
protected double getDouble(boolean insert, FilterBypass fb, int offs,
String str, int length) throws BadLocationException
{
StringBuffer sb = getTextPrototype(insert, fb, offs, str, length);
if(sb.toString().isEmpty())
{
return 0; //We treat an empty string as 0.
}
try
{
return Double.parseDouble(sb.toString());
}
catch(NumberFormatException ex)
{
return Double.NaN; //String was not a number.
}
}//End of get double
/**
* This is supposed to check if the resulting text would be a valid temperature.
* For simplicity's sake that just means any valid double.
*/
private boolean isValidTemperature(boolean insert, FilterBypass fb, int offs,
String str, int length) throws BadLocationException
{
//Allow staring a temperature with a - (minus), or deleting everything and ending up with an empty field.
StringBuffer sb = getTextPrototype(insert, fb, offs, str, length);
if(minus.contentEquals(sb) || blank.contentEquals(sb))
{
return true; //We allow "-" as a valid temperature despite the fact that it is not a valid double.
}
else
{
double d = getDouble(insert, fb, offs, str, length);
return !Double.isNaN(d);//If it is NOT a NaN, then it is a valid temperature.
}
}//End of check if valid temperature
/**
* The shared value instance is updated only when updateValue is true.
*/
public void setUpdateValue(boolean b)
{
updateValue = b;
}
protected boolean updateValue;
/**
* Updates the shared value instance if updateValue is true.
* Always forces the redraw of everything.
*/
protected void doValueUpdate(FilterBypass fb)
throws BadLocationException
{
Document doc = fb.getDocument();
String text = doc.getText(0, doc.getLength());
if(text.isEmpty())
{
text = "0";
}
if(updateValue == true)
{
try
{
Double value = new Double(text);
double newValue = multiplier.getValueToBeSet(value.doubleValue());
cValue.setValue(newValue);
}
catch (NumberFormatException e)
{
//do nothing, since we allow '-'
}
}
topContainer.forceRedraw();
}
public void insertString(FilterBypass fb, int offs,
String str, AttributeSet a)
throws BadLocationException
{
if(isValidTemperature(true, fb, offs, str, 0))
{
fb.insertString(offs, str, a);
doValueUpdate(fb);
}
}//insertString
public void replace(FilterBypass fb, int offs,
int length,
String str, AttributeSet a)
throws BadLocationException
{
if(isValidTemperature(false, fb, offs, str, length))
{
fb.replace(offs, length, str, a);
doValueUpdate(fb);
}
}//replace
public void remove(DocumentFilter.FilterBypass fb,
int offs,
int length)
throws BadLocationException
{
//Insertion is just replacement of a 0 lenght string.
//Delete is a kind of insertion.
//It's just replacement of non-zero lenght string with a 0 lenght string.
//So we will insert a 0 length text.
String str ="";
//And we pass in false for insertion which means replacement.
if(isValidTemperature(false, fb, offs, str, length))
{
fb.remove(offs, length);
doValueUpdate(fb);
}
}//End of remove
}//End of temperature filter.
/**
* This class will be used for weights and distances.
* The math for negative weight and distances works just fine.
* But we will not allow the user to enter negative weigths and distances.
* So this filter is a stricter version of the temperature filter.
*/
class DocumentPositiveNumberFilter extends DocumentTemperatureFilter
{
public DocumentPositiveNumberFilter(ConvertibleValue cv, Converter m, CFrame frame)
{
super(cv, m, frame);
}
private boolean isValidPostiveNumber(boolean insert, FilterBypass fb, int offs,
String str, int length) throws BadLocationException
{
double d = getDouble(insert, fb, offs, str, length);
//In order evaluation, AND should test for NaN first.
if (!Double.isNaN(d) && d >= 0)
{
return true;
}
return false;
}//End of isValidPostiveNumber
public void remove(DocumentFilter.FilterBypass fb,
int offs,
int length)
throws BadLocationException
{
String str ="";
if(isValidPostiveNumber(false, fb, offs, str, length))
{
fb.remove(offs, length);
doValueUpdate(fb);
}
}//End of remove
public void insertString(DocumentFilter.FilterBypass fb,
int offs,
String str,
AttributeSet a)
throws BadLocationException
{
if(isValidPostiveNumber(true, fb, offs, str, 0))
{
fb.insertString(offs, str, a);
doValueUpdate(fb);
}
}//End of insert
public void replace(DocumentFilter.FilterBypass fb,
int offs,
int length,
String str,
AttributeSet a)
throws BadLocationException
{
if(isValidPostiveNumber(false, fb, offs, str, length))
{
fb.replace(offs, length, str, a);
doValueUpdate(fb);
}
}//End of replace
}//end of positive number filter
/**
* We will customize JTextPane so that it takes references
* to a shared convertible value instance and to a converter instance.
*/
class CTalkativeTextPane extends JTextPane
{
public CTalkativeTextPane(ConvertibleValue V, DocumentTemperatureFilter F)
{
super();
value = V; //Shared value instance
setPreferredSize(new Dimension(100,25));
setBorder(new BevelBorder(BevelBorder.RAISED));
filter = F;
((AbstractDocument)getStyledDocument()).setDocumentFilter(filter);;
multiplier = filter.getConverter();
df = new java.text.DecimalFormat("#.##");
bypassFilterAndSetText(getStyledDocument(), "0");
}
protected DocumentTemperatureFilter filter;
protected java.text.DecimalFormat df;
protected void bypassFilterAndSetText(StyledDocument doc, String text)
{
try
{
filter.setUpdateValue(false);
doc.remove(0, doc.getLength());
doc.insertString(0, text, null);
}
catch(BadLocationException e)
{
java.awt.Toolkit.getDefaultToolkit().beep();
}
finally
{
filter.setUpdateValue(true);
}
}
/**
* Gets the displayed value, and uses the converter to convert the
* shared value to units this instance is supposed to represent.
* If the displayed value and shared value are different,
* then we turn off our document filter and update our value
* to match the shared value. We re-enable the document filter afterwards.
*/
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
try
{
StyledDocument doc = getStyledDocument();
double actualValue = (multiplier.getDisplayValue(value.getValue()));
String actualValueAsString = df.format(actualValue);
String displayedValue;
try
{
displayedValue = doc.getText(0, doc.getLength());
}
catch(BadLocationException e)
{
displayedValue = "";
}
if(displayedValue.isEmpty())
{
displayedValue = "0";
}
double displayedValueAsDouble = Double.parseDouble(displayedValue);
if(!actualValueAsString.equals(displayedValue) && displayedValueAsDouble != actualValue) //Allow user to enter trailing zeroes.
{
bypassFilterAndSetText(doc, actualValueAsString);
}
}
catch (NumberFormatException e)
{
//Swallow it as it's OK for the user to try and enter non-numbers.
}
}
@Override
public void repaint(long tm, int x, int y, int width, int height)
{
// This forces repaint to repaint the entire TextPane.
super.repaint(tm, 0, 0, getWidth(), getHeight());
}
protected Converter multiplier;
protected ConvertibleValue value;
}
/**
* This is a generic class to correctly handle pluralization.
* We use the term label for the getter method names,
* however the actual lable could be a Stirng or anything else.
*/
class LabelPair<Singular, Plural>
{
protected Singular s;
protected Plural p;
public LabelPair(Singular S, Plural P)
{
s = S;
p = P;
}
public Singular getSingular()
{
return s;
}
public Plural getPlural()
{
return p;
}
}
/**
* This class contains the input pane, as well as
* its label pair, which happens to be strings,
* and a filler panel to take up space and shove
* everything else to the left.
* This class also implements a document listener
* and uses is to display either the singular or
* plural form of the label, based on the value being displayed.
*/
class DisplayPanel extends JPanel implements DocumentListener
{
protected LabelPair<String, String> labelPair;
protected JLabel label;
public DisplayPanel()
{
super(new GridBagLayout());
setBackground(white);
}
public void addForDisplay(CTalkativeTextPane pane, LabelPair<String, String> lp)
{
GridBagLayout gridbag = (GridBagLayout)getLayout();
labelPair = lp;
label = new JLabel(labelPair.getSingular(), JLabel.LEFT);
pane.getDocument().addDocumentListener(this);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.1;
c.weighty = 0.1;
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(0,20,0,00);
gridbag.setConstraints(pane, c);
add(pane);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.1;
c.weighty = 0.1;
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(0,0,0,20);
gridbag.setConstraints(label, c);
add(label);
JPanel filler = new JPanel();
filler.setBackground(white);
c = new GridBagConstraints();
c.gridx = 2;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.8;
c.weighty = 0.8;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.LINE_END;
gridbag.setConstraints(filler, c);
add(filler);
filler = null;
c = null;
}
public void changedUpdate(DocumentEvent e)
{
updateTheLabel(e);
}
public void insertUpdate(DocumentEvent e)
{
updateTheLabel(e);
}
public void removeUpdate(DocumentEvent e)
{
updateTheLabel(e);
}
void updateTheLabel(DocumentEvent e)
{
Document doc = (Document)e.getDocument();
String text = null;
try
{
text = doc.getText(0, doc.getLength());
doc = null;
}
catch(BadLocationException ex)
{
text = null;
}
if(text != null)
{
try
{
double number = Double.parseDouble(text);
if(number > 1)
{
label.setText(labelPair.getPlural());
}
else
{
label.setText(labelPair.getSingular());
}
}
catch (NumberFormatException ex)
{
//Do nothing
}
finally
{
text = null;
}
}
}
}
/**
* This class is an abstract customized JPanel.
*/
abstract class ConverterPanel extends JPanel
{
/** Returns an ImageIcon, or null if the path was invalid. */
protected ImageIcon createImageIcon(String path, String description)
{
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null)
{
return new ImageIcon(imgURL, description);
}
else
{
System.err.println("Couldn't find file: " + path);
return null;
}
}
protected CFrame frame;
protected ConvertibleValue cValue; //reference to shared instance.
public Dimension getPreferredSize()
{
return preferedSize;
}
protected void newTextPane(GridBagConstraints c, GridBagLayout gridbag, DocumentTemperatureFilter filter, LabelPair names)
{
DisplayPanel panel = new DisplayPanel();
panel.addForDisplay(new CTalkativeTextPane(cValue, filter), names);
gridbag.setConstraints(panel, c);
add(panel);
panel = null;
}
protected GridBagConstraints createConstraints(int x, int y)
{
GridBagConstraints c = new GridBagConstraints();
c.gridx = x;
c.gridy = y;
c.gridwidth = 2;
c.gridheight = 1;
c.ipadx = 1;
c.weightx = 0.5;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
return c;
}
protected Dimension preferedSize;
public ConverterPanel(CFrame F, ConvertibleValue CV)
{
super(new GridBagLayout());//call to super must be first statement in constructor
setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.blue));
frame = F;
cValue = CV;
preferedSize = new Dimension(100, 40);
setBackground(white);
}//constructor
public abstract void layoutConverters();
}//end of class ConverterPanel
/**
* This panel contains the metric distances units.
*/
class MetricDistancesPanel extends ConverterPanel
{
/**
* This array declaration and initialization results in an unchecked or unsafe operations warning.
* However, I could not figgure out a way initialize an ArrayList in this class' constuctor which did not produce the samme warning.
* So I am sticking with plain arrays.
* Also, it's kind of hackish that the size of the labels array must match the number and declaration order of the enum values, so: FIXME!
*/
protected final LabelPair[] labels = { new LabelPair("Millimeter","Millimeters"), new LabelPair("Centimeter", "Centimeters"), new LabelPair("Meter","Meters"), new LabelPair("Kilometer","Kilometers") };
public MetricDistancesPanel(CFrame F, ConvertibleValue CV)
{
super(F, CV);
layoutConverters();
}
public void layoutConverters()
{
GridBagLayout gridbag = (GridBagLayout)getLayout();
int x = 0;
int y = 0;
GridBagConstraints c = createConstraints(x,y++);
ImageIcon icon = createImageIcon("images/90km.png", "EU");
JLabel label = new JLabel(icon);
gridbag.setConstraints(label, c);
add(label);
int just_metric = 0;
for(DistanceMultipliers dm : DistanceMultipliers.values())
{
if(just_metric > 3)
{
return;
}
newTextPane(createConstraints(x,y++), gridbag, new DocumentPositiveNumberFilter(cValue, dm, frame), labels[just_metric]);
just_metric++;
}
}
}
/**
* This panel contains the imperial distances.
*/
class ImperialDistancesPanel extends ConverterPanel
{
protected final LabelPair[] labels = { new LabelPair("Inch","Inches"), new LabelPair("Foot","Feet"), new LabelPair("Yard","Yards"), new LabelPair("Mile","Miles") };
public ImperialDistancesPanel(CFrame F, ConvertibleValue CV)
{
super(F, CV);
layoutConverters();
}
public void layoutConverters()
{
GridBagLayout gridbag = (GridBagLayout)getLayout();
int x = 0;
int y = 0;
GridBagConstraints c = createConstraints(x,y++);
ImageIcon icon = createImageIcon("images/55.png", "EU");
JLabel label = new JLabel(icon);
gridbag.setConstraints(label, c);
add(label);
int ignore_metric = 0;
for(DistanceMultipliers dm : DistanceMultipliers.values())
{
if(ignore_metric > 3)
{
newTextPane(createConstraints(x,y++), gridbag, new DocumentPositiveNumberFilter(cValue, dm, frame), labels[ignore_metric-4]);
}
ignore_metric++;
}
}
}//End of ImperialDistancesPanel
/**
* This panel contains both temperature units.
* And it's constructor requires a temperate scale.
* This value sets which of the two units will be dispalyed.
*/
class TemperaturePanel extends ConverterPanel
{
protected static final String centegrade = "Celsius";
protected static final String fahrenheit = "Fahrenheit";
protected TemperatureScales scale;
public TemperaturePanel(CFrame F, ConvertibleValue CV, TemperatureScales s)
{
super(F, CV);
scale = s;
layoutConverters();
}
public void layoutConverters()
{
GridBagLayout gridbag = (GridBagLayout)getLayout();
ImageIcon sunIcon = createImageIcon("images/Sun.png", "Visit the Mediterranean.");
ImageIcon cloudIcon = createImageIcon("images/Cloud.png", "UK weather.");
JLabel label;
LabelPair scalePair = null;
if(scale == TemperatureScales.FAHRENHEIT)
{
scalePair = new LabelPair(fahrenheit, fahrenheit);
label = new JLabel(cloudIcon);
}
else
{
scalePair = new LabelPair(centegrade, centegrade);
label = new JLabel(sunIcon);
}
GridBagConstraints c = createConstraints(0,0);
gridbag.setConstraints(label, c);
add(label);
newTextPane(createConstraints(0,1), gridbag, new DocumentTemperatureFilter(cValue, new TemperatureFactors(scale), frame), scalePair);
}
}
/**
* This panel contains the metric weights.
*/
class MetricWeightsPanel extends ConverterPanel
{
protected final LabelPair[] labels = { new LabelPair("Milligram","Milligrams"), new LabelPair("Gram","Grams"), new LabelPair("Kilogram","Kilograms"), new LabelPair("Tonne","Tonnes"), new LabelPair("Liter (Water)","Liters (Water)"), new LabelPair("Milliliter","Milliliters") };
public MetricWeightsPanel(CFrame F, ConvertibleValue CV)
{
super(F, CV);
layoutConverters();
}
public void layoutConverters()
{
GridBagLayout gridbag = (GridBagLayout)getLayout();
int x = 0;
int y = 0;
GridBagConstraints c = createConstraints(x,y++);
ImageIcon icon = createImageIcon("images/kg.png", "EU");
JLabel label = new JLabel(icon);
gridbag.setConstraints(label, c);