001: package de.java2html.gui;
002:
003: import java.awt.GridBagConstraints;
004: import java.awt.GridBagLayout;
005: import java.awt.Insets;
006: import java.awt.event.ItemEvent;
007: import java.awt.event.ItemListener;
008: import java.util.ArrayList;
009: import java.util.Iterator;
010: import java.util.List;
011:
012: import javax.swing.JCheckBox;
013: import javax.swing.JComboBox;
014: import javax.swing.JComponent;
015: import javax.swing.JLabel;
016: import javax.swing.JPanel;
017: import javax.swing.JSpinner;
018: import javax.swing.SpinnerNumberModel;
019: import javax.swing.event.ChangeEvent;
020: import javax.swing.event.ChangeListener;
021:
022: import de.java2html.JavaSourceConversionSettings;
023: import de.java2html.converter.IJavaSourceConverter;
024: import de.java2html.converter.JavaSourceConverterProvider;
025: import de.java2html.options.HorizontalAlignment;
026: import de.java2html.options.JavaSourceConversionOptions;
027: import de.java2html.options.JavaSourceStyleTable;
028:
029: public class Java2HtmlOptionsPanel {
030: private final static JavaSourceStyleTable[] styles = JavaSourceStyleTable
031: .getPredefinedTables();
032: private final static HorizontalAlignment[] alignments = new HorizontalAlignment[] {
033: HorizontalAlignment.LEFT, HorizontalAlignment.CENTER,
034: HorizontalAlignment.RIGHT };
035:
036: private final SpinnerNumberModel tabModel = new SpinnerNumberModel(
037: 2, 0, 16, 1);
038:
039: private final JSpinner chTab;
040: private final JComboBox chTarget;
041: private final JComboBox chStyle;
042: private final JComboBox chAlignment;
043: private final JCheckBox cbShowLineNumbers;
044: private final JCheckBox cbShowTableBorder;
045: private final List/*<ChangeListener>*/listeners = new ArrayList();
046:
047: private final JComponent content;
048:
049: public Java2HtmlOptionsPanel() {
050: chTab = new JSpinner(tabModel);
051: ItemListener itemListener = new ItemListener() {
052: public void itemStateChanged(ItemEvent e) {
053: fireStateChanged();
054: }
055: };
056: tabModel.addChangeListener(new ChangeListener() {
057: public void stateChanged(ChangeEvent e) {
058: fireStateChanged();
059: }
060: });
061:
062: chTarget = new JComboBox(JavaSourceConverterProvider
063: .getAllConverters());
064: chTarget.setRenderer(new AbstractSimpleListCellRenderer() {
065: protected String getLabel(Object value) {
066: IJavaSourceConverter converter = (IJavaSourceConverter) value;
067: return converter.getMetaData().getPrintName();
068: }
069: });
070: chTarget.addItemListener(itemListener);
071:
072: chStyle = new JComboBox(styles);
073: chStyle.setRenderer(new AbstractSimpleListCellRenderer() {
074: protected String getLabel(Object value) {
075: JavaSourceStyleTable table = (JavaSourceStyleTable) value;
076: return table.getName();
077: }
078: });
079: chStyle.addItemListener(itemListener);
080:
081: chAlignment = new JComboBox(alignments);
082: chAlignment.setRenderer(new AbstractSimpleListCellRenderer() {
083: protected String getLabel(Object value) {
084: HorizontalAlignment alignment = (HorizontalAlignment) value;
085: return alignment.getName();
086: }
087: });
088: chAlignment.addItemListener(itemListener);
089:
090: cbShowLineNumbers = new JCheckBox("Line numbers", true);
091: cbShowLineNumbers.addItemListener(itemListener);
092:
093: cbShowTableBorder = new JCheckBox("Table border", true);
094: cbShowTableBorder.addItemListener(itemListener);
095:
096: JPanel panel = new JPanel(new GridBagLayout());
097: final GridBagConstraints leftColumnConstraints = new GridBagConstraints();
098: leftColumnConstraints.insets = new Insets(2, 2, 2, 2);
099: leftColumnConstraints.gridx = 0;
100: leftColumnConstraints.anchor = GridBagConstraints.EAST;
101:
102: final GridBagConstraints rightColumnConstraints = new GridBagConstraints();
103: rightColumnConstraints.insets = new Insets(2, 2, 2, 2);
104: rightColumnConstraints.gridx = 1;
105: rightColumnConstraints.fill = GridBagConstraints.NONE;
106: rightColumnConstraints.anchor = GridBagConstraints.WEST;
107:
108: final GridBagConstraints twoColumnConstraints = new GridBagConstraints();
109: twoColumnConstraints.insets = new Insets(2, 2, 2, 2);
110: twoColumnConstraints.gridx = 0;
111: twoColumnConstraints.gridwidth = 2;
112: twoColumnConstraints.anchor = GridBagConstraints.WEST;
113:
114: panel.add(new JLabel("Target:"), leftColumnConstraints);
115: panel.add(chTarget, rightColumnConstraints);
116: panel.add(new JLabel("Style:"), leftColumnConstraints);
117: panel.add(chStyle, rightColumnConstraints);
118: panel.add(new JLabel("Tab-space:"), leftColumnConstraints);
119: panel.add(chTab, rightColumnConstraints);
120: panel.add(new JLabel("Alignment:"), leftColumnConstraints);
121: panel.add(chAlignment, rightColumnConstraints);
122: panel.add(cbShowLineNumbers, twoColumnConstraints);
123: panel.add(cbShowTableBorder, twoColumnConstraints);
124:
125: this .content = panel;
126:
127: initDefaultOptions();
128: }
129:
130: public JComponent getContent() {
131: return content;
132: }
133:
134: private void initDefaultOptions() {
135: JavaSourceConversionOptions options = JavaSourceConversionOptions
136: .getDefault();
137: tabModel.setValue(new Integer(options.getTabSize()));
138:
139: chStyle.setSelectedItem(options.getStyleTable());
140: cbShowLineNumbers.setSelected(options.isShowLineNumbers());
141: cbShowTableBorder.setSelected(options.isShowTableBorder());
142: chAlignment.setSelectedItem(options.getHorizontalAlignment());
143: }
144:
145: public JavaSourceConversionSettings getConversionSettings() {
146: final IJavaSourceConverter selectedConverter = JavaSourceConverterProvider
147: .getAllConverters()[chTarget.getSelectedIndex()];
148: String converterName = selectedConverter.getMetaData()
149: .getName();
150: return new JavaSourceConversionSettings(getConversionOptions(),
151: converterName);
152: }
153:
154: private JavaSourceConversionOptions getConversionOptions() {
155: JavaSourceConversionOptions options = JavaSourceConversionOptions
156: .getDefault();
157: int tabs = tabModel.getNumber().intValue();
158: options.setTabSize(tabs);
159: options.setStyleTable(styles[chStyle.getSelectedIndex()]);
160: options.setShowLineNumbers(cbShowLineNumbers.isSelected());
161: options.setShowTableBorder(cbShowTableBorder.isSelected());
162: options.setHorizontalAlignment(alignments[chAlignment
163: .getSelectedIndex()]);
164: return options;
165: }
166:
167: public synchronized void addChangeListener(ChangeListener listener) {
168: listeners.add(listener);
169: }
170:
171: public synchronized void removeChangeListener(
172: ChangeListener listener) {
173: listeners.remove(listener);
174: }
175:
176: private synchronized void fireStateChanged() {
177: ChangeEvent event = new ChangeEvent(this );
178: for (Iterator iter = new ArrayList(listeners).iterator(); iter
179: .hasNext();) {
180: ChangeListener listener = (ChangeListener) iter.next();
181: listener.stateChanged(event);
182: }
183: }
184: }
|