1
2 package org.freehep.graphicsio.exportchooser;
3
4 import java.awt.Insets;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.ItemEvent;
8 import java.awt.event.ItemListener;
9 import java.text.ParseException;
10 import java.util.Properties;
11
12 import javax.swing.JComboBox;
13 import javax.swing.JFormattedTextField;
14 import javax.swing.JLabel;
15
16 import org.freehep.graphicsio.PageConstants;
17 import org.freehep.swing.layout.TableLayout;
18 import org.freehep.util.UserProperties;
19
20
21
22
23
24
25 public class PageMarginPanel extends OptionPanel {
26
27 final private static String pageMarginList[] = { "Custom",
28 PageConstants.SMALL, PageConstants.MEDIUM, PageConstants.LARGE };
29
30 private String key;
31
32 private Insets initialMargins;
33
34 private JComboBox pageMarginCombo;
35
36 private JFormattedTextField top, left, bottom, right;
37
38 public PageMarginPanel(Properties user, String rootKey) {
39 super("Page Margins");
40 key = rootKey + "." + PageConstants.PAGE_MARGINS;
41
42 UserProperties options = new UserProperties(user);
43 initialMargins = options.getPropertyInsets(key);
44
45 pageMarginCombo = new JComboBox(pageMarginList);
46 add(TableLayout.LEFT, new JLabel("Preset Margins"));
47 add(TableLayout.RIGHT, pageMarginCombo);
48
49 add(TableLayout.LEFT, new JLabel("Top"));
50 top = new JFormattedTextField(new TextFieldFormatter());
51 top.setColumns(10);
52 add(TableLayout.RIGHT, top);
53
54 add(TableLayout.LEFT, new JLabel("Bottom"));
55 bottom = new JFormattedTextField(new TextFieldFormatter());
56 bottom.setColumns(10);
57 add(TableLayout.RIGHT, bottom);
58
59 add(TableLayout.LEFT, new JLabel("Left"));
60 left = new JFormattedTextField(new TextFieldFormatter());
61 left.setColumns(10);
62 add(TableLayout.RIGHT, left);
63
64 add(TableLayout.LEFT, new JLabel("Right"));
65 right = new JFormattedTextField(new TextFieldFormatter());
66 right.setColumns(10);
67 add(TableLayout.RIGHT, right);
68
69 pageMarginCombo.addItemListener(new ComboListener());
70 top.addActionListener(new TextFieldListener());
71 bottom.addActionListener(new TextFieldListener());
72 left.addActionListener(new TextFieldListener());
73 right.addActionListener(new TextFieldListener());
74
75
76 top.setValue(new Integer(initialMargins.top));
77 bottom.setValue(new Integer(initialMargins.bottom));
78 left.setValue(new Integer(initialMargins.left));
79 right.setValue(new Integer(initialMargins.right));
80
81
82 new TextFieldListener().actionPerformed(null);
83 new ComboListener().itemStateChanged(null);
84 }
85
86 public boolean applyChangedOptions(Properties options) {
87 boolean changed = false;
88
89 Insets margins = new Insets(((Number) top.getValue()).intValue(),
90 ((Number) left.getValue()).intValue(), ((Number) bottom
91 .getValue()).intValue(), ((Number) right.getValue())
92 .intValue());
93
94 if (!margins.equals(initialMargins)) {
95 UserProperties.setProperty(options, key, margins);
96 changed = true;
97 }
98
99 return changed;
100 }
101
102 private class ComboListener implements ItemListener {
103 public void itemStateChanged(ItemEvent e) {
104 int index = pageMarginCombo.getSelectedIndex();
105 if (index != 0) {
106 Insets insets = PageConstants.getMargins(pageMarginList[index]);
107 top.setValue(new Integer(insets.top));
108 bottom.setValue(new Integer(insets.bottom));
109 left.setValue(new Integer(insets.left));
110 right.setValue(new Integer(insets.right));
111 }
112 }
113 }
114
115 private class TextFieldListener implements ActionListener {
116
117 public void actionPerformed(ActionEvent event) {
118 Insets margins = new Insets(((Number) top.getValue()).intValue(),
119 ((Number) left.getValue()).intValue(), ((Number) bottom
120 .getValue()).intValue(),
121 ((Number) right.getValue()).intValue());
122
123 for (int i = 1; i < pageMarginList.length; i++) {
124 Insets insets = PageConstants.getMargins(pageMarginList[i]);
125 if (margins.equals(insets)) {
126 pageMarginCombo.setSelectedIndex(i);
127 return;
128 }
129 }
130
131 pageMarginCombo.setSelectedIndex(0);
132 }
133 }
134
135 private class TextFieldFormatter extends
136 JFormattedTextField.AbstractFormatter {
137 JFormattedTextField field;
138
139 public void install(JFormattedTextField field) {
140 super.install(field);
141 this.field = field;
142 }
143
144 public void uninstall() {
145 field = null;
146 }
147
148
149 public Object stringToValue(String text) throws ParseException {
150 try {
151 Integer value = new Integer(text);
152 return value;
153 } catch (NumberFormatException nfe) {
154 Object value = field.getValue();
155 field.setValue(value);
156 return value;
157 }
158 }
159
160 public String valueToString(Object value) throws ParseException {
161 if (value == null)
162 return "0";
163
164 return value.toString();
165 }
166 }
167 }