KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > FormatWin


1 package jimm.datavision.gui;
2 import jimm.datavision.*;
3 import jimm.datavision.field.*;
4 import jimm.datavision.gui.cmd.FormatCommand;
5 import jimm.util.I18N;
6 import java.awt.*;
7 import java.awt.event.*;
8 import java.util.ArrayList JavaDoc;
9 import javax.swing.*;
10
11 /**
12  * A field format editing dialog box. There are tabs for text format and
13  * borders. The initial format and border values are retrieved from one of
14  * two places: either a field passed in to the constructor or the first
15  * selected field in the desing window. The format and border are applied to
16  * either a single field or all selected fields.
17  * <p>
18  * <i>Warning</i>: this code depends upon the fact that the strings in
19  * <code>edgeCountChoices</code> equals the integer value of the string
20  * (zero, one, two, etc.) and that the strings in
21  * <code>edgeStyleChoices</code> correspond to the numeric values of the
22  * <code>BorderEdge.STYLE_*</code> constants.
23  *
24  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
25  */

26 public class FormatWin extends EditWin implements FieldWalker {
27
28 // ================================================================
29
/**
30  * Holds a border edge and the widgets used for editing it.
31  */

32 static class EdgeWidgets {
33
34 protected BorderEdge edge;
35 protected String JavaDoc name;
36 protected JComboBox numberComboBox;
37 protected JComboBox styleComboBox;
38 protected JTextField thicknessText;
39
40 EdgeWidgets(BorderEdge e, String JavaDoc name) {
41     edge = e;
42     this.name = name;
43 }
44
45 String JavaDoc getName() { return name; }
46
47 }
48 // ================================================================
49

50 /**
51  * List of pre-approved font size choices. Don't use this directly; instead
52  * call {@link #sizeChoices}.
53  */

54 protected static Integer JavaDoc[] SIZE_CHOICES;
55
56 protected static final int FORMAT_TEXT_FIELD_COLS = 20;
57 protected static final int THICKNESS_COLS = 8;
58
59 protected static final int TOP = 0;
60 protected static final int LEFT = 1;
61 protected static final int RIGHT = 2;
62 protected static final int BOTTOM = 3;
63
64 protected static String JavaDoc[] fontFamilyNames;
65
66 protected Field field;
67 protected Format format;
68 protected Border border;
69 protected Format origFormat;
70 protected Border origBorder;
71 protected boolean saveRevertInfo;
72 protected JComboBox fontFamily;
73 protected JComboBox size;
74 protected JCheckBox bold;
75 protected JCheckBox italic;
76 protected JCheckBox underline;
77 protected JCheckBox wrap;
78 protected JComboBox align;
79 protected JTextField formatText;
80 protected JLabel fieldColorLabel;
81 protected JLabel borderColorLabel;
82 protected EdgeWidgets[] edgeWidgets;
83
84 /**
85  * This method loads all the font family names in a separate thread. It is
86  * called each time a design window is created, though it only does its
87  * thang the first time it is called.
88  */

89 public static void loadFontChoices() {
90     if (fontFamilyNames == null) {
91     new Thread JavaDoc(new Runnable JavaDoc() {
92         public void run() {
93         fontFamilyNames =
94             GraphicsEnvironment.getLocalGraphicsEnvironment()
95             .getAvailableFontFamilyNames();
96         }
97         }).start();
98     }
99 }
100
101 /**
102  * Constructor.
103  *
104  * @param designer the window to which this dialog belongs
105  * @param f a field from which we will take the format and border
106  * @param whichTab the index of the tab to display when opened
107  */

108 public FormatWin(Designer designer, Field f, int whichTab) {
109     super(designer,
110       I18N.get("FormatWin.title")
111       + (f.designLabel().startsWith("{") ? " " : " (")
112       + f.designLabel()
113       + (designer.countSelectedFields() > 1 ? " +" : "")
114       + (f.designLabel().startsWith("{") ? " " : " )"),
115       "FormatCommand.name");
116
117     field = f;
118
119     origFormat = field.getFormat();
120     if (origFormat != null) origFormat = (Format)origFormat.clone();
121     origBorder = field.getBorder();
122     if (origBorder != null) origBorder = (Border)origBorder.clone();
123
124     copyFormatAndBorder(field.getFormat(), field.getBorder());
125
126     buildWindow(whichTab);
127     pack();
128     setVisible(true);
129 }
130
131 /**
132  * Saves copies of format and border into the objects that we really edit.
133  * Either may be <code>null</code>. Called from constructor and
134  * {@link #doRevert}.
135  *
136  * @param origFormat the format we are copying; not necessarily that of
137  * the field
138  * @param origBorder the border we are copying; not necessarily that of
139  * the field
140  */

141 protected void copyFormatAndBorder(Format origFormat, Border origBorder) {
142     format = (Format)origFormat.clone();
143
144     if (origBorder == null) {
145     border = (Border)field.getReport().getDefaultField().getBorder()
146         .clone();
147     border.setField(field);
148     }
149     else
150     border = (Border)origBorder.clone();
151
152     if (border.getTop() == null)
153     border.setTop(new BorderEdge(BorderEdge.STYLE_LINE, 1, 0));
154     if (border.getBottom() == null)
155     border.setBottom(new BorderEdge(BorderEdge.STYLE_LINE, 1, 0));
156     if (border.getLeft() == null)
157     border.setLeft(new BorderEdge(BorderEdge.STYLE_LINE, 1, 0));
158     if (border.getRight() == null)
159     border.setRight(new BorderEdge(BorderEdge.STYLE_LINE, 1, 0));
160 }
161
162 /**
163  * Builds the window contents.
164  *
165  * @param whichTab the index of the tab to display when opened
166  */

167 protected void buildWindow(int whichTab) {
168     JTabbedPane tabbedPane = new JTabbedPane();
169     tabbedPane.addTab(I18N.get("FormatWin.format_tab"), buildFormatTab());
170     tabbedPane.addTab(I18N.get("FormatWin.border_tab"), buildBorderTab());
171     tabbedPane.setSelectedIndex(whichTab);
172
173     // Ok, Apply, Revert, and Cancel Buttons
174
JPanel buttonPanel = closeButtonPanel();
175
176     // Add edit panes and buttons to window
177
getContentPane().add(tabbedPane, BorderLayout.CENTER);
178     getContentPane().add(buttonPanel, BorderLayout.SOUTH);
179
180     fillFormatTab();
181     fillBorderTab();
182 }
183
184 /**
185  * Builds the format tab contents.
186  */

187 protected java.awt.Container JavaDoc buildFormatTab() {
188     EditFieldLayout efl = new EditFieldLayout();
189
190     fontFamily = efl.addComboBox(I18N.get("FormatWin.font"), fontChoices(),
191                  true);
192     size = efl.addComboBox(I18N.get("FormatWin.size"), sizeChoices(), true);
193     bold = efl.addCheckBox(I18N.get("FormatWin.bold"), KeyEvent.VK_B);
194     italic = efl.addCheckBox(I18N.get("FormatWin.italic"), KeyEvent.VK_I);
195     underline = efl.addCheckBox(I18N.get("FormatWin.underline"),
196                 KeyEvent.VK_U);
197     wrap = efl.addCheckBox(I18N.get("FormatWin.wrap"), KeyEvent.VK_W);
198     align = efl.addComboBox(I18N.get("FormatWin.align"), alignChoices());
199     formatText = efl.addTextField(I18N.get("FormatWin.format"),
200                   FORMAT_TEXT_FIELD_COLS);
201     efl.add(I18N.get("FormatWin.color"), buildFieldColorWidgets());
202
203     // Put a wrapper around that panel so everything is centered in the tab.
204
Box outerBox = Box.createVerticalBox();
205     outerBox.add(Box.createGlue());
206     outerBox.add(efl.getPanel());
207     outerBox.add(Box.createGlue());
208
209     return outerBox;
210 }
211
212 protected Box buildFieldColorWidgets() {
213     Box box = Box.createHorizontalBox();
214     fieldColorLabel = new JLabel(I18N.get("FormatWin.sample_text"));
215     box.add(fieldColorLabel);
216     setFieldExampleColor();
217     box.add(Box.createHorizontalStrut(16));
218     box.add(createFieldColorChooserButton());
219     return box;
220 }
221
222 protected Box buildBorderColorWidgets() {
223     Box box = Box.createHorizontalBox();
224     borderColorLabel = new JLabel(I18N.get("FormatWin.sample_text"));
225     box.add(borderColorLabel);
226     setBorderExampleColor();
227     box.add(Box.createHorizontalStrut(16));
228     box.add(createBorderColorChooserButton());
229
230     Box widgetBox = Box.createVerticalBox();
231     widgetBox.add(Box.createVerticalGlue());
232     widgetBox.add(new JLabel(I18N.get("FormatWin.color")));
233     widgetBox.add(Box.createVerticalStrut(6));
234     widgetBox.add(box);
235     widgetBox.add(Box.createVerticalGlue());
236
237     return widgetBox;
238 }
239
240
241 protected void setFieldExampleColor() {
242     fieldColorLabel.setForeground(format.getColor());
243 }
244
245 protected void setBorderExampleColor() {
246     borderColorLabel.setForeground(border.getColor());
247 }
248
249 protected JButton createFieldColorChooserButton() {
250     JButton b = new JButton(I18N.get("FormatWin.choose"));
251     b.addActionListener(new ActionListener() {
252     public void actionPerformed(ActionEvent e) {
253         Color c = JColorChooser.showDialog(FormatWin.this,
254                            I18N.get("FormatWin.field_color_title"),
255                            format.getColor());
256         if (c != null) {
257         format.setColor(c);
258         setFieldExampleColor();
259         }
260     }
261     });
262     return b;
263 }
264
265 protected JButton createBorderColorChooserButton() {
266     JButton b = new JButton(I18N.get("FormatWin.choose"));
267     b.addActionListener(new ActionListener() {
268     public void actionPerformed(ActionEvent e) {
269         Color c = JColorChooser.showDialog(FormatWin.this,
270                            I18N.get("FormatWin.border_color_title"),
271                            border.getColor());
272         if (c != null) {
273         border.setColor(c);
274         setBorderExampleColor();
275         }
276     }
277     });
278     return b;
279 }
280
281 /**
282  * Returns the list of font size choices, lazily instantiating it if
283  * necessary.
284  *
285  * @return an array of <code>Integer</code> size choices
286  */

287 protected Integer JavaDoc[] sizeChoices() {
288     if (SIZE_CHOICES == null) {
289     ArrayList JavaDoc list = new ArrayList JavaDoc();
290
291     for (int size = 6; size <= 12; ++size)
292         list.add(new Integer JavaDoc(size));
293     for (int size = 14; size <= 36; size += 2)
294         list.add(new Integer JavaDoc(size));
295
296     SIZE_CHOICES = new Integer JavaDoc[list.size()];
297     list.toArray(SIZE_CHOICES);
298     }
299     return SIZE_CHOICES;
300 }
301
302 /**
303  * Returns the index of the specified size value in the SIZE_CHOICES list.
304  * Returns -1 if not found.
305  *
306  * @return an index, or -1 if not found
307  */

308 protected int sizeIndexOf(int size) {
309     Integer JavaDoc[] sizeChoices = sizeChoices();
310     for (int i = 0; i < sizeChoices.length; ++i)
311     if (sizeChoices[i].intValue() == size)
312         return i;
313     return -1;
314 }
315
316 /**
317  * Returns the list of font choices.
318  *
319  * @return an array of font family names
320  */

321 protected String JavaDoc[] fontChoices() {
322     return fontFamilyNames;
323 }
324
325 /**
326  * Returns the index of the specified font family name. Returns -1 if
327  * not found.
328  *
329  * @return an index, or -1 if not found
330  */

331 protected int fontIndexOf(String JavaDoc fontFamilyName) {
332     if (fontFamilyName == null || fontFamilyName.length() == 0)
333     return -1;
334
335     String JavaDoc[] names = fontChoices();
336     for (int i = 0; i < names.length; ++i)
337     if (names[i].equals(fontFamilyName))
338         return i;
339
340     return -1;
341 }
342
343 protected String JavaDoc[] alignChoices() {
344     String JavaDoc[] choices = new String JavaDoc[3];
345     int i = 0;
346     choices[i++] = I18N.get("FormatWin.align_left");
347     choices[i++] = I18N.get("FormatWin.align_center");
348     choices[i++] = I18N.get("FormatWin.align_right");
349     return choices;
350 }
351
352 protected String JavaDoc[] edgeCountChoices() {
353     String JavaDoc[] choices = new String JavaDoc[4];
354     for (int i = 0; i < 4; ++i)
355     choices[i] = I18N.get("FormatWin.edge_count_" + i);
356     return choices;
357 }
358
359 protected String JavaDoc[] edgeStyleChoices() {
360     String JavaDoc[] choices = new String JavaDoc[3];
361     int i = 0;
362     choices[i++] = I18N.get("FormatWin.edge_style_line");
363     choices[i++] = I18N.get("FormatWin.edge_style_dashed");
364     choices[i++] = I18N.get("FormatWin.edge_style_dotted");
365     return choices;
366 }
367
368 /**
369  * Builds the border tab contents.
370  */

371 protected Box buildBorderTab() {
372     edgeWidgets = new EdgeWidgets[4];
373
374     Box vertBox = Box.createVerticalBox();
375
376     Box horizBox = Box.createHorizontalBox();
377     horizBox.add(Box.createHorizontalGlue());
378     horizBox.add(buildBorderEdge(TOP, I18N.get("FormatWin.edge_top"),
379                  border.getTop()));
380     horizBox.add(Box.createHorizontalGlue());
381     vertBox.add(horizBox);
382
383     horizBox = Box.createHorizontalBox();
384     horizBox.add(buildBorderEdge(LEFT, I18N.get("FormatWin.edge_left"),
385                  border.getLeft()));
386     horizBox.add(buildBorderColorWidgets());
387     horizBox.add(buildBorderEdge(RIGHT, I18N.get("FormatWin.edge_right"),
388                  border.getRight()));
389     vertBox.add(horizBox);
390
391     horizBox = Box.createHorizontalBox();
392     horizBox.add(Box.createHorizontalGlue());
393     horizBox.add(buildBorderEdge(BOTTOM, I18N.get("FormatWin.edge_bottom"),
394                  border.getBottom()));
395     horizBox.add(Box.createHorizontalGlue());
396     vertBox.add(horizBox);
397
398     return vertBox;
399 }
400
401 /**
402  * Builds one of the edges of the border.
403  * <p>
404  * <i>Warning</i>: this code depends upon the fact that the strings in
405  * <code>edgeCountChoices</code> equals the integer value of the string
406  * (zero, one, two, etc.) and that the strings in
407  * <code>edgeStyleChoices</code> correspond to the numeric values of the
408  * <code>BorderEdge.STYLE_*</code> constants.
409  *
410  * @param edgeIndex one of <code>TOP</code>, <code>LEFT</code>, etc.
411  * @param edgeName the text name of the widget
412  * @param edge the edge we are representing visually
413  * @return one box to rule them all, one box to bind them
414  */

415 protected Box buildBorderEdge(int edgeIndex, String JavaDoc edgeName,
416                   final BorderEdge edge)
417 {
418     // Create container for all widgets associated with this edge
419
EdgeWidgets ew = new EdgeWidgets(edge, edgeName);
420     edgeWidgets[edgeIndex] = ew;
421
422     EditFieldLayout efl = new EditFieldLayout();
423     ew.numberComboBox = efl.addComboBox(I18N.get("FormatWin.count"),
424                     edgeCountChoices());
425     ew.styleComboBox = efl.addComboBox(I18N.get("FormatWin.style"),
426                        edgeStyleChoices());
427     ew.thicknessText = efl.addTextField(I18N.get("FormatWin.thickness"),
428                     THICKNESS_COLS);
429
430     // Behavior for line count combo box
431
final JComboBox edgeBox = ew.numberComboBox;
432     edgeBox.addActionListener(new ActionListener() {
433     public void actionPerformed(ActionEvent e) {
434         String JavaDoc countStr = (String JavaDoc)edgeBox.getSelectedItem();
435         String JavaDoc[] choices = edgeCountChoices();
436         for (int i = 0; i < choices.length; ++i)
437         if (choices[i].equals(countStr)) {
438             edge.setNumber(i);
439             break;
440         }
441     }
442     });
443
444     // Behavior for line style combo box
445
final JComboBox styleBox = ew.styleComboBox;
446     styleBox.addActionListener(new ActionListener() {
447     public void actionPerformed(ActionEvent e) {
448         String JavaDoc countStr = (String JavaDoc)styleBox.getSelectedItem();
449         String JavaDoc[] choices = edgeStyleChoices();
450         for (int i = 0; i < choices.length; ++i)
451         if (choices[i].equals(countStr)) {
452             edge.setStyle(i);
453             break;
454         }
455     }
456     });
457
458     // Parent panel
459
Box box = Box.createVerticalBox();
460     box.add(new JLabel(ew.getName()));
461     box.add(Box.createVerticalStrut(8));
462     box.add(efl.getPanel());
463
464     return box;
465 }
466
467 /**
468  * Fills the format tab edit fields with values of format.
469  */

470 protected void fillFormatTab() {
471     String JavaDoc fontFamilyName = format.getFontFamilyName();
472     if (fontFamilyName == null) fontFamilyName = "";
473     int index = fontIndexOf(fontFamilyName);
474     if (index == -1) {
475     fontFamily.setSelectedItem(null);
476     fontFamily.configureEditor(fontFamily.getEditor(), fontFamilyName);
477     }
478     else
479     fontFamily.setSelectedIndex(index);
480
481     int sizeVal = (int)format.getSize();
482     index = sizeIndexOf(sizeVal);
483     if (index == -1) {
484     size.setSelectedItem(null);
485     size.configureEditor(size.getEditor(), "" + sizeVal);
486     }
487     else
488     size.setSelectedIndex(index);
489
490     bold.setSelected(format.isBold());
491     italic.setSelected(format.isItalic());
492     underline.setSelected(format.isUnderline());
493     wrap.setSelected(format.isWrap());
494
495     // Assumes format align value == index in alignChoices()
496
align.setSelectedIndex(format.getAlign());
497
498     formatText.setText(format.getFormat());
499 }
500
501 /**
502  * Fills the border tab edit fields with values of border.
503  */

504 protected void fillBorderTab() {
505     for (int i = 0; i < 4; ++i) {
506     EdgeWidgets ew = edgeWidgets[i];
507
508     // Fill count. Assumes edge number (line count) value == index in
509
// edgeCountChoices().
510
ew.numberComboBox.setSelectedIndex(ew.edge.getNumber());
511
512     // Fill styles. Assumes edge style value == index in
513
// edgeStyleChoices().
514
ew.styleComboBox.setSelectedIndex(ew.edge.getStyle());
515
516     // Fill thicknesses.
517
ew.thicknessText.setText("" + ew.edge.getThickness());
518     }
519 }
520
521 protected void doSave() {
522     Object JavaDoc selectedFont = fontFamily.getSelectedItem();
523     if (selectedFont == null || selectedFont.toString().length() == 0)
524     format.setFontFamilyName(null);
525     else
526     format.setFontFamilyName(selectedFont.toString());
527
528     double fontSize = Double.parseDouble(size.getSelectedItem().toString());
529     format.setSize(fontSize);
530     format.setBold(bold.isSelected());
531
532     format.setItalic(italic.isSelected());
533     format.setUnderline(underline.isSelected());
534     format.setWrap(wrap.isSelected());
535
536     String JavaDoc alignText = (String JavaDoc)align.getSelectedItem();
537     if (alignText.equals(I18N.get("FormatWin.align_left")))
538     format.setAlign(Format.ALIGN_LEFT);
539     else if (alignText.equals(I18N.get("FormatWin.align_center")))
540     format.setAlign(Format.ALIGN_CENTER);
541     else // Right
542
format.setAlign(Format.ALIGN_RIGHT);
543
544     format.setFormat(formatText.getText());
545
546     // The border count (number) and style save themselves, but we need to
547
// read and set the thickness.
548
for (int i = 0; i < 4; ++i) {
549     EdgeWidgets ew = edgeWidgets[i];
550     ew.edge.setThickness(Double.parseDouble(ew.thicknessText.getText()));
551     }
552
553     if (designer.countSelectedFields() == 0
554     || field == field.getReport().getDefaultField()) // "==", not "equals"
555
step(field);
556     else // Call step() for all selected fields
557
designer.withSelectedFieldsDo(this);
558 }
559
560 /**
561  * Creates and performs a command that gives the format and borders to the
562  * specified field.
563  *
564  * @param f the field
565  */

566 public void step(Field f) {
567     FormatCommand cmd = new FormatCommand(f, format, border);
568     cmd.perform();
569     commands.add(cmd);
570 }
571
572 protected void doRevert() {
573     fillFormatTab();
574     fillBorderTab();
575 }
576
577 }
578
Popular Tags